Application.Mouse.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #if DEBUG_IDISPOSABLE
  119. if (view.WasDisposed)
  120. {
  121. throw new ObjectDisposedException (view.GetType ().FullName);
  122. }
  123. #endif
  124. mouseEvent.View = view;
  125. }
  126. MouseEvent?.Invoke (null, mouseEvent);
  127. if (mouseEvent.Handled)
  128. {
  129. return;
  130. }
  131. if (MouseGrabView is { })
  132. {
  133. #if DEBUG_IDISPOSABLE
  134. if (MouseGrabView.WasDisposed)
  135. {
  136. throw new ObjectDisposedException (MouseGrabView.GetType ().FullName);
  137. }
  138. #endif
  139. // If the mouse is grabbed, send the event to the view that grabbed it.
  140. // The coordinates are relative to the Bounds of the view that grabbed the mouse.
  141. Point frameLoc = MouseGrabView.ScreenToViewport (mouseEvent.Position);
  142. var viewRelativeMouseEvent = new MouseEvent
  143. {
  144. Position = frameLoc,
  145. Flags = mouseEvent.Flags,
  146. ScreenPosition = mouseEvent.Position,
  147. View = view ?? MouseGrabView
  148. };
  149. if ((MouseGrabView.Viewport with { Location = Point.Empty }).Contains (viewRelativeMouseEvent.Position) is false)
  150. {
  151. // The mouse has moved outside the bounds of the view that grabbed the mouse
  152. MouseGrabView.NewMouseLeaveEvent (mouseEvent);
  153. }
  154. //System.Diagnostics.Debug.WriteLine ($"{nme.Flags};{nme.X};{nme.Y};{mouseGrabView}");
  155. if (MouseGrabView.NewMouseEvent (viewRelativeMouseEvent) is true)
  156. {
  157. return;
  158. }
  159. }
  160. // We can combine this into the switch expression to reduce cognitive complexity even more and likely
  161. // avoid one or two of these checks in the process, as well.
  162. WantContinuousButtonPressedView = view switch
  163. {
  164. { WantContinuousButtonPressed: true } => view,
  165. _ => null
  166. };
  167. if (view is not Adornment
  168. && (view is null || view == ApplicationOverlapped.OverlappedTop)
  169. && Current is { Modal: false }
  170. && ApplicationOverlapped.OverlappedTop != null
  171. && mouseEvent.Flags is not MouseFlags.ReportMousePosition and not 0)
  172. {
  173. // This occurs when there are multiple overlapped "tops"
  174. // E.g. "Mdi" - in the Background Worker Scenario
  175. View? top = ApplicationOverlapped.FindDeepestTop (Top!, mouseEvent.Position);
  176. view = View.FindDeepestView (top, mouseEvent.Position);
  177. if (view is { } && view != ApplicationOverlapped.OverlappedTop && top != Current && top is { })
  178. {
  179. ApplicationOverlapped.MoveCurrent ((Toplevel)top);
  180. }
  181. }
  182. // May be null before the prior condition or the condition may set it as null.
  183. // So, the checking must be outside the prior condition.
  184. if (view is null)
  185. {
  186. return;
  187. }
  188. MouseEvent? me;
  189. if (view is Adornment adornment)
  190. {
  191. Point frameLoc = adornment.ScreenToFrame (mouseEvent.Position);
  192. me = new ()
  193. {
  194. Position = frameLoc,
  195. Flags = mouseEvent.Flags,
  196. ScreenPosition = mouseEvent.Position,
  197. View = view
  198. };
  199. }
  200. else if (view.ViewportToScreen (Rectangle.Empty with { Size = view.Viewport.Size }).Contains (mouseEvent.Position))
  201. {
  202. Point viewportLocation = view.ScreenToViewport (mouseEvent.Position);
  203. me = new ()
  204. {
  205. Position = viewportLocation,
  206. Flags = mouseEvent.Flags,
  207. ScreenPosition = mouseEvent.Position,
  208. View = view
  209. };
  210. }
  211. else
  212. {
  213. return;
  214. }
  215. if (MouseEnteredView is null)
  216. {
  217. MouseEnteredView = view;
  218. view.NewMouseEnterEvent (me);
  219. }
  220. else if (MouseEnteredView != view)
  221. {
  222. MouseEnteredView.NewMouseLeaveEvent (me);
  223. view.NewMouseEnterEvent (me);
  224. MouseEnteredView = view;
  225. }
  226. if (!view.WantMousePositionReports && mouseEvent.Flags == MouseFlags.ReportMousePosition)
  227. {
  228. return;
  229. }
  230. WantContinuousButtonPressedView = view.WantContinuousButtonPressed ? view : null;
  231. //Debug.WriteLine ($"OnMouseEvent: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags}");
  232. while (view.NewMouseEvent (me) is not true && MouseGrabView is not { })
  233. {
  234. if (view is Adornment adornmentView)
  235. {
  236. view = adornmentView.Parent!.SuperView;
  237. }
  238. else
  239. {
  240. view = view.SuperView;
  241. }
  242. if (view is null)
  243. {
  244. break;
  245. }
  246. Point boundsPoint = view.ScreenToViewport (mouseEvent.Position);
  247. me = new ()
  248. {
  249. Position = boundsPoint,
  250. Flags = mouseEvent.Flags,
  251. ScreenPosition = mouseEvent.Position,
  252. View = view
  253. };
  254. }
  255. ApplicationOverlapped.BringOverlappedTopToFront ();
  256. }
  257. #endregion Mouse handling
  258. }