ContextMenu.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. namespace Terminal.Gui {
  3. /// <summary>
  4. /// A context menu window derived from <see cref="MenuBar"/> containing menu items
  5. /// which can be opened in any position.
  6. /// </summary>
  7. public sealed class ContextMenu : IDisposable {
  8. private static MenuBar menuBar;
  9. private Key key = Key.F10 | Key.ShiftMask;
  10. private MouseFlags mouseFlags = MouseFlags.Button3Clicked;
  11. private Toplevel container;
  12. /// <summary>
  13. /// Initialize a context menu with empty menu items.
  14. /// </summary>
  15. public ContextMenu () : this (0, 0, new MenuBarItem ()) { }
  16. /// <summary>
  17. /// Initialize a context menu with menu items from a host <see cref="View"/>.
  18. /// </summary>
  19. /// <param name="host">The host view.</param>
  20. /// <param name="menuItems">The menu items.</param>
  21. public ContextMenu (View host, MenuBarItem menuItems) :
  22. this (host.Frame.X, host.Frame.Y, menuItems)
  23. {
  24. Host = host;
  25. }
  26. /// <summary>
  27. /// Initialize a context menu with menu items.
  28. /// </summary>
  29. /// <param name="x">The left position.</param>
  30. /// <param name="y">The top position.</param>
  31. /// <param name="menuItems">The menu items.</param>
  32. public ContextMenu (int x, int y, MenuBarItem menuItems)
  33. {
  34. if (IsShow) {
  35. Hide ();
  36. }
  37. MenuItens = menuItems;
  38. Position = new Point (x, y);
  39. }
  40. private void MenuBar_MenuAllClosed ()
  41. {
  42. Dispose ();
  43. }
  44. /// <inheritdoc/>
  45. public void Dispose ()
  46. {
  47. if (IsShow) {
  48. menuBar.MenuAllClosed -= MenuBar_MenuAllClosed;
  49. menuBar.Dispose ();
  50. menuBar = null;
  51. IsShow = false;
  52. }
  53. if (container != null) {
  54. container.Closing -= Container_Closing;
  55. container.Resized -= Container_Resized;
  56. }
  57. }
  58. /// <summary>
  59. /// Open the <see cref="MenuItens"/> menu items.
  60. /// </summary>
  61. public void Show ()
  62. {
  63. if (menuBar != null) {
  64. Hide ();
  65. }
  66. container = Application.Current;
  67. container.Closing += Container_Closing;
  68. container.Resized += Container_Resized;
  69. var frame = container.Frame;
  70. var position = Position;
  71. if (Host != null) {
  72. Host.ViewToScreen (container.Frame.X, container.Frame.Y, out int x, out int y);
  73. var pos = new Point (x, y);
  74. pos.Y += Host.Frame.Height - 1;
  75. if (position != pos) {
  76. Position = position = pos;
  77. }
  78. }
  79. var rect = Menu.MakeFrame (position.X, position.Y, MenuItens.Children);
  80. if (rect.Right >= frame.Right) {
  81. if (frame.Right - rect.Width >= 0 || !ForceMinimumPosToZero) {
  82. position.X = frame.Right - rect.Width;
  83. } else if (ForceMinimumPosToZero) {
  84. position.X = 0;
  85. }
  86. } else if (ForceMinimumPosToZero && position.X < 0) {
  87. position.X = 0;
  88. }
  89. if (rect.Bottom >= frame.Bottom) {
  90. if (frame.Bottom - rect.Height - 1 >= 0 || !ForceMinimumPosToZero) {
  91. if (Host == null) {
  92. position.Y = frame.Bottom - rect.Height - 1;
  93. } else {
  94. Host.ViewToScreen (container.Frame.X, container.Frame.Y, out int x, out int y);
  95. var pos = new Point (x, y);
  96. position.Y = pos.Y - rect.Height - 1;
  97. }
  98. } else if (ForceMinimumPosToZero) {
  99. position.Y = 0;
  100. }
  101. } else if (ForceMinimumPosToZero && position.Y < 0) {
  102. position.Y = 0;
  103. }
  104. menuBar = new MenuBar (new [] { MenuItens }) {
  105. X = position.X,
  106. Y = position.Y,
  107. Width = 0,
  108. Height = 0,
  109. UseSubMenusSingleFrame = UseSubMenusSingleFrame
  110. };
  111. menuBar.isContextMenuLoading = true;
  112. menuBar.MenuAllClosed += MenuBar_MenuAllClosed;
  113. IsShow = true;
  114. menuBar.OpenMenu ();
  115. }
  116. private void Container_Resized (Size obj)
  117. {
  118. if (IsShow) {
  119. Show ();
  120. }
  121. }
  122. private void Container_Closing (ToplevelClosingEventArgs obj)
  123. {
  124. Hide ();
  125. }
  126. /// <summary>
  127. /// Close the <see cref="MenuItens"/> menu items.
  128. /// </summary>
  129. public void Hide ()
  130. {
  131. menuBar.CleanUp ();
  132. Dispose ();
  133. }
  134. /// <summary>
  135. /// Event invoked when the <see cref="ContextMenu.Key"/> is changed.
  136. /// </summary>
  137. public event Action<Key> KeyChanged;
  138. /// <summary>
  139. /// Event invoked when the <see cref="ContextMenu.MouseFlags"/> is changed.
  140. /// </summary>
  141. public event Action<MouseFlags> MouseFlagsChanged;
  142. /// <summary>
  143. /// Gets or set the menu position.
  144. /// </summary>
  145. public Point Position { get; set; }
  146. /// <summary>
  147. /// Gets or sets the menu items for this context menu.
  148. /// </summary>
  149. public MenuBarItem MenuItens { get; set; }
  150. /// <summary>
  151. /// The <see cref="Gui.Key"/> used to activate the context menu by keyboard.
  152. /// </summary>
  153. public Key Key {
  154. get => key;
  155. set {
  156. var oldKey = key;
  157. key = value;
  158. KeyChanged?.Invoke (oldKey);
  159. }
  160. }
  161. /// <summary>
  162. /// The <see cref="Gui.MouseFlags"/> used to activate the context menu by mouse.
  163. /// </summary>
  164. public MouseFlags MouseFlags {
  165. get => mouseFlags;
  166. set {
  167. var oldFlags = mouseFlags;
  168. mouseFlags = value;
  169. MouseFlagsChanged?.Invoke (oldFlags);
  170. }
  171. }
  172. /// <summary>
  173. /// Gets information whether menu is showing or not.
  174. /// </summary>
  175. public static bool IsShow { get; private set; }
  176. /// <summary>
  177. /// The host <see cref="View "/> which position will be used,
  178. /// otherwise if it's null the container will be used.
  179. /// </summary>
  180. public View Host { get; set; }
  181. /// <summary>
  182. /// Gets or sets whether forces the minimum position to zero
  183. /// if the left or right position are negative.
  184. /// </summary>
  185. public bool ForceMinimumPosToZero { get; set; } = true;
  186. /// <summary>
  187. /// Gets the <see cref="Gui.MenuBar"/> that is hosting this context menu.
  188. /// </summary>
  189. public MenuBar MenuBar { get => menuBar; }
  190. /// <summary>
  191. /// Gets or sets if the sub-menus must be displayed in a single or multiple frames.
  192. /// </summary>
  193. public bool UseSubMenusSingleFrame { get; set; }
  194. }
  195. }