MouseGrabHandler.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #nullable disable
  2. #nullable enable
  3. namespace Terminal.Gui.App;
  4. /// <summary>
  5. /// INTERNAL: Implements <see cref="IMouseGrabHandler"/> to manage which <see cref="View"/> (if any) has 'grabbed' the mouse,
  6. /// giving it exclusive priority for mouse events such as movement, button presses, and release.
  7. /// <para>
  8. /// Used for scenarios like dragging, scrolling, or any interaction where a view needs to receive all mouse events
  9. /// until the operation completes (e.g., a scrollbar thumb being dragged).
  10. /// </para>
  11. /// <para>
  12. /// See <see cref="IMouseGrabHandler"/> for usage details.
  13. /// </para>
  14. /// </summary>
  15. internal class MouseGrabHandler : IMouseGrabHandler
  16. {
  17. /// <inheritdoc/>
  18. public View? MouseGrabView { get; private set; }
  19. /// <inheritdoc/>
  20. public event EventHandler<GrabMouseEventArgs>? GrabbingMouse;
  21. /// <inheritdoc/>
  22. public event EventHandler<GrabMouseEventArgs>? UnGrabbingMouse;
  23. /// <inheritdoc/>
  24. public event EventHandler<ViewEventArgs>? GrabbedMouse;
  25. /// <inheritdoc/>
  26. public event EventHandler<ViewEventArgs>? UnGrabbedMouse;
  27. /// <inheritdoc/>
  28. public void GrabMouse (View? view)
  29. {
  30. if (view is null || RaiseGrabbingMouseEvent (view))
  31. {
  32. return;
  33. }
  34. RaiseGrabbedMouseEvent (view);
  35. // MouseGrabView is a static; only set if the application is initialized.
  36. MouseGrabView = view;
  37. }
  38. /// <inheritdoc/>
  39. public void UngrabMouse ()
  40. {
  41. if (MouseGrabView is null)
  42. {
  43. return;
  44. }
  45. #if DEBUG_IDISPOSABLE
  46. if (View.EnableDebugIDisposableAsserts)
  47. {
  48. ObjectDisposedException.ThrowIf (MouseGrabView.WasDisposed, MouseGrabView);
  49. }
  50. #endif
  51. if (!RaiseUnGrabbingMouseEvent (MouseGrabView))
  52. {
  53. View view = MouseGrabView;
  54. MouseGrabView = null;
  55. RaiseUnGrabbedMouseEvent (view);
  56. }
  57. }
  58. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  59. private bool RaiseGrabbingMouseEvent (View? view)
  60. {
  61. if (view is null)
  62. {
  63. return false;
  64. }
  65. var evArgs = new GrabMouseEventArgs (view);
  66. GrabbingMouse?.Invoke (view, evArgs);
  67. return evArgs.Cancel;
  68. }
  69. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  70. private bool RaiseUnGrabbingMouseEvent (View? view)
  71. {
  72. if (view is null)
  73. {
  74. return false;
  75. }
  76. var evArgs = new GrabMouseEventArgs (view);
  77. UnGrabbingMouse?.Invoke (view, evArgs);
  78. return evArgs.Cancel;
  79. }
  80. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  81. private void RaiseGrabbedMouseEvent (View? view)
  82. {
  83. if (view is null)
  84. {
  85. return;
  86. }
  87. GrabbedMouse?.Invoke (view, new (view));
  88. }
  89. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  90. private void RaiseUnGrabbedMouseEvent (View? view)
  91. {
  92. if (view is null)
  93. {
  94. return;
  95. }
  96. UnGrabbedMouse?.Invoke (view, new (view));
  97. }
  98. /// <inheritdoc/>
  99. public bool HandleMouseGrab (View? deepestViewUnderMouse, MouseEventArgs mouseEvent)
  100. {
  101. if (MouseGrabView is { })
  102. {
  103. #if DEBUG_IDISPOSABLE
  104. if (View.EnableDebugIDisposableAsserts && MouseGrabView.WasDisposed)
  105. {
  106. throw new ObjectDisposedException (MouseGrabView.GetType ().FullName);
  107. }
  108. #endif
  109. // If the mouse is grabbed, send the event to the view that grabbed it.
  110. // The coordinates are relative to the Bounds of the view that grabbed the mouse.
  111. Point frameLoc = MouseGrabView.ScreenToViewport (mouseEvent.ScreenPosition);
  112. var viewRelativeMouseEvent = new MouseEventArgs
  113. {
  114. Position = frameLoc,
  115. Flags = mouseEvent.Flags,
  116. ScreenPosition = mouseEvent.ScreenPosition,
  117. View = deepestViewUnderMouse ?? MouseGrabView
  118. };
  119. //System.Diagnostics.Debug.WriteLine ($"{nme.Flags};{nme.X};{nme.Y};{mouseGrabView}");
  120. if (MouseGrabView?.NewMouseEvent (viewRelativeMouseEvent) is true)
  121. {
  122. return true;
  123. }
  124. // ReSharper disable once ConditionIsAlwaysTrueOrFalse
  125. if (MouseGrabView is null && deepestViewUnderMouse is Adornment)
  126. {
  127. // The view that grabbed the mouse has been disposed
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. }