KeyBinding.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // These classes use a key binding system based on the design implemented in Scintilla.Net which is an
  2. // MIT licensed open source project https://github.com/jacobslusser/ScintillaNET/blob/master/src/ScintillaNET/Command.cs
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Defines the scope of a <see cref="Command"/> that has been bound to a key with
  6. /// <see cref="KeyBindings.Add(Key, Terminal.Gui.Command[])"/>.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>Key bindings are scoped to the most-focused view (<see cref="Focused"/>) by default.</para>
  10. /// </remarks>
  11. [Flags]
  12. public enum KeyBindingScope
  13. {
  14. /// <summary>The key binding is scoped to just the view that has focus.</summary>
  15. Focused = 1,
  16. /// <summary>
  17. /// The key binding is scoped to the View's SuperView and will be triggered even when the View does not have focus, as
  18. /// long as the SuperView does have focus. This is typically used for <see cref="View.HotKey"/>s.
  19. /// <remarks>
  20. /// <para>
  21. /// Use for Views such as MenuBar and StatusBar which provide commands (shortcuts etc...) that trigger even
  22. /// when not focused.
  23. /// </para>
  24. /// <para>
  25. /// HotKey-scoped key bindings are only invoked if the key down event was not handled by the focused view or
  26. /// any of its subviews.
  27. /// </para>
  28. /// </remarks>
  29. /// </summary>
  30. HotKey = 2,
  31. /// <summary>
  32. /// The key binding will be triggered regardless of which view has focus. This is typically used for global
  33. /// commands.
  34. /// </summary>
  35. /// <remarks>
  36. /// Application-scoped key bindings are only invoked if the key down event was not handled by the focused view or
  37. /// any of its subviews, and if the key down event was not bound to a <see cref="View.HotKey"/>.
  38. /// </remarks>
  39. Application = 4
  40. }
  41. /// <summary>Provides a collection of <see cref="Command"/> objects that are scoped to <see cref="KeyBindingScope"/>.</summary>
  42. public class KeyBinding
  43. {
  44. /// <summary>Initializes a new instance.</summary>
  45. /// <param name="commands"></param>
  46. /// <param name="scope"></param>
  47. public KeyBinding (Command [] commands, KeyBindingScope scope)
  48. {
  49. Commands = commands;
  50. Scope = scope;
  51. }
  52. /// <summary>The actions which can be performed by the application or bound to keys in a <see cref="View"/> control.</summary>
  53. public Command [] Commands { get; set; }
  54. /// <summary>The scope of the <see cref="Commands"/> bound to a key.</summary>
  55. public KeyBindingScope Scope { get; set; }
  56. }
  57. /// <summary>A class that provides a collection of <see cref="KeyBinding"/> objects bound to a <see cref="Key"/>.</summary>
  58. public class KeyBindings
  59. {
  60. // TODO: Add a dictionary comparer that ignores Scope
  61. /// <summary>The collection of <see cref="KeyBinding"/> objects.</summary>
  62. public Dictionary<Key, KeyBinding> Bindings { get; } = new ();
  63. /// <summary>Adds a <see cref="KeyBinding"/> to the collection.</summary>
  64. /// <param name="key"></param>
  65. /// <param name="binding"></param>
  66. public void Add (Key key, KeyBinding binding) { Bindings.Add (key, binding); }
  67. /// <summary>
  68. /// <para>Adds a new key combination that will trigger the commands in <paramref name="commands"/>.</para>
  69. /// <para>
  70. /// If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
  71. /// <paramref name="commands"/>.
  72. /// </para>
  73. /// </summary>
  74. /// <remarks>
  75. /// Commands are only ever applied to the current <see cref="View"/> (i.e. this feature cannot be used to switch
  76. /// focus to another view and perform multiple commands there).
  77. /// </remarks>
  78. /// <param name="key">The key to check.</param>
  79. /// <param name="scope">The scope for the command.</param>
  80. /// <param name="commands">
  81. /// The command to invoked on the <see cref="View"/> when <paramref name="key"/> is pressed. When
  82. /// multiple commands are provided,they will be applied in sequence. The bound <paramref name="key"/> strike will be
  83. /// consumed if any took effect.
  84. /// </param>
  85. public void Add (Key key, KeyBindingScope scope, params Command [] commands)
  86. {
  87. if (key is null || !key.IsValid)
  88. {
  89. //throw new ArgumentException ("Invalid Key", nameof (commands));
  90. return;
  91. }
  92. if (commands.Length == 0)
  93. {
  94. throw new ArgumentException (@"At least one command must be specified", nameof (commands));
  95. }
  96. if (TryGet (key, out KeyBinding _))
  97. {
  98. Bindings [key] = new KeyBinding (commands, scope);
  99. }
  100. else
  101. {
  102. Bindings.Add (key, new KeyBinding (commands, scope));
  103. }
  104. }
  105. /// <summary>
  106. /// <para>
  107. /// Adds a new key combination that will trigger the commands in <paramref name="commands"/> (if supported by the
  108. /// View - see <see cref="View.GetSupportedCommands"/>).
  109. /// </para>
  110. /// <para>
  111. /// This is a helper function for <see cref="Add(Key,KeyBindingScope,Terminal.Gui.Command[])"/> for
  112. /// <see cref="KeyBindingScope.Focused"/> scoped commands.
  113. /// </para>
  114. /// <para>
  115. /// If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
  116. /// <paramref name="commands"/>.
  117. /// </para>
  118. /// </summary>
  119. /// <remarks>
  120. /// Commands are only ever applied to the current <see cref="View"/> (i.e. this feature cannot be used to switch
  121. /// focus to another view and perform multiple commands there).
  122. /// </remarks>
  123. /// <param name="key">The key to check.</param>
  124. /// <param name="commands">
  125. /// The command to invoked on the <see cref="View"/> when <paramref name="key"/> is pressed. When
  126. /// multiple commands are provided,they will be applied in sequence. The bound <paramref name="key"/> strike will be
  127. /// consumed if any took effect.
  128. /// </param>
  129. public void Add (Key key, params Command [] commands) { Add (key, KeyBindingScope.Focused, commands); }
  130. /// <summary>Removes all <see cref="KeyBinding"/> objects from the collection.</summary>
  131. public void Clear () { Bindings.Clear (); }
  132. /// <summary>
  133. /// Removes all key bindings that trigger the given command set. Views can have multiple different keys bound to
  134. /// the same command sets and this method will clear all of them.
  135. /// </summary>
  136. /// <param name="command"></param>
  137. public void Clear (params Command [] command)
  138. {
  139. var kvps = Bindings
  140. .Where (kvp => kvp.Value.Commands.SequenceEqual (command))
  141. .ToArray ();
  142. foreach (KeyValuePair<Key, KeyBinding> kvp in kvps)
  143. {
  144. Bindings.Remove (kvp.Key);
  145. }
  146. }
  147. /// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
  148. /// <param name="key"></param>
  149. /// <returns></returns>
  150. public KeyBinding Get (Key key) { return TryGet (key, out KeyBinding binding) ? binding : null; }
  151. /// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
  152. /// <param name="key"></param>
  153. /// <param name="scope"></param>
  154. /// <returns></returns>
  155. public KeyBinding Get (Key key, KeyBindingScope scope) { return TryGet (key, scope, out KeyBinding binding) ? binding : null; }
  156. /// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="key"/> if it exists.</summary>
  157. /// <param name="key">The key to check.</param>
  158. /// <returns>
  159. /// The array of <see cref="Command"/>s if <paramref name="key"/> is bound. An empty <see cref="Command"/> array
  160. /// if not.
  161. /// </returns>
  162. public Command [] GetCommands (Key key)
  163. {
  164. if (TryGet (key, out KeyBinding bindings))
  165. {
  166. return bindings.Commands;
  167. }
  168. return Array.Empty<Command> ();
  169. }
  170. /// <summary>Gets the Key used by a set of commands.</summary>
  171. /// <remarks></remarks>
  172. /// <param name="commands">The set of commands to search.</param>
  173. /// <returns>The <see cref="Key"/> used by a <see cref="Command"/></returns>
  174. /// <exception cref="InvalidOperationException">If no matching set of commands was found.</exception>
  175. public Key GetKeyFromCommands (params Command [] commands) { return Bindings.First (a => a.Value.Commands.SequenceEqual (commands)).Key; }
  176. /// <summary>Removes a <see cref="KeyBinding"/> from the collection.</summary>
  177. /// <param name="key"></param>
  178. public void Remove (Key key) { Bindings.Remove (key); }
  179. /// <summary>Replaces a key combination already bound to a set of <see cref="Command"/>s.</summary>
  180. /// <remarks></remarks>
  181. /// <param name="fromKey">The key to be replaced.</param>
  182. /// <param name="toKey">The new key to be used.</param>
  183. public void Replace (Key fromKey, Key toKey)
  184. {
  185. if (!TryGet (fromKey, out KeyBinding _))
  186. {
  187. return;
  188. }
  189. KeyBinding value = Bindings [fromKey];
  190. Bindings.Remove (fromKey);
  191. Bindings [toKey] = value;
  192. }
  193. /// <summary>Gets the commands bound with the specified Key.</summary>
  194. /// <remarks></remarks>
  195. /// <param name="key">The key to check.</param>
  196. /// <param name="binding">
  197. /// When this method returns, contains the commands bound with the specified Key, if the Key is
  198. /// found; otherwise, null. This parameter is passed uninitialized.
  199. /// </param>
  200. /// <returns><see langword="true"/> if the Key is bound; otherwise <see langword="false"/>.</returns>
  201. public bool TryGet (Key key, out KeyBinding binding)
  202. {
  203. if (key.IsValid)
  204. {
  205. return Bindings.TryGetValue (key, out binding);
  206. }
  207. binding = new KeyBinding (Array.Empty<Command> (), KeyBindingScope.Focused);
  208. return false;
  209. }
  210. /// <summary>Gets the commands bound with the specified Key that are scoped to a particular scope.</summary>
  211. /// <remarks></remarks>
  212. /// <param name="key">The key to check.</param>
  213. /// <param name="scope">the scope to filter on</param>
  214. /// <param name="binding">
  215. /// When this method returns, contains the commands bound with the specified Key, if the Key is
  216. /// found; otherwise, null. This parameter is passed uninitialized.
  217. /// </param>
  218. /// <returns><see langword="true"/> if the Key is bound; otherwise <see langword="false"/>.</returns>
  219. public bool TryGet (Key key, KeyBindingScope scope, out KeyBinding binding)
  220. {
  221. if (key.IsValid && Bindings.TryGetValue (key, out binding))
  222. {
  223. if (scope.HasFlag (binding.Scope))
  224. {
  225. return true;
  226. }
  227. }
  228. binding = new KeyBinding (Array.Empty<Command> (), KeyBindingScope.Focused);
  229. return false;
  230. }
  231. }