ContextMenuv2.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// ContextMenuv2 provides a Popover menu that can be positioned anywhere within a <see cref="View"/>.
  6. /// <para>
  7. /// To show the ContextMenu, set <see cref="Application.Popover"/> to the ContextMenu object and set
  8. /// <see cref="View.Visible"/> property to <see langword="true"/>.
  9. /// </para>
  10. /// <para>
  11. /// The menu will be hidden when the user clicks outside the menu or when the user presses <see cref="Application.QuitKey"/>.
  12. /// </para>
  13. /// <para>
  14. /// To explicitly hide the menu, set <see cref="View.Visible"/> property to <see langword="false"/>.
  15. /// </para>
  16. /// <para>
  17. /// <see cref="Key"/> is the key used to activate the ContextMenus (<c>Shift+F10</c> by default). Callers can use this in
  18. /// their keyboard handling code.
  19. /// </para>
  20. /// <para>The menu will be displayed at the current mouse coordinates.</para>
  21. /// </summary>
  22. public class ContextMenuv2 : PopoverMenu, IDesignable
  23. {
  24. /// <summary>
  25. /// The mouse flags that will trigger the context menu. The default is <see cref="MouseFlags.Button3Clicked"/> which is typically the right mouse button.
  26. /// </summary>
  27. public MouseFlags MouseFlags { get; set; } = MouseFlags.Button3Clicked;
  28. /// <summary>Initializes a context menu with no menu items.</summary>
  29. public ContextMenuv2 () : this ([]) { }
  30. /// <inheritdoc/>
  31. public ContextMenuv2 (Menuv2? menu) : base (menu)
  32. {
  33. Key = DefaultKey;
  34. }
  35. /// <inheritdoc/>
  36. public ContextMenuv2 (IEnumerable<View>? menuItems) : this (new Menuv2 (menuItems))
  37. {
  38. }
  39. private Key _key = DefaultKey;
  40. /// <summary>Specifies the key that will activate the context menu.</summary>
  41. public Key Key
  42. {
  43. get => _key;
  44. set
  45. {
  46. Key oldKey = _key;
  47. _key = value;
  48. KeyChanged?.Invoke (this, new KeyChangedEventArgs (oldKey, _key));
  49. }
  50. }
  51. /// <summary>Event raised when the <see cref="ContextMenu.Key"/> is changed.</summary>
  52. public event EventHandler<KeyChangedEventArgs>? KeyChanged;
  53. /// <inheritdoc />
  54. public bool EnableForDesign ()
  55. {
  56. var shortcut = new Shortcut
  57. {
  58. Text = "Quit",
  59. Title = "Q_uit",
  60. Key = Key.Z.WithCtrl,
  61. };
  62. Add (shortcut);
  63. shortcut = new Shortcut
  64. {
  65. Text = "Help Text",
  66. Title = "Help",
  67. Key = Key.F1,
  68. };
  69. Add (shortcut);
  70. shortcut = new Shortcut
  71. {
  72. Text = "Czech",
  73. CommandView = new CheckBox ()
  74. {
  75. Title = "_Check"
  76. },
  77. Key = Key.F9,
  78. CanFocus = false
  79. };
  80. Add (shortcut);
  81. // HACK: This enables All Views Tester to show the CM if DefaultKey is pressed
  82. AddCommand (Command.Context, () => Visible = true);
  83. HotKeyBindings.Add (DefaultKey, Command.Context);
  84. return true;
  85. }
  86. }