KeyBindings.cs 9.6 KB

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