KeyBindings.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. if (scope.FastHasFlags (KeyBindingScope.Application))
  76. {
  77. Application.AddKeyBinding (key, BoundView);
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// <para>
  83. /// Adds a new key combination that will trigger the commands in <paramref name="commands"/> (if supported by the
  84. /// View - see <see cref="View.GetSupportedCommands"/>).
  85. /// </para>
  86. /// <para>
  87. /// This is a helper function for <see cref="Add(Key,KeyBindingScope,Terminal.Gui.Command[])"/> for
  88. /// <see cref="KeyBindingScope.Focused"/> scoped commands.
  89. /// </para>
  90. /// <para>
  91. /// If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
  92. /// <paramref name="commands"/>.
  93. /// </para>
  94. /// </summary>
  95. /// <remarks>
  96. /// Commands are only ever applied to the current <see cref="View"/> (i.e. this feature cannot be used to switch
  97. /// focus to another view and perform multiple commands there).
  98. /// </remarks>
  99. /// <param name="key">The key to check.</param>
  100. /// <param name="commands">
  101. /// The command to invoked on the <see cref="View"/> when <paramref name="key"/> is pressed. When
  102. /// multiple commands are provided,they will be applied in sequence. The bound <paramref name="key"/> strike will be
  103. /// consumed if any took effect.
  104. /// </param>
  105. public void Add (Key key, params Command [] commands)
  106. {
  107. Add (key, KeyBindingScope.Focused, commands);
  108. }
  109. /// <summary>Removes all <see cref="KeyBinding"/> objects from the collection.</summary>
  110. public void Clear ()
  111. {
  112. Application.RemoveAllKeyBindings (BoundView);
  113. Bindings.Clear ();
  114. }
  115. /// <summary>
  116. /// Removes all key bindings that trigger the given command set. Views can have multiple different keys bound to
  117. /// the same command sets and this method will clear all of them.
  118. /// </summary>
  119. /// <param name="command"></param>
  120. public void Clear (params Command [] command)
  121. {
  122. KeyValuePair<Key, KeyBinding> [] kvps = Bindings
  123. .Where (kvp => kvp.Value.Commands.SequenceEqual (command))
  124. .ToArray ();
  125. foreach (KeyValuePair<Key, KeyBinding> kvp in kvps)
  126. {
  127. Remove (kvp.Key);
  128. }
  129. }
  130. /// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
  131. /// <param name="key"></param>
  132. /// <returns></returns>
  133. public KeyBinding Get (Key key)
  134. {
  135. if (TryGet (key, out KeyBinding binding))
  136. {
  137. return binding;
  138. }
  139. throw new InvalidOperationException ($"Key {key} is not bound.");
  140. }
  141. /// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
  142. /// <param name="key"></param>
  143. /// <param name="scope"></param>
  144. /// <returns></returns>
  145. public KeyBinding Get (Key key, KeyBindingScope scope)
  146. {
  147. if (TryGet (key, scope, out KeyBinding binding))
  148. {
  149. return binding;
  150. }
  151. throw new InvalidOperationException ($"Key {key}/{scope} is not bound.");
  152. }
  153. /// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="key"/> if it exists.</summary>
  154. /// <param name="key">The key to check.</param>
  155. /// <returns>
  156. /// The array of <see cref="Command"/>s if <paramref name="key"/> is bound. An empty <see cref="Command"/> array
  157. /// if not.
  158. /// </returns>
  159. public Command [] GetCommands (Key key)
  160. {
  161. if (TryGet (key, out KeyBinding bindings))
  162. {
  163. return bindings.Commands;
  164. }
  165. return Array.Empty<Command> ();
  166. }
  167. /// <summary>Gets the Key used by a set of commands.</summary>
  168. /// <remarks></remarks>
  169. /// <param name="commands">The set of commands to search.</param>
  170. /// <returns>The <see cref="Key"/> used by a <see cref="Command"/></returns>
  171. /// <exception cref="InvalidOperationException">If no matching set of commands was found.</exception>
  172. public Key GetKeyFromCommands (params Command [] commands) { return Bindings.First (a => a.Value.Commands.SequenceEqual (commands)).Key; }
  173. /// <summary>Removes a <see cref="KeyBinding"/> from the collection.</summary>
  174. /// <param name="key"></param>
  175. public void Remove (Key key)
  176. {
  177. Bindings.Remove (key);
  178. Application.RemoveKeyBinding (key, BoundView);
  179. }
  180. /// <summary>Replaces a key combination already bound to a set of <see cref="Command"/>s.</summary>
  181. /// <remarks></remarks>
  182. /// <param name="oldKey">The key to be replaced.</param>
  183. /// <param name="newKey">The new key to be used.</param>
  184. public void Replace (Key oldKey, Key newKey)
  185. {
  186. if (!TryGet (oldKey, out KeyBinding _))
  187. {
  188. return;
  189. }
  190. KeyBinding value = Bindings [oldKey];
  191. Remove (oldKey);
  192. Add (newKey, value);
  193. }
  194. /// <summary>Gets the commands bound with the specified Key.</summary>
  195. /// <remarks></remarks>
  196. /// <param name="key">The key to check.</param>
  197. /// <param name="binding">
  198. /// When this method returns, contains the commands bound with the specified Key, if the Key is
  199. /// found; otherwise, null. This parameter is passed uninitialized.
  200. /// </param>
  201. /// <returns><see langword="true"/> if the Key is bound; otherwise <see langword="false"/>.</returns>
  202. public bool TryGet (Key key, out KeyBinding binding)
  203. {
  204. if (key.IsValid)
  205. {
  206. return Bindings.TryGetValue (key, out binding);
  207. }
  208. binding = new (Array.Empty<Command> (), KeyBindingScope.Focused);
  209. return false;
  210. }
  211. /// <summary>Gets the commands bound with the specified Key that are scoped to a particular scope.</summary>
  212. /// <remarks></remarks>
  213. /// <param name="key">The key to check.</param>
  214. /// <param name="scope">the scope to filter on</param>
  215. /// <param name="binding">
  216. /// When this method returns, contains the commands bound with the specified Key, if the Key is
  217. /// found; otherwise, null. This parameter is passed uninitialized.
  218. /// </param>
  219. /// <returns><see langword="true"/> if the Key is bound; otherwise <see langword="false"/>.</returns>
  220. public bool TryGet (Key key, KeyBindingScope scope, out KeyBinding binding)
  221. {
  222. if (key.IsValid && Bindings.TryGetValue (key, out binding))
  223. {
  224. if (scope.HasFlag (binding.Scope))
  225. {
  226. return true;
  227. }
  228. }
  229. binding = new (Array.Empty<Command> (), KeyBindingScope.Focused);
  230. return false;
  231. }
  232. }