MenuItemv2.cs 6.9 KB

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