Menu.cs 28 KB

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