View.Command.cs 16 KB

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