Menu.cs 26 KB

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