MenuItemv2.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui.Views;
  4. /// <summary>
  5. /// A <see cref="Shortcut"/>-derived object to be used as a menu item in a <see cref="Menuv2"/>. Has title, an
  6. /// A <see cref="Shortcut"/>-derived object to be used as a menu item in a <see cref="Menuv2"/>. Has title, an
  7. /// associated help text, and an action to execute on activation.
  8. /// </summary>
  9. public class MenuItemv2 : Shortcut
  10. {
  11. /// <summary>
  12. /// Creates a new instance of <see cref="MenuItemv2"/>.
  13. /// </summary>
  14. public MenuItemv2 () : base (Key.Empty, null, null) { }
  15. /// <summary>
  16. /// Creates a new instance of <see cref="MenuItemv2"/>, binding it to <paramref name="targetView"/> and
  17. /// <paramref name="command"/>. The Key <paramref name="targetView"/>
  18. /// has bound to <paramref name="command"/> will be used as <see cref="Key"/>.
  19. /// </summary>
  20. /// <remarks>
  21. /// </remarks>
  22. /// <param name="targetView">
  23. /// The View that <paramref name="command"/> will be invoked on when user does something that causes the Shortcut's
  24. /// Accept
  25. /// event to be raised.
  26. /// </param>
  27. /// <param name="command">
  28. /// The Command to invoke on <paramref name="targetView"/>. The Key <paramref name="targetView"/>
  29. /// has bound to <paramref name="command"/> will be used as <see cref="Key"/>
  30. /// </param>
  31. /// <param name="commandText">The text to display for the command.</param>
  32. /// <param name="helpText">The help text to display.</param>
  33. /// <param name="subMenu">The submenu to display when the user selects this menu item.</param>
  34. public MenuItemv2 (View? targetView, Command command, string? commandText = null, string? helpText = null, Menuv2? subMenu = null)
  35. : base (
  36. targetView?.HotKeyBindings.GetFirstFromCommands (command)!,
  37. string.IsNullOrEmpty (commandText) ? GlobalResources.GetString ($"cmd.{command}") : commandText,
  38. null,
  39. string.IsNullOrEmpty (helpText) ? GlobalResources.GetString ($"cmd.{command}.Help") : helpText
  40. )
  41. {
  42. TargetView = targetView;
  43. Command = command;
  44. SubMenu = subMenu;
  45. }
  46. /// <inheritdoc/>
  47. public MenuItemv2 (string? commandText = null, string? helpText = null, Action? action = null, Key? key = null)
  48. : base (key ?? Key.Empty, commandText, action, helpText)
  49. { }
  50. /// <inheritdoc/>
  51. public MenuItemv2 (string commandText, Key key, Action? action = null)
  52. : base (key ?? Key.Empty, commandText, action, null)
  53. { }
  54. /// <inheritdoc/>
  55. public MenuItemv2 (string? commandText = null, string? helpText = null, Menuv2? subMenu = null)
  56. : base (Key.Empty, commandText, null, helpText)
  57. {
  58. SubMenu = subMenu;
  59. }
  60. // TODO: Consider moving TargetView and Command to Shortcut?
  61. /// <summary>
  62. /// Gets the target <see cref="View"/> that the <see cref="Command"/> will be invoked on.
  63. /// </summary>
  64. public View? TargetView { get; set; }
  65. private Command _command;
  66. /// <summary>
  67. /// Gets the <see cref="Command"/> that will be invoked on <see cref="TargetView"/> when the MenuItem is selected.
  68. /// </summary>
  69. public Command Command
  70. {
  71. get => _command;
  72. set
  73. {
  74. if (_command == value)
  75. {
  76. return;
  77. }
  78. _command = value;
  79. if (string.IsNullOrEmpty (Title))
  80. {
  81. Title = GlobalResources.GetString ($"cmd.{_command}") ?? string.Empty;
  82. }
  83. if (string.IsNullOrEmpty (HelpText))
  84. {
  85. HelpText = GlobalResources.GetString ($"cmd.{_command}.Help") ?? string.Empty;
  86. }
  87. }
  88. }
  89. internal override bool? DispatchCommand (ICommandContext? commandContext)
  90. {
  91. // Logging.Debug ($"{Title} - {commandContext?.Source?.Title} Command: {commandContext?.Command}");
  92. bool? ret = null;
  93. bool quit = false;
  94. if (commandContext is CommandContext<KeyBinding> keyCommandContext)
  95. {
  96. if (keyCommandContext.Binding.Key is { } && keyCommandContext.Binding.Key == Application.QuitKey && SuperView is { Visible: true })
  97. {
  98. // This supports a MenuItem with Key = Application.QuitKey/Command = Command.Quit
  99. // Logging.Debug ($"{Title} - Ignoring Key = Application.QuitKey/Command = Command.Quit");
  100. quit = true;
  101. //ret = true;
  102. }
  103. }
  104. // Translate the incoming command to Command
  105. if (Command != Command.NotBound && commandContext is { })
  106. {
  107. commandContext.Command = Command;
  108. }
  109. if (!quit)
  110. {
  111. if (TargetView is { })
  112. {
  113. // Logging.Debug ($"{Title} - InvokeCommand on TargetView ({TargetView.Title})...");
  114. ret = TargetView.InvokeCommand (Command, commandContext);
  115. }
  116. else
  117. {
  118. // Is this an Application-bound command?
  119. // Logging.Debug ($"{Title} - Application.InvokeCommandsBoundToKey ({Key})...");
  120. ret = Application.InvokeCommandsBoundToKey (Key);
  121. }
  122. }
  123. if (ret is not true)
  124. {
  125. // Logging.Debug ($"{Title} - calling base.DispatchCommand...");
  126. // Base will Raise Selected, then Accepting, then invoke the Action, if any
  127. ret = base.DispatchCommand (commandContext);
  128. }
  129. if (ret is true)
  130. {
  131. // Logging.Debug ($"{Title} - Calling RaiseAccepted");
  132. RaiseAccepted (commandContext);
  133. }
  134. return ret;
  135. }
  136. ///// <inheritdoc />
  137. //protected override bool OnAccepting (CommandEventArgs e)
  138. //{
  139. // // Logging.Debug ($"{Title} - calling base.OnAccepting: {e.Context?.Command}");
  140. // bool? ret = base.OnAccepting (e);
  141. // if (ret is true || e.Cancel)
  142. // {
  143. // return true;
  144. // }
  145. // //RaiseAccepted (e.Context);
  146. // return ret is true;
  147. //}
  148. private Menuv2? _subMenu;
  149. /// <summary>
  150. /// The submenu to display when the user selects this menu item.
  151. /// </summary>
  152. public Menuv2? SubMenu
  153. {
  154. get => _subMenu;
  155. set
  156. {
  157. _subMenu = value;
  158. if (_subMenu is { })
  159. {
  160. SubMenu!.Visible = false;
  161. // TODO: This is a temporary hack - add a flag or something instead
  162. KeyView.Text = $"{Glyphs.RightArrow}";
  163. _subMenu.SuperMenuItem = this;
  164. }
  165. }
  166. }
  167. /// <inheritdoc/>
  168. protected override bool OnMouseEnter (CancelEventArgs eventArgs)
  169. {
  170. // When the mouse enters a menuitem, we set focus to it automatically.
  171. // Logging.Trace($"OnEnter {Title}");
  172. SetFocus ();
  173. return base.OnMouseEnter (eventArgs);
  174. }
  175. // TODO: Consider moving Accepted to Shortcut?
  176. /// <summary>
  177. /// Raises the <see cref="OnAccepted"/>/<see cref="Accepted"/> event indicating this item (or submenu)
  178. /// was accepted. This is used to determine when to hide the menu.
  179. /// </summary>
  180. /// <param name="ctx"></param>
  181. /// <returns></returns>
  182. protected void RaiseAccepted (ICommandContext? ctx)
  183. {
  184. //Logging.Trace ($"RaiseAccepted: {ctx}");
  185. CommandEventArgs args = new () { Context = ctx };
  186. OnAccepted (args);
  187. Accepted?.Invoke (this, args);
  188. }
  189. /// <summary>
  190. /// Called when the user has accepted an item in this menu (or submenu). This is used to determine when to hide the
  191. /// menu.
  192. /// </summary>
  193. /// <remarks>
  194. /// </remarks>
  195. /// <param name="args"></param>
  196. protected virtual void OnAccepted (CommandEventArgs args) { }
  197. /// <summary>
  198. /// Raised when the user has accepted an item in this menu (or submenu). This is used to determine when to hide the
  199. /// menu.
  200. /// </summary>
  201. /// <remarks>
  202. /// <para>
  203. /// See <see cref="RaiseAccepted"/> for more information.
  204. /// </para>
  205. /// </remarks>
  206. public event EventHandler<CommandEventArgs>? Accepted;
  207. /// <inheritdoc/>
  208. protected override void Dispose (bool disposing)
  209. {
  210. if (disposing)
  211. {
  212. SubMenu?.Dispose ();
  213. SubMenu = null;
  214. }
  215. base.Dispose (disposing);
  216. }
  217. }