View.Command.cs 19 KB

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