ContextMenu.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. namespace Terminal.Gui {
  3. /// <summary>
  4. /// ContextMenu provides a pop-up menu that can be positioned anywhere within a <see cref="View"/>.
  5. /// ContextMenu is analogous to <see cref="MenuBar"/> and, once activated, works like a sub-menu
  6. /// of a <see cref="MenuBarItem"/> (but can be positioned anywhere).
  7. /// <para>
  8. /// By default, a ContextMenu with sub-menus is displayed in a cascading manner, where each sub-menu pops out of the ContextMenu frame
  9. /// (either to the right or left, depending on where the ContextMenu is relative to the edge of the screen). By setting
  10. /// <see cref="UseSubMenusSingleFrame"/> to <see langword="true"/>, this behavior can be changed such that all sub-menus are
  11. /// drawn within the ContextMenu frame.
  12. /// </para>
  13. /// <para>
  14. /// ContextMenus can be activated using the Shift-F10 key (by default; use the <see cref="Key"/> to change to another key).
  15. /// </para>
  16. /// <para>
  17. /// Callers can cause the ContextMenu to be activated on a right-mouse click (or other interaction) by calling <see cref="Show()"/>.
  18. /// </para>
  19. /// <para>
  20. /// ContextMenus are located using screen using screen coordinates and appear above all other Views.
  21. /// </para>
  22. /// </summary>
  23. public sealed class ContextMenu : IDisposable {
  24. private static MenuBar menuBar;
  25. private Key key = Key.F10 | Key.ShiftMask;
  26. private MouseFlags mouseFlags = MouseFlags.Button3Clicked;
  27. private Toplevel container;
  28. /// <summary>
  29. /// Initializes a context menu with no menu items.
  30. /// </summary>
  31. public ContextMenu () : this (0, 0, new MenuBarItem ()) { }
  32. /// <summary>
  33. /// Initializes a context menu, with a <see cref="View"/> specifying the parent/host of the menu.
  34. /// </summary>
  35. /// <param name="host">The host view.</param>
  36. /// <param name="menuItems">The menu items for the context menu.</param>
  37. public ContextMenu (View host, MenuBarItem menuItems) :
  38. this (host.Frame.X, host.Frame.Y, menuItems)
  39. {
  40. Host = host;
  41. }
  42. /// <summary>
  43. /// Initializes a context menu with menu items at a specific screen location.
  44. /// </summary>
  45. /// <param name="x">The left position (screen relative).</param>
  46. /// <param name="y">The top position (screen relative).</param>
  47. /// <param name="menuItems">The menu items.</param>
  48. public ContextMenu (int x, int y, MenuBarItem menuItems)
  49. {
  50. if (IsShow) {
  51. if (menuBar.SuperView != null) {
  52. Hide ();
  53. }
  54. IsShow = false;
  55. }
  56. MenuItems = menuItems;
  57. Position = new Point (x, y);
  58. }
  59. private void MenuBar_MenuAllClosed (object sender, EventArgs e)
  60. {
  61. Dispose ();
  62. }
  63. /// <summary>
  64. /// Disposes the context menu object.
  65. /// </summary>
  66. public void Dispose ()
  67. {
  68. if (IsShow) {
  69. menuBar.MenuAllClosed -= MenuBar_MenuAllClosed;
  70. menuBar.Dispose ();
  71. menuBar = null;
  72. IsShow = false;
  73. }
  74. if (container != null) {
  75. container.Closing -= Container_Closing;
  76. }
  77. }
  78. /// <summary>
  79. /// Shows (opens) the ContextMenu, displaying the <see cref="MenuItem"/>s it contains.
  80. /// </summary>
  81. public void Show ()
  82. {
  83. if (menuBar != null) {
  84. Hide ();
  85. }
  86. container = Application.Current;
  87. container.Closing += Container_Closing;
  88. var frame = new Rect (0, 0, View.Driver.Cols, View.Driver.Rows);
  89. var position = Position;
  90. if (Host != null) {
  91. Host.BoundsToScreen (frame.X, frame.Y, out int x, out int y);
  92. var pos = new Point (x, y);
  93. pos.Y += Host.Frame.Height - 1;
  94. if (position != pos) {
  95. Position = position = pos;
  96. }
  97. }
  98. var rect = Menu.MakeFrame (position.X, position.Y, MenuItems.Children);
  99. if (rect.Right >= frame.Right) {
  100. if (frame.Right - rect.Width >= 0 || !ForceMinimumPosToZero) {
  101. position.X = frame.Right - rect.Width;
  102. } else if (ForceMinimumPosToZero) {
  103. position.X = 0;
  104. }
  105. } else if (ForceMinimumPosToZero && position.X < 0) {
  106. position.X = 0;
  107. }
  108. if (rect.Bottom >= frame.Bottom) {
  109. if (frame.Bottom - rect.Height - 1 >= 0 || !ForceMinimumPosToZero) {
  110. if (Host == null) {
  111. position.Y = frame.Bottom - rect.Height - 1;
  112. } else {
  113. Host.BoundsToScreen (frame.X, frame.Y, out int x, out int y);
  114. var pos = new Point (x, y);
  115. position.Y = pos.Y - rect.Height - 1;
  116. }
  117. } else if (ForceMinimumPosToZero) {
  118. position.Y = 0;
  119. }
  120. } else if (ForceMinimumPosToZero && position.Y < 0) {
  121. position.Y = 0;
  122. }
  123. menuBar = new MenuBar (new [] { MenuItems }) {
  124. X = position.X,
  125. Y = position.Y,
  126. Width = 0,
  127. Height = 0,
  128. UseSubMenusSingleFrame = UseSubMenusSingleFrame,
  129. Key = Key
  130. };
  131. menuBar.isContextMenuLoading = true;
  132. menuBar.MenuAllClosed += MenuBar_MenuAllClosed;
  133. IsShow = true;
  134. menuBar.OpenMenu ();
  135. }
  136. private void Container_Closing (object sender, ToplevelClosingEventArgs obj)
  137. {
  138. Hide ();
  139. }
  140. /// <summary>
  141. /// Hides (closes) the ContextMenu.
  142. /// </summary>
  143. public void Hide ()
  144. {
  145. menuBar?.CleanUp ();
  146. Dispose ();
  147. }
  148. /// <summary>
  149. /// Event invoked when the <see cref="ContextMenu.Key"/> is changed.
  150. /// </summary>
  151. public event EventHandler<KeyChangedEventArgs> KeyChanged;
  152. /// <summary>
  153. /// Event invoked when the <see cref="ContextMenu.MouseFlags"/> is changed.
  154. /// </summary>
  155. public event EventHandler<MouseFlagsChangedEventArgs> MouseFlagsChanged;
  156. /// <summary>
  157. /// Gets or sets the menu position.
  158. /// </summary>
  159. public Point Position { get; set; }
  160. /// <summary>
  161. /// Gets or sets the menu items for this context menu.
  162. /// </summary>
  163. public MenuBarItem MenuItems { get; set; }
  164. /// <summary>
  165. /// <see cref="Gui.Key"/> specifies they keyboard key that will activate the context menu with the keyboard.
  166. /// </summary>
  167. public Key Key {
  168. get => key;
  169. set {
  170. var oldKey = key;
  171. key = value;
  172. KeyChanged?.Invoke (this, new KeyChangedEventArgs (oldKey, key));
  173. }
  174. }
  175. /// <summary>
  176. /// <see cref="Gui.MouseFlags"/> specifies the mouse action used to activate the context menu by mouse.
  177. /// </summary>
  178. public MouseFlags MouseFlags {
  179. get => mouseFlags;
  180. set {
  181. var oldFlags = mouseFlags;
  182. mouseFlags = value;
  183. MouseFlagsChanged?.Invoke (this, new MouseFlagsChangedEventArgs (oldFlags, value));
  184. }
  185. }
  186. /// <summary>
  187. /// Gets whether the ContextMenu is showing or not.
  188. /// </summary>
  189. public static bool IsShow { get; private set; }
  190. /// <summary>
  191. /// The host <see cref="View "/> which position will be used,
  192. /// otherwise if it's null the container will be used.
  193. /// </summary>
  194. public View Host { get; set; }
  195. /// <summary>
  196. /// Sets or gets whether the context menu be forced to the right, ensuring it is not clipped, if the x position
  197. /// is less than zero. The default is <see langword="true"/> which means the context menu will be forced to the right.
  198. /// If set to <see langword="false"/>, the context menu will be clipped on the left if x is less than zero.
  199. /// </summary>
  200. public bool ForceMinimumPosToZero { get; set; } = true;
  201. /// <summary>
  202. /// Gets the <see cref="Gui.MenuBar"/> that is hosting this context menu.
  203. /// </summary>
  204. public MenuBar MenuBar { get => menuBar; }
  205. /// <summary>
  206. /// Gets or sets if sub-menus will be displayed using a "single frame" menu style. If <see langword="true"/>, the ContextMenu
  207. /// and any sub-menus that would normally cascade will be displayed within a single frame. If <see langword="false"/> (the default),
  208. /// sub-menus will cascade using separate frames for each level of the menu hierarchy.
  209. /// </summary>
  210. public bool UseSubMenusSingleFrame { get; set; }
  211. }
  212. }