KeyBindings.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Provides a collection of <see cref="Key"/>s bound to <see cref="Command"/>s.
  5. /// </summary>
  6. /// <seealso cref="Application.KeyBindings"/>
  7. /// <seealso cref="View.KeyBindings"/>
  8. /// <seealso cref="Command"/>
  9. public class KeyBindings : InputBindings<Key, KeyBinding>
  10. {
  11. /// <summary>Initializes a new instance bound to <paramref name="target"/>.</summary>
  12. public KeyBindings (View? target) : base ((commands, key) => new (commands), new KeyEqualityComparer ())
  13. {
  14. Target = target;
  15. }
  16. /// <inheritdoc />
  17. public override bool IsValid (Key eventArgs) { return eventArgs.IsValid; }
  18. /// <summary>
  19. /// <para>
  20. /// Adds a new key combination that will trigger the commands in <paramref name="commands"/> on the View
  21. /// specified by <paramref name="target"/>.
  22. /// </para>
  23. /// <para>
  24. /// If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
  25. /// <paramref name="commands"/>.
  26. /// </para>
  27. /// </summary>
  28. /// <remarks>
  29. /// </remarks>
  30. /// <param name="key">The key to check.</param>
  31. /// <param name="target">
  32. /// The View the commands will be invoked on. If <see langword="null"/>, the key will be bound to
  33. /// <see cref="Application"/>.
  34. /// </param>
  35. /// <param name="commands">
  36. /// The command to invoked on the <see paramref="target"/> when <paramref name="key"/> is pressed. When
  37. /// multiple commands are provided,they will be applied in sequence. The bound <paramref name="key"/> strike will be
  38. /// consumed if any took effect.
  39. /// </param>
  40. public void Add (Key key, View? target, params Command [] commands)
  41. {
  42. KeyBinding binding = new (commands, target);
  43. Add (key, binding);
  44. }
  45. /// <summary>
  46. /// The view that the <see cref="KeyBindings"/> are bound to.
  47. /// </summary>
  48. /// <remarks>
  49. /// If <see langword="null"/> the KeyBindings object is being used for Application.KeyBindings.
  50. /// </remarks>
  51. public View? Target { get; init; }
  52. }