IMouseGrabHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #nullable enable
  2. namespace Terminal.Gui.App;
  3. /// <summary>
  4. /// Defines a contract for tracking which <see cref="View"/> (if any) has 'grabbed' the mouse,
  5. /// giving it exclusive priority for mouse events such as movement, button presses, and release.
  6. /// <para>
  7. /// This is typically used for scenarios like dragging, scrolling, or any interaction where a view
  8. /// needs to receive all mouse events until the operation completes (e.g., a scrollbar thumb being dragged).
  9. /// </para>
  10. /// <para>
  11. /// Usage pattern:
  12. /// <list type="number">
  13. /// <item>
  14. /// <description>Call <see cref="GrabMouse"/> to route all mouse events to a specific view.</description>
  15. /// </item>
  16. /// <item>
  17. /// <description>Call <see cref="UngrabMouse"/> to release the grab and restore normal mouse routing.</description>
  18. /// </item>
  19. /// <item>
  20. /// <description>
  21. /// Listen to <see cref="GrabbingMouse"/>, <see cref="GrabbedMouse"/>, <see cref="UnGrabbingMouse"/>,
  22. /// and <see cref="UnGrabbedMouse"/> for grab lifecycle events.
  23. /// </description>
  24. /// </item>
  25. /// </list>
  26. /// </para>
  27. /// </summary>
  28. public interface IMouseGrabHandler
  29. {
  30. /// <summary>
  31. /// Occurs after a view has grabbed the mouse.
  32. /// <para>
  33. /// This event is raised after the mouse grab operation is complete and the specified view will receive all mouse
  34. /// events.
  35. /// </para>
  36. /// </summary>
  37. public event EventHandler<ViewEventArgs>? GrabbedMouse;
  38. /// <summary>
  39. /// Occurs when a view requests to grab the mouse; can be canceled.
  40. /// <para>
  41. /// Handlers can set <c>e.Cancel</c> to <see langword="true"/> to prevent the grab.
  42. /// </para>
  43. /// </summary>
  44. public event EventHandler<GrabMouseEventArgs>? GrabbingMouse;
  45. /// <summary>
  46. /// Grabs the mouse, forcing all mouse events to be routed to the specified view until <see cref="UngrabMouse"/> is
  47. /// called.
  48. /// </summary>
  49. /// <param name="view">
  50. /// The <see cref="View"/> that will receive all mouse events until <see cref="UngrabMouse"/> is invoked.
  51. /// If <see langword="null"/>, the grab is released.
  52. /// </param>
  53. public void GrabMouse (View? view);
  54. /// <summary>
  55. /// Gets the view that currently has grabbed the mouse (e.g., for dragging).
  56. /// <para>
  57. /// When this property is not <see langword="null"/>, all mouse events are routed to this view until
  58. /// <see cref="UngrabMouse"/> is called or the mouse is released.
  59. /// </para>
  60. /// </summary>
  61. public View? MouseGrabView { get; }
  62. /// <summary>
  63. /// Occurs after a view has released the mouse grab.
  64. /// <para>
  65. /// This event is raised after the mouse grab has been released and normal mouse routing resumes.
  66. /// </para>
  67. /// </summary>
  68. public event EventHandler<ViewEventArgs>? UnGrabbedMouse;
  69. /// <summary>
  70. /// Occurs when a view requests to release the mouse grab; can be canceled.
  71. /// <para>
  72. /// Handlers can set <c>e.Cancel</c> to <see langword="true"/> to prevent the ungrab.
  73. /// </para>
  74. /// </summary>
  75. public event EventHandler<GrabMouseEventArgs>? UnGrabbingMouse;
  76. /// <summary>
  77. /// Releases the mouse grab, so mouse events will be routed to the view under the mouse pointer.
  78. /// </summary>
  79. public void UngrabMouse ();
  80. }