Menu.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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], 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. 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. bool? handled = null;
  208. return _host.InvokeCommandsBoundToHotKey (keyEvent, ref handled) == 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 CloseAllMenus ()
  427. {
  428. Application.UngrabMouse ();
  429. _host.CloseAllMenus ();
  430. }
  431. private void Current_TerminalResized (object? sender, SizeChangedEventArgs e)
  432. {
  433. if (_host.IsMenuOpen)
  434. {
  435. _host.CloseAllMenus ();
  436. }
  437. }
  438. /// <summary>Called when a key bound to Command.ToggleExpandCollapse is pressed. This means a hot key was pressed.</summary>
  439. /// <returns></returns>
  440. private bool ExpandCollapse (MenuItem? menuItem)
  441. {
  442. if (!IsInitialized || !Visible)
  443. {
  444. return true;
  445. }
  446. for (var c = 0; c < _barItems!.Children!.Length; c++)
  447. {
  448. if (_barItems.Children [c] == menuItem)
  449. {
  450. _currentChild = c;
  451. break;
  452. }
  453. }
  454. if (menuItem is { })
  455. {
  456. var m = menuItem as MenuBarItem;
  457. if (m?.Children?.Length > 0)
  458. {
  459. MenuItem? item = _barItems.Children [_currentChild];
  460. if (item is null)
  461. {
  462. return true;
  463. }
  464. // ReSharper disable once ConditionIsAlwaysTrueOrFalse
  465. bool disabled = item is null || !item.IsEnabled ();
  466. if (!disabled && (_host.UseSubMenusSingleFrame || !CheckSubMenu ()))
  467. {
  468. SetNeedsDraw ();
  469. SetParentSetNeedsDisplay ();
  470. return true;
  471. }
  472. if (!disabled)
  473. {
  474. _host.OnMenuOpened ();
  475. }
  476. }
  477. else
  478. {
  479. _host.SelectItem (menuItem);
  480. }
  481. }
  482. else if (_host.IsMenuOpen)
  483. {
  484. _host.CloseAllMenus ();
  485. }
  486. else
  487. {
  488. _host.OpenMenu ();
  489. }
  490. return true;
  491. }
  492. private bool MoveDown ()
  493. {
  494. if (_barItems!.IsTopLevel)
  495. {
  496. return true;
  497. }
  498. bool disabled;
  499. do
  500. {
  501. _currentChild++;
  502. if (_currentChild >= _barItems?.Children?.Length)
  503. {
  504. _currentChild = 0;
  505. }
  506. if (this != _host.OpenCurrentMenu && _barItems?.Children? [_currentChild]?.IsFromSubMenu == true && _host._selectedSub > -1)
  507. {
  508. _host.PreviousMenu (true);
  509. _host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild);
  510. _host.OpenCurrentMenu = this;
  511. }
  512. MenuItem? item = _barItems?.Children? [_currentChild];
  513. if (item?.IsEnabled () != true)
  514. {
  515. disabled = true;
  516. }
  517. else
  518. {
  519. disabled = false;
  520. }
  521. if (_host is { UseSubMenusSingleFrame: false, UseKeysUpDownAsKeysLeftRight: true }
  522. && _barItems?.SubMenu (_barItems?.Children? [_currentChild]!) != null
  523. && !disabled
  524. && _host.IsMenuOpen)
  525. {
  526. if (!CheckSubMenu ())
  527. {
  528. return false;
  529. }
  530. break;
  531. }
  532. if (!_host.IsMenuOpen)
  533. {
  534. _host.OpenMenu (_host._selected);
  535. }
  536. }
  537. while (_barItems?.Children? [_currentChild] is null || disabled);
  538. SetNeedsDraw ();
  539. SetParentSetNeedsDisplay ();
  540. if (!_host.UseSubMenusSingleFrame)
  541. {
  542. _host.OnMenuOpened ();
  543. }
  544. return true;
  545. }
  546. private bool MoveUp ()
  547. {
  548. if (_barItems!.IsTopLevel || _currentChild == -1)
  549. {
  550. return true;
  551. }
  552. bool disabled;
  553. do
  554. {
  555. _currentChild--;
  556. if (_host.UseKeysUpDownAsKeysLeftRight && !_host.UseSubMenusSingleFrame)
  557. {
  558. if ((_currentChild == -1 || this != _host.OpenCurrentMenu)
  559. && _barItems.Children! [_currentChild + 1]!.IsFromSubMenu
  560. && _host._selectedSub > -1)
  561. {
  562. _currentChild++;
  563. _host.PreviousMenu (true);
  564. if (_currentChild > 0)
  565. {
  566. _currentChild--;
  567. _host.OpenCurrentMenu = this;
  568. }
  569. break;
  570. }
  571. }
  572. if (_currentChild < 0)
  573. {
  574. _currentChild = _barItems.Children!.Length - 1;
  575. }
  576. if (!_host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild, false))
  577. {
  578. _currentChild = 0;
  579. if (!_host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild) && !_host.CloseMenu ())
  580. {
  581. return false;
  582. }
  583. break;
  584. }
  585. MenuItem item = _barItems.Children! [_currentChild]!;
  586. disabled = item.IsEnabled () != true;
  587. if (_host.UseSubMenusSingleFrame
  588. || !_host.UseKeysUpDownAsKeysLeftRight
  589. || _barItems.SubMenu (_barItems.Children [_currentChild]!) == null!
  590. || disabled
  591. || !_host.IsMenuOpen)
  592. {
  593. continue;
  594. }
  595. if (!CheckSubMenu ())
  596. {
  597. return false;
  598. }
  599. break;
  600. }
  601. while (_barItems.Children [_currentChild] is null || disabled);
  602. SetNeedsDraw ();
  603. SetParentSetNeedsDisplay ();
  604. if (!_host.UseSubMenusSingleFrame)
  605. {
  606. _host.OnMenuOpened ();
  607. }
  608. return true;
  609. }
  610. private void RemoveKeyBindingsHotKey (MenuBarItem? menuBarItem)
  611. {
  612. if (menuBarItem is null || menuBarItem.Children is null)
  613. {
  614. return;
  615. }
  616. IEnumerable<MenuItem> menuItems = menuBarItem.Children.Where (m => m is { })!;
  617. foreach (MenuItem menuItem in menuItems)
  618. {
  619. if (menuItem.HotKey != Key.Empty)
  620. {
  621. KeyBindings.Remove (menuItem.HotKey!);
  622. KeyBindings.Remove (menuItem.HotKey!.WithAlt);
  623. }
  624. }
  625. }
  626. private void RunSelected ()
  627. {
  628. if (_barItems!.IsTopLevel)
  629. {
  630. Run (_barItems.Action);
  631. }
  632. else
  633. {
  634. switch (_currentChild)
  635. {
  636. case > -1 when _barItems.Children! [_currentChild]!.Action != null!:
  637. Run (_barItems.Children [_currentChild]?.Action);
  638. break;
  639. case 0 when _host.UseSubMenusSingleFrame && _barItems.Children [_currentChild]?.Parent!.Parent != null:
  640. _host.PreviousMenu (_barItems.Children [_currentChild]!.Parent!.IsFromSubMenu, true);
  641. break;
  642. case > -1 when _barItems.SubMenu (_barItems.Children [_currentChild]) != null!:
  643. CheckSubMenu ();
  644. break;
  645. }
  646. }
  647. }
  648. private void SetParentSetNeedsDisplay ()
  649. {
  650. if (_host._openSubMenu is { })
  651. {
  652. foreach (Menu menu in _host._openSubMenu)
  653. {
  654. menu.SetNeedsDraw ();
  655. }
  656. }
  657. _host._openMenu?.SetNeedsDraw ();
  658. _host.SetNeedsDraw ();
  659. }
  660. // By doing this we draw last, over everything else.
  661. private void Top_DrawComplete (object? sender, DrawEventArgs e)
  662. {
  663. if (!Visible)
  664. {
  665. return;
  666. }
  667. if (_barItems!.Children is null)
  668. {
  669. return;
  670. }
  671. DrawBorderAndPadding ();
  672. RenderLineCanvas ();
  673. // BUGBUG: Views should not change the clip. Doing so is an indcation of poor design or a bug in the framework.
  674. Region? savedClip = SetClipToScreen ();
  675. SetAttribute (GetNormalColor ());
  676. for (int i = Viewport.Y; i < _barItems!.Children.Length; i++)
  677. {
  678. if (i < 0)
  679. {
  680. continue;
  681. }
  682. if (ViewportToScreen (Viewport).Y + i >= Driver.Rows)
  683. {
  684. break;
  685. }
  686. MenuItem? item = _barItems.Children [i];
  687. SetAttribute (
  688. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  689. item is null ? GetNormalColor () :
  690. i == _currentChild ? GetFocusColor () : GetNormalColor ()
  691. );
  692. if (item is null && BorderStyle != LineStyle.None)
  693. {
  694. Point s = ViewportToScreen (new Point (-1, i));
  695. Driver.Move (s.X, s.Y);
  696. Driver.AddRune (Glyphs.LeftTee);
  697. }
  698. else if (Frame.X < Driver.Cols)
  699. {
  700. Move (0, i);
  701. }
  702. SetAttribute (DetermineColorSchemeFor (item, i));
  703. for (int p = Viewport.X; p < Frame.Width - 2; p++)
  704. {
  705. // This - 2 is for the border
  706. if (p < 0)
  707. {
  708. continue;
  709. }
  710. if (ViewportToScreen (Viewport).X + p >= Driver.Cols)
  711. {
  712. break;
  713. }
  714. if (item is null)
  715. {
  716. Driver.AddRune (Glyphs.HLine);
  717. }
  718. else if (i == 0 && p == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  719. {
  720. Driver.AddRune (Glyphs.LeftArrow);
  721. }
  722. // This `- 3` is left border + right border + one row in from right
  723. else if (p == Frame.Width - 3 && _barItems?.SubMenu (_barItems.Children [i]!) is { })
  724. {
  725. Driver.AddRune (Glyphs.RightArrow);
  726. }
  727. else
  728. {
  729. Driver.AddRune ((Rune)' ');
  730. }
  731. }
  732. if (item is null)
  733. {
  734. if (BorderStyle != LineStyle.None && SuperView?.Frame.Right - Frame.X > Frame.Width)
  735. {
  736. Point s = ViewportToScreen (new Point (Frame.Width - 2, i));
  737. Driver.Move (s.X, s.Y);
  738. Driver.AddRune (Glyphs.RightTee);
  739. }
  740. continue;
  741. }
  742. string? textToDraw;
  743. Rune nullCheckedChar = Glyphs.CheckStateNone;
  744. Rune checkChar = Glyphs.Selected;
  745. Rune uncheckedChar = Glyphs.UnSelected;
  746. if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked))
  747. {
  748. checkChar = Glyphs.CheckStateChecked;
  749. uncheckedChar = Glyphs.CheckStateUnChecked;
  750. }
  751. // Support Checked even though CheckType wasn't set
  752. if (item is { CheckType: MenuItemCheckStyle.Checked, Checked: null })
  753. {
  754. textToDraw = $"{nullCheckedChar} {item.Title}";
  755. }
  756. else if (item.Checked == true)
  757. {
  758. textToDraw = $"{checkChar} {item.Title}";
  759. }
  760. else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) || item.CheckType.HasFlag (MenuItemCheckStyle.Radio))
  761. {
  762. textToDraw = $"{uncheckedChar} {item.Title}";
  763. }
  764. else
  765. {
  766. textToDraw = item.Title;
  767. }
  768. Point screen = ViewportToScreen (new Point (0, i));
  769. if (screen.X < Driver.Cols)
  770. {
  771. Driver.Move (screen.X + 1, screen.Y);
  772. if (!item.IsEnabled ())
  773. {
  774. DrawHotString (textToDraw, ColorScheme!.Disabled, ColorScheme.Disabled);
  775. }
  776. else if (i == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  777. {
  778. var tf = new TextFormatter
  779. {
  780. ConstrainToWidth = Frame.Width - 3,
  781. ConstrainToHeight = 1,
  782. Alignment = Alignment.Center, HotKeySpecifier = MenuBar.HotKeySpecifier, Text = textToDraw
  783. };
  784. // The -3 is left/right border + one space (not sure what for)
  785. tf.Draw (
  786. ViewportToScreen (new Rectangle (1, i, Frame.Width - 3, 1)),
  787. i == _currentChild ? GetFocusColor () : GetNormalColor (),
  788. i == _currentChild ? ColorScheme!.HotFocus : ColorScheme!.HotNormal,
  789. SuperView?.ViewportToScreen (SuperView.Viewport) ?? Rectangle.Empty
  790. );
  791. }
  792. else
  793. {
  794. DrawHotString (
  795. textToDraw,
  796. i == _currentChild ? ColorScheme!.HotFocus : ColorScheme!.HotNormal,
  797. i == _currentChild ? GetFocusColor () : GetNormalColor ()
  798. );
  799. }
  800. // The help string
  801. int l = item.ShortcutTag.GetColumns () == 0
  802. ? item.Help.GetColumns ()
  803. : item.Help.GetColumns () + item.ShortcutTag.GetColumns () + 2;
  804. int col = Frame.Width - l - 3;
  805. screen = ViewportToScreen (new Point (col, i));
  806. if (screen.X < Driver.Cols)
  807. {
  808. Driver.Move (screen.X, screen.Y);
  809. Driver.AddStr (item.Help);
  810. // The shortcut tag string
  811. if (!string.IsNullOrEmpty (item.ShortcutTag))
  812. {
  813. Driver.Move (screen.X + l - item.ShortcutTag.GetColumns (), screen.Y);
  814. Driver.AddStr (item.ShortcutTag);
  815. }
  816. }
  817. }
  818. }
  819. SetClip (savedClip);
  820. }
  821. }