ContextMenu.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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"/> specifiying the parent/hose 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. container.TerminalResized -= Container_Resized;
  77. }
  78. }
  79. /// <summary>
  80. /// Shows (opens) the ContextMenu, displaying the <see cref="MenuItem"/>s it contains.
  81. /// </summary>
  82. public void Show ()
  83. {
  84. if (menuBar != null) {
  85. Hide ();
  86. }
  87. container = Application.Top;
  88. container.Closing += Container_Closing;
  89. container.TerminalResized += Container_Resized;
  90. var frame = container.Frame;
  91. var position = Position;
  92. if (Host != null) {
  93. Host.ViewToScreen (container.Frame.X, container.Frame.Y, out int x, out int y);
  94. var pos = new Point (x, y);
  95. pos.Y += Host.Frame.Height - 1;
  96. if (position != pos) {
  97. Position = position = pos;
  98. }
  99. }
  100. var rect = Menu.MakeFrame (position.X, position.Y, MenuItems.Children);
  101. if (rect.Right >= frame.Right) {
  102. if (frame.Right - rect.Width >= 0 || !ForceMinimumPosToZero) {
  103. position.X = frame.Right - rect.Width;
  104. } else if (ForceMinimumPosToZero) {
  105. position.X = 0;
  106. }
  107. } else if (ForceMinimumPosToZero && position.X < 0) {
  108. position.X = 0;
  109. }
  110. if (rect.Bottom >= frame.Bottom) {
  111. if (frame.Bottom - rect.Height - 1 >= 0 || !ForceMinimumPosToZero) {
  112. if (Host == null) {
  113. position.Y = frame.Bottom - rect.Height - 1;
  114. } else {
  115. Host.ViewToScreen (container.Frame.X, container.Frame.Y, out int x, out int y);
  116. var pos = new Point (x, y);
  117. position.Y = pos.Y - rect.Height - 1;
  118. }
  119. } else if (ForceMinimumPosToZero) {
  120. position.Y = 0;
  121. }
  122. } else if (ForceMinimumPosToZero && position.Y < 0) {
  123. position.Y = 0;
  124. }
  125. menuBar = new MenuBar (new [] { MenuItems }) {
  126. X = position.X,
  127. Y = position.Y,
  128. Width = 0,
  129. Height = 0,
  130. UseSubMenusSingleFrame = UseSubMenusSingleFrame,
  131. Key = Key
  132. };
  133. menuBar.isContextMenuLoading = true;
  134. menuBar.MenuAllClosed += MenuBar_MenuAllClosed;
  135. IsShow = true;
  136. menuBar.OpenMenu ();
  137. }
  138. private void Container_Resized (object sender, SizeChangedEventArgs e)
  139. {
  140. if (IsShow) {
  141. Show ();
  142. }
  143. }
  144. private void Container_Closing (object sender, ToplevelClosingEventArgs obj)
  145. {
  146. Hide ();
  147. }
  148. /// <summary>
  149. /// Hides (closes) the ContextMenu.
  150. /// </summary>
  151. public void Hide ()
  152. {
  153. menuBar?.CleanUp ();
  154. Dispose ();
  155. }
  156. /// <summary>
  157. /// Event invoked when the <see cref="ContextMenu.Key"/> is changed.
  158. /// </summary>
  159. public event EventHandler<KeyChangedEventArgs> KeyChanged;
  160. /// <summary>
  161. /// Event invoked when the <see cref="ContextMenu.MouseFlags"/> is changed.
  162. /// </summary>
  163. public event EventHandler<MouseFlagsChangedEventArgs> MouseFlagsChanged;
  164. /// <summary>
  165. /// Gets or sets the menu position.
  166. /// </summary>
  167. public Point Position { get; set; }
  168. /// <summary>
  169. /// Gets or sets the menu items for this context menu.
  170. /// </summary>
  171. public MenuBarItem MenuItems { get; set; }
  172. /// <summary>
  173. /// <see cref="Gui.Key"/> specifies they keyboard key that will activate the context menu with the keyboard.
  174. /// </summary>
  175. public Key Key {
  176. get => key;
  177. set {
  178. var oldKey = key;
  179. key = value;
  180. KeyChanged?.Invoke (this, new KeyChangedEventArgs (oldKey, key));
  181. }
  182. }
  183. /// <summary>
  184. /// <see cref="Gui.MouseFlags"/> specifies the mouse action used to activate the context menu by mouse.
  185. /// </summary>
  186. public MouseFlags MouseFlags {
  187. get => mouseFlags;
  188. set {
  189. var oldFlags = mouseFlags;
  190. mouseFlags = value;
  191. MouseFlagsChanged?.Invoke (this, new MouseFlagsChangedEventArgs (oldFlags, value));
  192. }
  193. }
  194. /// <summary>
  195. /// Gets whether the ContextMenu is showing or not.
  196. /// </summary>
  197. public static bool IsShow { get; private set; }
  198. /// <summary>
  199. /// The host <see cref="View "/> which position will be used,
  200. /// otherwise if it's null the container will be used.
  201. /// </summary>
  202. public View Host { get; set; }
  203. /// <summary>
  204. /// Sets or gets whether the context menu be forced to the right, ensuring it is not clipped, if the x position
  205. /// is less than zero. The default is <see langword="true"/> which means the context menu will be forced to the right.
  206. /// If set to <see langword="false"/>, the context menu will be clipped on the left if x is less than zero.
  207. /// </summary>
  208. public bool ForceMinimumPosToZero { get; set; } = true;
  209. /// <summary>
  210. /// Gets the <see cref="Gui.MenuBar"/> that is hosting this context menu.
  211. /// </summary>
  212. public MenuBar MenuBar { get => menuBar; }
  213. /// <summary>
  214. /// Gets or sets if sub-menus will be displayed using a "single frame" menu style. If <see langword="true"/>, the ContextMenu
  215. /// and any sub-menus that would normally cascade will be displayed within a single frame. If <see langword="false"/> (the default),
  216. /// sub-menus will cascade using separate frames for each level of the menu hierarchy.
  217. /// </summary>
  218. public bool UseSubMenusSingleFrame { get; set; }
  219. }
  220. }