Menu.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. #nullable enable
  2. using static System.Formats.Asn1.AsnWriter;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// An internal class used to represent a menu pop-up menu. Created and managed by <see cref="MenuBar"/> and
  6. /// <see cref="ContextMenu"/>.
  7. /// </summary>
  8. internal sealed class Menu : View
  9. {
  10. private readonly MenuBarItem? _barItems;
  11. private readonly MenuBar _host;
  12. internal int _currentChild;
  13. internal View? _previousSubFocused;
  14. internal static Rectangle MakeFrame (int x, int y, MenuItem? []? items, Menu? parent = null)
  15. {
  16. if (items is null || items.Length == 0)
  17. {
  18. return Rectangle.Empty;
  19. }
  20. int minX = x;
  21. int minY = y;
  22. const int borderOffset = 2; // This 2 is for the space around
  23. int maxW = (items.Max (z => z?.Width) ?? 0) + borderOffset;
  24. int maxH = items.Length + borderOffset;
  25. if (parent is { } && x + maxW > Driver.Cols)
  26. {
  27. minX = Math.Max (parent.Frame.Right - parent.Frame.Width - maxW, 0);
  28. }
  29. if (y + maxH > Driver.Rows)
  30. {
  31. minY = Math.Max (Driver.Rows - maxH, 0);
  32. }
  33. return new (minX, minY, maxW, maxH);
  34. }
  35. internal required MenuBar Host
  36. {
  37. get => _host;
  38. init
  39. {
  40. ArgumentNullException.ThrowIfNull (value);
  41. _host = value;
  42. }
  43. }
  44. internal required MenuBarItem? BarItems
  45. {
  46. get => _barItems!;
  47. init
  48. {
  49. ArgumentNullException.ThrowIfNull (value);
  50. _barItems = value;
  51. // Debugging aid so ToString() is helpful
  52. Text = _barItems.Title;
  53. }
  54. }
  55. internal Menu? Parent { get; init; }
  56. public override void BeginInit ()
  57. {
  58. base.BeginInit ();
  59. Frame = MakeFrame (Frame.X, Frame.Y, _barItems!.Children!, Parent);
  60. if (_barItems.Children is { })
  61. {
  62. foreach (MenuItem? menuItem in _barItems.Children)
  63. {
  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 () == true)
  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.Top is { })
  125. {
  126. Application.Top.DrawContentComplete += Current_DrawContentComplete;
  127. Application.Top.SizeChanging += Current_TerminalResized;
  128. }
  129. Application.MouseEvent += Application_RootMouseEvent;
  130. // Things this view knows how to do
  131. AddCommand (Command.Up, () => MoveUp ());
  132. AddCommand (Command.Down, () => 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.Toggle, 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.Up);
  162. KeyBindings.Add (Key.CursorDown, Command.Down);
  163. KeyBindings.Add (Key.CursorLeft, Command.Left);
  164. KeyBindings.Add (Key.CursorRight, Command.Right);
  165. KeyBindings.Add (Key.Esc, Command.Cancel);
  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. protected override bool OnKeyDownNotHandled (Key keyEvent)
  258. {
  259. // We didn't handle the key, pass it on to host
  260. return _host.InvokeCommandsBoundToKey (keyEvent) == true;
  261. }
  262. private void Current_TerminalResized (object? sender, SizeChangedEventArgs e)
  263. {
  264. if (_host.IsMenuOpen)
  265. {
  266. _host.CloseAllMenus ();
  267. }
  268. }
  269. /// <inheritdoc/>
  270. protected override void OnVisibleChanged ()
  271. {
  272. base.OnVisibleChanged ();
  273. if (Visible)
  274. {
  275. Application.MouseEvent += Application_RootMouseEvent;
  276. }
  277. else
  278. {
  279. Application.MouseEvent -= Application_RootMouseEvent;
  280. }
  281. }
  282. private void Application_RootMouseEvent (object? sender, MouseEventArgs a)
  283. {
  284. if (a.View is { } and (MenuBar or not Menu))
  285. {
  286. return;
  287. }
  288. if (!Visible)
  289. {
  290. throw new InvalidOperationException ("This shouldn't running on a invisible menu!");
  291. }
  292. View view = a.View ?? this;
  293. Point boundsPoint = view.ScreenToViewport (new (a.Position.X, a.Position.Y));
  294. var me = new MouseEventArgs
  295. {
  296. Position = boundsPoint,
  297. Flags = a.Flags,
  298. ScreenPosition = a.Position,
  299. View = view
  300. };
  301. if (view.NewMouseEvent (me) == true || a.Flags == MouseFlags.Button1Pressed || a.Flags == MouseFlags.Button1Released)
  302. {
  303. a.Handled = true;
  304. }
  305. }
  306. internal Attribute DetermineColorSchemeFor (MenuItem? item, int index)
  307. {
  308. if (item is null)
  309. {
  310. return GetNormalColor ();
  311. }
  312. if (index == _currentChild)
  313. {
  314. return GetFocusColor ();
  315. }
  316. return !item.IsEnabled () ? ColorScheme!.Disabled : GetNormalColor ();
  317. }
  318. public override void OnDrawContent (Rectangle viewport)
  319. {
  320. if (_barItems!.Children is null)
  321. {
  322. return;
  323. }
  324. Rectangle savedClip = Driver.Clip;
  325. Driver.Clip = new (0, 0, Driver.Cols, Driver.Rows);
  326. Driver.SetAttribute (GetNormalColor ());
  327. OnDrawAdornments ();
  328. OnRenderLineCanvas ();
  329. for (int i = Viewport.Y; i < _barItems!.Children.Length; i++)
  330. {
  331. if (i < 0)
  332. {
  333. continue;
  334. }
  335. if (ViewportToScreen (Viewport).Y + i >= Driver.Rows)
  336. {
  337. break;
  338. }
  339. MenuItem? item = _barItems.Children [i];
  340. Driver.SetAttribute (
  341. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  342. item is null ? GetNormalColor () :
  343. i == _currentChild ? GetFocusColor () : GetNormalColor ()
  344. );
  345. if (item is null && BorderStyle != LineStyle.None)
  346. {
  347. Point s = ViewportToScreen (new Point (-1, i));
  348. Driver.Move (s.X, s.Y);
  349. Driver.AddRune (Glyphs.LeftTee);
  350. }
  351. else if (Frame.X < Driver.Cols)
  352. {
  353. Move (0, i);
  354. }
  355. Driver.SetAttribute (DetermineColorSchemeFor (item, i));
  356. for (int p = Viewport.X; p < Frame.Width - 2; p++)
  357. {
  358. // This - 2 is for the border
  359. if (p < 0)
  360. {
  361. continue;
  362. }
  363. if (ViewportToScreen (Viewport).X + p >= Driver.Cols)
  364. {
  365. break;
  366. }
  367. if (item is null)
  368. {
  369. Driver.AddRune (Glyphs.HLine);
  370. }
  371. else if (i == 0 && p == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  372. {
  373. Driver.AddRune (Glyphs.LeftArrow);
  374. }
  375. // This `- 3` is left border + right border + one row in from right
  376. else if (p == Frame.Width - 3 && _barItems?.SubMenu (_barItems.Children [i]!) is { })
  377. {
  378. Driver.AddRune (Glyphs.RightArrow);
  379. }
  380. else
  381. {
  382. Driver.AddRune ((Rune)' ');
  383. }
  384. }
  385. if (item is null)
  386. {
  387. if (BorderStyle != LineStyle.None && SuperView?.Frame.Right - Frame.X > Frame.Width)
  388. {
  389. Point s = ViewportToScreen (new Point (Frame.Width - 2, i));
  390. Driver.Move (s.X, s.Y);
  391. Driver.AddRune (Glyphs.RightTee);
  392. }
  393. continue;
  394. }
  395. string? textToDraw;
  396. Rune nullCheckedChar = Glyphs.CheckStateNone;
  397. Rune checkChar = Glyphs.Selected;
  398. Rune uncheckedChar = Glyphs.UnSelected;
  399. if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked))
  400. {
  401. checkChar = Glyphs.CheckStateChecked;
  402. uncheckedChar = Glyphs.CheckStateUnChecked;
  403. }
  404. // Support Checked even though CheckType wasn't set
  405. if (item is { CheckType: MenuItemCheckStyle.Checked, Checked: null })
  406. {
  407. textToDraw = $"{nullCheckedChar} {item.Title}";
  408. }
  409. else if (item.Checked == true)
  410. {
  411. textToDraw = $"{checkChar} {item.Title}";
  412. }
  413. else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) || item.CheckType.HasFlag (MenuItemCheckStyle.Radio))
  414. {
  415. textToDraw = $"{uncheckedChar} {item.Title}";
  416. }
  417. else
  418. {
  419. textToDraw = item.Title;
  420. }
  421. Point screen = ViewportToScreen (new Point (0, i));
  422. if (screen.X < Driver.Cols)
  423. {
  424. Driver.Move (screen.X + 1, screen.Y);
  425. if (!item.IsEnabled ())
  426. {
  427. DrawHotString (textToDraw, ColorScheme!.Disabled, ColorScheme.Disabled);
  428. }
  429. else if (i == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  430. {
  431. var tf = new TextFormatter
  432. {
  433. ConstrainToWidth = Frame.Width - 3,
  434. ConstrainToHeight = 1,
  435. Alignment = Alignment.Center, HotKeySpecifier = MenuBar.HotKeySpecifier, Text = textToDraw
  436. };
  437. // The -3 is left/right border + one space (not sure what for)
  438. tf.Draw (
  439. ViewportToScreen (new Rectangle (1, i, Frame.Width - 3, 1)),
  440. i == _currentChild ? GetFocusColor () : GetNormalColor (),
  441. i == _currentChild ? ColorScheme!.HotFocus : ColorScheme!.HotNormal,
  442. SuperView?.ViewportToScreen (SuperView.Viewport) ?? Rectangle.Empty
  443. );
  444. }
  445. else
  446. {
  447. DrawHotString (
  448. textToDraw,
  449. i == _currentChild ? ColorScheme!.HotFocus : ColorScheme!.HotNormal,
  450. i == _currentChild ? GetFocusColor () : GetNormalColor ()
  451. );
  452. }
  453. // The help string
  454. int l = item.ShortcutTag.GetColumns () == 0
  455. ? item.Help.GetColumns ()
  456. : item.Help.GetColumns () + item.ShortcutTag.GetColumns () + 2;
  457. int col = Frame.Width - l - 3;
  458. screen = ViewportToScreen (new Point (col, i));
  459. if (screen.X < Driver.Cols)
  460. {
  461. Driver.Move (screen.X, screen.Y);
  462. Driver.AddStr (item.Help);
  463. // The shortcut tag string
  464. if (!string.IsNullOrEmpty (item.ShortcutTag))
  465. {
  466. Driver.Move (screen.X + l - item.ShortcutTag.GetColumns (), screen.Y);
  467. Driver.AddStr (item.ShortcutTag);
  468. }
  469. }
  470. }
  471. }
  472. Driver.Clip = savedClip;
  473. // PositionCursor ();
  474. }
  475. private void Current_DrawContentComplete (object? sender, DrawEventArgs e)
  476. {
  477. if (Visible)
  478. {
  479. OnDrawContent (Viewport);
  480. }
  481. }
  482. public override Point? PositionCursor ()
  483. {
  484. if (_host.IsMenuOpen)
  485. {
  486. if (_barItems!.IsTopLevel)
  487. {
  488. return _host.PositionCursor ();
  489. }
  490. Move (2, 1 + _currentChild);
  491. return null; // Don't show the cursor
  492. }
  493. return _host.PositionCursor ();
  494. }
  495. public void Run (Action? action)
  496. {
  497. if (action is null)
  498. {
  499. return;
  500. }
  501. Application.UngrabMouse ();
  502. _host.CloseAllMenus ();
  503. Application.Driver!.ClearContents ();
  504. Application.Refresh ();
  505. _host.Run (action);
  506. }
  507. protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view)
  508. {
  509. if (!newHasFocus)
  510. {
  511. _host.LostFocus (previousFocusedView!);
  512. }
  513. }
  514. private void RunSelected ()
  515. {
  516. if (_barItems!.IsTopLevel)
  517. {
  518. Run (_barItems.Action);
  519. }
  520. else
  521. {
  522. switch (_currentChild)
  523. {
  524. case > -1 when _barItems.Children! [_currentChild]!.Action != null!:
  525. Run (_barItems.Children [_currentChild]?.Action);
  526. break;
  527. case 0 when _host.UseSubMenusSingleFrame && _barItems.Children [_currentChild]?.Parent!.Parent != null:
  528. _host.PreviousMenu (_barItems.Children [_currentChild]!.Parent!.IsFromSubMenu, true);
  529. break;
  530. case > -1 when _barItems.SubMenu (_barItems.Children [_currentChild]) != null!:
  531. CheckSubMenu ();
  532. break;
  533. }
  534. }
  535. }
  536. private void CloseAllMenus ()
  537. {
  538. Application.UngrabMouse ();
  539. _host.CloseAllMenus ();
  540. }
  541. private bool MoveDown ()
  542. {
  543. if (_barItems!.IsTopLevel)
  544. {
  545. return true;
  546. }
  547. bool disabled;
  548. do
  549. {
  550. _currentChild++;
  551. if (_currentChild >= _barItems?.Children?.Length)
  552. {
  553. _currentChild = 0;
  554. }
  555. if (this != _host.OpenCurrentMenu && _barItems?.Children? [_currentChild]?.IsFromSubMenu == true && _host._selectedSub > -1)
  556. {
  557. _host.PreviousMenu (true);
  558. _host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild);
  559. _host.OpenCurrentMenu = this;
  560. }
  561. MenuItem? item = _barItems?.Children? [_currentChild];
  562. if (item?.IsEnabled () != true)
  563. {
  564. disabled = true;
  565. }
  566. else
  567. {
  568. disabled = false;
  569. }
  570. if (_host is { UseSubMenusSingleFrame: false, UseKeysUpDownAsKeysLeftRight: true }
  571. && _barItems?.SubMenu (_barItems?.Children? [_currentChild]!) != null
  572. && !disabled
  573. && _host.IsMenuOpen)
  574. {
  575. if (!CheckSubMenu ())
  576. {
  577. return false;
  578. }
  579. break;
  580. }
  581. if (!_host.IsMenuOpen)
  582. {
  583. _host.OpenMenu (_host._selected);
  584. }
  585. }
  586. while (_barItems?.Children? [_currentChild] is null || disabled);
  587. SetNeedsDisplay ();
  588. SetParentSetNeedsDisplay ();
  589. if (!_host.UseSubMenusSingleFrame)
  590. {
  591. _host.OnMenuOpened ();
  592. }
  593. return true;
  594. }
  595. private bool MoveUp ()
  596. {
  597. if (_barItems!.IsTopLevel || _currentChild == -1)
  598. {
  599. return true;
  600. }
  601. bool disabled;
  602. do
  603. {
  604. _currentChild--;
  605. if (_host.UseKeysUpDownAsKeysLeftRight && !_host.UseSubMenusSingleFrame)
  606. {
  607. if ((_currentChild == -1 || this != _host.OpenCurrentMenu)
  608. && _barItems.Children! [_currentChild + 1]!.IsFromSubMenu
  609. && _host._selectedSub > -1)
  610. {
  611. _currentChild++;
  612. _host.PreviousMenu (true);
  613. if (_currentChild > 0)
  614. {
  615. _currentChild--;
  616. _host.OpenCurrentMenu = this;
  617. }
  618. break;
  619. }
  620. }
  621. if (_currentChild < 0)
  622. {
  623. _currentChild = _barItems.Children!.Length - 1;
  624. }
  625. if (!_host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild, false))
  626. {
  627. _currentChild = 0;
  628. if (!_host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild) && !_host.CloseMenu ())
  629. {
  630. return false;
  631. }
  632. break;
  633. }
  634. MenuItem item = _barItems.Children! [_currentChild]!;
  635. disabled = item.IsEnabled () != true;
  636. if (_host.UseSubMenusSingleFrame
  637. || !_host.UseKeysUpDownAsKeysLeftRight
  638. || _barItems.SubMenu (_barItems.Children [_currentChild]!) == null!
  639. || disabled
  640. || !_host.IsMenuOpen)
  641. {
  642. continue;
  643. }
  644. if (!CheckSubMenu ())
  645. {
  646. return false;
  647. }
  648. break;
  649. }
  650. while (_barItems.Children [_currentChild] is null || disabled);
  651. SetNeedsDisplay ();
  652. SetParentSetNeedsDisplay ();
  653. if (!_host.UseSubMenusSingleFrame)
  654. {
  655. _host.OnMenuOpened ();
  656. }
  657. return true;
  658. }
  659. private void SetParentSetNeedsDisplay ()
  660. {
  661. if (_host._openSubMenu is { })
  662. {
  663. foreach (Menu menu in _host._openSubMenu)
  664. {
  665. menu.SetNeedsDisplay ();
  666. }
  667. }
  668. _host._openMenu?.SetNeedsDisplay ();
  669. _host.SetNeedsDisplay ();
  670. }
  671. protected override bool OnMouseEvent (MouseEventArgs me)
  672. {
  673. if (!_host._handled && !_host.HandleGrabView (me, this))
  674. {
  675. return false;
  676. }
  677. _host._handled = false;
  678. bool disabled;
  679. if (me.Flags == MouseFlags.Button1Clicked)
  680. {
  681. disabled = false;
  682. if (me.Position.Y < 0)
  683. {
  684. return me.Handled = true;
  685. }
  686. if (me.Position.Y >= _barItems!.Children!.Length)
  687. {
  688. return me.Handled = true;
  689. }
  690. MenuItem item = _barItems.Children [me.Position.Y]!;
  691. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  692. if (item is null || !item.IsEnabled ())
  693. {
  694. disabled = true;
  695. }
  696. if (disabled)
  697. {
  698. return me.Handled = true;
  699. }
  700. _currentChild = me.Position.Y;
  701. RunSelected ();
  702. return me.Handled = true;
  703. }
  704. if (me.Flags != MouseFlags.Button1Pressed
  705. && me.Flags != MouseFlags.Button1DoubleClicked
  706. && me.Flags != MouseFlags.Button1TripleClicked
  707. && me.Flags != MouseFlags.ReportMousePosition
  708. && !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  709. {
  710. return false;
  711. }
  712. {
  713. disabled = false;
  714. if (me.Position.Y < 0 || me.Position.Y >= _barItems!.Children!.Length)
  715. {
  716. return me.Handled = true;
  717. }
  718. MenuItem item = _barItems.Children [me.Position.Y]!;
  719. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  720. if (item is null)
  721. {
  722. return me.Handled = true;
  723. }
  724. if (item.IsEnabled () != true)
  725. {
  726. disabled = true;
  727. }
  728. if (!disabled)
  729. {
  730. _currentChild = me.Position.Y;
  731. }
  732. if (_host.UseSubMenusSingleFrame || !CheckSubMenu ())
  733. {
  734. SetNeedsDisplay ();
  735. SetParentSetNeedsDisplay ();
  736. return me.Handled = true;
  737. }
  738. _host.OnMenuOpened ();
  739. return me.Handled = true;
  740. }
  741. }
  742. internal bool CheckSubMenu ()
  743. {
  744. if (_currentChild == -1 || _barItems?.Children? [_currentChild] is null)
  745. {
  746. return true;
  747. }
  748. MenuBarItem? subMenu = _barItems.SubMenu (_barItems.Children [_currentChild]!);
  749. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  750. if (subMenu is { })
  751. {
  752. int pos = -1;
  753. if (_host._openSubMenu is { })
  754. {
  755. pos = _host._openSubMenu.FindIndex (o => o._barItems == subMenu);
  756. }
  757. if (pos == -1
  758. && this != _host.OpenCurrentMenu
  759. && subMenu.Children != _host.OpenCurrentMenu!._barItems!.Children
  760. && !_host.CloseMenu (false, true))
  761. {
  762. return false;
  763. }
  764. _host.Activate (_host._selected, pos, subMenu);
  765. }
  766. else if (_host._openSubMenu?.Count == 0 || _host._openSubMenu?.Last ()._barItems!.IsSubMenuOf (_barItems.Children [_currentChild]!) == false)
  767. {
  768. return _host.CloseMenu (false, true);
  769. }
  770. else
  771. {
  772. SetNeedsDisplay ();
  773. SetParentSetNeedsDisplay ();
  774. }
  775. return true;
  776. }
  777. protected override void Dispose (bool disposing)
  778. {
  779. RemoveKeyBindingsHotKey (_barItems);
  780. if (Application.Top is { })
  781. {
  782. Application.Top.DrawContentComplete -= Current_DrawContentComplete;
  783. Application.Top.SizeChanging -= Current_TerminalResized;
  784. }
  785. Application.MouseEvent -= Application_RootMouseEvent;
  786. base.Dispose (disposing);
  787. }
  788. }