View.Command.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. public partial class View // Command APIs
  5. {
  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. // Enter - Raise Accepted
  13. AddCommand (Command.Accept, RaiseAccepted);
  14. // HotKey - SetFocus and raise HotKeyHandled
  15. AddCommand (Command.HotKey,
  16. () =>
  17. {
  18. SetFocus ();
  19. return RaiseHotKeyHandled ();
  20. });
  21. // Space or single-click - Raise Selected
  22. AddCommand (Command.Select, () =>
  23. {
  24. bool? cancelled = RaiseSelected ();
  25. if (cancelled is null or false && CanFocus)
  26. {
  27. SetFocus ();
  28. return true;
  29. }
  30. return cancelled is true;
  31. });
  32. }
  33. /// <summary>
  34. /// Called when the View's state has been accepted by the user. Calls <see cref="OnAccepted"/> which can be cancelled; if not cancelled raises <see cref="Accepted"/>.
  35. /// event. The default <see cref="Command.Accept"/> handler calls this method.
  36. /// </summary>
  37. /// <remarks>
  38. /// The <see cref="Accepted"/> event should raised after the state of the View has changed (after <see cref="Selected"/> is raised).
  39. /// </remarks>
  40. /// <returns>
  41. /// If <see langword="true"/> the event was canceled. If <see langword="false"/> the event was raised but not canceled.
  42. /// If <see langword="null"/> no event was raised.
  43. /// </returns>
  44. protected bool? RaiseAccepted ()
  45. {
  46. HandledEventArgs args = new ();
  47. // Best practice is to invoke the virtual method first.
  48. // This allows derived classes to handle the event and potentially cancel it.
  49. args.Handled = OnAccepted (args) || args.Handled;
  50. if (!args.Handled)
  51. {
  52. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  53. Accepted?.Invoke (this, args);
  54. }
  55. // Accept is a special case where if the event is not canceled, the event is
  56. // - Invoked on any peer-View with IsDefault == true
  57. // - bubbled up the SuperView hierarchy.
  58. if (!args.Handled)
  59. {
  60. // If there's an IsDefault peer view in Subviews, try it
  61. var isDefaultView = SuperView?.Subviews.FirstOrDefault (v => v is Button { IsDefault: true });
  62. if (isDefaultView != this && isDefaultView is Button { IsDefault: true } button)
  63. {
  64. bool? handled = isDefaultView.InvokeCommand (Command.Accept);
  65. if (handled == true)
  66. {
  67. return true;
  68. }
  69. }
  70. return SuperView?.InvokeCommand (Command.Accept) == true;
  71. }
  72. return Accepted is null ? null : args.Handled;
  73. }
  74. // TODO: Change this to CancelEventArgs
  75. /// <summary>
  76. /// Called when the View's state has been accepted by the user. Set <see cref="HandledEventArgs.Handled"/> to
  77. /// <see langword="true"/> to stop processing.
  78. /// </summary>
  79. /// <param name="args"></param>
  80. /// <returns><see langword="true"/> to stop processing.</returns>
  81. protected virtual bool OnAccepted (HandledEventArgs args) { return false; }
  82. /// <summary>
  83. /// Cancelable event raised when the View's state has been accepted by the user. Set
  84. /// <see cref="HandledEventArgs.Handled"/> to cancel the event.
  85. /// </summary>
  86. public event EventHandler<HandledEventArgs>? Accepted;
  87. /// <summary>
  88. /// Called when the user has selected the View or otherwise changed the state of the View. Calls <see cref="OnSelected"/> which can be cancelled; if not cancelled raises <see cref="Accepted"/>.
  89. /// event. The default <see cref="Command.Select"/> handler calls this method.
  90. /// </summary>
  91. /// <remarks>
  92. /// The <see cref="Selected"/> event should raised after the state of the View has been changed and before see <see cref="Accepted"/>.
  93. /// </remarks>
  94. /// <returns>
  95. /// If <see langword="true"/> the event was canceled. If <see langword="false"/> the event was raised but not canceled.
  96. /// If <see langword="null"/> no event was raised.
  97. /// </returns>
  98. protected bool? RaiseSelected ()
  99. {
  100. HandledEventArgs args = new ();
  101. // Best practice is to invoke the virtual method first.
  102. // This allows derived classes to handle the event and potentially cancel it.
  103. if (OnSelected (args) || args.Handled)
  104. {
  105. return true;
  106. }
  107. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  108. Selected?.Invoke (this, args);
  109. return Selected is null ? null : args.Handled;
  110. }
  111. /// <summary>
  112. /// Called when the user has selected the View or otherwise changed the state of the View. Set <see cref="HandledEventArgs.Handled"/> to
  113. /// <see langword="true"/> to stop processing.
  114. /// </summary>
  115. /// <param name="args"></param>
  116. /// <returns><see langword="true"/> to stop processing.</returns>
  117. protected virtual bool OnSelected (HandledEventArgs args) { return false; }
  118. /// <summary>
  119. /// Cancelable event raised when the user has selected the View or otherwise changed the state of the View. Set
  120. /// <see cref="HandledEventArgs.Handled"/>
  121. /// to cancel the event.
  122. /// </summary>
  123. public event EventHandler<HandledEventArgs>? Selected;
  124. // TODO: What does this event really do? "Called when the user has pressed the View's hot key or otherwise invoked the View's hot key command.???"
  125. /// <summary>
  126. /// Called when the View has handled the user pressing the View's <see cref="HotKey"/>. Calls <see cref="OnHotKeyHandled"/> which can be cancelled; if not cancelled raises <see cref="Accepted"/>.
  127. /// event. The default <see cref="Command.HotKey"/> handler calls this method.
  128. /// </summary>
  129. /// <returns>
  130. /// If <see langword="true"/> the event was handled. If <see langword="false"/> the event was raised but not handled.
  131. /// If <see langword="null"/> no event was raised.
  132. /// </returns>
  133. protected bool? RaiseHotKeyHandled ()
  134. {
  135. HandledEventArgs args = new ();
  136. // Best practice is to invoke the virtual method first.
  137. // This allows derived classes to handle the event and potentially cancel it.
  138. if (OnHotKeyHandled (args) || args.Handled)
  139. {
  140. return true;
  141. }
  142. // If the event is not canceled by the virtual method, raise the event to notify any external subscribers.
  143. HotKeyHandled?.Invoke (this, args);
  144. return HotKeyHandled is null ? null : args.Handled;
  145. }
  146. /// <summary>
  147. /// Called when the View has handled the user pressing the View's <see cref="HotKey"/>. Set <see cref="HandledEventArgs.Handled"/> to
  148. /// <see langword="true"/> to stop processing.
  149. /// </summary>
  150. /// <param name="args"></param>
  151. /// <returns><see langword="true"/> to stop processing.</returns>
  152. protected virtual bool OnHotKeyHandled (HandledEventArgs args) { return false; }
  153. /// <summary>
  154. /// Cancelable event raised when the <see cref="Command.HotKey"/> command is invoked. Set
  155. /// <see cref="HandledEventArgs.Handled"/>
  156. /// to cancel the event.
  157. /// </summary>
  158. public event EventHandler<HandledEventArgs>? HotKeyHandled;
  159. #endregion Default Implementation
  160. /// <summary>
  161. /// <para>
  162. /// Sets the function that will be invoked for a <see cref="Command"/>. Views should call
  163. /// AddCommand for each command they support.
  164. /// </para>
  165. /// <para>
  166. /// If AddCommand has already been called for <paramref name="command"/> <paramref name="f"/> will
  167. /// replace the old one.
  168. /// </para>
  169. /// </summary>
  170. /// <remarks>
  171. /// <para>
  172. /// This version of AddCommand is for commands that require <see cref="CommandContext"/>. Use
  173. /// <see cref="AddCommand(Command,Func{System.Nullable{bool}})"/>
  174. /// in cases where the command does not require a <see cref="CommandContext"/>.
  175. /// </para>
  176. /// </remarks>
  177. /// <param name="command">The command.</param>
  178. /// <param name="f">The function.</param>
  179. protected void AddCommand (Command command, Func<CommandContext, bool?> f) { CommandImplementations [command] = f; }
  180. /// <summary>
  181. /// <para>
  182. /// Sets the function that will be invoked for a <see cref="Command"/>. Views should call
  183. /// AddCommand for each command they support.
  184. /// </para>
  185. /// <para>
  186. /// If AddCommand has already been called for <paramref name="command"/> <paramref name="f"/> will
  187. /// replace the old one.
  188. /// </para>
  189. /// </summary>
  190. /// <remarks>
  191. /// <para>
  192. /// This version of AddCommand is for commands that do not require a <see cref="CommandContext"/>.
  193. /// If the command requires context, use
  194. /// <see cref="AddCommand(Command,Func{CommandContext,System.Nullable{bool}})"/>
  195. /// </para>
  196. /// </remarks>
  197. /// <param name="command">The command.</param>
  198. /// <param name="f">The function.</param>
  199. protected void AddCommand (Command command, Func<bool?> f) { CommandImplementations [command] = ctx => f (); }
  200. /// <summary>Returns all commands that are supported by this <see cref="View"/>.</summary>
  201. /// <returns></returns>
  202. public IEnumerable<Command> GetSupportedCommands () { return CommandImplementations.Keys; }
  203. /// <summary>
  204. /// Invokes the specified commands.
  205. /// </summary>
  206. /// <param name="commands"></param>
  207. /// <param name="key">The key that caused the commands to be invoked, if any.</param>
  208. /// <param name="keyBinding"></param>
  209. /// <returns>
  210. /// <see langword="null"/> if no command was found.
  211. /// <see langword="true"/> if the command was invoked the command was handled (or cancelled)
  212. /// <see langword="false"/> if the command was invoked and the command was not handled.
  213. /// </returns>
  214. public bool? InvokeCommands (Command [] commands, Key? key = null, KeyBinding? keyBinding = null)
  215. {
  216. bool? toReturn = null;
  217. foreach (Command command in commands)
  218. {
  219. if (!CommandImplementations.ContainsKey (command))
  220. {
  221. throw new NotSupportedException (@$"{command} is not supported by ({GetType ().Name}).");
  222. }
  223. // each command has its own return value
  224. bool? thisReturn = InvokeCommand (command, key, keyBinding);
  225. // if we haven't got anything yet, the current command result should be used
  226. toReturn ??= thisReturn;
  227. // if ever see a true then that's what we will return
  228. if (thisReturn ?? false)
  229. {
  230. toReturn = true;
  231. }
  232. }
  233. return toReturn;
  234. }
  235. /// <summary>Invokes the specified command.</summary>
  236. /// <param name="command">The command to invoke.</param>
  237. /// <param name="key">The key that caused the command to be invoked, if any.</param>
  238. /// <param name="keyBinding"></param>
  239. /// <returns>
  240. /// <see langword="null"/> if no command was found. <see langword="true"/> if the command was invoked, and it
  241. /// handled (or cancelled) the command. <see langword="false"/> if the command was invoked, and it did not handle (or cancel) the command.
  242. /// </returns>
  243. public bool? InvokeCommand (Command command, Key? key = null, KeyBinding? keyBinding = null)
  244. {
  245. if (CommandImplementations.TryGetValue (command, out Func<CommandContext, bool?>? implementation))
  246. {
  247. var context = new CommandContext (command, key, keyBinding); // Create the context here
  248. return implementation (context);
  249. }
  250. return null;
  251. }
  252. }