Menu.cs 27 KB

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