MouseBindings.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Provides a collection of <see cref="MouseBinding"/> objects bound to a combination of <see cref="MouseFlags"/>.
  5. /// </summary>
  6. /// <seealso cref="View.MouseBindings"/>
  7. /// <seealso cref="Command"/>
  8. public class MouseBindings : Bindings<MouseFlags, MouseBinding>
  9. {
  10. /// <summary>
  11. /// Initializes a new instance.
  12. /// </summary>
  13. public MouseBindings () : base (
  14. (commands, flags) => new (commands, flags),
  15. EqualityComparer<MouseFlags>.Default)
  16. { }
  17. /// <summary>
  18. /// Gets combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  19. /// <paramref name="commands"/>.
  20. /// </summary>
  21. /// <param name="commands">The set of commands to search.</param>
  22. /// <returns>
  23. /// The combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  24. /// <paramref name="commands"/>. An empty list if the set of caommands was not found.
  25. /// </returns>
  26. public IEnumerable<MouseFlags> GetAllMouseFlagsFromCommands (params Command [] commands)
  27. {
  28. return _bindings.Where (a => a.Value.Commands.SequenceEqual (commands)).Select (a => a.Key);
  29. }
  30. /// <summary>
  31. /// Gets the <see cref="MouseFlags"/> that are bound.
  32. /// </summary>
  33. /// <returns></returns>
  34. public IEnumerable<MouseFlags> GetBoundMouseFlags () { return _bindings.Keys; }
  35. /// <summary>Gets the array of <see cref="Command"/>s bound to <paramref name="mouseFlags"/> if it exists.</summary>
  36. /// <param name="mouseFlags">The key to check.</param>
  37. /// <returns>
  38. /// The array of <see cref="Command"/>s if <paramref name="mouseFlags"/> is bound. An empty <see cref="Command"/>
  39. /// array
  40. /// if not.
  41. /// </returns>
  42. public Command [] GetCommands (MouseFlags mouseFlags)
  43. {
  44. if (TryGet (mouseFlags, out MouseBinding bindings))
  45. {
  46. return bindings.Commands;
  47. }
  48. return [];
  49. }
  50. /// <summary>
  51. /// Gets the first combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  52. /// <paramref name="commands"/>.
  53. /// </summary>
  54. /// <param name="commands">The set of commands to search.</param>
  55. /// <returns>
  56. /// The first combination of <see cref="MouseFlags"/> bound to the set of commands specified by
  57. /// <paramref name="commands"/>. <see langword="null"/> if the set of caommands was not found.
  58. /// </returns>
  59. public MouseFlags GetMouseFlagsFromCommands (params Command [] commands)
  60. {
  61. return _bindings.FirstOrDefault (a => a.Value.Commands.SequenceEqual (commands)).Key;
  62. }
  63. /// <summary>Replaces the commands already bound to a combination of <see cref="MouseFlags"/>.</summary>
  64. /// <remarks>
  65. /// <para>
  66. /// If the combination of <see cref="MouseFlags"/> is not already bound, it will be added.
  67. /// </para>
  68. /// </remarks>
  69. /// <param name="mouseEventArgs">The combination of <see cref="MouseFlags"/> bound to the command to be replaced.</param>
  70. /// <param name="newCommands">The set of commands to replace the old ones with.</param>
  71. public void ReplaceCommands (MouseFlags mouseEventArgs, params Command [] newCommands)
  72. {
  73. if (TryGet (mouseEventArgs, out MouseBinding binding))
  74. {
  75. Remove (mouseEventArgs);
  76. Add (mouseEventArgs, newCommands);
  77. }
  78. else
  79. {
  80. Add (mouseEventArgs, newCommands);
  81. }
  82. }
  83. /// <summary>Replaces a <see cref="MouseFlags"/> combination already bound to a set of <see cref="Command"/>s.</summary>
  84. /// <remarks></remarks>
  85. /// <param name="oldMouseFlags">The <see cref="MouseFlags"/> to be replaced.</param>
  86. /// <param name="newMouseFlags">
  87. /// The new <see cref="MouseFlags"/> to be used. If <see cref="Key.Empty"/> no action
  88. /// will be taken.
  89. /// </param>
  90. public void ReplaceMouseFlag (MouseFlags oldMouseFlags, MouseFlags newMouseFlags)
  91. {
  92. if (newMouseFlags == MouseFlags.None)
  93. {
  94. throw new ArgumentException (@"Invalid MouseFlag", nameof (newMouseFlags));
  95. }
  96. if (TryGet (oldMouseFlags, out MouseBinding binding))
  97. {
  98. Remove (oldMouseFlags);
  99. Add (newMouseFlags, binding);
  100. }
  101. else
  102. {
  103. Add (newMouseFlags, binding);
  104. }
  105. }
  106. /// <summary>Gets the commands bound with the specified <see cref="MouseFlags"/>.</summary>
  107. /// <remarks></remarks>
  108. /// <param name="mouseEventArgs">The key to check.</param>
  109. /// <param name="binding">
  110. /// When this method returns, contains the commands bound with the specified mouse flags, if the mouse flags are
  111. /// found; otherwise, null. This parameter is passed uninitialized.
  112. /// </param>
  113. /// <returns><see langword="true"/> if the mouse flags are bound; otherwise <see langword="false"/>.</returns>
  114. public bool TryGet (MouseFlags mouseEventArgs, out MouseBinding binding)
  115. {
  116. binding = new ([], mouseEventArgs);
  117. return _bindings.TryGetValue (mouseEventArgs, out binding);
  118. }
  119. }