Menu.cs 27 KB

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