View.Command.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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
  42. // - Invoked on any peer-View with IsDefault == true
  43. // - bubbled up the SuperView hierarchy.
  44. if (!args.Handled)
  45. {
  46. // If there's an IsDefault peer view in Subviews, try it
  47. var isDefaultView = SuperView?.Subviews.FirstOrDefault (v => v is Button { IsDefault: true });
  48. if (isDefaultView != this && isDefaultView is Button { IsDefault: true } button)
  49. {
  50. bool? handled = isDefaultView.InvokeCommand (Command.Accept);
  51. if (handled == true)
  52. {
  53. return true;
  54. }
  55. }
  56. return SuperView?.InvokeCommand (Command.Accept) == true;
  57. }
  58. return Accept is null ? null : args.Handled;
  59. }
  60. /// <summary>
  61. /// Called when the <see cref="Command.Accept"/> command is received. Set <see cref="HandledEventArgs.Handled"/> to
  62. /// <see langword="true"/> to stop processing.
  63. /// </summary>
  64. /// <param name="args"></param>
  65. /// <returns><see langword="true"/> to stop processing.</returns>
  66. protected virtual bool OnAccept (HandledEventArgs args) { return false; }
  67. /// <summary>
  68. /// Cancelable event raised when the <see cref="Command.Accept"/> command is invoked. Set
  69. /// <see cref="HandledEventArgs.Handled"/>
  70. /// to cancel the event.
  71. /// </summary>
  72. public event EventHandler<HandledEventArgs>? Accept;
  73. /// <summary>
  74. /// Called when the <see cref="Command.Select"/> command is invoked. Raises <see cref="Select"/>
  75. /// event.
  76. /// </summary>
  77. /// <returns>
  78. /// If <see langword="true"/> the event was canceled. If <see langword="false"/> the event was raised but not canceled.
  79. /// If <see langword="null"/> no event was raised.
  80. /// </returns>
  81. protected bool? RaiseSelectEvent ()
  82. {
  83. HandledEventArgs args = new ();
  84. // Best practice is to invoke the virtual method first.
  85. // This allows derived classes to handle the event and potentially cancel it.
  86. if (OnSelect (args) || args.Handled)
  87. {
  88. return true;
  89. }
  90. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  91. Select?.Invoke (this, args);
  92. if ((Select is null || !args.Handled) && CanFocus)
  93. {
  94. SetFocus ();
  95. }
  96. return Select is null ? null : args.Handled;
  97. }
  98. /// <summary>
  99. /// Called when the <see cref="Command.Select"/> command is received. Set <see cref="HandledEventArgs.Handled"/> to
  100. /// <see langword="true"/> to stop processing.
  101. /// </summary>
  102. /// <param name="args"></param>
  103. /// <returns><see langword="true"/> to stop processing.</returns>
  104. protected virtual bool OnSelect (HandledEventArgs args) { return false; }
  105. /// <summary>
  106. /// Cancelable event raised when the <see cref="Command.Select"/> command is invoked. Set
  107. /// <see cref="HandledEventArgs.Handled"/>
  108. /// to cancel the event.
  109. /// </summary>
  110. public event EventHandler<HandledEventArgs>? Select;
  111. /// <summary>
  112. /// Called when the <see cref="Command.HotKey"/> command is invoked. Raises <see cref="HotKey"/>
  113. /// event.
  114. /// </summary>
  115. /// <returns>
  116. /// If <see langword="true"/> the event was handled. If <see langword="false"/> the event was raised but not handled.
  117. /// If <see langword="null"/> no event was raised.
  118. /// </returns>
  119. protected bool? RaiseHotKeyCommandEvent ()
  120. {
  121. HandledEventArgs args = new ();
  122. // Best practice is to invoke the virtual method first.
  123. // This allows derived classes to handle the event and potentially cancel it.
  124. if (OnHotKeyCommand (args) || args.Handled)
  125. {
  126. return true;
  127. }
  128. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  129. HotKeyCommand?.Invoke (this, args);
  130. return HotKeyCommand is null ? null : args.Handled;
  131. }
  132. /// <summary>
  133. /// Called when the <see cref="Command.HotKey"/> command is received. Set <see cref="HandledEventArgs.Handled"/> to
  134. /// <see langword="true"/> to stop processing.
  135. /// </summary>
  136. /// <param name="args"></param>
  137. /// <returns><see langword="true"/> to stop processing.</returns>
  138. protected virtual bool OnHotKeyCommand (HandledEventArgs args) { return false; }
  139. /// <summary>
  140. /// Cancelable event raised when the <see cref="Command.HotKey"/> command is invoked. Set
  141. /// <see cref="HandledEventArgs.Handled"/>
  142. /// to cancel the event.
  143. /// </summary>
  144. public event EventHandler<HandledEventArgs>? HotKeyCommand;
  145. #endregion Default Implementation
  146. /// <summary>
  147. /// <para>
  148. /// Sets the function that will be invoked for a <see cref="Command"/>. Views should call
  149. /// AddCommand for each command they support.
  150. /// </para>
  151. /// <para>
  152. /// If AddCommand has already been called for <paramref name="command"/> <paramref name="f"/> will
  153. /// replace the old one.
  154. /// </para>
  155. /// </summary>
  156. /// <remarks>
  157. /// <para>
  158. /// This version of AddCommand is for commands that require <see cref="CommandContext"/>. Use
  159. /// <see cref="AddCommand(Command,Func{System.Nullable{bool}})"/>
  160. /// in cases where the command does not require a <see cref="CommandContext"/>.
  161. /// </para>
  162. /// </remarks>
  163. /// <param name="command">The command.</param>
  164. /// <param name="f">The function.</param>
  165. protected void AddCommand (Command command, Func<CommandContext, bool?> f) { CommandImplementations [command] = f; }
  166. /// <summary>
  167. /// <para>
  168. /// Sets the function that will be invoked for a <see cref="Command"/>. Views should call
  169. /// AddCommand for each command they support.
  170. /// </para>
  171. /// <para>
  172. /// If AddCommand has already been called for <paramref name="command"/> <paramref name="f"/> will
  173. /// replace the old one.
  174. /// </para>
  175. /// </summary>
  176. /// <remarks>
  177. /// <para>
  178. /// This version of AddCommand is for commands that do not require a <see cref="CommandContext"/>.
  179. /// If the command requires context, use
  180. /// <see cref="AddCommand(Command,Func{CommandContext,System.Nullable{bool}})"/>
  181. /// </para>
  182. /// </remarks>
  183. /// <param name="command">The command.</param>
  184. /// <param name="f">The function.</param>
  185. protected void AddCommand (Command command, Func<bool?> f) { CommandImplementations [command] = ctx => f (); }
  186. /// <summary>Returns all commands that are supported by this <see cref="View"/>.</summary>
  187. /// <returns></returns>
  188. public IEnumerable<Command> GetSupportedCommands () { return CommandImplementations.Keys; }
  189. /// <summary>
  190. /// Invokes the specified commands.
  191. /// </summary>
  192. /// <param name="commands"></param>
  193. /// <param name="key">The key that caused the commands to be invoked, if any.</param>
  194. /// <param name="keyBinding"></param>
  195. /// <returns>
  196. /// <see langword="null"/> if no command was found.
  197. /// <see langword="true"/> if the command was invoked the command was handled.
  198. /// <see langword="false"/> if the command was invoked and the command was not handled.
  199. /// </returns>
  200. public bool? InvokeCommands (Command [] commands, Key? key = null, KeyBinding? keyBinding = null)
  201. {
  202. bool? toReturn = null;
  203. foreach (Command command in commands)
  204. {
  205. if (!CommandImplementations.ContainsKey (command))
  206. {
  207. throw new NotSupportedException (@$"{command} is not supported by ({GetType ().Name}).");
  208. }
  209. // each command has its own return value
  210. bool? thisReturn = InvokeCommand (command, key, keyBinding);
  211. // if we haven't got anything yet, the current command result should be used
  212. toReturn ??= thisReturn;
  213. // if ever see a true then that's what we will return
  214. if (thisReturn ?? false)
  215. {
  216. toReturn = true;
  217. }
  218. }
  219. return toReturn;
  220. }
  221. /// <summary>Invokes the specified command.</summary>
  222. /// <param name="command">The command to invoke.</param>
  223. /// <param name="key">The key that caused the command to be invoked, if any.</param>
  224. /// <param name="keyBinding"></param>
  225. /// <returns>
  226. /// <see langword="null"/> if no command was found. <see langword="true"/> if the command was invoked, and it
  227. /// handled the command. <see langword="false"/> if the command was invoked, and it did not handle the command.
  228. /// </returns>
  229. public bool? InvokeCommand (Command command, Key? key = null, KeyBinding? keyBinding = null)
  230. {
  231. if (CommandImplementations.TryGetValue (command, out Func<CommandContext, bool?>? implementation))
  232. {
  233. var context = new CommandContext (command, key, keyBinding); // Create the context here
  234. return implementation (context);
  235. }
  236. return null;
  237. }
  238. }