Menu.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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.DrawComplete += Top_DrawComplete;
  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. SetNeedsDraw ();
  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. // By doing this we draw last, over everything else.
  329. private void Top_DrawComplete (object? sender, DrawEventArgs e)
  330. {
  331. if (!Visible)
  332. {
  333. return;
  334. }
  335. if (_barItems!.Children is null)
  336. {
  337. return;
  338. }
  339. DrawAdornments ();
  340. RenderLineCanvas ();
  341. Region savedClip = Driver.Clip;
  342. Driver.Clip = new (new (0, 0, Driver.Cols, Driver.Rows));
  343. SetAttribute (GetNormalColor ());
  344. for (int i = Viewport.Y; i < _barItems!.Children.Length; i++)
  345. {
  346. if (i < 0)
  347. {
  348. continue;
  349. }
  350. if (ViewportToScreen (Viewport).Y + i >= Driver.Rows)
  351. {
  352. break;
  353. }
  354. MenuItem? item = _barItems.Children [i];
  355. SetAttribute (
  356. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  357. item is null ? GetNormalColor () :
  358. i == _currentChild ? GetFocusColor () : GetNormalColor ()
  359. );
  360. if (item is null && BorderStyle != LineStyle.None)
  361. {
  362. Point s = ViewportToScreen (new Point (-1, i));
  363. Driver.Move (s.X, s.Y);
  364. Driver.AddRune (Glyphs.LeftTee);
  365. }
  366. else if (Frame.X < Driver.Cols)
  367. {
  368. Move (0, i);
  369. }
  370. SetAttribute (DetermineColorSchemeFor (item, i));
  371. for (int p = Viewport.X; p < Frame.Width - 2; p++)
  372. {
  373. // This - 2 is for the border
  374. if (p < 0)
  375. {
  376. continue;
  377. }
  378. if (ViewportToScreen (Viewport).X + p >= Driver.Cols)
  379. {
  380. break;
  381. }
  382. if (item is null)
  383. {
  384. Driver.AddRune (Glyphs.HLine);
  385. }
  386. else if (i == 0 && p == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  387. {
  388. Driver.AddRune (Glyphs.LeftArrow);
  389. }
  390. // This `- 3` is left border + right border + one row in from right
  391. else if (p == Frame.Width - 3 && _barItems?.SubMenu (_barItems.Children [i]!) is { })
  392. {
  393. Driver.AddRune (Glyphs.RightArrow);
  394. }
  395. else
  396. {
  397. Driver.AddRune ((Rune)' ');
  398. }
  399. }
  400. if (item is null)
  401. {
  402. if (BorderStyle != LineStyle.None && SuperView?.Frame.Right - Frame.X > Frame.Width)
  403. {
  404. Point s = ViewportToScreen (new Point (Frame.Width - 2, i));
  405. Driver.Move (s.X, s.Y);
  406. Driver.AddRune (Glyphs.RightTee);
  407. }
  408. continue;
  409. }
  410. string? textToDraw;
  411. Rune nullCheckedChar = Glyphs.CheckStateNone;
  412. Rune checkChar = Glyphs.Selected;
  413. Rune uncheckedChar = Glyphs.UnSelected;
  414. if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked))
  415. {
  416. checkChar = Glyphs.CheckStateChecked;
  417. uncheckedChar = Glyphs.CheckStateUnChecked;
  418. }
  419. // Support Checked even though CheckType wasn't set
  420. if (item is { CheckType: MenuItemCheckStyle.Checked, Checked: null })
  421. {
  422. textToDraw = $"{nullCheckedChar} {item.Title}";
  423. }
  424. else if (item.Checked == true)
  425. {
  426. textToDraw = $"{checkChar} {item.Title}";
  427. }
  428. else if (item.CheckType.HasFlag (MenuItemCheckStyle.Checked) || item.CheckType.HasFlag (MenuItemCheckStyle.Radio))
  429. {
  430. textToDraw = $"{uncheckedChar} {item.Title}";
  431. }
  432. else
  433. {
  434. textToDraw = item.Title;
  435. }
  436. Point screen = ViewportToScreen (new Point (0, i));
  437. if (screen.X < Driver.Cols)
  438. {
  439. Driver.Move (screen.X + 1, screen.Y);
  440. if (!item.IsEnabled ())
  441. {
  442. DrawHotString (textToDraw, ColorScheme!.Disabled, ColorScheme.Disabled);
  443. }
  444. else if (i == 0 && _host.UseSubMenusSingleFrame && item.Parent!.Parent is { })
  445. {
  446. var tf = new TextFormatter
  447. {
  448. ConstrainToWidth = Frame.Width - 3,
  449. ConstrainToHeight = 1,
  450. Alignment = Alignment.Center, HotKeySpecifier = MenuBar.HotKeySpecifier, Text = textToDraw
  451. };
  452. // The -3 is left/right border + one space (not sure what for)
  453. tf.Draw (
  454. ViewportToScreen (new Rectangle (1, i, Frame.Width - 3, 1)),
  455. i == _currentChild ? GetFocusColor () : GetNormalColor (),
  456. i == _currentChild ? ColorScheme!.HotFocus : ColorScheme!.HotNormal,
  457. SuperView?.ViewportToScreen (SuperView.Viewport) ?? Rectangle.Empty
  458. );
  459. }
  460. else
  461. {
  462. DrawHotString (
  463. textToDraw,
  464. i == _currentChild ? ColorScheme!.HotFocus : ColorScheme!.HotNormal,
  465. i == _currentChild ? GetFocusColor () : GetNormalColor ()
  466. );
  467. }
  468. // The help string
  469. int l = item.ShortcutTag.GetColumns () == 0
  470. ? item.Help.GetColumns ()
  471. : item.Help.GetColumns () + item.ShortcutTag.GetColumns () + 2;
  472. int col = Frame.Width - l - 3;
  473. screen = ViewportToScreen (new Point (col, i));
  474. if (screen.X < Driver.Cols)
  475. {
  476. Driver.Move (screen.X, screen.Y);
  477. Driver.AddStr (item.Help);
  478. // The shortcut tag string
  479. if (!string.IsNullOrEmpty (item.ShortcutTag))
  480. {
  481. Driver.Move (screen.X + l - item.ShortcutTag.GetColumns (), screen.Y);
  482. Driver.AddStr (item.ShortcutTag);
  483. }
  484. }
  485. }
  486. }
  487. Driver.Clip = savedClip;
  488. }
  489. public override Point? PositionCursor ()
  490. {
  491. if (_host.IsMenuOpen)
  492. {
  493. if (_barItems!.IsTopLevel)
  494. {
  495. return _host.PositionCursor ();
  496. }
  497. Move (2, 1 + _currentChild);
  498. return null; // Don't show the cursor
  499. }
  500. return _host.PositionCursor ();
  501. }
  502. public void Run (Action? action)
  503. {
  504. if (action is null)
  505. {
  506. return;
  507. }
  508. Application.UngrabMouse ();
  509. _host.CloseAllMenus ();
  510. Application.Driver!.ClearContents ();
  511. Application.LayoutAndDrawToplevels ();
  512. _host.Run (action);
  513. }
  514. protected override void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? view)
  515. {
  516. if (!newHasFocus)
  517. {
  518. _host.LostFocus (previousFocusedView!);
  519. }
  520. }
  521. private void RunSelected ()
  522. {
  523. if (_barItems!.IsTopLevel)
  524. {
  525. Run (_barItems.Action);
  526. }
  527. else
  528. {
  529. switch (_currentChild)
  530. {
  531. case > -1 when _barItems.Children! [_currentChild]!.Action != null!:
  532. Run (_barItems.Children [_currentChild]?.Action);
  533. break;
  534. case 0 when _host.UseSubMenusSingleFrame && _barItems.Children [_currentChild]?.Parent!.Parent != null:
  535. _host.PreviousMenu (_barItems.Children [_currentChild]!.Parent!.IsFromSubMenu, true);
  536. break;
  537. case > -1 when _barItems.SubMenu (_barItems.Children [_currentChild]) != null!:
  538. CheckSubMenu ();
  539. break;
  540. }
  541. }
  542. }
  543. private void CloseAllMenus ()
  544. {
  545. Application.UngrabMouse ();
  546. _host.CloseAllMenus ();
  547. }
  548. private bool MoveDown ()
  549. {
  550. if (_barItems!.IsTopLevel)
  551. {
  552. return true;
  553. }
  554. bool disabled;
  555. do
  556. {
  557. _currentChild++;
  558. if (_currentChild >= _barItems?.Children?.Length)
  559. {
  560. _currentChild = 0;
  561. }
  562. if (this != _host.OpenCurrentMenu && _barItems?.Children? [_currentChild]?.IsFromSubMenu == true && _host._selectedSub > -1)
  563. {
  564. _host.PreviousMenu (true);
  565. _host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild);
  566. _host.OpenCurrentMenu = this;
  567. }
  568. MenuItem? item = _barItems?.Children? [_currentChild];
  569. if (item?.IsEnabled () != true)
  570. {
  571. disabled = true;
  572. }
  573. else
  574. {
  575. disabled = false;
  576. }
  577. if (_host is { UseSubMenusSingleFrame: false, UseKeysUpDownAsKeysLeftRight: true }
  578. && _barItems?.SubMenu (_barItems?.Children? [_currentChild]!) != null
  579. && !disabled
  580. && _host.IsMenuOpen)
  581. {
  582. if (!CheckSubMenu ())
  583. {
  584. return false;
  585. }
  586. break;
  587. }
  588. if (!_host.IsMenuOpen)
  589. {
  590. _host.OpenMenu (_host._selected);
  591. }
  592. }
  593. while (_barItems?.Children? [_currentChild] is null || disabled);
  594. SetNeedsDraw ();
  595. SetParentSetNeedsDisplay ();
  596. if (!_host.UseSubMenusSingleFrame)
  597. {
  598. _host.OnMenuOpened ();
  599. }
  600. return true;
  601. }
  602. private bool MoveUp ()
  603. {
  604. if (_barItems!.IsTopLevel || _currentChild == -1)
  605. {
  606. return true;
  607. }
  608. bool disabled;
  609. do
  610. {
  611. _currentChild--;
  612. if (_host.UseKeysUpDownAsKeysLeftRight && !_host.UseSubMenusSingleFrame)
  613. {
  614. if ((_currentChild == -1 || this != _host.OpenCurrentMenu)
  615. && _barItems.Children! [_currentChild + 1]!.IsFromSubMenu
  616. && _host._selectedSub > -1)
  617. {
  618. _currentChild++;
  619. _host.PreviousMenu (true);
  620. if (_currentChild > 0)
  621. {
  622. _currentChild--;
  623. _host.OpenCurrentMenu = this;
  624. }
  625. break;
  626. }
  627. }
  628. if (_currentChild < 0)
  629. {
  630. _currentChild = _barItems.Children!.Length - 1;
  631. }
  632. if (!_host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild, false))
  633. {
  634. _currentChild = 0;
  635. if (!_host.SelectEnabledItem (_barItems.Children!, _currentChild, out _currentChild) && !_host.CloseMenu ())
  636. {
  637. return false;
  638. }
  639. break;
  640. }
  641. MenuItem item = _barItems.Children! [_currentChild]!;
  642. disabled = item.IsEnabled () != true;
  643. if (_host.UseSubMenusSingleFrame
  644. || !_host.UseKeysUpDownAsKeysLeftRight
  645. || _barItems.SubMenu (_barItems.Children [_currentChild]!) == null!
  646. || disabled
  647. || !_host.IsMenuOpen)
  648. {
  649. continue;
  650. }
  651. if (!CheckSubMenu ())
  652. {
  653. return false;
  654. }
  655. break;
  656. }
  657. while (_barItems.Children [_currentChild] is null || disabled);
  658. SetNeedsDraw ();
  659. SetParentSetNeedsDisplay ();
  660. if (!_host.UseSubMenusSingleFrame)
  661. {
  662. _host.OnMenuOpened ();
  663. }
  664. return true;
  665. }
  666. private void SetParentSetNeedsDisplay ()
  667. {
  668. if (_host._openSubMenu is { })
  669. {
  670. foreach (Menu menu in _host._openSubMenu)
  671. {
  672. menu.SetNeedsDraw ();
  673. }
  674. }
  675. _host._openMenu?.SetNeedsDraw ();
  676. _host.SetNeedsDraw ();
  677. }
  678. protected override bool OnMouseEvent (MouseEventArgs me)
  679. {
  680. if (!_host._handled && !_host.HandleGrabView (me, this))
  681. {
  682. return false;
  683. }
  684. _host._handled = false;
  685. bool disabled;
  686. if (me.Flags == MouseFlags.Button1Clicked)
  687. {
  688. disabled = false;
  689. if (me.Position.Y < 0)
  690. {
  691. return me.Handled = true;
  692. }
  693. if (me.Position.Y >= _barItems!.Children!.Length)
  694. {
  695. return me.Handled = true;
  696. }
  697. MenuItem item = _barItems.Children [me.Position.Y]!;
  698. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  699. if (item is null || !item.IsEnabled ())
  700. {
  701. disabled = true;
  702. }
  703. if (disabled)
  704. {
  705. return me.Handled = true;
  706. }
  707. _currentChild = me.Position.Y;
  708. RunSelected ();
  709. return me.Handled = true;
  710. }
  711. if (me.Flags != MouseFlags.Button1Pressed
  712. && me.Flags != MouseFlags.Button1DoubleClicked
  713. && me.Flags != MouseFlags.Button1TripleClicked
  714. && me.Flags != MouseFlags.ReportMousePosition
  715. && !me.Flags.HasFlag (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  716. {
  717. return false;
  718. }
  719. {
  720. disabled = false;
  721. if (me.Position.Y < 0 || me.Position.Y >= _barItems!.Children!.Length)
  722. {
  723. return me.Handled = true;
  724. }
  725. MenuItem item = _barItems.Children [me.Position.Y]!;
  726. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  727. if (item is null)
  728. {
  729. return me.Handled = true;
  730. }
  731. if (item.IsEnabled () != true)
  732. {
  733. disabled = true;
  734. }
  735. if (!disabled)
  736. {
  737. _currentChild = me.Position.Y;
  738. }
  739. if (_host.UseSubMenusSingleFrame || !CheckSubMenu ())
  740. {
  741. SetNeedsDraw ();
  742. SetParentSetNeedsDisplay ();
  743. return me.Handled = true;
  744. }
  745. _host.OnMenuOpened ();
  746. return me.Handled = true;
  747. }
  748. }
  749. internal bool CheckSubMenu ()
  750. {
  751. if (_currentChild == -1 || _barItems?.Children? [_currentChild] is null)
  752. {
  753. return true;
  754. }
  755. MenuBarItem? subMenu = _barItems.SubMenu (_barItems.Children [_currentChild]!);
  756. // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
  757. if (subMenu is { })
  758. {
  759. int pos = -1;
  760. if (_host._openSubMenu is { })
  761. {
  762. pos = _host._openSubMenu.FindIndex (o => o._barItems == subMenu);
  763. }
  764. if (pos == -1
  765. && this != _host.OpenCurrentMenu
  766. && subMenu.Children != _host.OpenCurrentMenu!._barItems!.Children
  767. && !_host.CloseMenu (false, true))
  768. {
  769. return false;
  770. }
  771. _host.Activate (_host._selected, pos, subMenu);
  772. }
  773. else if (_host._openSubMenu?.Count == 0 || _host._openSubMenu?.Last ()._barItems!.IsSubMenuOf (_barItems.Children [_currentChild]!) == false)
  774. {
  775. return _host.CloseMenu (false, true);
  776. }
  777. else
  778. {
  779. SetNeedsDraw ();
  780. SetParentSetNeedsDisplay ();
  781. }
  782. return true;
  783. }
  784. protected override void Dispose (bool disposing)
  785. {
  786. RemoveKeyBindingsHotKey (_barItems);
  787. if (Application.Top is { })
  788. {
  789. Application.Top.DrawComplete -= Top_DrawComplete;
  790. Application.Top.SizeChanging -= Current_TerminalResized;
  791. }
  792. Application.MouseEvent -= Application_RootMouseEvent;
  793. base.Dispose (disposing);
  794. }
  795. }