Application.Mouse.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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)
  32. {
  33. return;
  34. }
  35. if (!OnGrabbingMouse (view))
  36. {
  37. OnGrabbedMouse (view);
  38. MouseGrabView = view;
  39. }
  40. }
  41. /// <summary>Releases the mouse grab, so mouse events will be routed to the view on which the mouse is.</summary>
  42. public static void UngrabMouse ()
  43. {
  44. if (MouseGrabView is null)
  45. {
  46. return;
  47. }
  48. if (!OnUnGrabbingMouse (MouseGrabView))
  49. {
  50. View view = MouseGrabView;
  51. MouseGrabView = null;
  52. OnUnGrabbedMouse (view);
  53. }
  54. }
  55. private static bool OnGrabbingMouse (View? view)
  56. {
  57. if (view is null)
  58. {
  59. return false;
  60. }
  61. var evArgs = new GrabMouseEventArgs (view);
  62. GrabbingMouse?.Invoke (view, evArgs);
  63. return evArgs.Cancel;
  64. }
  65. private static bool OnUnGrabbingMouse (View? view)
  66. {
  67. if (view is null)
  68. {
  69. return false;
  70. }
  71. var evArgs = new GrabMouseEventArgs (view);
  72. UnGrabbingMouse?.Invoke (view, evArgs);
  73. return evArgs.Cancel;
  74. }
  75. private static void OnGrabbedMouse (View? view)
  76. {
  77. if (view is null)
  78. {
  79. return;
  80. }
  81. GrabbedMouse?.Invoke (view, new (view));
  82. }
  83. private static void OnUnGrabbedMouse (View? view)
  84. {
  85. if (view is null)
  86. {
  87. return;
  88. }
  89. UnGrabbedMouse?.Invoke (view, new (view));
  90. }
  91. #nullable enable
  92. // Used by OnMouseEvent to track the last view that was clicked on.
  93. internal static View? MouseEnteredView { get; set; }
  94. /// <summary>Event fired when a mouse move or click occurs. Coordinates are screen relative.</summary>
  95. /// <remarks>
  96. /// <para>
  97. /// Use this event to receive mouse events in screen coordinates. Use <see cref="MouseEvent"/> to
  98. /// receive mouse events relative to a <see cref="View.Viewport"/>.
  99. /// </para>
  100. /// <para>The <see cref="MouseEvent.View"/> will contain the <see cref="View"/> that contains the mouse coordinates.</para>
  101. /// </remarks>
  102. public static event EventHandler<MouseEvent>? MouseEvent;
  103. /// <summary>Called when a mouse event occurs. Raises the <see cref="MouseEvent"/> event.</summary>
  104. /// <remarks>This method can be used to simulate a mouse event, e.g. in unit tests.</remarks>
  105. /// <param name="mouseEvent">The mouse event with coordinates relative to the screen.</param>
  106. internal static void OnMouseEvent (MouseEvent mouseEvent)
  107. {
  108. if (IsMouseDisabled)
  109. {
  110. return;
  111. }
  112. var view = View.FindDeepestView (Current, mouseEvent.Position);
  113. if (view is { })
  114. {
  115. #if DEBUG_IDISPOSABLE
  116. if (view.WasDisposed)
  117. {
  118. throw new ObjectDisposedException (view.GetType ().FullName);
  119. }
  120. #endif
  121. mouseEvent.View = view;
  122. }
  123. MouseEvent?.Invoke (null, mouseEvent);
  124. if (mouseEvent.Handled)
  125. {
  126. return;
  127. }
  128. if (MouseGrabView is { })
  129. {
  130. #if DEBUG_IDISPOSABLE
  131. if (MouseGrabView.WasDisposed)
  132. {
  133. throw new ObjectDisposedException (MouseGrabView.GetType ().FullName);
  134. }
  135. #endif
  136. // If the mouse is grabbed, send the event to the view that grabbed it.
  137. // The coordinates are relative to the Bounds of the view that grabbed the mouse.
  138. Point frameLoc = MouseGrabView.ScreenToViewport (mouseEvent.Position);
  139. var viewRelativeMouseEvent = new MouseEvent
  140. {
  141. Position = frameLoc,
  142. Flags = mouseEvent.Flags,
  143. ScreenPosition = mouseEvent.Position,
  144. View = view ?? MouseGrabView
  145. };
  146. if ((MouseGrabView.Viewport with { Location = Point.Empty }).Contains (viewRelativeMouseEvent.Position) is false)
  147. {
  148. // The mouse has moved outside the bounds of the view that grabbed the mouse
  149. MouseGrabView?.NewMouseLeaveEvent (mouseEvent);
  150. }
  151. //System.Diagnostics.Debug.WriteLine ($"{nme.Flags};{nme.X};{nme.Y};{mouseGrabView}");
  152. #if DEBUG_IDISPOSABLE
  153. if (MouseGrabView.WasDisposed)
  154. {
  155. throw new ObjectDisposedException (MouseGrabView.GetType ().FullName);
  156. }
  157. #endif
  158. if (MouseGrabView?.NewMouseEvent (viewRelativeMouseEvent) == true)
  159. {
  160. return;
  161. }
  162. }
  163. if (view is { WantContinuousButtonPressed: true })
  164. {
  165. WantContinuousButtonPressedView = view;
  166. }
  167. else
  168. {
  169. WantContinuousButtonPressedView = null;
  170. }
  171. if (view is not Adornment)
  172. {
  173. if ((view is null || view == ApplicationOverlapped.OverlappedTop)
  174. && Current is { Modal: false }
  175. && ApplicationOverlapped.OverlappedTop != null
  176. && mouseEvent.Flags != MouseFlags.ReportMousePosition
  177. && mouseEvent.Flags != 0)
  178. {
  179. // This occurs when there are multiple overlapped "tops"
  180. // E.g. "Mdi" - in the Background Worker Scenario
  181. View? top = ApplicationOverlapped.FindDeepestTop (Top!, mouseEvent.Position);
  182. view = View.FindDeepestView (top, mouseEvent.Position);
  183. if (view is { } && view != ApplicationOverlapped.OverlappedTop && top != Current && top is { })
  184. {
  185. ApplicationOverlapped.MoveCurrent ((Toplevel)top);
  186. }
  187. }
  188. }
  189. if (view is null)
  190. {
  191. return;
  192. }
  193. MouseEvent? me = null;
  194. if (view is Adornment adornment)
  195. {
  196. Point frameLoc = adornment.ScreenToFrame (mouseEvent.Position);
  197. me = new ()
  198. {
  199. Position = frameLoc,
  200. Flags = mouseEvent.Flags,
  201. ScreenPosition = mouseEvent.Position,
  202. View = view
  203. };
  204. }
  205. else if (view.ViewportToScreen (Rectangle.Empty with { Size = view.Viewport.Size }).Contains (mouseEvent.Position))
  206. {
  207. Point viewportLocation = view.ScreenToViewport (mouseEvent.Position);
  208. me = new ()
  209. {
  210. Position = viewportLocation,
  211. Flags = mouseEvent.Flags,
  212. ScreenPosition = mouseEvent.Position,
  213. View = view
  214. };
  215. }
  216. if (me is null)
  217. {
  218. return;
  219. }
  220. if (MouseEnteredView is null)
  221. {
  222. MouseEnteredView = view;
  223. view.NewMouseEnterEvent (me);
  224. }
  225. else if (MouseEnteredView != view)
  226. {
  227. MouseEnteredView.NewMouseLeaveEvent (me);
  228. view.NewMouseEnterEvent (me);
  229. MouseEnteredView = view;
  230. }
  231. if (!view.WantMousePositionReports && mouseEvent.Flags == MouseFlags.ReportMousePosition)
  232. {
  233. return;
  234. }
  235. WantContinuousButtonPressedView = view.WantContinuousButtonPressed ? view : null;
  236. //Debug.WriteLine ($"OnMouseEvent: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags}");
  237. while (view.NewMouseEvent (me) != true)
  238. {
  239. if (MouseGrabView is { })
  240. {
  241. break;
  242. }
  243. if (view is Adornment adornmentView)
  244. {
  245. view = adornmentView.Parent!.SuperView;
  246. }
  247. else
  248. {
  249. view = view.SuperView;
  250. }
  251. if (view is null)
  252. {
  253. break;
  254. }
  255. Point boundsPoint = view.ScreenToViewport (mouseEvent.Position);
  256. me = new ()
  257. {
  258. Position = boundsPoint,
  259. Flags = mouseEvent.Flags,
  260. ScreenPosition = mouseEvent.Position,
  261. View = view
  262. };
  263. }
  264. ApplicationOverlapped.BringOverlappedTopToFront ();
  265. }
  266. #endregion Mouse handling
  267. }