IMouseGrabHandler.cs 4.0 KB

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