Menu.cs 28 KB

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