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