ApplicationMouse.cs 9.1 KB

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