Menu.cs 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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 >= Application.Screen.Height)
  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. Move (-1, i);
  703. AddRune (Glyphs.LeftTee);
  704. }
  705. else if (Frame.X < Application.Screen.Width)
  706. {
  707. Move (0, i);
  708. }
  709. SetAttribute (DetermineSchemeFor (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 >= Application.Screen.Width)
  718. {
  719. break;
  720. }
  721. if (item is null)
  722. {
  723. AddRune (Glyphs.HLine);
  724. }
  725. else if (i == 0 && p == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  726. {
  727. 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. AddRune (Glyphs.RightArrow);
  733. }
  734. else
  735. {
  736. AddRune ((Rune)' ');
  737. }
  738. }
  739. if (item is null)
  740. {
  741. if (BorderStyle != LineStyle.None && SuperView?.Frame.Right - Frame.X > Frame.Width)
  742. {
  743. Move (Frame.Width - 2, i);
  744. AddRune (Glyphs.RightTee);
  745. }
  746. continue;
  747. }
  748. string? textToDraw;
  749. Rune nullCheckedChar = Glyphs.CheckStateNone;
  750. Rune checkChar = Glyphs.Selected;
  751. Rune uncheckedChar = Glyphs.UnSelected;
  752. if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked))
  753. {
  754. checkChar = Glyphs.CheckStateChecked;
  755. uncheckedChar = Glyphs.CheckStateUnChecked;
  756. }
  757. // Support Checked even though CheckType wasn't set
  758. if (item is { CheckType: MenuItemCheckStyle.Checked, Checked: null })
  759. {
  760. textToDraw = $"{nullCheckedChar} {item.Title}";
  761. }
  762. else if (item.Checked == true)
  763. {
  764. textToDraw = $"{checkChar} {item.Title}";
  765. }
  766. else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) || item.CheckType.HasFlag (MenuItemCheckStyle.Radio))
  767. {
  768. textToDraw = $"{uncheckedChar} {item.Title}";
  769. }
  770. else
  771. {
  772. textToDraw = item.Title;
  773. }
  774. Point screen = ViewportToScreen (new Point (0, i));
  775. if (screen.X < Application.Screen.Width)
  776. {
  777. Move (1, i);
  778. if (!item.IsEnabled ())
  779. {
  780. DrawHotString (textToDraw, GetAttributeForRole (VisualRole.Disabled), GetAttributeForRole (VisualRole.Disabled));
  781. }
  782. else if (i == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  783. {
  784. var tf = new TextFormatter
  785. {
  786. ConstrainToWidth = Frame.Width - 3,
  787. ConstrainToHeight = 1,
  788. Alignment = Alignment.Center, HotKeySpecifier = MenuBar.HotKeySpecifier, Text = textToDraw
  789. };
  790. // The -3 is left/right border + one space (not sure what for)
  791. tf.Draw (
  792. ViewportToScreen (new Rectangle (1, i, Frame.Width - 3, 1)),
  793. i == _currentChild ? GetAttributeForRole (VisualRole.Focus) : GetAttributeForRole (VisualRole.Normal),
  794. i == _currentChild ? GetAttributeForRole (VisualRole.HotFocus) : GetAttributeForRole (VisualRole.HotNormal),
  795. SuperView?.ViewportToScreen (SuperView.Viewport) ?? Rectangle.Empty
  796. );
  797. }
  798. else
  799. {
  800. DrawHotString (
  801. textToDraw,
  802. i == _currentChild ? GetAttributeForRole (VisualRole.HotFocus) : GetAttributeForRole (VisualRole.HotNormal),
  803. i == _currentChild ? GetAttributeForRole (VisualRole.Focus) : GetAttributeForRole (VisualRole.Normal)
  804. );
  805. }
  806. // The help string
  807. int l = item.ShortcutTag.GetColumns () == 0
  808. ? item.Help.GetColumns ()
  809. : item.Help.GetColumns () + item.ShortcutTag.GetColumns () + 2;
  810. int col = Frame.Width - l - 3;
  811. screen = ViewportToScreen (new Point (col, i));
  812. if (screen.X < Application.Screen.Width)
  813. {
  814. Move (col, i);
  815. AddStr (item.Help);
  816. // The shortcut tag string
  817. if (!string.IsNullOrEmpty (item.ShortcutTag))
  818. {
  819. Move (col + l - item.ShortcutTag.GetColumns (), i);
  820. AddStr (item.ShortcutTag);
  821. }
  822. }
  823. }
  824. }
  825. SetClip (savedClip);
  826. }
  827. }