Menu.cs 28 KB

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