View.Command.cs 11 KB

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