View.Command.cs 21 KB

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