Menu.cs 26 KB

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