Menu.cs 29 KB

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