Menu.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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"/> and
  5. /// <see cref="ContextMenu"/>.
  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. // 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], KeyBindingScope.HotKey, menuItem);
  109. // Remove an existent ShortcutKey
  110. menuItem._menuBar.KeyBindings.Remove (menuItem.ShortcutKey!);
  111. menuItem._menuBar.KeyBindings.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. base.Dispose (disposing);
  195. }
  196. protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view)
  197. {
  198. if (!newHasFocus)
  199. {
  200. _host.LostFocus (previousFocusedView!);
  201. }
  202. }
  203. /// <inheritdoc/>
  204. protected override bool OnKeyDownNotHandled (Key keyEvent)
  205. {
  206. // We didn't handle the key, pass it on to host
  207. return _host.InvokeCommandsBoundToKey (keyEvent) == true;
  208. }
  209. protected override bool OnMouseEvent (MouseEventArgs me)
  210. {
  211. if (!_host._handled && !_host.HandleGrabView (me, this))
  212. {
  213. return false;
  214. }
  215. _host._handled = false;
  216. bool disabled;
  217. if (me.Flags == MouseFlags.Button1Clicked)
  218. {
  219. disabled = false;
  220. if (me.Position.Y < 0)
  221. {
  222. return me.Handled = true;
  223. }
  224. if (me.Position.Y >= _barItems!.Children!.Length)
  225. {
  226. return me.Handled = true;
  227. }
  228. MenuItem item = _barItems.Children [me.Position.Y]!;
  229. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  230. if (item is null || !item.IsEnabled ())
  231. {
  232. disabled = true;
  233. }
  234. if (disabled)
  235. {
  236. return me.Handled = true;
  237. }
  238. _currentChild = me.Position.Y;
  239. RunSelected ();
  240. return me.Handled = true;
  241. }
  242. if (me.Flags != MouseFlags.Button1Pressed
  243. && me.Flags != MouseFlags.Button1DoubleClicked
  244. && me.Flags != MouseFlags.Button1TripleClicked
  245. && me.Flags != MouseFlags.ReportMousePosition
  246. && !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  247. {
  248. return false;
  249. }
  250. {
  251. disabled = false;
  252. if (me.Position.Y < 0 || me.Position.Y >= _barItems!.Children!.Length)
  253. {
  254. return me.Handled = true;
  255. }
  256. MenuItem item = _barItems.Children [me.Position.Y]!;
  257. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  258. if (item is null)
  259. {
  260. return me.Handled = true;
  261. }
  262. if (item.IsEnabled () != true)
  263. {
  264. disabled = true;
  265. }
  266. if (!disabled)
  267. {
  268. _currentChild = me.Position.Y;
  269. }
  270. if (_host.UseSubMenusSingleFrame || !CheckSubMenu ())
  271. {
  272. SetNeedsDraw ();
  273. SetParentSetNeedsDisplay ();
  274. return me.Handled = true;
  275. }
  276. _host.OnMenuOpened ();
  277. return me.Handled = true;
  278. }
  279. }
  280. /// <inheritdoc/>
  281. protected override void OnVisibleChanged ()
  282. {
  283. base.OnVisibleChanged ();
  284. if (Visible)
  285. {
  286. Application.MouseEvent += Application_RootMouseEvent;
  287. }
  288. else
  289. {
  290. Application.MouseEvent -= Application_RootMouseEvent;
  291. }
  292. }
  293. internal required MenuBarItem? BarItems
  294. {
  295. get => _barItems!;
  296. init
  297. {
  298. ArgumentNullException.ThrowIfNull (value);
  299. _barItems = value;
  300. // Debugging aid so ToString() is helpful
  301. Text = _barItems.Title;
  302. }
  303. }
  304. internal bool CheckSubMenu ()
  305. {
  306. if (_currentChild == -1 || _barItems?.Children? [_currentChild] is null)
  307. {
  308. return true;
  309. }
  310. MenuBarItem? subMenu = _barItems.SubMenu (_barItems.Children [_currentChild]!);
  311. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  312. if (subMenu is { })
  313. {
  314. int pos = -1;
  315. if (_host._openSubMenu is { })
  316. {
  317. pos = _host._openSubMenu.FindIndex (o => o._barItems == subMenu);
  318. }
  319. if (pos == -1
  320. && this != _host.OpenCurrentMenu
  321. && subMenu.Children != _host.OpenCurrentMenu!._barItems!.Children
  322. && !_host.CloseMenu (false, true))
  323. {
  324. return false;
  325. }
  326. _host.Activate (_host._selected, pos, subMenu);
  327. }
  328. else if (_host._openSubMenu?.Count == 0 || _host._openSubMenu?.Last ()._barItems!.IsSubMenuOf (_barItems.Children [_currentChild]!) == false)
  329. {
  330. return _host.CloseMenu (false, true);
  331. }
  332. else
  333. {
  334. SetNeedsDraw ();
  335. SetParentSetNeedsDisplay ();
  336. }
  337. return true;
  338. }
  339. internal Attribute DetermineColorSchemeFor (MenuItem? item, int index)
  340. {
  341. if (item is null)
  342. {
  343. return GetNormalColor ();
  344. }
  345. if (index == _currentChild)
  346. {
  347. return GetFocusColor ();
  348. }
  349. return !item.IsEnabled () ? ColorScheme!.Disabled : GetNormalColor ();
  350. }
  351. internal required MenuBar Host
  352. {
  353. get => _host;
  354. init
  355. {
  356. ArgumentNullException.ThrowIfNull (value);
  357. _host = value;
  358. }
  359. }
  360. internal static Rectangle MakeFrame (int x, int y, MenuItem? []? items, Menu? parent = null)
  361. {
  362. if (items is null || items.Length == 0)
  363. {
  364. return Rectangle.Empty;
  365. }
  366. int minX = x;
  367. int minY = y;
  368. const int borderOffset = 2; // This 2 is for the space around
  369. int maxW = (items.Max (z => z?.Width) ?? 0) + borderOffset;
  370. int maxH = items.Length + borderOffset;
  371. if (parent is { } && x + maxW > Driver.Cols)
  372. {
  373. minX = Math.Max (parent.Frame.Right - parent.Frame.Width - maxW, 0);
  374. }
  375. if (y + maxH > Driver.Rows)
  376. {
  377. minY = Math.Max (Driver.Rows - maxH, 0);
  378. }
  379. return new (minX, minY, maxW, maxH);
  380. }
  381. internal Menu? Parent { get; init; }
  382. private void AddKeyBindingsHotKey (MenuBarItem? menuBarItem)
  383. {
  384. if (menuBarItem is null || menuBarItem.Children is null)
  385. {
  386. return;
  387. }
  388. IEnumerable<MenuItem> menuItems = menuBarItem.Children.Where (m => m is { })!;
  389. foreach (MenuItem menuItem in menuItems)
  390. {
  391. KeyBinding keyBinding = new ([Command.Toggle], KeyBindingScope.HotKey, menuItem);
  392. if (menuItem.HotKey != Key.Empty)
  393. {
  394. KeyBindings.Remove (menuItem.HotKey!);
  395. KeyBindings.Add (menuItem.HotKey!, keyBinding);
  396. KeyBindings.Remove (menuItem.HotKey!.WithAlt);
  397. KeyBindings.Add (menuItem.HotKey.WithAlt, keyBinding);
  398. }
  399. }
  400. }
  401. private void Application_RootMouseEvent (object? sender, MouseEventArgs a)
  402. {
  403. if (a.View is { } and (MenuBar or not Menu))
  404. {
  405. return;
  406. }
  407. if (!Visible)
  408. {
  409. throw new InvalidOperationException ("This shouldn't running on a invisible menu!");
  410. }
  411. View view = a.View ?? this;
  412. Point boundsPoint = view.ScreenToViewport (new (a.Position.X, a.Position.Y));
  413. var me = new MouseEventArgs
  414. {
  415. Position = boundsPoint,
  416. Flags = a.Flags,
  417. ScreenPosition = a.Position,
  418. View = view
  419. };
  420. if (view.NewMouseEvent (me) == true || a.Flags == MouseFlags.Button1Pressed || a.Flags == MouseFlags.Button1Released)
  421. {
  422. a.Handled = true;
  423. }
  424. }
  425. private void CloseAllMenus ()
  426. {
  427. Application.UngrabMouse ();
  428. _host.CloseAllMenus ();
  429. }
  430. private void Current_TerminalResized (object? sender, SizeChangedEventArgs e)
  431. {
  432. if (_host.IsMenuOpen)
  433. {
  434. _host.CloseAllMenus ();
  435. }
  436. }
  437. /// <summary>Called when a key bound to Command.ToggleExpandCollapse is pressed. This means a hot key was pressed.</summary>
  438. /// <returns></returns>
  439. private bool ExpandCollapse (MenuItem? menuItem)
  440. {
  441. if (!IsInitialized || !Visible)
  442. {
  443. return true;
  444. }
  445. for (var c = 0; c < _barItems!.Children!.Length; c++)
  446. {
  447. if (_barItems.Children [c] == menuItem)
  448. {
  449. _currentChild = c;
  450. break;
  451. }
  452. }
  453. if (menuItem is { })
  454. {
  455. var m = menuItem as MenuBarItem;
  456. if (m?.Children?.Length > 0)
  457. {
  458. MenuItem? item = _barItems.Children [_currentChild];
  459. if (item is null)
  460. {
  461. return true;
  462. }
  463. // ReSharper disable once ConditionIsAlwaysTrueOrFalse
  464. bool disabled = item is null || !item.IsEnabled ();
  465. if (!disabled && (_host.UseSubMenusSingleFrame || !CheckSubMenu ()))
  466. {
  467. SetNeedsDraw ();
  468. SetParentSetNeedsDisplay ();
  469. return true;
  470. }
  471. if (!disabled)
  472. {
  473. _host.OnMenuOpened ();
  474. }
  475. }
  476. else
  477. {
  478. _host.SelectItem (menuItem);
  479. }
  480. }
  481. else if (_host.IsMenuOpen)
  482. {
  483. _host.CloseAllMenus ();
  484. }
  485. else
  486. {
  487. _host.OpenMenu ();
  488. }
  489. return true;
  490. }
  491. private bool MoveDown ()
  492. {
  493. if (_barItems!.IsTopLevel)
  494. {
  495. return true;
  496. }
  497. bool disabled;
  498. do
  499. {
  500. _currentChild++;
  501. if (_currentChild >= _barItems?.Children?.Length)
  502. {
  503. _currentChild = 0;
  504. }
  505. if (this != _host.OpenCurrentMenu && _barItems?.Children? [_currentChild]?.IsFromSubMenu == true && _host._selectedSub > -1)
  506. {
  507. _host.PreviousMenu (true);
  508. _host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild);
  509. _host.OpenCurrentMenu = this;
  510. }
  511. MenuItem? item = _barItems?.Children? [_currentChild];
  512. if (item?.IsEnabled () != true)
  513. {
  514. disabled = true;
  515. }
  516. else
  517. {
  518. disabled = false;
  519. }
  520. if (_host is { UseSubMenusSingleFrame: false, UseKeysUpDownAsKeysLeftRight: true }
  521. && _barItems?.SubMenu (_barItems?.Children? [_currentChild]!) != null
  522. && !disabled
  523. && _host.IsMenuOpen)
  524. {
  525. if (!CheckSubMenu ())
  526. {
  527. return false;
  528. }
  529. break;
  530. }
  531. if (!_host.IsMenuOpen)
  532. {
  533. _host.OpenMenu (_host._selected);
  534. }
  535. }
  536. while (_barItems?.Children? [_currentChild] is null || disabled);
  537. SetNeedsDraw ();
  538. SetParentSetNeedsDisplay ();
  539. if (!_host.UseSubMenusSingleFrame)
  540. {
  541. _host.OnMenuOpened ();
  542. }
  543. return true;
  544. }
  545. private bool MoveUp ()
  546. {
  547. if (_barItems!.IsTopLevel || _currentChild == -1)
  548. {
  549. return true;
  550. }
  551. bool disabled;
  552. do
  553. {
  554. _currentChild--;
  555. if (_host.UseKeysUpDownAsKeysLeftRight && !_host.UseSubMenusSingleFrame)
  556. {
  557. if ((_currentChild == -1 || this != _host.OpenCurrentMenu)
  558. && _barItems.Children! [_currentChild + 1]!.IsFromSubMenu
  559. && _host._selectedSub > -1)
  560. {
  561. _currentChild++;
  562. _host.PreviousMenu (true);
  563. if (_currentChild > 0)
  564. {
  565. _currentChild--;
  566. _host.OpenCurrentMenu = this;
  567. }
  568. break;
  569. }
  570. }
  571. if (_currentChild < 0)
  572. {
  573. _currentChild = _barItems.Children!.Length - 1;
  574. }
  575. if (!_host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild, false))
  576. {
  577. _currentChild = 0;
  578. if (!_host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild) && !_host.CloseMenu ())
  579. {
  580. return false;
  581. }
  582. break;
  583. }
  584. MenuItem item = _barItems.Children! [_currentChild]!;
  585. disabled = item.IsEnabled () != true;
  586. if (_host.UseSubMenusSingleFrame
  587. || !_host.UseKeysUpDownAsKeysLeftRight
  588. || _barItems.SubMenu (_barItems.Children [_currentChild]!) == null!
  589. || disabled
  590. || !_host.IsMenuOpen)
  591. {
  592. continue;
  593. }
  594. if (!CheckSubMenu ())
  595. {
  596. return false;
  597. }
  598. break;
  599. }
  600. while (_barItems.Children [_currentChild] is null || disabled);
  601. SetNeedsDraw ();
  602. SetParentSetNeedsDisplay ();
  603. if (!_host.UseSubMenusSingleFrame)
  604. {
  605. _host.OnMenuOpened ();
  606. }
  607. return true;
  608. }
  609. private void RemoveKeyBindingsHotKey (MenuBarItem? menuBarItem)
  610. {
  611. if (menuBarItem is null || menuBarItem.Children is null)
  612. {
  613. return;
  614. }
  615. IEnumerable<MenuItem> menuItems = menuBarItem.Children.Where (m => m is { })!;
  616. foreach (MenuItem menuItem in menuItems)
  617. {
  618. if (menuItem.HotKey != Key.Empty)
  619. {
  620. KeyBindings.Remove (menuItem.HotKey!);
  621. KeyBindings.Remove (menuItem.HotKey!.WithAlt);
  622. }
  623. }
  624. }
  625. private void RunSelected ()
  626. {
  627. if (_barItems!.IsTopLevel)
  628. {
  629. Run (_barItems.Action);
  630. }
  631. else
  632. {
  633. switch (_currentChild)
  634. {
  635. case > -1 when _barItems.Children! [_currentChild]!.Action != null!:
  636. Run (_barItems.Children [_currentChild]?.Action);
  637. break;
  638. case 0 when _host.UseSubMenusSingleFrame && _barItems.Children [_currentChild]?.Parent!.Parent != null:
  639. _host.PreviousMenu (_barItems.Children [_currentChild]!.Parent!.IsFromSubMenu, true);
  640. break;
  641. case > -1 when _barItems.SubMenu (_barItems.Children [_currentChild]) != null!:
  642. CheckSubMenu ();
  643. break;
  644. }
  645. }
  646. }
  647. private void SetParentSetNeedsDisplay ()
  648. {
  649. if (_host._openSubMenu is { })
  650. {
  651. foreach (Menu menu in _host._openSubMenu)
  652. {
  653. menu.SetNeedsDraw ();
  654. }
  655. }
  656. _host._openMenu?.SetNeedsDraw ();
  657. _host.SetNeedsDraw ();
  658. }
  659. // By doing this we draw last, over everything else.
  660. private void Top_DrawComplete (object? sender, DrawEventArgs e)
  661. {
  662. if (!Visible)
  663. {
  664. return;
  665. }
  666. if (_barItems!.Children is null)
  667. {
  668. return;
  669. }
  670. DrawBorderAndPadding ();
  671. RenderLineCanvas ();
  672. // BUGBUG: Views should not change the clip. Doing so is an indcation of poor design or a bug in the framework.
  673. Region? savedClip = SetClipToScreen ();
  674. SetAttribute (GetNormalColor ());
  675. for (int i = Viewport.Y; i < _barItems!.Children.Length; i++)
  676. {
  677. if (i < 0)
  678. {
  679. continue;
  680. }
  681. if (ViewportToScreen (Viewport).Y + i >= Driver.Rows)
  682. {
  683. break;
  684. }
  685. MenuItem? item = _barItems.Children [i];
  686. SetAttribute (
  687. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  688. item is null ? GetNormalColor () :
  689. i == _currentChild ? GetFocusColor () : GetNormalColor ()
  690. );
  691. if (item is null && BorderStyle != LineStyle.None)
  692. {
  693. Point s = ViewportToScreen (new Point (-1, i));
  694. Driver.Move (s.X, s.Y);
  695. Driver.AddRune (Glyphs.LeftTee);
  696. }
  697. else if (Frame.X < Driver.Cols)
  698. {
  699. Move (0, i);
  700. }
  701. SetAttribute (DetermineColorSchemeFor (item, i));
  702. for (int p = Viewport.X; p < Frame.Width - 2; p++)
  703. {
  704. // This - 2 is for the border
  705. if (p < 0)
  706. {
  707. continue;
  708. }
  709. if (ViewportToScreen (Viewport).X + p >= Driver.Cols)
  710. {
  711. break;
  712. }
  713. if (item is null)
  714. {
  715. Driver.AddRune (Glyphs.HLine);
  716. }
  717. else if (i == 0 && p == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  718. {
  719. Driver.AddRune (Glyphs.LeftArrow);
  720. }
  721. // This `- 3` is left border + right border + one row in from right
  722. else if (p == Frame.Width - 3 && _barItems?.SubMenu (_barItems.Children [i]!) is { })
  723. {
  724. Driver.AddRune (Glyphs.RightArrow);
  725. }
  726. else
  727. {
  728. Driver.AddRune ((Rune)' ');
  729. }
  730. }
  731. if (item is null)
  732. {
  733. if (BorderStyle != LineStyle.None && SuperView?.Frame.Right - Frame.X > Frame.Width)
  734. {
  735. Point s = ViewportToScreen (new Point (Frame.Width - 2, i));
  736. Driver.Move (s.X, s.Y);
  737. Driver.AddRune (Glyphs.RightTee);
  738. }
  739. continue;
  740. }
  741. string? textToDraw;
  742. Rune nullCheckedChar = Glyphs.CheckStateNone;
  743. Rune checkChar = Glyphs.Selected;
  744. Rune uncheckedChar = Glyphs.UnSelected;
  745. if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked))
  746. {
  747. checkChar = Glyphs.CheckStateChecked;
  748. uncheckedChar = Glyphs.CheckStateUnChecked;
  749. }
  750. // Support Checked even though CheckType wasn't set
  751. if (item is { CheckType: MenuItemCheckStyle.Checked, Checked: null })
  752. {
  753. textToDraw = $"{nullCheckedChar} {item.Title}";
  754. }
  755. else if (item.Checked == true)
  756. {
  757. textToDraw = $"{checkChar} {item.Title}";
  758. }
  759. else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) || item.CheckType.HasFlag (MenuItemCheckStyle.Radio))
  760. {
  761. textToDraw = $"{uncheckedChar} {item.Title}";
  762. }
  763. else
  764. {
  765. textToDraw = item.Title;
  766. }
  767. Point screen = ViewportToScreen (new Point (0, i));
  768. if (screen.X < Driver.Cols)
  769. {
  770. Driver.Move (screen.X + 1, screen.Y);
  771. if (!item.IsEnabled ())
  772. {
  773. DrawHotString (textToDraw, ColorScheme!.Disabled, ColorScheme.Disabled);
  774. }
  775. else if (i == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  776. {
  777. var tf = new TextFormatter
  778. {
  779. ConstrainToWidth = Frame.Width - 3,
  780. ConstrainToHeight = 1,
  781. Alignment = Alignment.Center, HotKeySpecifier = MenuBar.HotKeySpecifier, Text = textToDraw
  782. };
  783. // The -3 is left/right border + one space (not sure what for)
  784. tf.Draw (
  785. ViewportToScreen (new Rectangle (1, i, Frame.Width - 3, 1)),
  786. i == _currentChild ? GetFocusColor () : GetNormalColor (),
  787. i == _currentChild ? ColorScheme!.HotFocus : ColorScheme!.HotNormal,
  788. SuperView?.ViewportToScreen (SuperView.Viewport) ?? Rectangle.Empty
  789. );
  790. }
  791. else
  792. {
  793. DrawHotString (
  794. textToDraw,
  795. i == _currentChild ? ColorScheme!.HotFocus : ColorScheme!.HotNormal,
  796. i == _currentChild ? GetFocusColor () : GetNormalColor ()
  797. );
  798. }
  799. // The help string
  800. int l = item.ShortcutTag.GetColumns () == 0
  801. ? item.Help.GetColumns ()
  802. : item.Help.GetColumns () + item.ShortcutTag.GetColumns () + 2;
  803. int col = Frame.Width - l - 3;
  804. screen = ViewportToScreen (new Point (col, i));
  805. if (screen.X < Driver.Cols)
  806. {
  807. Driver.Move (screen.X, screen.Y);
  808. Driver.AddStr (item.Help);
  809. // The shortcut tag string
  810. if (!string.IsNullOrEmpty (item.ShortcutTag))
  811. {
  812. Driver.Move (screen.X + l - item.ShortcutTag.GetColumns (), screen.Y);
  813. Driver.AddStr (item.ShortcutTag);
  814. }
  815. }
  816. }
  817. }
  818. SetClip (savedClip);
  819. }
  820. }