View.Command.cs 20 KB

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