MenuItemv2.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. /// 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 = null, string? helpText = null, Menuv2? subMenu = null)
  52. : base (Key.Empty, commandText, null, helpText)
  53. {
  54. SubMenu = subMenu;
  55. }
  56. // TODO: Consider moving TargetView and Command to Shortcut?
  57. /// <summary>
  58. /// Gets the target <see cref="View"/> that the <see cref="Command"/> will be invoked on.
  59. /// </summary>
  60. public View? TargetView { get; set; }
  61. private Command _command;
  62. /// <summary>
  63. /// Gets the <see cref="Command"/> that will be invoked on <see cref="TargetView"/> when the MenuItem is selected.
  64. /// </summary>
  65. public Command Command
  66. {
  67. get => _command;
  68. set
  69. {
  70. if (_command == value)
  71. {
  72. return;
  73. }
  74. _command = value;
  75. if (string.IsNullOrEmpty (Title))
  76. {
  77. Title = GlobalResources.GetString ($"cmd.{_command}") ?? string.Empty;
  78. }
  79. if (string.IsNullOrEmpty (HelpText))
  80. {
  81. HelpText = GlobalResources.GetString ($"cmd.{_command}.Help") ?? string.Empty;
  82. }
  83. }
  84. }
  85. internal override bool? DispatchCommand (ICommandContext? commandContext)
  86. {
  87. bool? ret = null;
  88. if (commandContext is { Command: not Command.HotKey })
  89. {
  90. if (TargetView is { })
  91. {
  92. commandContext.Command = Command;
  93. ret = TargetView.InvokeCommand (Command, commandContext);
  94. }
  95. else
  96. {
  97. // Is this an Application-bound command?
  98. ret = Application.InvokeCommandsBoundToKey (Key);
  99. }
  100. }
  101. if (ret is not true)
  102. {
  103. ret = base.DispatchCommand (commandContext);
  104. }
  105. //Logging.Trace ($"{commandContext?.Source?.Title}");
  106. RaiseAccepted (commandContext);
  107. return ret;
  108. }
  109. private Menuv2? _subMenu;
  110. /// <summary>
  111. /// The submenu to display when the user selects this menu item.
  112. /// </summary>
  113. public Menuv2? SubMenu
  114. {
  115. get => _subMenu;
  116. set
  117. {
  118. _subMenu = value;
  119. if (_subMenu is { })
  120. {
  121. // TODO: This is a temporary hack - add a flag or something instead
  122. KeyView.Text = $"{Glyphs.RightArrow}";
  123. _subMenu.SuperMenuItem = this;
  124. }
  125. }
  126. }
  127. /// <inheritdoc/>
  128. protected override bool OnMouseEnter (CancelEventArgs eventArgs)
  129. {
  130. // When the mouse enters a menuitem, we set focus to it automatically.
  131. // Logging.Trace($"OnEnter {Title}");
  132. SetFocus ();
  133. return base.OnMouseEnter (eventArgs);
  134. }
  135. // TODO: Consider moving Accepted to Shortcut?
  136. /// <summary>
  137. /// Raises the <see cref="OnAccepted"/>/<see cref="Accepted"/> event indicating this item (or submenu)
  138. /// was accepted. This is used to determine when to hide the menu.
  139. /// </summary>
  140. /// <param name="ctx"></param>
  141. /// <returns></returns>
  142. protected bool? RaiseAccepted (ICommandContext? ctx)
  143. {
  144. Logging.Trace ($"RaiseAccepted: {ctx}");
  145. CommandEventArgs args = new () { Context = ctx };
  146. OnAccepted (args);
  147. Accepted?.Invoke (this, args);
  148. return true;
  149. }
  150. /// <summary>
  151. /// Called when the user has accepted an item in this menu (or submenu). This is used to determine when to hide the
  152. /// menu.
  153. /// </summary>
  154. /// <remarks>
  155. /// </remarks>
  156. /// <param name="args"></param>
  157. protected virtual void OnAccepted (CommandEventArgs args) { }
  158. /// <summary>
  159. /// Raised when the user has accepted an item in this menu (or submenu). This is used to determine when to hide the
  160. /// menu.
  161. /// </summary>
  162. /// <remarks>
  163. /// <para>
  164. /// See <see cref="RaiseAccepted"/> for more information.
  165. /// </para>
  166. /// </remarks>
  167. public event EventHandler<CommandEventArgs>? Accepted;
  168. /// <inheritdoc/>
  169. protected override void Dispose (bool disposing)
  170. {
  171. if (disposing)
  172. {
  173. SubMenu?.Dispose ();
  174. SubMenu = null;
  175. }
  176. base.Dispose (disposing);
  177. }
  178. }