Application.Mouse.cs 9.2 KB

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