ContextMenu.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. };
  103. menuBar.isContextMenuLoading = true;
  104. menuBar.MenuAllClosed += MenuBar_MenuAllClosed;
  105. IsShow = true;
  106. menuBar.OpenMenu ();
  107. }
  108. private void Container_Resized (Size obj)
  109. {
  110. if (IsShow) {
  111. Show ();
  112. }
  113. }
  114. private void Container_Closing (ToplevelClosingEventArgs obj)
  115. {
  116. Hide ();
  117. }
  118. /// <summary>
  119. /// Close the <see cref="MenuItens"/> menu items.
  120. /// </summary>
  121. public void Hide ()
  122. {
  123. menuBar.CleanUp ();
  124. Dispose ();
  125. }
  126. /// <summary>
  127. /// Event invoked when the <see cref="ContextMenu.Key"/> is changed.
  128. /// </summary>
  129. public event Action<Key> KeyChanged;
  130. /// <summary>
  131. /// Event invoked when the <see cref="ContextMenu.MouseFlags"/> is changed.
  132. /// </summary>
  133. public event Action<MouseFlags> MouseFlagsChanged;
  134. /// <summary>
  135. /// Gets or set the menu position.
  136. /// </summary>
  137. public Point Position { get; set; }
  138. /// <summary>
  139. /// Gets or sets the menu items for this context menu.
  140. /// </summary>
  141. public MenuBarItem MenuItens { get; set; }
  142. /// <summary>
  143. /// The <see cref="Gui.Key"/> used to activate the context menu by keyboard.
  144. /// </summary>
  145. public Key Key {
  146. get => key;
  147. set {
  148. var oldKey = key;
  149. key = value;
  150. KeyChanged?.Invoke (oldKey);
  151. }
  152. }
  153. /// <summary>
  154. /// The <see cref="Gui.MouseFlags"/> used to activate the context menu by mouse.
  155. /// </summary>
  156. public MouseFlags MouseFlags {
  157. get => mouseFlags;
  158. set {
  159. var oldFlags = mouseFlags;
  160. mouseFlags = value;
  161. MouseFlagsChanged?.Invoke (oldFlags);
  162. }
  163. }
  164. /// <summary>
  165. /// Gets information whether menu is showing or not.
  166. /// </summary>
  167. public static bool IsShow { get; private set; }
  168. /// <summary>
  169. /// The host <see cref="View "/> which position will be used,
  170. /// otherwise if it's null the container will be used.
  171. /// </summary>
  172. public View Host { get; set; }
  173. /// <summary>
  174. /// Gets or sets whether forces the minimum position to zero
  175. /// if the left or right position are negative.
  176. /// </summary>
  177. public bool ForceMinimumPosToZero { get; set; } = true;
  178. /// <summary>
  179. /// Gets the <see cref="Gui.MenuBar"/> that is hosting this context menu.
  180. /// </summary>
  181. public MenuBar MenuBar { get => menuBar; }
  182. }
  183. }