View.Command.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. public partial class View // Command APIs
  5. {
  6. #region Default Implementation
  7. /// <summary>
  8. /// Helper to configure all things Command related for a View. Called from the View constructor.
  9. /// </summary>
  10. private void SetupCommands ()
  11. {
  12. AddCommand (Command.Accept, RaiseAcceptEvent);
  13. AddCommand (
  14. Command.HotKey,
  15. () =>
  16. {
  17. SetFocus ();
  18. return RaiseHotKeyCommandEvent ();
  19. });
  20. AddCommand (Command.Select, RaiseSelectEvent);
  21. }
  22. /// <summary>
  23. /// Called when the <see cref="Command.Accept"/> command is invoked. Raises <see cref="Accept"/>
  24. /// event.
  25. /// </summary>
  26. /// <returns>
  27. /// If <see langword="true"/> the event was canceled. If <see langword="false"/> the event was raised but not canceled.
  28. /// If <see langword="null"/> no event was raised.
  29. /// </returns>
  30. protected bool? RaiseAcceptEvent ()
  31. {
  32. HandledEventArgs args = new ();
  33. // Best practice is to invoke the virtual method first.
  34. // This allows derived classes to handle the event and potentially cancel it.
  35. args.Handled = OnAccept (args) || args.Handled;
  36. if (!args.Handled)
  37. {
  38. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  39. Accept?.Invoke (this, args);
  40. }
  41. // Accept is a special case where if the event is not canceled, the event is bubbled up the SuperView hierarchy.
  42. if (!args.Handled)
  43. {
  44. return SuperView?.InvokeCommand (Command.Accept) == true;
  45. }
  46. return Accept is null ? null : args.Handled;
  47. }
  48. /// <summary>
  49. /// Called when the <see cref="Command.Accept"/> command is received. Set <see cref="HandledEventArgs.Handled"/> to
  50. /// <see langword="true"/> to stop processing.
  51. /// </summary>
  52. /// <param name="args"></param>
  53. /// <returns><see langword="true"/> to stop processing.</returns>
  54. protected virtual bool OnAccept (HandledEventArgs args) { return false; }
  55. /// <summary>
  56. /// Cancelable event raised when the <see cref="Command.Accept"/> command is invoked. Set
  57. /// <see cref="HandledEventArgs.Handled"/>
  58. /// to cancel the event.
  59. /// </summary>
  60. public event EventHandler<HandledEventArgs>? Accept;
  61. /// <summary>
  62. /// Called when the <see cref="Command.Select"/> command is invoked. Raises <see cref="Select"/>
  63. /// event.
  64. /// </summary>
  65. /// <returns>
  66. /// If <see langword="true"/> the event was canceled. If <see langword="false"/> the event was raised but not canceled.
  67. /// If <see langword="null"/> no event was raised.
  68. /// </returns>
  69. protected bool? RaiseSelectEvent ()
  70. {
  71. HandledEventArgs args = new ();
  72. // Best practice is to invoke the virtual method first.
  73. // This allows derived classes to handle the event and potentially cancel it.
  74. if (OnSelect (args) || args.Handled)
  75. {
  76. return true;
  77. }
  78. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  79. Select?.Invoke (this, args);
  80. return Select is null ? null : args.Handled;
  81. }
  82. /// <summary>
  83. /// Called when the <see cref="Command.Select"/> command is received. Set <see cref="HandledEventArgs.Handled"/> to
  84. /// <see langword="true"/> to stop processing.
  85. /// </summary>
  86. /// <param name="args"></param>
  87. /// <returns><see langword="true"/> to stop processing.</returns>
  88. protected virtual bool OnSelect (HandledEventArgs args) { return false; }
  89. /// <summary>
  90. /// Cancelable event raised when the <see cref="Command.Select"/> command is invoked. Set
  91. /// <see cref="HandledEventArgs.Handled"/>
  92. /// to cancel the event.
  93. /// </summary>
  94. public event EventHandler<HandledEventArgs>? Select;
  95. /// <summary>
  96. /// Called when the <see cref="Command.HotKey"/> command is invoked. Raises <see cref="HotKey"/>
  97. /// event.
  98. /// </summary>
  99. /// <returns>
  100. /// If <see langword="true"/> the event was handled. If <see langword="false"/> the event was raised but not handled.
  101. /// If <see langword="null"/> no event was raised.
  102. /// </returns>
  103. protected bool? RaiseHotKeyCommandEvent ()
  104. {
  105. HandledEventArgs args = new ();
  106. // Best practice is to invoke the virtual method first.
  107. // This allows derived classes to handle the event and potentially cancel it.
  108. if (OnHotKeyCommand (args) || args.Handled)
  109. {
  110. return true;
  111. }
  112. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  113. HotKeyCommand?.Invoke (this, args);
  114. return HotKeyCommand is null ? null : args.Handled;
  115. }
  116. /// <summary>
  117. /// Called when the <see cref="Command.HotKey"/> command is received. Set <see cref="HandledEventArgs.Handled"/> to
  118. /// <see langword="true"/> to stop processing.
  119. /// </summary>
  120. /// <param name="args"></param>
  121. /// <returns><see langword="true"/> to stop processing.</returns>
  122. protected virtual bool OnHotKeyCommand (HandledEventArgs args) { return false; }
  123. /// <summary>
  124. /// Cancelable event raised when the <see cref="Command.HotKey"/> command is invoked. Set
  125. /// <see cref="HandledEventArgs.Handled"/>
  126. /// to cancel the event.
  127. /// </summary>
  128. public event EventHandler<HandledEventArgs>? HotKeyCommand;
  129. #endregion Default Implementation
  130. /// <summary>
  131. /// <para>
  132. /// Sets the function that will be invoked for a <see cref="Command"/>. Views should call
  133. /// AddCommand for each command they support.
  134. /// </para>
  135. /// <para>
  136. /// If AddCommand has already been called for <paramref name="command"/> <paramref name="f"/> will
  137. /// replace the old one.
  138. /// </para>
  139. /// </summary>
  140. /// <remarks>
  141. /// <para>
  142. /// This version of AddCommand is for commands that require <see cref="CommandContext"/>. Use
  143. /// <see cref="AddCommand(Command,Func{System.Nullable{bool}})"/>
  144. /// in cases where the command does not require a <see cref="CommandContext"/>.
  145. /// </para>
  146. /// </remarks>
  147. /// <param name="command">The command.</param>
  148. /// <param name="f">The function.</param>
  149. protected void AddCommand (Command command, Func<CommandContext, bool?> f) { CommandImplementations [command] = f; }
  150. /// <summary>
  151. /// <para>
  152. /// Sets the function that will be invoked for a <see cref="Command"/>. Views should call
  153. /// AddCommand for each command they support.
  154. /// </para>
  155. /// <para>
  156. /// If AddCommand has already been called for <paramref name="command"/> <paramref name="f"/> will
  157. /// replace the old one.
  158. /// </para>
  159. /// </summary>
  160. /// <remarks>
  161. /// <para>
  162. /// This version of AddCommand is for commands that do not require a <see cref="CommandContext"/>.
  163. /// If the command requires context, use
  164. /// <see cref="AddCommand(Command,Func{CommandContext,System.Nullable{bool}})"/>
  165. /// </para>
  166. /// </remarks>
  167. /// <param name="command">The command.</param>
  168. /// <param name="f">The function.</param>
  169. protected void AddCommand (Command command, Func<bool?> f) { CommandImplementations [command] = ctx => f (); }
  170. /// <summary>Returns all commands that are supported by this <see cref="View"/>.</summary>
  171. /// <returns></returns>
  172. public IEnumerable<Command> GetSupportedCommands () { return CommandImplementations.Keys; }
  173. /// <summary>
  174. /// Invokes the specified commands.
  175. /// </summary>
  176. /// <param name="commands"></param>
  177. /// <param name="key">The key that caused the commands to be invoked, if any.</param>
  178. /// <param name="keyBinding"></param>
  179. /// <returns>
  180. /// <see langword="null"/> if no command was found.
  181. /// <see langword="true"/> if the command was invoked the command was handled.
  182. /// <see langword="false"/> if the command was invoked and the command was not handled.
  183. /// </returns>
  184. public bool? InvokeCommands (Command [] commands, Key? key = null, KeyBinding? keyBinding = null)
  185. {
  186. bool? toReturn = null;
  187. foreach (Command command in commands)
  188. {
  189. if (!CommandImplementations.ContainsKey (command))
  190. {
  191. throw new NotSupportedException (@$"{command} is not supported by ({GetType ().Name}).");
  192. }
  193. // each command has its own return value
  194. bool? thisReturn = InvokeCommand (command, key, keyBinding);
  195. // if we haven't got anything yet, the current command result should be used
  196. toReturn ??= thisReturn;
  197. // if ever see a true then that's what we will return
  198. if (thisReturn ?? false)
  199. {
  200. toReturn = true;
  201. }
  202. }
  203. return toReturn;
  204. }
  205. /// <summary>Invokes the specified command.</summary>
  206. /// <param name="command">The command to invoke.</param>
  207. /// <param name="key">The key that caused the command to be invoked, if any.</param>
  208. /// <param name="keyBinding"></param>
  209. /// <returns>
  210. /// <see langword="null"/> if no command was found. <see langword="true"/> if the command was invoked, and it
  211. /// handled the command. <see langword="false"/> if the command was invoked, and it did not handle the command.
  212. /// </returns>
  213. public bool? InvokeCommand (Command command, Key? key = null, KeyBinding? keyBinding = null)
  214. {
  215. if (CommandImplementations.TryGetValue (command, out Func<CommandContext, bool?>? implementation))
  216. {
  217. var context = new CommandContext (command, key, keyBinding); // Create the context here
  218. return implementation (context);
  219. }
  220. return null;
  221. }
  222. }