Menu.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// An internal class used to represent a menu pop-up menu. Created and managed by <see cref="MenuBar"/>.
  5. /// </summary>
  6. internal sealed class Menu : View
  7. {
  8. public Menu ()
  9. {
  10. if (Application.Top is { })
  11. {
  12. Application.Top.DrawComplete += Top_DrawComplete;
  13. Application.Top.SizeChanging += Current_TerminalResized;
  14. }
  15. Application.MouseEvent += Application_RootMouseEvent;
  16. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  17. // Things this view knows how to do
  18. AddCommand (Command.Up, () => MoveUp ());
  19. AddCommand (Command.Down, () => MoveDown ());
  20. AddCommand (
  21. Command.Left,
  22. () =>
  23. {
  24. _host!.PreviousMenu (true);
  25. return true;
  26. }
  27. );
  28. AddCommand (
  29. Command.Cancel,
  30. () =>
  31. {
  32. CloseAllMenus ();
  33. return true;
  34. }
  35. );
  36. AddCommand (
  37. Command.Accept,
  38. () =>
  39. {
  40. RunSelected ();
  41. return true;
  42. }
  43. );
  44. AddCommand (
  45. Command.Select,
  46. ctx =>
  47. {
  48. if (ctx is not CommandContext<KeyBinding> keyCommandContext)
  49. {
  50. return false;
  51. }
  52. return _host?.SelectItem ((keyCommandContext.Binding.Data as MenuItem)!);
  53. });
  54. AddCommand (
  55. Command.Toggle,
  56. ctx =>
  57. {
  58. if (ctx is not CommandContext<KeyBinding> keyCommandContext)
  59. {
  60. return false;
  61. }
  62. return ExpandCollapse ((keyCommandContext.Binding.Data as MenuItem)!);
  63. });
  64. AddCommand (
  65. Command.HotKey,
  66. ctx =>
  67. {
  68. if (ctx is not CommandContext<KeyBinding> keyCommandContext)
  69. {
  70. return false;
  71. }
  72. return _host?.SelectItem ((keyCommandContext.Binding.Data as MenuItem)!);
  73. });
  74. // Default key bindings for this view
  75. KeyBindings.Add (Key.CursorUp, Command.Up);
  76. KeyBindings.Add (Key.CursorDown, Command.Down);
  77. KeyBindings.Add (Key.CursorLeft, Command.Left);
  78. KeyBindings.Add (Key.CursorRight, Command.Right);
  79. KeyBindings.Add (Key.Esc, Command.Cancel);
  80. }
  81. internal int _currentChild;
  82. internal View? _previousSubFocused;
  83. private readonly MenuBarItem? _barItems;
  84. private readonly MenuBar _host;
  85. public override void BeginInit ()
  86. {
  87. base.BeginInit ();
  88. Rectangle frame = MakeFrame (Frame.X, Frame.Y, _barItems!.Children!, Parent);
  89. if (Frame.X != frame.X)
  90. {
  91. X = frame.X;
  92. }
  93. if (Frame.Y != frame.Y)
  94. {
  95. Y = frame.Y;
  96. }
  97. Width = frame.Width;
  98. Height = frame.Height;
  99. if (_barItems.Children is { })
  100. {
  101. foreach (MenuItem? menuItem in _barItems.Children)
  102. {
  103. if (menuItem is { })
  104. {
  105. menuItem._menuBar = Host;
  106. if (menuItem.ShortcutKey != Key.Empty)
  107. {
  108. KeyBinding keyBinding = new ([Command.Select], this, data: menuItem);
  109. // Remove an existent ShortcutKey
  110. menuItem._menuBar.HotKeyBindings.Remove (menuItem.ShortcutKey!);
  111. menuItem._menuBar.HotKeyBindings.Add (menuItem.ShortcutKey!, keyBinding);
  112. }
  113. }
  114. }
  115. }
  116. if (_barItems is { IsTopLevel: true })
  117. {
  118. // This is a standalone MenuItem on a MenuBar
  119. ColorScheme = _host.ColorScheme;
  120. CanFocus = true;
  121. }
  122. else
  123. {
  124. _currentChild = -1;
  125. for (var i = 0; i < _barItems.Children?.Length; i++)
  126. {
  127. if (_barItems.Children [i]?.IsEnabled () == true)
  128. {
  129. _currentChild = i;
  130. break;
  131. }
  132. }
  133. ColorScheme = _host.ColorScheme;
  134. CanFocus = true;
  135. WantMousePositionReports = _host.WantMousePositionReports;
  136. }
  137. BorderStyle = _host.MenusBorderStyle;
  138. AddCommand (
  139. Command.Right,
  140. () =>
  141. {
  142. _host.NextMenu (
  143. !_barItems.IsTopLevel
  144. || (_barItems.Children is { Length: > 0 }
  145. && _currentChild > -1
  146. && _currentChild < _barItems.Children.Length
  147. && _barItems.Children [_currentChild]!.IsFromSubMenu),
  148. _barItems.Children is { Length: > 0 }
  149. && _currentChild > -1
  150. && _host.UseSubMenusSingleFrame
  151. && _barItems.SubMenu (
  152. _barItems.Children [_currentChild]!
  153. )
  154. != null!
  155. );
  156. return true;
  157. }
  158. );
  159. AddKeyBindingsHotKey (_barItems);
  160. }
  161. public override Point? PositionCursor ()
  162. {
  163. if (_host.IsMenuOpen)
  164. {
  165. if (_barItems!.IsTopLevel)
  166. {
  167. return _host.PositionCursor ();
  168. }
  169. Move (2, 1 + _currentChild);
  170. return null; // Don't show the cursor
  171. }
  172. return _host.PositionCursor ();
  173. }
  174. public void Run (Action? action)
  175. {
  176. if (action is null)
  177. {
  178. return;
  179. }
  180. Application.UngrabMouse ();
  181. _host.CloseAllMenus ();
  182. Application.LayoutAndDraw (true);
  183. _host.Run (action);
  184. }
  185. protected override void Dispose (bool disposing)
  186. {
  187. RemoveKeyBindingsHotKey (_barItems);
  188. if (Application.Top is { })
  189. {
  190. Application.Top.DrawComplete -= Top_DrawComplete;
  191. Application.Top.SizeChanging -= Current_TerminalResized;
  192. }
  193. Application.MouseEvent -= Application_RootMouseEvent;
  194. Application.UnGrabbedMouse -= Application_UnGrabbedMouse;
  195. base.Dispose (disposing);
  196. }
  197. protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view)
  198. {
  199. if (!newHasFocus)
  200. {
  201. _host.LostFocus (previousFocusedView!);
  202. }
  203. }
  204. /// <inheritdoc/>
  205. protected override bool OnKeyDownNotHandled (Key keyEvent)
  206. {
  207. // We didn't handle the key, pass it on to host
  208. return _host.InvokeCommandsBoundToHotKey (keyEvent) is true;
  209. }
  210. protected override bool OnMouseEvent (MouseEventArgs me)
  211. {
  212. if (!_host._handled && !_host.HandleGrabView (me, this))
  213. {
  214. return false;
  215. }
  216. _host._handled = false;
  217. bool disabled;
  218. if (me.Flags == MouseFlags.Button1Clicked)
  219. {
  220. disabled = false;
  221. if (me.Position.Y < 0)
  222. {
  223. return me.Handled = true;
  224. }
  225. if (me.Position.Y >= _barItems!.Children!.Length)
  226. {
  227. return me.Handled = true;
  228. }
  229. MenuItem item = _barItems.Children [me.Position.Y]!;
  230. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  231. if (item is null || !item.IsEnabled ())
  232. {
  233. disabled = true;
  234. }
  235. if (disabled)
  236. {
  237. return me.Handled = true;
  238. }
  239. _currentChild = me.Position.Y;
  240. RunSelected ();
  241. return me.Handled = true;
  242. }
  243. if (me.Flags != MouseFlags.Button1Pressed
  244. && me.Flags != MouseFlags.Button1DoubleClicked
  245. && me.Flags != MouseFlags.Button1TripleClicked
  246. && me.Flags != MouseFlags.ReportMousePosition
  247. && !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  248. {
  249. return false;
  250. }
  251. {
  252. disabled = false;
  253. if (me.Position.Y < 0 || me.Position.Y >= _barItems!.Children!.Length)
  254. {
  255. return me.Handled = true;
  256. }
  257. MenuItem item = _barItems.Children [me.Position.Y]!;
  258. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  259. if (item is null)
  260. {
  261. return me.Handled = true;
  262. }
  263. if (item.IsEnabled () != true)
  264. {
  265. disabled = true;
  266. }
  267. if (!disabled)
  268. {
  269. _currentChild = me.Position.Y;
  270. }
  271. if (_host.UseSubMenusSingleFrame || !CheckSubMenu ())
  272. {
  273. SetNeedsDraw ();
  274. SetParentSetNeedsDisplay ();
  275. return me.Handled = true;
  276. }
  277. _host.OnMenuOpened ();
  278. return me.Handled = true;
  279. }
  280. }
  281. /// <inheritdoc/>
  282. protected override void OnVisibleChanged ()
  283. {
  284. base.OnVisibleChanged ();
  285. if (Visible)
  286. {
  287. Application.MouseEvent += Application_RootMouseEvent;
  288. }
  289. else
  290. {
  291. Application.MouseEvent -= Application_RootMouseEvent;
  292. }
  293. }
  294. internal required MenuBarItem? BarItems
  295. {
  296. get => _barItems!;
  297. init
  298. {
  299. ArgumentNullException.ThrowIfNull (value);
  300. _barItems = value;
  301. // Debugging aid so ToString() is helpful
  302. Text = _barItems.Title;
  303. }
  304. }
  305. internal bool CheckSubMenu ()
  306. {
  307. if (_currentChild == -1 || _barItems?.Children? [_currentChild] is null)
  308. {
  309. return true;
  310. }
  311. MenuBarItem? subMenu = _barItems.SubMenu (_barItems.Children [_currentChild]!);
  312. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  313. if (subMenu is { })
  314. {
  315. int pos = -1;
  316. if (_host._openSubMenu is { })
  317. {
  318. pos = _host._openSubMenu.FindIndex (o => o._barItems == subMenu);
  319. }
  320. if (pos == -1
  321. && this != _host.OpenCurrentMenu
  322. && subMenu.Children != _host.OpenCurrentMenu!._barItems!.Children
  323. && !_host.CloseMenu (false, true))
  324. {
  325. return false;
  326. }
  327. _host.Activate (_host._selected, pos, subMenu);
  328. }
  329. else if (_host._openSubMenu?.Count == 0 || _host._openSubMenu?.Last ()._barItems!.IsSubMenuOf (_barItems.Children [_currentChild]!) == false)
  330. {
  331. return _host.CloseMenu (false, true);
  332. }
  333. else
  334. {
  335. SetNeedsDraw ();
  336. SetParentSetNeedsDisplay ();
  337. }
  338. return true;
  339. }
  340. internal Attribute DetermineColorSchemeFor (MenuItem? item, int index)
  341. {
  342. if (item is null)
  343. {
  344. return GetNormalColor ();
  345. }
  346. if (index == _currentChild)
  347. {
  348. return GetFocusColor ();
  349. }
  350. return !item.IsEnabled () ? ColorScheme!.Disabled : GetNormalColor ();
  351. }
  352. internal required MenuBar Host
  353. {
  354. get => _host;
  355. init
  356. {
  357. ArgumentNullException.ThrowIfNull (value);
  358. _host = value;
  359. }
  360. }
  361. internal static Rectangle MakeFrame (int x, int y, MenuItem? []? items, Menu? parent = null)
  362. {
  363. if (items is null || items.Length == 0)
  364. {
  365. return Rectangle.Empty;
  366. }
  367. int minX = x;
  368. int minY = y;
  369. const int borderOffset = 2; // This 2 is for the space around
  370. int maxW = (items.Max (z => z?.Width) ?? 0) + borderOffset;
  371. int maxH = items.Length + borderOffset;
  372. if (parent is { } && x + maxW > Driver.Cols)
  373. {
  374. minX = Math.Max (parent.Frame.Right - parent.Frame.Width - maxW, 0);
  375. }
  376. if (y + maxH > Driver.Rows)
  377. {
  378. minY = Math.Max (Driver.Rows - maxH, 0);
  379. }
  380. return new (minX, minY, maxW, maxH);
  381. }
  382. internal Menu? Parent { get; init; }
  383. private void AddKeyBindingsHotKey (MenuBarItem? menuBarItem)
  384. {
  385. if (menuBarItem is null || menuBarItem.Children is null)
  386. {
  387. return;
  388. }
  389. IEnumerable<MenuItem> menuItems = menuBarItem.Children.Where (m => m is { })!;
  390. foreach (MenuItem menuItem in menuItems)
  391. {
  392. KeyBinding keyBinding = new ([Command.Toggle], this, data: menuItem);
  393. if (menuItem.HotKey != Key.Empty)
  394. {
  395. HotKeyBindings.Remove (menuItem.HotKey!);
  396. HotKeyBindings.Add (menuItem.HotKey!, keyBinding);
  397. HotKeyBindings.Remove (menuItem.HotKey!.WithAlt);
  398. HotKeyBindings.Add (menuItem.HotKey.WithAlt, keyBinding);
  399. }
  400. }
  401. }
  402. private void Application_RootMouseEvent (object? sender, MouseEventArgs a)
  403. {
  404. if (a.View is { } and (MenuBar or not Menu))
  405. {
  406. return;
  407. }
  408. if (!Visible)
  409. {
  410. throw new InvalidOperationException ("This shouldn't running on a invisible menu!");
  411. }
  412. View view = a.View ?? this;
  413. Point boundsPoint = view.ScreenToViewport (new (a.Position.X, a.Position.Y));
  414. var me = new MouseEventArgs
  415. {
  416. Position = boundsPoint,
  417. Flags = a.Flags,
  418. ScreenPosition = a.Position,
  419. View = view
  420. };
  421. if (view.NewMouseEvent (me) == true || a.Flags == MouseFlags.Button1Pressed || a.Flags == MouseFlags.Button1Released)
  422. {
  423. a.Handled = true;
  424. }
  425. }
  426. private void Application_UnGrabbedMouse (object? sender, ViewEventArgs a)
  427. {
  428. if (_host.IsMenuOpen)
  429. {
  430. _host.CloseAllMenus ();
  431. }
  432. }
  433. private void CloseAllMenus ()
  434. {
  435. Application.UngrabMouse ();
  436. _host.CloseAllMenus ();
  437. }
  438. private void Current_TerminalResized (object? sender, SizeChangedEventArgs e)
  439. {
  440. if (_host.IsMenuOpen)
  441. {
  442. _host.CloseAllMenus ();
  443. }
  444. }
  445. /// <summary>Called when a key bound to Command.ToggleExpandCollapse is pressed. This means a hot key was pressed.</summary>
  446. /// <returns></returns>
  447. private bool ExpandCollapse (MenuItem? menuItem)
  448. {
  449. if (!IsInitialized || !Visible)
  450. {
  451. return true;
  452. }
  453. for (var c = 0; c < _barItems!.Children!.Length; c++)
  454. {
  455. if (_barItems.Children [c] == menuItem)
  456. {
  457. _currentChild = c;
  458. break;
  459. }
  460. }
  461. if (menuItem is { })
  462. {
  463. var m = menuItem as MenuBarItem;
  464. if (m?.Children?.Length > 0)
  465. {
  466. MenuItem? item = _barItems.Children [_currentChild];
  467. if (item is null)
  468. {
  469. return true;
  470. }
  471. // ReSharper disable once ConditionIsAlwaysTrueOrFalse
  472. bool disabled = item is null || !item.IsEnabled ();
  473. if (!disabled && (_host.UseSubMenusSingleFrame || !CheckSubMenu ()))
  474. {
  475. SetNeedsDraw ();
  476. SetParentSetNeedsDisplay ();
  477. return true;
  478. }
  479. if (!disabled)
  480. {
  481. _host.OnMenuOpened ();
  482. }
  483. }
  484. else
  485. {
  486. _host.SelectItem (menuItem);
  487. }
  488. }
  489. else if (_host.IsMenuOpen)
  490. {
  491. _host.CloseAllMenus ();
  492. }
  493. else
  494. {
  495. _host.OpenMenu ();
  496. }
  497. return true;
  498. }
  499. private bool MoveDown ()
  500. {
  501. if (_barItems!.IsTopLevel)
  502. {
  503. return true;
  504. }
  505. bool disabled;
  506. do
  507. {
  508. _currentChild++;
  509. if (_currentChild >= _barItems?.Children?.Length)
  510. {
  511. _currentChild = 0;
  512. }
  513. if (this != _host.OpenCurrentMenu && _barItems?.Children? [_currentChild]?.IsFromSubMenu == true && _host._selectedSub > -1)
  514. {
  515. _host.PreviousMenu (true);
  516. _host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild);
  517. _host.OpenCurrentMenu = this;
  518. }
  519. MenuItem? item = _barItems?.Children? [_currentChild];
  520. if (item?.IsEnabled () != true)
  521. {
  522. disabled = true;
  523. }
  524. else
  525. {
  526. disabled = false;
  527. }
  528. if (_host is { UseSubMenusSingleFrame: false, UseKeysUpDownAsKeysLeftRight: true }
  529. && _barItems?.SubMenu (_barItems?.Children? [_currentChild]!) != null
  530. && !disabled
  531. && _host.IsMenuOpen)
  532. {
  533. if (!CheckSubMenu ())
  534. {
  535. return false;
  536. }
  537. break;
  538. }
  539. if (!_host.IsMenuOpen)
  540. {
  541. _host.OpenMenu (_host._selected);
  542. }
  543. }
  544. while (_barItems?.Children? [_currentChild] is null || disabled);
  545. SetNeedsDraw ();
  546. SetParentSetNeedsDisplay ();
  547. if (!_host.UseSubMenusSingleFrame)
  548. {
  549. _host.OnMenuOpened ();
  550. }
  551. return true;
  552. }
  553. private bool MoveUp ()
  554. {
  555. if (_barItems!.IsTopLevel || _currentChild == -1)
  556. {
  557. return true;
  558. }
  559. bool disabled;
  560. do
  561. {
  562. _currentChild--;
  563. if (_host.UseKeysUpDownAsKeysLeftRight && !_host.UseSubMenusSingleFrame)
  564. {
  565. if ((_currentChild == -1 || this != _host.OpenCurrentMenu)
  566. && _barItems.Children! [_currentChild + 1]!.IsFromSubMenu
  567. && _host._selectedSub > -1)
  568. {
  569. _currentChild++;
  570. _host.PreviousMenu (true);
  571. if (_currentChild > 0)
  572. {
  573. _currentChild--;
  574. _host.OpenCurrentMenu = this;
  575. }
  576. break;
  577. }
  578. }
  579. if (_currentChild < 0)
  580. {
  581. _currentChild = _barItems.Children!.Length - 1;
  582. }
  583. if (!_host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild, false))
  584. {
  585. _currentChild = 0;
  586. if (!_host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild) && !_host.CloseMenu ())
  587. {
  588. return false;
  589. }
  590. break;
  591. }
  592. MenuItem item = _barItems.Children! [_currentChild]!;
  593. disabled = item.IsEnabled () != true;
  594. if (_host.UseSubMenusSingleFrame
  595. || !_host.UseKeysUpDownAsKeysLeftRight
  596. || _barItems.SubMenu (_barItems.Children [_currentChild]!) == null!
  597. || disabled
  598. || !_host.IsMenuOpen)
  599. {
  600. continue;
  601. }
  602. if (!CheckSubMenu ())
  603. {
  604. return false;
  605. }
  606. break;
  607. }
  608. while (_barItems.Children [_currentChild] is null || disabled);
  609. SetNeedsDraw ();
  610. SetParentSetNeedsDisplay ();
  611. if (!_host.UseSubMenusSingleFrame)
  612. {
  613. _host.OnMenuOpened ();
  614. }
  615. return true;
  616. }
  617. private void RemoveKeyBindingsHotKey (MenuBarItem? menuBarItem)
  618. {
  619. if (menuBarItem is null || menuBarItem.Children is null)
  620. {
  621. return;
  622. }
  623. IEnumerable<MenuItem> menuItems = menuBarItem.Children.Where (m => m is { })!;
  624. foreach (MenuItem menuItem in menuItems)
  625. {
  626. if (menuItem.HotKey != Key.Empty)
  627. {
  628. KeyBindings.Remove (menuItem.HotKey!);
  629. KeyBindings.Remove (menuItem.HotKey!.WithAlt);
  630. }
  631. }
  632. }
  633. private void RunSelected ()
  634. {
  635. if (_barItems!.IsTopLevel)
  636. {
  637. Run (_barItems.Action);
  638. }
  639. else
  640. {
  641. switch (_currentChild)
  642. {
  643. case > -1 when _barItems.Children! [_currentChild]!.Action != null!:
  644. Run (_barItems.Children [_currentChild]?.Action);
  645. break;
  646. case 0 when _host.UseSubMenusSingleFrame && _barItems.Children [_currentChild]?.Parent!.Parent != null:
  647. _host.PreviousMenu (_barItems.Children [_currentChild]!.Parent!.IsFromSubMenu, true);
  648. break;
  649. case > -1 when _barItems.SubMenu (_barItems.Children [_currentChild]) != null!:
  650. CheckSubMenu ();
  651. break;
  652. }
  653. }
  654. }
  655. private void SetParentSetNeedsDisplay ()
  656. {
  657. if (_host._openSubMenu is { })
  658. {
  659. foreach (Menu menu in _host._openSubMenu)
  660. {
  661. menu.SetNeedsDraw ();
  662. }
  663. }
  664. _host._openMenu?.SetNeedsDraw ();
  665. _host.SetNeedsDraw ();
  666. }
  667. // By doing this we draw last, over everything else.
  668. private void Top_DrawComplete (object? sender, DrawEventArgs e)
  669. {
  670. if (!Visible)
  671. {
  672. return;
  673. }
  674. if (_barItems!.Children is null)
  675. {
  676. return;
  677. }
  678. DrawAdornments ();
  679. RenderLineCanvas ();
  680. // BUGBUG: Views should not change the clip. Doing so is an indcation of poor design or a bug in the framework.
  681. Region? savedClip = SetClipToScreen ();
  682. SetAttribute (GetNormalColor ());
  683. for (int i = Viewport.Y; i < _barItems!.Children.Length; i++)
  684. {
  685. if (i < 0)
  686. {
  687. continue;
  688. }
  689. if (ViewportToScreen (Viewport).Y + i >= Driver.Rows)
  690. {
  691. break;
  692. }
  693. MenuItem? item = _barItems.Children [i];
  694. SetAttribute (
  695. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  696. item is null ? GetNormalColor () :
  697. i == _currentChild ? GetFocusColor () : GetNormalColor ()
  698. );
  699. if (item is null && BorderStyle != LineStyle.None)
  700. {
  701. Point s = ViewportToScreen (new Point (-1, i));
  702. Driver.Move (s.X, s.Y);
  703. Driver.AddRune (Glyphs.LeftTee);
  704. }
  705. else if (Frame.X < Driver.Cols)
  706. {
  707. Move (0, i);
  708. }
  709. SetAttribute (DetermineColorSchemeFor (item, i));
  710. for (int p = Viewport.X; p < Frame.Width - 2; p++)
  711. {
  712. // This - 2 is for the border
  713. if (p < 0)
  714. {
  715. continue;
  716. }
  717. if (ViewportToScreen (Viewport).X + p >= Driver.Cols)
  718. {
  719. break;
  720. }
  721. if (item is null)
  722. {
  723. Driver.AddRune (Glyphs.HLine);
  724. }
  725. else if (i == 0 && p == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  726. {
  727. Driver.AddRune (Glyphs.LeftArrow);
  728. }
  729. // This `- 3` is left border + right border + one row in from right
  730. else if (p == Frame.Width - 3 && _barItems?.SubMenu (_barItems.Children [i]!) is { })
  731. {
  732. Driver.AddRune (Glyphs.RightArrow);
  733. }
  734. else
  735. {
  736. Driver.AddRune ((Rune)' ');
  737. }
  738. }
  739. if (item is null)
  740. {
  741. if (BorderStyle != LineStyle.None && SuperView?.Frame.Right - Frame.X > Frame.Width)
  742. {
  743. Point s = ViewportToScreen (new Point (Frame.Width - 2, i));
  744. Driver.Move (s.X, s.Y);
  745. Driver.AddRune (Glyphs.RightTee);
  746. }
  747. continue;
  748. }
  749. string? textToDraw;
  750. Rune nullCheckedChar = Glyphs.CheckStateNone;
  751. Rune checkChar = Glyphs.Selected;
  752. Rune uncheckedChar = Glyphs.UnSelected;
  753. if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked))
  754. {
  755. checkChar = Glyphs.CheckStateChecked;
  756. uncheckedChar = Glyphs.CheckStateUnChecked;
  757. }
  758. // Support Checked even though CheckType wasn't set
  759. if (item is { CheckType: MenuItemCheckStyle.Checked, Checked: null })
  760. {
  761. textToDraw = $"{nullCheckedChar} {item.Title}";
  762. }
  763. else if (item.Checked == true)
  764. {
  765. textToDraw = $"{checkChar} {item.Title}";
  766. }
  767. else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) || item.CheckType.HasFlag (MenuItemCheckStyle.Radio))
  768. {
  769. textToDraw = $"{uncheckedChar} {item.Title}";
  770. }
  771. else
  772. {
  773. textToDraw = item.Title;
  774. }
  775. Point screen = ViewportToScreen (new Point (0, i));
  776. if (screen.X < Driver.Cols)
  777. {
  778. Driver.Move (screen.X + 1, screen.Y);
  779. if (!item.IsEnabled ())
  780. {
  781. DrawHotString (textToDraw, ColorScheme!.Disabled, ColorScheme.Disabled);
  782. }
  783. else if (i == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  784. {
  785. var tf = new TextFormatter
  786. {
  787. ConstrainToWidth = Frame.Width - 3,
  788. ConstrainToHeight = 1,
  789. Alignment = Alignment.Center, HotKeySpecifier = MenuBar.HotKeySpecifier, Text = textToDraw
  790. };
  791. // The -3 is left/right border + one space (not sure what for)
  792. tf.Draw (
  793. ViewportToScreen (new Rectangle (1, i, Frame.Width - 3, 1)),
  794. i == _currentChild ? GetFocusColor () : GetNormalColor (),
  795. i == _currentChild ? ColorScheme!.HotFocus : ColorScheme!.HotNormal,
  796. SuperView?.ViewportToScreen (SuperView.Viewport) ?? Rectangle.Empty
  797. );
  798. }
  799. else
  800. {
  801. DrawHotString (
  802. textToDraw,
  803. i == _currentChild ? ColorScheme!.HotFocus : ColorScheme!.HotNormal,
  804. i == _currentChild ? GetFocusColor () : GetNormalColor ()
  805. );
  806. }
  807. // The help string
  808. int l = item.ShortcutTag.GetColumns () == 0
  809. ? item.Help.GetColumns ()
  810. : item.Help.GetColumns () + item.ShortcutTag.GetColumns () + 2;
  811. int col = Frame.Width - l - 3;
  812. screen = ViewportToScreen (new Point (col, i));
  813. if (screen.X < Driver.Cols)
  814. {
  815. Driver.Move (screen.X, screen.Y);
  816. Driver.AddStr (item.Help);
  817. // The shortcut tag string
  818. if (!string.IsNullOrEmpty (item.ShortcutTag))
  819. {
  820. Driver.Move (screen.X + l - item.ShortcutTag.GetColumns (), screen.Y);
  821. Driver.AddStr (item.ShortcutTag);
  822. }
  823. }
  824. }
  825. }
  826. SetClip (savedClip);
  827. }
  828. }