ContextMenu.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 + 1, host.Frame.Bottom, 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 && position != new Point (Host.Frame.X + 1, Host.Frame.Bottom)) {
  72. Position = position = new Point (Host.Frame.X + 1, Host.Frame.Bottom);
  73. }
  74. var rect = Menu.MakeFrame (position.X, position.Y, MenuItens.Children);
  75. if (rect.Right >= frame.Right) {
  76. if (frame.Right - rect.Width >= 0 || !ForceMinimumPosToZero) {
  77. position.X = frame.Right - rect.Width;
  78. } else if (ForceMinimumPosToZero) {
  79. position.X = 0;
  80. }
  81. } else if (ForceMinimumPosToZero && position.X < 0) {
  82. position.X = 0;
  83. }
  84. if (rect.Bottom >= frame.Bottom) {
  85. if (frame.Bottom - rect.Height - 1 >= 0 || !ForceMinimumPosToZero) {
  86. if (Host == null) {
  87. position.Y = frame.Bottom - rect.Height - 1;
  88. } else {
  89. position.Y = Host.Frame.Y - rect.Height;
  90. }
  91. } else if (ForceMinimumPosToZero) {
  92. position.Y = 0;
  93. }
  94. } else if (ForceMinimumPosToZero && position.Y < 0) {
  95. position.Y = 0;
  96. }
  97. menuBar = new MenuBar (new [] { MenuItens }) {
  98. X = position.X,
  99. Y = position.Y,
  100. Width = 0,
  101. Height = 0,
  102. UseSubMenusSingleFrame = UseSubMenusSingleFrame
  103. };
  104. menuBar.isContextMenuLoading = true;
  105. menuBar.MenuAllClosed += MenuBar_MenuAllClosed;
  106. IsShow = true;
  107. menuBar.OpenMenu ();
  108. }
  109. private void Container_Resized (Size obj)
  110. {
  111. if (IsShow) {
  112. Show ();
  113. }
  114. }
  115. private void Container_Closing (ToplevelClosingEventArgs obj)
  116. {
  117. Hide ();
  118. }
  119. /// <summary>
  120. /// Close the <see cref="MenuItens"/> menu items.
  121. /// </summary>
  122. public void Hide ()
  123. {
  124. menuBar.CleanUp ();
  125. Dispose ();
  126. }
  127. /// <summary>
  128. /// Event invoked when the <see cref="ContextMenu.Key"/> is changed.
  129. /// </summary>
  130. public event Action<Key> KeyChanged;
  131. /// <summary>
  132. /// Event invoked when the <see cref="ContextMenu.MouseFlags"/> is changed.
  133. /// </summary>
  134. public event Action<MouseFlags> MouseFlagsChanged;
  135. /// <summary>
  136. /// Gets or set the menu position.
  137. /// </summary>
  138. public Point Position { get; set; }
  139. /// <summary>
  140. /// Gets or sets the menu items for this context menu.
  141. /// </summary>
  142. public MenuBarItem MenuItens { get; set; }
  143. /// <summary>
  144. /// The <see cref="Gui.Key"/> used to activate the context menu by keyboard.
  145. /// </summary>
  146. public Key Key {
  147. get => key;
  148. set {
  149. var oldKey = key;
  150. key = value;
  151. KeyChanged?.Invoke (oldKey);
  152. }
  153. }
  154. /// <summary>
  155. /// The <see cref="Gui.MouseFlags"/> used to activate the context menu by mouse.
  156. /// </summary>
  157. public MouseFlags MouseFlags {
  158. get => mouseFlags;
  159. set {
  160. var oldFlags = mouseFlags;
  161. mouseFlags = value;
  162. MouseFlagsChanged?.Invoke (oldFlags);
  163. }
  164. }
  165. /// <summary>
  166. /// Gets information whether menu is showing or not.
  167. /// </summary>
  168. public static bool IsShow { get; private set; }
  169. /// <summary>
  170. /// The host <see cref="View "/> which position will be used,
  171. /// otherwise if it's null the container will be used.
  172. /// </summary>
  173. public View Host { get; set; }
  174. /// <summary>
  175. /// Gets or sets whether forces the minimum position to zero
  176. /// if the left or right position are negative.
  177. /// </summary>
  178. public bool ForceMinimumPosToZero { get; set; } = true;
  179. /// <summary>
  180. /// Gets the <see cref="Gui.MenuBar"/> that is hosting this context menu.
  181. /// </summary>
  182. public MenuBar MenuBar { get => menuBar; }
  183. /// <summary>
  184. /// Gets or sets if the sub-menus must be displayed in a single or multiple frames.
  185. /// </summary>
  186. public bool UseSubMenusSingleFrame { get; set; }
  187. }
  188. }