View.Command.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. // Enter - Raise Accepted
  13. AddCommand (Command.Accept, RaiseAccepting);
  14. // HotKey - SetFocus and raise HandlingHotKey
  15. AddCommand (Command.HotKey,
  16. () =>
  17. {
  18. if (RaiseHandlingHotKey () is true)
  19. {
  20. return true;
  21. }
  22. SetFocus ();
  23. return true;
  24. });
  25. // Space or single-click - Raise Selecting
  26. AddCommand (Command.Select, (ctx) =>
  27. {
  28. if (RaiseSelecting (ctx) is true)
  29. {
  30. return true;
  31. }
  32. if (CanFocus)
  33. {
  34. SetFocus ();
  35. return true;
  36. }
  37. return false;
  38. });
  39. }
  40. /// <summary>
  41. /// Called when the user is accepting the state of the View and the <see cref="Command.Accept"/> has been invoked. Calls <see cref="OnAccepting"/> which can be cancelled; if not cancelled raises <see cref="Accepting"/>.
  42. /// event. The default <see cref="Command.Accept"/> handler calls this method.
  43. /// </summary>
  44. /// <remarks>
  45. /// <para>
  46. /// The <see cref="Accepting"/> event should raised after the state of the View has changed (after <see cref="Selecting"/> is raised).
  47. /// </para>
  48. /// <para>
  49. /// If the Accepting event is not handled, <see cref="Command.Accept"/> will be invoked on the SuperView, enabling default Accept behavior.
  50. /// </para>
  51. /// <para>
  52. /// If a peer-View raises the Accepting event and the event is not cancelled, the <see cref="Command.Accept"/> will be invoked on the
  53. /// first Button in the SuperView that has <see cref="Button.IsDefault"/> set to <see langword="true"/>.
  54. /// </para>
  55. /// </remarks>
  56. /// <returns>
  57. /// If <see langword="true"/> the event was canceled. If <see langword="false"/> the event was raised but not canceled.
  58. /// If <see langword="null"/> no event was raised.
  59. /// </returns>
  60. protected bool? RaiseAccepting (CommandContext ctx)
  61. {
  62. CommandEventArgs args = new () { Context = ctx };
  63. // Best practice is to invoke the virtual method first.
  64. // This allows derived classes to handle the event and potentially cancel it.
  65. args.Cancel = OnAccepting (args) || args.Cancel;
  66. if (!args.Cancel)
  67. {
  68. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  69. Accepting?.Invoke (this, args);
  70. }
  71. // Accept is a special case where if the event is not canceled, the event is
  72. // - Invoked on any peer-View with IsDefault == true
  73. // - bubbled up the SuperView hierarchy.
  74. if (!args.Cancel)
  75. {
  76. // If there's an IsDefault peer view in Subviews, try it
  77. var isDefaultView = SuperView?.Subviews.FirstOrDefault (v => v is Button { IsDefault: true });
  78. if (isDefaultView != this && isDefaultView is Button { IsDefault: true } button)
  79. {
  80. bool? handled = isDefaultView.InvokeCommand (Command.Accept, ctx: new (Command.Accept, null, null, this));
  81. if (handled == true)
  82. {
  83. return true;
  84. }
  85. }
  86. return SuperView?.InvokeCommand (Command.Accept, ctx: new (Command.Accept, null, null, this)) == true;
  87. }
  88. return Accepting is null ? null : args.Cancel;
  89. }
  90. /// <summary>
  91. /// Called when the user is accepting the state of the View and the <see cref="Command.Accept"/> has been invoked. Set CommandEventArgs.Cancel to
  92. /// <see langword="true"/> and return <see langword="true"/> to stop processing.
  93. /// </summary>
  94. /// <remarks>
  95. /// <para>
  96. /// See <see cref="View.RaiseAccepting"/> for more information.
  97. /// </para>
  98. /// </remarks>
  99. /// <param name="args"></param>
  100. /// <returns><see langword="true"/> to stop processing.</returns>
  101. protected virtual bool OnAccepting (CommandEventArgs args) { return false; }
  102. /// <summary>
  103. /// Cancelable event raised when the user is accepting the state of the View and the <see cref="Command.Accept"/> has been invoked. Set
  104. /// CommandEventArgs.Cancel to cancel the event.
  105. /// </summary>
  106. /// <remarks>
  107. /// <para>
  108. /// See <see cref="View.RaiseAccepting"/> for more information.
  109. /// </para>
  110. /// </remarks>
  111. public event EventHandler<CommandEventArgs>? Accepting;
  112. /// <summary>
  113. /// Called when the user has performed an action (e.g. <see cref="Command.Select"/>) causing the View to change state. Calls <see cref="OnSelecting"/> which can be cancelled; if not cancelled raises <see cref="Accepting"/>.
  114. /// event. The default <see cref="Command.Select"/> handler calls this method.
  115. /// </summary>
  116. /// <remarks>
  117. /// The <see cref="Selecting"/> event should raised after the state of the View has been changed and before see <see cref="Accepting"/>.
  118. /// </remarks>
  119. /// <returns>
  120. /// If <see langword="true"/> the event was canceled. If <see langword="false"/> the event was raised but not canceled.
  121. /// If <see langword="null"/> no event was raised.
  122. /// </returns>
  123. protected bool? RaiseSelecting (CommandContext ctx)
  124. {
  125. CommandEventArgs args = new () { Context = ctx };
  126. // Best practice is to invoke the virtual method first.
  127. // This allows derived classes to handle the event and potentially cancel it.
  128. if (OnSelecting (args) || args.Cancel)
  129. {
  130. return true;
  131. }
  132. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  133. Selecting?.Invoke (this, args);
  134. return Selecting is null ? null : args.Cancel;
  135. }
  136. /// <summary>
  137. /// Called when the user has performed an action (e.g. <see cref="Command.Select"/>) causing the View to change state.
  138. /// Set CommandEventArgs.Cancel to
  139. /// <see langword="true"/> and return <see langword="true"/> to cancel the state change. The default implementation does nothing.
  140. /// </summary>
  141. /// <param name="args">The event arguments.</param>
  142. /// <returns><see langword="true"/> to stop processing.</returns>
  143. protected virtual bool OnSelecting (CommandEventArgs args) { return false; }
  144. /// <summary>
  145. /// Cancelable event raised when the user has performed an action (e.g. <see cref="Command.Select"/>) causing the View to change state.
  146. /// CommandEventArgs.Cancel to <see langword="true"/> to cancel the state change.
  147. /// </summary>
  148. public event EventHandler<CommandEventArgs>? Selecting;
  149. /// <summary>
  150. /// Called when the View is handling the user pressing the View's <see cref="HotKey"/>s. Calls <see cref="OnHandlingHotKey"/> which can be cancelled; if not cancelled raises <see cref="Accepting"/>.
  151. /// event. The default <see cref="Command.HotKey"/> handler calls this method.
  152. /// </summary>
  153. /// <returns>
  154. /// If <see langword="true"/> the event was handled. If <see langword="false"/> the event was raised but not handled.
  155. /// If <see langword="null"/> no event was raised.
  156. /// </returns>
  157. protected bool? RaiseHandlingHotKey ()
  158. {
  159. CommandEventArgs args = new ();
  160. // Best practice is to invoke the virtual method first.
  161. // This allows derived classes to handle the event and potentially cancel it.
  162. if (OnHandlingHotKey (args) || args.Cancel)
  163. {
  164. return true;
  165. }
  166. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  167. HandlingHotKey?.Invoke (this, args);
  168. return HandlingHotKey is null ? null : args.Cancel;
  169. }
  170. /// <summary>
  171. /// Called when the View is handling the user pressing the View's <see cref="HotKey"/>. Set CommandEventArgs.Cancel to
  172. /// <see langword="true"/> to stop processing.
  173. /// </summary>
  174. /// <param name="args"></param>
  175. /// <returns><see langword="true"/> to stop processing.</returns>
  176. protected virtual bool OnHandlingHotKey (CommandEventArgs args) { return false; }
  177. /// <summary>
  178. /// Cancelable event raised when the View is handling the user pressing the View's <see cref="HotKey"/>. Set
  179. /// CommandEventArgs.Cancel to cancel the event.
  180. /// </summary>
  181. public event EventHandler<CommandEventArgs>? HandlingHotKey;
  182. #endregion Default Implementation
  183. /// <summary>
  184. /// <para>
  185. /// Sets the function that will be invoked for a <see cref="Command"/>. Views should call
  186. /// AddCommand for each command they support.
  187. /// </para>
  188. /// <para>
  189. /// If AddCommand has already been called for <paramref name="command"/> <paramref name="f"/> will
  190. /// replace the old one.
  191. /// </para>
  192. /// </summary>
  193. /// <remarks>
  194. /// <para>
  195. /// This version of AddCommand is for commands that require <see cref="CommandContext"/>. Use
  196. /// <see cref="AddCommand(Command,Func{System.Nullable{bool}})"/>
  197. /// in cases where the command does not require a <see cref="CommandContext"/>.
  198. /// </para>
  199. /// </remarks>
  200. /// <param name="command">The command.</param>
  201. /// <param name="f">The function.</param>
  202. protected void AddCommand (Command command, Func<CommandContext, bool?> f) { CommandImplementations [command] = f; }
  203. /// <summary>
  204. /// <para>
  205. /// Sets the function that will be invoked for a <see cref="Command"/>. Views should call
  206. /// AddCommand for each command they support.
  207. /// </para>
  208. /// <para>
  209. /// If AddCommand has already been called for <paramref name="command"/> <paramref name="f"/> will
  210. /// replace the old one.
  211. /// </para>
  212. /// </summary>
  213. /// <remarks>
  214. /// <para>
  215. /// This version of AddCommand is for commands that do not require a <see cref="CommandContext"/>.
  216. /// If the command requires context, use
  217. /// <see cref="AddCommand(Command,Func{CommandContext,System.Nullable{bool}})"/>
  218. /// </para>
  219. /// </remarks>
  220. /// <param name="command">The command.</param>
  221. /// <param name="f">The function.</param>
  222. protected void AddCommand (Command command, Func<bool?> f) { CommandImplementations [command] = ctx => f (); }
  223. /// <summary>Returns all commands that are supported by this <see cref="View"/>.</summary>
  224. /// <returns></returns>
  225. public IEnumerable<Command> GetSupportedCommands () { return CommandImplementations.Keys; }
  226. /// <summary>
  227. /// Invokes the specified commands.
  228. /// </summary>
  229. /// <param name="commands"></param>
  230. /// <param name="key">The key that caused the commands to be invoked, if any.</param>
  231. /// <param name="keyBinding"></param>
  232. /// <returns>
  233. /// <see langword="null"/> if no command was found.
  234. /// <see langword="true"/> if the command was invoked the command was handled (or cancelled)
  235. /// <see langword="false"/> if the command was invoked and the command was not handled.
  236. /// </returns>
  237. public bool? InvokeCommands (Command [] commands, Key? key = null, KeyBinding? keyBinding = null)
  238. {
  239. bool? toReturn = null;
  240. foreach (Command command in commands)
  241. {
  242. if (!CommandImplementations.ContainsKey (command))
  243. {
  244. throw new NotSupportedException (@$"{command} is not supported by ({GetType ().Name}).");
  245. }
  246. // each command has its own return value
  247. bool? thisReturn = InvokeCommand (command, key, keyBinding);
  248. // if we haven't got anything yet, the current command result should be used
  249. toReturn ??= thisReturn;
  250. // if ever see a true then that's what we will return
  251. if (thisReturn ?? false)
  252. {
  253. toReturn = true;
  254. }
  255. }
  256. return toReturn;
  257. }
  258. /// <summary>Invokes the specified command.</summary>
  259. /// <param name="command">The command to invoke.</param>
  260. /// <param name="key">The key that caused the command to be invoked, if any.</param>
  261. /// <param name="keyBinding"></param>
  262. /// <returns>
  263. /// <see langword="null"/> if no command was found. <see langword="true"/> if the command was invoked, and it
  264. /// handled (or cancelled) the command. <see langword="false"/> if the command was invoked, and it did not handle (or cancel) the command.
  265. /// </returns>
  266. public bool? InvokeCommand (Command command, Key? key = null, KeyBinding? keyBinding = null)
  267. {
  268. if (CommandImplementations.TryGetValue (command, out Func<CommandContext, bool?>? implementation))
  269. {
  270. var context = new CommandContext (command, key, keyBinding); // Create the context here
  271. return implementation (context);
  272. }
  273. return null;
  274. }
  275. /// <summary>
  276. /// Invokes the specified command.
  277. /// </summary>
  278. /// <param name="command">The command to invoke.</param>
  279. /// <param name="ctx">Context to pass with the invocation.</param>
  280. /// <returns>
  281. /// <see langword="null"/> if no command was found. <see langword="true"/> if the command was invoked, and it
  282. /// handled (or cancelled) the command. <see langword="false"/> if the command was invoked, and it did not handle (or cancel) the command.
  283. /// </returns>
  284. public bool? InvokeCommand (Command command, CommandContext ctx)
  285. {
  286. if (CommandImplementations.TryGetValue (command, out Func<CommandContext, bool?>? implementation))
  287. {
  288. return implementation (ctx);
  289. }
  290. return null;
  291. }
  292. }