Menu.cs 28 KB

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