Menu.cs 27 KB

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