View.Command.cs 23 KB

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