Application.Mouse.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. public static partial class Application // Mouse handling
  4. {
  5. #region Mouse handling
  6. /// <summary>Disable or enable the mouse. The mouse is enabled by default.</summary>
  7. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
  8. public static bool IsMouseDisabled { get; set; }
  9. /// <summary>The current <see cref="View"/> object that wants continuous mouse button pressed events.</summary>
  10. public static View? WantContinuousButtonPressedView { get; private set; }
  11. /// <summary>
  12. /// Gets the view that grabbed the mouse (e.g. for dragging). When this is set, all mouse events will be routed to
  13. /// this view until the view calls <see cref="UngrabMouse"/> or the mouse is released.
  14. /// </summary>
  15. public static View? MouseGrabView { get; private set; }
  16. /// <summary>Invoked when a view wants to grab the mouse; can be canceled.</summary>
  17. public static event EventHandler<GrabMouseEventArgs>? GrabbingMouse;
  18. /// <summary>Invoked when a view wants un-grab the mouse; can be canceled.</summary>
  19. public static event EventHandler<GrabMouseEventArgs>? UnGrabbingMouse;
  20. /// <summary>Invoked after a view has grabbed the mouse.</summary>
  21. public static event EventHandler<ViewEventArgs>? GrabbedMouse;
  22. /// <summary>Invoked after a view has un-grabbed the mouse.</summary>
  23. public static event EventHandler<ViewEventArgs>? UnGrabbedMouse;
  24. /// <summary>
  25. /// Grabs the mouse, forcing all mouse events to be routed to the specified view until <see cref="UngrabMouse"/>
  26. /// is called.
  27. /// </summary>
  28. /// <param name="view">View that will receive all mouse events until <see cref="UngrabMouse"/> is invoked.</param>
  29. public static void GrabMouse (View? view)
  30. {
  31. if (view is null || OnGrabbingMouse (view))
  32. {
  33. return;
  34. }
  35. OnGrabbedMouse (view);
  36. MouseGrabView = view;
  37. }
  38. /// <summary>Releases the mouse grab, so mouse events will be routed to the view on which the mouse is.</summary>
  39. public static void UngrabMouse ()
  40. {
  41. if (MouseGrabView is null)
  42. {
  43. return;
  44. }
  45. #if DEBUG_IDISPOSABLE
  46. ObjectDisposedException.ThrowIf (MouseGrabView.WasDisposed, MouseGrabView);
  47. #endif
  48. if (!OnUnGrabbingMouse (MouseGrabView))
  49. {
  50. View view = MouseGrabView;
  51. MouseGrabView = null;
  52. OnUnGrabbedMouse (view);
  53. }
  54. }
  55. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  56. private static bool OnGrabbingMouse (View? view)
  57. {
  58. if (view is null)
  59. {
  60. return false;
  61. }
  62. var evArgs = new GrabMouseEventArgs (view);
  63. GrabbingMouse?.Invoke (view, evArgs);
  64. return evArgs.Cancel;
  65. }
  66. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  67. private static bool OnUnGrabbingMouse (View? view)
  68. {
  69. if (view is null)
  70. {
  71. return false;
  72. }
  73. var evArgs = new GrabMouseEventArgs (view);
  74. UnGrabbingMouse?.Invoke (view, evArgs);
  75. return evArgs.Cancel;
  76. }
  77. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  78. private static void OnGrabbedMouse (View? view)
  79. {
  80. if (view is null)
  81. {
  82. return;
  83. }
  84. GrabbedMouse?.Invoke (view, new (view));
  85. }
  86. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  87. private static void OnUnGrabbedMouse (View? view)
  88. {
  89. if (view is null)
  90. {
  91. return;
  92. }
  93. UnGrabbedMouse?.Invoke (view, new (view));
  94. }
  95. // Used by OnMouseEvent to track the last view that was clicked on.
  96. internal static View? MouseEnteredView { get; set; }
  97. /// <summary>Event fired when a mouse move or click occurs. Coordinates are screen relative.</summary>
  98. /// <remarks>
  99. /// <para>
  100. /// Use this event to receive mouse events in screen coordinates. Use <see cref="MouseEvent"/> to
  101. /// receive mouse events relative to a <see cref="View.Viewport"/>.
  102. /// </para>
  103. /// <para>The <see cref="MouseEvent.View"/> will contain the <see cref="View"/> that contains the mouse coordinates.</para>
  104. /// </remarks>
  105. public static event EventHandler<MouseEvent>? MouseEvent;
  106. /// <summary>Called when a mouse event occurs. Raises the <see cref="MouseEvent"/> event.</summary>
  107. /// <remarks>This method can be used to simulate a mouse event, e.g. in unit tests.</remarks>
  108. /// <param name="mouseEvent">The mouse event with coordinates relative to the screen.</param>
  109. internal static void OnMouseEvent (MouseEvent mouseEvent)
  110. {
  111. if (IsMouseDisabled)
  112. {
  113. return;
  114. }
  115. var view = View.FindDeepestView (Current, mouseEvent.Position);
  116. if (view is { })
  117. {
  118. mouseEvent.View = view;
  119. }
  120. MouseEvent?.Invoke (null, mouseEvent);
  121. if (mouseEvent.Handled)
  122. {
  123. return;
  124. }
  125. if (MouseGrabView is { })
  126. {
  127. // If the mouse is grabbed, send the event to the view that grabbed it.
  128. // The coordinates are relative to the Bounds of the view that grabbed the mouse.
  129. Point frameLoc = MouseGrabView.ScreenToViewport (mouseEvent.Position);
  130. var viewRelativeMouseEvent = new MouseEvent
  131. {
  132. Position = frameLoc,
  133. Flags = mouseEvent.Flags,
  134. ScreenPosition = mouseEvent.Position,
  135. View = view ?? MouseGrabView
  136. };
  137. if ((MouseGrabView.Viewport with { Location = Point.Empty }).Contains (viewRelativeMouseEvent.Position) is false)
  138. {
  139. // The mouse has moved outside the bounds of the view that grabbed the mouse
  140. MouseGrabView.NewMouseLeaveEvent (mouseEvent);
  141. }
  142. //System.Diagnostics.Debug.WriteLine ($"{nme.Flags};{nme.X};{nme.Y};{mouseGrabView}");
  143. if (MouseGrabView.NewMouseEvent (viewRelativeMouseEvent) is true)
  144. {
  145. return;
  146. }
  147. }
  148. // We can combine this into the switch expression to reduce cognitive complexity even more and likely
  149. // avoid one or two of these checks in the process, as well.
  150. WantContinuousButtonPressedView = view switch
  151. {
  152. { WantContinuousButtonPressed: true } => view,
  153. _ => null
  154. };
  155. if (view is not Adornment
  156. && (view is null || view == ApplicationOverlapped.OverlappedTop)
  157. && Current is { Modal: false }
  158. && ApplicationOverlapped.OverlappedTop != null
  159. && mouseEvent.Flags is not MouseFlags.ReportMousePosition and not 0)
  160. {
  161. // This occurs when there are multiple overlapped "tops"
  162. // E.g. "Mdi" - in the Background Worker Scenario
  163. View? top = ApplicationOverlapped.FindDeepestTop (Top!, mouseEvent.Position);
  164. view = View.FindDeepestView (top, mouseEvent.Position);
  165. if (view is { } && view != ApplicationOverlapped.OverlappedTop && top != Current && top is { })
  166. {
  167. ApplicationOverlapped.MoveCurrent ((Toplevel)top);
  168. }
  169. }
  170. // May be null before the prior condition or the condition may set it as null.
  171. // So, the checking must be outside the prior condition.
  172. if (view is null)
  173. {
  174. return;
  175. }
  176. MouseEvent? me;
  177. if (view is Adornment adornment)
  178. {
  179. Point frameLoc = adornment.ScreenToFrame (mouseEvent.Position);
  180. me = new ()
  181. {
  182. Position = frameLoc,
  183. Flags = mouseEvent.Flags,
  184. ScreenPosition = mouseEvent.Position,
  185. View = view
  186. };
  187. }
  188. else if (view.ViewportToScreen (Rectangle.Empty with { Size = view.Viewport.Size }).Contains (mouseEvent.Position))
  189. {
  190. Point viewportLocation = view.ScreenToViewport (mouseEvent.Position);
  191. me = new ()
  192. {
  193. Position = viewportLocation,
  194. Flags = mouseEvent.Flags,
  195. ScreenPosition = mouseEvent.Position,
  196. View = view
  197. };
  198. }
  199. else
  200. {
  201. return;
  202. }
  203. if (MouseEnteredView is null)
  204. {
  205. MouseEnteredView = view;
  206. view.NewMouseEnterEvent (me);
  207. }
  208. else if (MouseEnteredView != view)
  209. {
  210. MouseEnteredView.NewMouseLeaveEvent (me);
  211. view.NewMouseEnterEvent (me);
  212. MouseEnteredView = view;
  213. }
  214. if (!view.WantMousePositionReports && mouseEvent.Flags == MouseFlags.ReportMousePosition)
  215. {
  216. return;
  217. }
  218. WantContinuousButtonPressedView = view.WantContinuousButtonPressed ? view : null;
  219. //Debug.WriteLine ($"OnMouseEvent: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags}");
  220. while (view.NewMouseEvent (me) is not true && MouseGrabView is not { })
  221. {
  222. if (view is Adornment adornmentView)
  223. {
  224. view = adornmentView.Parent!.SuperView;
  225. }
  226. else
  227. {
  228. view = view.SuperView;
  229. }
  230. if (view is null)
  231. {
  232. break;
  233. }
  234. Point boundsPoint = view.ScreenToViewport (mouseEvent.Position);
  235. me = new ()
  236. {
  237. Position = boundsPoint,
  238. Flags = mouseEvent.Flags,
  239. ScreenPosition = mouseEvent.Position,
  240. View = view
  241. };
  242. }
  243. ApplicationOverlapped.BringOverlappedTopToFront ();
  244. }
  245. #endregion Mouse handling
  246. }