KeyBinding.cs 9.9 KB

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