PopoverMenu.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. namespace Terminal.Gui.Views;
  2. /// <summary>
  3. /// Provides a cascading menu that pops over all other content. Can be used as a context menu or a drop-down
  4. /// all other content. Can be used as a context menu or a drop-down
  5. /// menu as part of <see cref="MenuBar"/> as part of <see cref="MenuBar"/>.
  6. /// </summary>
  7. /// <remarks>
  8. /// <para>
  9. /// To use as a context menu, register the popover menu with <see cref="IApplication.Popover"/> and call
  10. /// <see cref="MakeVisible"/>.
  11. /// </para>
  12. /// </remarks>
  13. public class PopoverMenu : PopoverBaseImpl, IDesignable
  14. {
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="PopoverMenu"/> class.
  17. /// </summary>
  18. public PopoverMenu () : this ((Menu?)null) { }
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="PopoverMenu"/> class. If any of the elements of
  21. /// <paramref name="menuItems"/> is <see langword="null"/>,
  22. /// a see <see cref="Line"/> will be created instead.
  23. /// </summary>
  24. public PopoverMenu (IEnumerable<View>? menuItems) : this (
  25. new Menu (menuItems?.Select (item => item ?? new Line ()))
  26. {
  27. Title = "Popover Root"
  28. })
  29. { }
  30. /// <inheritdoc/>
  31. public PopoverMenu (IEnumerable<MenuItem>? menuItems) : this (
  32. new Menu (menuItems)
  33. {
  34. Title = "Popover Root"
  35. })
  36. { }
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="PopoverMenu"/> class with the specified root <see cref="Menu"/>.
  39. /// </summary>
  40. public PopoverMenu (Menu? root)
  41. {
  42. // Do this to support debugging traces where Title gets set
  43. base.HotKeySpecifier = (Rune)'\xffff';
  44. if (Border is { })
  45. {
  46. Border.Settings &= ~BorderSettings.Title;
  47. }
  48. Key = DefaultKey;
  49. base.Visible = false;
  50. Root = root;
  51. AddCommand (Command.Right, MoveRight);
  52. KeyBindings.Add (Key.CursorRight, Command.Right);
  53. AddCommand (Command.Left, MoveLeft);
  54. KeyBindings.Add (Key.CursorLeft, Command.Left);
  55. // PopoverBaseImpl sets a key binding for Quit, so we
  56. // don't need to do it here.
  57. AddCommand (Command.Quit, Quit);
  58. return;
  59. bool? Quit (ICommandContext? ctx)
  60. {
  61. // Logging.Debug ($"{Title} Command.Quit - {ctx?.Source?.Title}");
  62. if (!Visible)
  63. {
  64. // If we're not visible, the command is not for us
  65. return false;
  66. }
  67. // This ensures the quit command gets propagated to the owner of the popover.
  68. // This is important for MenuBarItems to ensure the MenuBar loses focus when
  69. // the user presses QuitKey to cause the menu to close.
  70. // Note, we override OnAccepting, which will set Visible to false
  71. // Logging.Debug ($"{Title} Command.Quit - Calling RaiseAccepting {ctx?.Source?.Title}");
  72. bool? ret = RaiseAccepting (ctx);
  73. if (Visible && ret is not true)
  74. {
  75. Visible = false;
  76. return true;
  77. }
  78. // If we are Visible, returning true will stop the QuitKey from propagating
  79. // If we are not Visible, returning false will allow the QuitKey to propagate
  80. return Visible;
  81. }
  82. bool? MoveLeft (ICommandContext? ctx)
  83. {
  84. if (Focused == Root)
  85. {
  86. return false;
  87. }
  88. if (MostFocused is MenuItem { SuperView: Menu focusedMenu })
  89. {
  90. focusedMenu.SuperMenuItem?.SetFocus ();
  91. return true;
  92. }
  93. return AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop);
  94. }
  95. bool? MoveRight (ICommandContext? ctx)
  96. {
  97. if (MostFocused is MenuItem { SubMenu.Visible: true } focused)
  98. {
  99. focused.SubMenu.SetFocus ();
  100. return true;
  101. }
  102. return false;
  103. }
  104. }
  105. private Key _key = DefaultKey;
  106. /// <summary>Specifies the key that will activate the context menu.</summary>
  107. public Key Key
  108. {
  109. get => _key;
  110. set
  111. {
  112. Key oldKey = _key;
  113. _key = value;
  114. KeyChanged?.Invoke (this, new (oldKey, _key));
  115. }
  116. }
  117. /// <summary>Raised when <see cref="Key"/> is changed.</summary>
  118. public event EventHandler<KeyChangedEventArgs>? KeyChanged;
  119. /// <summary>The default key for activating popover menus.</summary>
  120. [ConfigurationProperty (Scope = typeof (SettingsScope))]
  121. public static Key DefaultKey { get; set; } = Key.F10.WithShift;
  122. /// <summary>
  123. /// The mouse flags that will cause the popover menu to be visible. The default is
  124. /// <see cref="MouseFlags.Button3Clicked"/> which is typically the right mouse button.
  125. /// </summary>
  126. public MouseFlags MouseFlags { get; set; } = MouseFlags.Button3Clicked;
  127. /// <summary>
  128. /// Makes the popover menu visible and locates it at <paramref name="idealScreenPosition"/>. The actual position of the
  129. /// menu
  130. /// will be adjusted to
  131. /// ensure the menu fully fits on the screen, and the mouse cursor is over the first cell of the
  132. /// first MenuItem.
  133. /// </summary>
  134. /// <param name="idealScreenPosition">If <see langword="null"/>, the current mouse position will be used.</param>
  135. public void MakeVisible (Point? idealScreenPosition = null)
  136. {
  137. if (Visible)
  138. {
  139. // Logging.Debug ($"{Title} - Already Visible");
  140. return;
  141. }
  142. UpdateKeyBindings ();
  143. SetPosition (idealScreenPosition);
  144. App!.Popover?.Show (this);
  145. }
  146. /// <summary>
  147. /// Locates the popover menu at <paramref name="idealScreenPosition"/>. The actual position of the menu will be
  148. /// adjusted to
  149. /// ensure the menu fully fits on the screen, and the mouse cursor is over the first cell of the
  150. /// first MenuItem (if possible).
  151. /// </summary>
  152. /// <param name="idealScreenPosition">If <see langword="null"/>, the current mouse position will be used.</param>
  153. public void SetPosition (Point? idealScreenPosition = null)
  154. {
  155. idealScreenPosition ??= App?.Mouse.LastMousePosition;
  156. if (idealScreenPosition is null || Root is null)
  157. {
  158. return;
  159. }
  160. Point pos = idealScreenPosition.Value;
  161. if (!Root.IsInitialized)
  162. {
  163. Root.App ??= App;
  164. Root.BeginInit ();
  165. Root.EndInit ();
  166. Root.Layout ();
  167. }
  168. pos = GetMostVisibleLocationForSubMenu (Root, pos);
  169. Root.X = pos.X;
  170. Root.Y = pos.Y;
  171. }
  172. /// <inheritdoc/>
  173. protected override void OnVisibleChanged ()
  174. {
  175. // Logging.Debug ($"{Title} - Visible: {Visible}");
  176. base.OnVisibleChanged ();
  177. if (Visible)
  178. {
  179. AddAndShowSubMenu (_root);
  180. }
  181. else
  182. {
  183. HideAndRemoveSubMenu (_root);
  184. App?.Popover?.Hide (this);
  185. }
  186. }
  187. private Menu? _root;
  188. /// <summary>
  189. /// Gets or sets the <see cref="Menu"/> that is the root of the Popover Menu.
  190. /// </summary>
  191. public Menu? Root
  192. {
  193. get => _root;
  194. set
  195. {
  196. if (_root == value)
  197. {
  198. return;
  199. }
  200. HideAndRemoveSubMenu (_root);
  201. _root = value;
  202. if (_root is { })
  203. {
  204. _root.App = App;
  205. }
  206. // TODO: This needs to be done whenever any MenuItem in the menu tree changes to support dynamic menus
  207. // TODO: And it needs to clear the old bindings first
  208. UpdateKeyBindings ();
  209. // TODO: This needs to be done whenever any MenuItem in the menu tree changes to support dynamic menus
  210. IEnumerable<Menu> allMenus = GetAllSubMenus ();
  211. foreach (Menu menu in allMenus)
  212. {
  213. menu.App = App;
  214. menu.Visible = false;
  215. menu.Accepting += MenuOnAccepting;
  216. menu.Accepted += MenuAccepted;
  217. menu.SelectedMenuItemChanged += MenuOnSelectedMenuItemChanged;
  218. }
  219. }
  220. }
  221. private void UpdateKeyBindings ()
  222. {
  223. IEnumerable<MenuItem> all = GetMenuItemsOfAllSubMenus ();
  224. foreach (MenuItem menuItem in all.Where (mi => mi.Command != Command.NotBound))
  225. {
  226. Key? key;
  227. if (menuItem.TargetView is { })
  228. {
  229. // A TargetView implies HotKey
  230. key = menuItem.TargetView.HotKeyBindings.GetFirstFromCommands (menuItem.Command);
  231. }
  232. else
  233. {
  234. // No TargetView implies Application HotKey
  235. key = App?.Keyboard.KeyBindings.GetFirstFromCommands (menuItem.Command);
  236. }
  237. if (key is not { IsValid: true })
  238. {
  239. continue;
  240. }
  241. if (menuItem.Key.IsValid)
  242. {
  243. //Logging.Warning ("Do not specify a Key for MenuItems where a Command is specified. Key will be determined automatically.");
  244. }
  245. menuItem.Key = key;
  246. // Logging.Debug ($"{Title} - HotKey: {menuItem.Key}->{menuItem.Command}");
  247. }
  248. }
  249. /// <inheritdoc/>
  250. protected override bool OnKeyDownNotHandled (Key key)
  251. {
  252. // See if any of our MenuItems have this key as Key
  253. IEnumerable<MenuItem> all = GetMenuItemsOfAllSubMenus ();
  254. foreach (MenuItem menuItem in all)
  255. {
  256. if (key != Application.QuitKey && menuItem.Key == key)
  257. {
  258. // Logging.Debug ($"{Title} - key: {key}");
  259. return menuItem.NewKeyDownEvent (key);
  260. }
  261. }
  262. return base.OnKeyDownNotHandled (key);
  263. }
  264. /// <summary>
  265. /// Gets all the submenus in the PopoverMenu.
  266. /// </summary>
  267. /// <returns></returns>
  268. public IEnumerable<Menu> GetAllSubMenus ()
  269. {
  270. List<Menu> result = [];
  271. if (Root == null)
  272. {
  273. return result;
  274. }
  275. Stack<Menu> stack = new ();
  276. stack.Push (Root);
  277. while (stack.Count > 0)
  278. {
  279. Menu currentMenu = stack.Pop ();
  280. result.Add (currentMenu);
  281. foreach (View subView in currentMenu.SubViews)
  282. {
  283. if (subView is MenuItem { SubMenu: { } } menuItem)
  284. {
  285. stack.Push (menuItem.SubMenu);
  286. }
  287. }
  288. }
  289. return result;
  290. }
  291. /// <summary>
  292. /// Gets all the MenuItems in the PopoverMenu.
  293. /// </summary>
  294. /// <returns></returns>
  295. internal IEnumerable<MenuItem> GetMenuItemsOfAllSubMenus ()
  296. {
  297. List<MenuItem> result = [];
  298. foreach (Menu menu in GetAllSubMenus ())
  299. {
  300. foreach (View subView in menu.SubViews)
  301. {
  302. if (subView is MenuItem menuItem)
  303. {
  304. result.Add (menuItem);
  305. }
  306. }
  307. }
  308. return result;
  309. }
  310. /// <summary>
  311. /// Pops up the submenu of the specified MenuItem, if there is one.
  312. /// </summary>
  313. /// <param name="menuItem"></param>
  314. internal void ShowSubMenu (MenuItem? menuItem)
  315. {
  316. var menu = menuItem?.SuperView as Menu;
  317. // Logging.Debug ($"{Title} - menuItem: {menuItem?.Title}, menu: {menu?.Title}");
  318. menu?.Layout ();
  319. // If there's a visible peer, remove / hide it
  320. if (menu?.SubViews.FirstOrDefault (v => v is MenuItem { SubMenu.Visible: true }) is MenuItem visiblePeer)
  321. {
  322. HideAndRemoveSubMenu (visiblePeer.SubMenu);
  323. visiblePeer.ForceFocusColors = false;
  324. }
  325. if (menuItem is { SubMenu: { Visible: false } })
  326. {
  327. AddAndShowSubMenu (menuItem.SubMenu);
  328. Point idealLocation = ScreenToViewport (
  329. new (
  330. menuItem.FrameToScreen ().Right - menuItem.SubMenu.GetAdornmentsThickness ().Left,
  331. menuItem.FrameToScreen ().Top - menuItem.SubMenu.GetAdornmentsThickness ().Top));
  332. Point pos = GetMostVisibleLocationForSubMenu (menuItem.SubMenu, idealLocation);
  333. menuItem.SubMenu.X = pos.X;
  334. menuItem.SubMenu.Y = pos.Y;
  335. menuItem.ForceFocusColors = true;
  336. }
  337. }
  338. /// <summary>
  339. /// Gets the most visible screen-relative location for <paramref name="menu"/>.
  340. /// </summary>
  341. /// <param name="menu">The menu to locate.</param>
  342. /// <param name="idealLocation">Ideal screen-relative location.</param>
  343. /// <returns></returns>
  344. internal Point GetMostVisibleLocationForSubMenu (Menu menu, Point idealLocation)
  345. {
  346. var pos = Point.Empty;
  347. // Calculate the initial position to the right of the menu item
  348. GetLocationEnsuringFullVisibility (
  349. menu,
  350. idealLocation.X,
  351. idealLocation.Y,
  352. out int nx,
  353. out int ny);
  354. return new (nx, ny);
  355. }
  356. private void AddAndShowSubMenu (Menu? menu)
  357. {
  358. if (menu is { SuperView: null, Visible: false })
  359. {
  360. // Logging.Debug ($"{Title} ({menu?.Title}) - menu.Visible: {menu?.Visible}");
  361. // TODO: Find the menu item below the mouse, if any, and select it
  362. if (!menu!.IsInitialized)
  363. {
  364. menu.App ??= App;
  365. menu.BeginInit ();
  366. menu.EndInit ();
  367. }
  368. menu.ClearFocus ();
  369. base.Add (menu);
  370. // IMPORTANT: This must be done after adding the menu to the super view or Add will try
  371. // to set focus to it.
  372. menu.Visible = true;
  373. menu.Layout ();
  374. }
  375. }
  376. private void HideAndRemoveSubMenu (Menu? menu)
  377. {
  378. if (menu is { Visible: true })
  379. {
  380. // Logging.Debug ($"{Title} ({menu?.Title}) - menu.Visible: {menu?.Visible}");
  381. // If there's a visible submenu, remove / hide it
  382. if (menu.SubViews.FirstOrDefault (v => v is MenuItem { SubMenu.Visible: true }) is MenuItem visiblePeer)
  383. {
  384. HideAndRemoveSubMenu (visiblePeer.SubMenu);
  385. visiblePeer.ForceFocusColors = false;
  386. }
  387. menu.Visible = false;
  388. menu.ClearFocus ();
  389. base.Remove (menu);
  390. if (menu == Root)
  391. {
  392. Visible = false;
  393. }
  394. }
  395. }
  396. private void MenuOnAccepting (object? sender, CommandEventArgs e)
  397. {
  398. var senderView = sender as View;
  399. // Logging.Debug ($"{Title} ({e.Context?.Source?.Title}) Command: {e.Context?.Command} - Sender: {senderView?.GetType ().Name}");
  400. if (e.Context?.Command != Command.HotKey)
  401. {
  402. // Logging.Debug ($"{Title} - Setting Visible = false");
  403. Visible = false;
  404. }
  405. if (e.Context is CommandContext<KeyBinding> keyCommandContext)
  406. {
  407. if (keyCommandContext.Binding.Key is { } && keyCommandContext.Binding.Key == Application.QuitKey && SuperView is { Visible: true })
  408. {
  409. // Logging.Debug ($"{Title} - Setting e.Handled = true - Application.QuitKey/Command = Command.Quit");
  410. e.Handled = true;
  411. }
  412. }
  413. }
  414. private void MenuAccepted (object? sender, CommandEventArgs e)
  415. {
  416. // Logging.Debug ($"{Title} ({e.Context?.Source?.Title}) Command: {e.Context?.Command}");
  417. if (e.Context?.Source is MenuItem { SubMenu: null })
  418. {
  419. HideAndRemoveSubMenu (_root);
  420. }
  421. else if (e.Context?.Source is MenuItem { SubMenu: { } } menuItemWithSubMenu)
  422. {
  423. ShowSubMenu (menuItemWithSubMenu);
  424. }
  425. RaiseAccepted (e.Context);
  426. }
  427. /// <inheritdoc/>
  428. protected override bool OnAccepting (CommandEventArgs args)
  429. {
  430. // Logging.Debug ($"{Title} ({args.Context?.Source?.Title}) Command: {args.Context?.Command}");
  431. // If we're not visible, ignore any keys that are not hotkeys
  432. CommandContext<KeyBinding>? keyCommandContext = args.Context as CommandContext<KeyBinding>? ?? default (CommandContext<KeyBinding>);
  433. if (!Visible && keyCommandContext is { Binding.Key: { } })
  434. {
  435. if (GetMenuItemsOfAllSubMenus ().All (i => i.Key != keyCommandContext.Value.Binding.Key))
  436. {
  437. // Logging.Debug ($"{Title} ({args.Context?.Source?.Title}) Command: {args.Context?.Command} - ignore any keys that are not hotkeys");
  438. return false;
  439. }
  440. }
  441. // Logging.Debug ($"{Title} - calling base.OnAccepting: {args.Context?.Command}");
  442. bool? ret = base.OnAccepting (args);
  443. if (ret is true || args.Handled)
  444. {
  445. return args.Handled = true;
  446. }
  447. // Only raise Accepted if the command came from one of our MenuItems
  448. //if (GetMenuItemsOfAllSubMenus ().Contains (args.Context?.Source))
  449. {
  450. // Logging.Debug ($"{Title} - Calling RaiseAccepted {args.Context?.Command}");
  451. RaiseAccepted (args.Context);
  452. }
  453. // Always return false to enable accepting to continue propagating
  454. return false;
  455. }
  456. /// <summary>
  457. /// Raises the <see cref="OnAccepted"/>/<see cref="Accepted"/> event indicating a menu (or submenu)
  458. /// was accepted and the Menus in the PopoverMenu were hidden. Use this to determine when to hide the PopoverMenu.
  459. /// </summary>
  460. /// <param name="ctx"></param>
  461. /// <returns></returns>
  462. protected void RaiseAccepted (ICommandContext? ctx)
  463. {
  464. // Logging.Debug ($"{Title} - RaiseAccepted: {ctx}");
  465. CommandEventArgs args = new () { Context = ctx };
  466. OnAccepted (args);
  467. Accepted?.Invoke (this, args);
  468. }
  469. /// <summary>
  470. /// Called when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the
  471. /// menu.
  472. /// </summary>
  473. /// <remarks>
  474. /// </remarks>
  475. /// <param name="args"></param>
  476. protected virtual void OnAccepted (CommandEventArgs args) { }
  477. /// <summary>
  478. /// Raised when the user has accepted an item in this menu (or submenu. This is used to determine when to hide the
  479. /// menu.
  480. /// </summary>
  481. /// <remarks>
  482. /// <para>
  483. /// See <see cref="RaiseAccepted"/> for more information.
  484. /// </para>
  485. /// </remarks>
  486. public event EventHandler<CommandEventArgs>? Accepted;
  487. private void MenuOnSelectedMenuItemChanged (object? sender, MenuItem? e)
  488. {
  489. // Logging.Debug ($"{Title} - e.Title: {e?.Title}");
  490. ShowSubMenu (e);
  491. }
  492. /// <inheritdoc/>
  493. protected override void OnSubViewAdded (View view)
  494. {
  495. if (Root is null && (view is Menu || view is MenuItem))
  496. {
  497. throw new InvalidOperationException ("Do not add MenuItems or Menus directly to a PopoverMenu. Use the Root property.");
  498. }
  499. base.OnSubViewAdded (view);
  500. }
  501. /// <inheritdoc/>
  502. protected override void Dispose (bool disposing)
  503. {
  504. if (disposing)
  505. {
  506. IEnumerable<Menu> allMenus = GetAllSubMenus ();
  507. foreach (Menu menu in allMenus)
  508. {
  509. menu.Accepting -= MenuOnAccepting;
  510. menu.Accepted -= MenuAccepted;
  511. menu.SelectedMenuItemChanged -= MenuOnSelectedMenuItemChanged;
  512. }
  513. _root?.Dispose ();
  514. _root = null;
  515. }
  516. base.Dispose (disposing);
  517. }
  518. /// <inheritdoc/>
  519. public bool EnableForDesign<TContext> (ref TContext targetView) where TContext : notnull
  520. {
  521. // Note: This menu is used by unit tests. If you modify it, you'll likely have to update
  522. // unit tests.
  523. Root = new (
  524. [
  525. new MenuItem (targetView as View, Command.Cut),
  526. new MenuItem (targetView as View, Command.Copy),
  527. new MenuItem (targetView as View, Command.Paste),
  528. new Line (),
  529. new MenuItem (targetView as View, Command.SelectAll),
  530. new Line (),
  531. new MenuItem (targetView as View, Command.Quit)
  532. ])
  533. {
  534. Title = "Popover Demo Root"
  535. };
  536. // NOTE: This is a workaround for the fact that the PopoverMenu is not visible in the designer
  537. // NOTE: without being activated via App?.Popover. But we want it to be visible.
  538. // NOTE: If you use PopoverView.EnableForDesign for real Popover scenarios, change back to false
  539. // NOTE: after calling EnableForDesign.
  540. //Visible = true;
  541. return true;
  542. }
  543. }