KeyBindings.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using static System.Formats.Asn1.AsnWriter;
  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.HasFlag (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.HasFlag (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) { return TryGet (key, out KeyBinding binding) ? binding : null; }
  134. /// <summary>Gets the <see cref="KeyBinding"/> for the specified <see cref="Key"/>.</summary>
  135. /// <param name="key"></param>
  136. /// <param name="scope"></param>
  137. /// <returns></returns>
  138. public KeyBinding Get (Key key, KeyBindingScope scope) { return TryGet (key, scope, out KeyBinding binding) ? binding : null; }
  139. /// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="key"/> if it exists.</summary>
  140. /// <param name="key">The key to check.</param>
  141. /// <returns>
  142. /// The array of <see cref="Command"/>s if <paramref name="key"/> is bound. An empty <see cref="Command"/> array
  143. /// if not.
  144. /// </returns>
  145. public Command [] GetCommands (Key key)
  146. {
  147. if (TryGet (key, out KeyBinding bindings))
  148. {
  149. return bindings.Commands;
  150. }
  151. return Array.Empty<Command> ();
  152. }
  153. /// <summary>Gets the Key used by a set of commands.</summary>
  154. /// <remarks></remarks>
  155. /// <param name="commands">The set of commands to search.</param>
  156. /// <returns>The <see cref="Key"/> used by a <see cref="Command"/></returns>
  157. /// <exception cref="InvalidOperationException">If no matching set of commands was found.</exception>
  158. public Key GetKeyFromCommands (params Command [] commands) { return Bindings.First (a => a.Value.Commands.SequenceEqual (commands)).Key; }
  159. /// <summary>Removes a <see cref="KeyBinding"/> from the collection.</summary>
  160. /// <param name="key"></param>
  161. public void Remove (Key key)
  162. {
  163. Bindings.Remove (key);
  164. Application.RemoveKeyBinding (key, BoundView);
  165. }
  166. /// <summary>Replaces a key combination already bound to a set of <see cref="Command"/>s.</summary>
  167. /// <remarks></remarks>
  168. /// <param name="oldKey">The key to be replaced.</param>
  169. /// <param name="newKey">The new key to be used.</param>
  170. public void Replace (Key oldKey, Key newKey)
  171. {
  172. if (!TryGet (oldKey, out KeyBinding _))
  173. {
  174. return;
  175. }
  176. KeyBinding value = Bindings [oldKey];
  177. Remove (oldKey);
  178. Add (newKey, value);
  179. }
  180. /// <summary>Gets the commands bound with the specified Key.</summary>
  181. /// <remarks></remarks>
  182. /// <param name="key">The key to check.</param>
  183. /// <param name="binding">
  184. /// When this method returns, contains the commands bound with the specified Key, if the Key is
  185. /// found; otherwise, null. This parameter is passed uninitialized.
  186. /// </param>
  187. /// <returns><see langword="true"/> if the Key is bound; otherwise <see langword="false"/>.</returns>
  188. public bool TryGet (Key key, out KeyBinding binding)
  189. {
  190. if (key.IsValid)
  191. {
  192. return Bindings.TryGetValue (key, out binding);
  193. }
  194. binding = new (Array.Empty<Command> (), KeyBindingScope.Focused);
  195. return false;
  196. }
  197. /// <summary>Gets the commands bound with the specified Key that are scoped to a particular scope.</summary>
  198. /// <remarks></remarks>
  199. /// <param name="key">The key to check.</param>
  200. /// <param name="scope">the scope to filter on</param>
  201. /// <param name="binding">
  202. /// When this method returns, contains the commands bound with the specified Key, if the Key is
  203. /// found; otherwise, null. This parameter is passed uninitialized.
  204. /// </param>
  205. /// <returns><see langword="true"/> if the Key is bound; otherwise <see langword="false"/>.</returns>
  206. public bool TryGet (Key key, KeyBindingScope scope, out KeyBinding binding)
  207. {
  208. if (key.IsValid && Bindings.TryGetValue (key, out binding))
  209. {
  210. if (scope.HasFlag (binding.Scope))
  211. {
  212. return true;
  213. }
  214. }
  215. binding = new (Array.Empty<Command> (), KeyBindingScope.Focused);
  216. return false;
  217. }
  218. }