Application.Mouse.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #nullable enable
  2. <<<<<<< Updated upstream
  3. =======
  4. using Microsoft.CodeAnalysis;
  5. using Microsoft.CodeAnalysis.VisualBasic.Syntax;
  6. >>>>>>> Stashed changes
  7. namespace Terminal.Gui;
  8. public static partial class Application // Mouse handling
  9. {
  10. #region Mouse handling
  11. /// <summary>Disable or enable the mouse. The mouse is enabled by default.</summary>
  12. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
  13. public static bool IsMouseDisabled { get; set; }
  14. /// <summary>The current <see cref="View"/> object that wants continuous mouse button pressed events.</summary>
  15. public static View? WantContinuousButtonPressedView { get; private set; }
  16. /// <summary>
  17. /// Gets the view that grabbed the mouse (e.g. for dragging). When this is set, all mouse events will be routed to
  18. /// this view until the view calls <see cref="UngrabMouse"/> or the mouse is released.
  19. /// </summary>
  20. public static View? MouseGrabView { get; private set; }
  21. /// <summary>Invoked when a view wants to grab the mouse; can be canceled.</summary>
  22. public static event EventHandler<GrabMouseEventArgs>? GrabbingMouse;
  23. /// <summary>Invoked when a view wants un-grab the mouse; can be canceled.</summary>
  24. public static event EventHandler<GrabMouseEventArgs>? UnGrabbingMouse;
  25. /// <summary>Invoked after a view has grabbed the mouse.</summary>
  26. public static event EventHandler<ViewEventArgs>? GrabbedMouse;
  27. /// <summary>Invoked after a view has un-grabbed the mouse.</summary>
  28. public static event EventHandler<ViewEventArgs>? UnGrabbedMouse;
  29. /// <summary>
  30. /// Grabs the mouse, forcing all mouse events to be routed to the specified view until <see cref="UngrabMouse"/>
  31. /// is called.
  32. /// </summary>
  33. /// <param name="view">View that will receive all mouse events until <see cref="UngrabMouse"/> is invoked.</param>
  34. public static void GrabMouse (View? view)
  35. {
  36. if (view is null || OnGrabbingMouse (view))
  37. {
  38. return;
  39. }
  40. OnGrabbedMouse (view);
  41. MouseGrabView = view;
  42. }
  43. /// <summary>Releases the mouse grab, so mouse events will be routed to the view on which the mouse is.</summary>
  44. public static void UngrabMouse ()
  45. {
  46. if (MouseGrabView is null)
  47. {
  48. return;
  49. }
  50. #if DEBUG_IDISPOSABLE
  51. ObjectDisposedException.ThrowIf (MouseGrabView.WasDisposed, MouseGrabView);
  52. #endif
  53. if (!OnUnGrabbingMouse (MouseGrabView))
  54. {
  55. View view = MouseGrabView;
  56. MouseGrabView = null;
  57. OnUnGrabbedMouse (view);
  58. }
  59. }
  60. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  61. private static bool OnGrabbingMouse (View? view)
  62. {
  63. if (view is null)
  64. {
  65. return false;
  66. }
  67. var evArgs = new GrabMouseEventArgs (view);
  68. GrabbingMouse?.Invoke (view, evArgs);
  69. return evArgs.Cancel;
  70. }
  71. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  72. private static bool OnUnGrabbingMouse (View? view)
  73. {
  74. if (view is null)
  75. {
  76. return false;
  77. }
  78. var evArgs = new GrabMouseEventArgs (view);
  79. UnGrabbingMouse?.Invoke (view, evArgs);
  80. return evArgs.Cancel;
  81. }
  82. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  83. private static void OnGrabbedMouse (View? view)
  84. {
  85. if (view is null)
  86. {
  87. return;
  88. }
  89. GrabbedMouse?.Invoke (view, new (view));
  90. }
  91. /// <exception cref="Exception">A delegate callback throws an exception.</exception>
  92. private static void OnUnGrabbedMouse (View? view)
  93. {
  94. if (view is null)
  95. {
  96. return;
  97. }
  98. UnGrabbedMouse?.Invoke (view, new (view));
  99. }
  100. // Used by OnMouseEvent to suppport MouseEnter and MouseLeave events
  101. internal static List<View?> ViewsUnderMouse { get; } = new ();
  102. /// <summary>Event fired when a mouse move or click occurs. Coordinates are screen relative.</summary>
  103. /// <remarks>
  104. /// <para>
  105. /// Use this event to receive mouse events in screen coordinates. Use <see cref="MouseEvent"/> to
  106. /// receive mouse events relative to a <see cref="View.Viewport"/>.
  107. /// </para>
  108. /// <para>The <see cref="MouseEvent.View"/> will contain the <see cref="View"/> that contains the mouse coordinates.</para>
  109. /// </remarks>
  110. public static event EventHandler<MouseEvent>? MouseEvent;
  111. /// <summary>Called when a mouse event occurs. Raises the <see cref="MouseEvent"/> event.</summary>
  112. /// <remarks>This method can be used to simulate a mouse event, e.g. in unit tests.</remarks>
  113. /// <param name="mouseEvent">The mouse event with coordinates relative to the screen.</param>
  114. internal static void OnMouseEvent (MouseEvent mouseEvent)
  115. {
  116. if (IsMouseDisabled)
  117. {
  118. return;
  119. }
  120. <<<<<<< Updated upstream
  121. var view = View.FindDeepestView (Current, mouseEvent.Position);
  122. =======
  123. Stack<View?> viewsUnderMouse = View.GetViewsUnderMouse (mouseEvent.Position);
  124. View? deepestViewUnderMouse = viewsUnderMouse.TryPeek (out View? result) ? result : null;
  125. if ((mouseEvent.Flags == MouseFlags.Button1Pressed
  126. || mouseEvent.Flags == MouseFlags.Button2Pressed
  127. || mouseEvent.Flags == MouseFlags.Button3Pressed
  128. || mouseEvent.Flags == MouseFlags.Button4Pressed)
  129. && Popover is { Visible: true } && !ApplicationNavigation.IsInHierarchy (Popover, deepestViewUnderMouse, includeAdornments: true))
  130. {
  131. Popover.Visible = false;
  132. }
  133. >>>>>>> Stashed changes
  134. if (deepestViewUnderMouse is { })
  135. {
  136. #if DEBUG_IDISPOSABLE
  137. if (deepestViewUnderMouse.WasDisposed)
  138. {
  139. throw new ObjectDisposedException (deepestViewUnderMouse.GetType ().FullName);
  140. }
  141. #endif
  142. mouseEvent.View = deepestViewUnderMouse;
  143. }
  144. MouseEvent?.Invoke (null, mouseEvent);
  145. if (mouseEvent.Handled)
  146. {
  147. return;
  148. }
  149. if (MouseGrabView is { })
  150. {
  151. #if DEBUG_IDISPOSABLE
  152. if (MouseGrabView.WasDisposed)
  153. {
  154. throw new ObjectDisposedException (MouseGrabView.GetType ().FullName);
  155. }
  156. #endif
  157. // If the mouse is grabbed, send the event to the view that grabbed it.
  158. // The coordinates are relative to the Bounds of the view that grabbed the mouse.
  159. Point frameLoc = MouseGrabView.ScreenToViewport (mouseEvent.Position);
  160. var viewRelativeMouseEvent = new MouseEvent
  161. {
  162. Position = frameLoc,
  163. Flags = mouseEvent.Flags,
  164. ScreenPosition = mouseEvent.Position,
  165. View = deepestViewUnderMouse ?? MouseGrabView
  166. };
  167. if ((MouseGrabView.Viewport with { Location = Point.Empty }).Contains (viewRelativeMouseEvent.Position) is false)
  168. {
  169. // The mouse has moved outside the bounds of the view that grabbed the mouse
  170. MouseGrabView.NewMouseLeaveEvent (mouseEvent);
  171. }
  172. //System.Diagnostics.Debug.WriteLine ($"{nme.Flags};{nme.X};{nme.Y};{mouseGrabView}");
  173. if (MouseGrabView?.NewMouseEvent (viewRelativeMouseEvent) is true)
  174. {
  175. return;
  176. }
  177. // ReSharper disable once ConditionIsAlwaysTrueOrFalse
  178. if (MouseGrabView is null && deepestViewUnderMouse is Adornment)
  179. {
  180. // The view that grabbed the mouse has been disposed
  181. return;
  182. }
  183. }
  184. // We can combine this into the switch expression to reduce cognitive complexity even more and likely
  185. // avoid one or two of these checks in the process, as well.
  186. <<<<<<< Updated upstream
  187. WantContinuousButtonPressedView = view switch
  188. {
  189. { WantContinuousButtonPressed: true } => view,
  190. _ => null
  191. };
  192. if (view is not Adornment
  193. && (view is null || view == ApplicationOverlapped.OverlappedTop)
  194. && Current is { Modal: false }
  195. && ApplicationOverlapped.OverlappedTop != null
  196. && mouseEvent.Flags is not MouseFlags.ReportMousePosition and not 0)
  197. {
  198. // This occurs when there are multiple overlapped "tops"
  199. // E.g. "Mdi" - in the Background Worker Scenario
  200. View? top = ApplicationOverlapped.FindDeepestTop (Top!, mouseEvent.Position);
  201. view = View.FindDeepestView (top, mouseEvent.Position);
  202. if (view is { } && view != ApplicationOverlapped.OverlappedTop && top != Current && top is { })
  203. {
  204. ApplicationOverlapped.MoveCurrent ((Toplevel)top);
  205. }
  206. }
  207. =======
  208. WantContinuousButtonPressedView = deepestViewUnderMouse switch
  209. {
  210. { WantContinuousButtonPressed: true } => deepestViewUnderMouse,
  211. _ => null
  212. };
  213. >>>>>>> Stashed changes
  214. // May be null before the prior condition or the condition may set it as null.
  215. // So, the checking must be outside the prior condition.
  216. if (deepestViewUnderMouse is null)
  217. {
  218. return;
  219. }
  220. MouseEvent? me;
  221. if (deepestViewUnderMouse is Adornment adornment)
  222. {
  223. Point frameLoc = adornment.ScreenToFrame (mouseEvent.Position);
  224. me = new ()
  225. {
  226. Position = frameLoc,
  227. Flags = mouseEvent.Flags,
  228. ScreenPosition = mouseEvent.Position,
  229. View = deepestViewUnderMouse
  230. };
  231. }
  232. else if (deepestViewUnderMouse.ViewportToScreen (Rectangle.Empty with { Size = deepestViewUnderMouse.Viewport.Size }).Contains (mouseEvent.Position))
  233. {
  234. Point viewportLocation = deepestViewUnderMouse.ScreenToViewport (mouseEvent.Position);
  235. me = new ()
  236. {
  237. Position = viewportLocation,
  238. Flags = mouseEvent.Flags,
  239. ScreenPosition = mouseEvent.Position,
  240. View = deepestViewUnderMouse
  241. };
  242. }
  243. else
  244. {
  245. return;
  246. }
  247. // Mouse Enter/Leave events
  248. // Tell any views that are no longer under the mouse that the mouse has left and remove them from list
  249. List<View?> viewsToLeave = ViewsUnderMouse.Where (v => v is { } && !viewsUnderMouse.Contains (v)).ToList ();
  250. foreach (View? view in viewsToLeave)
  251. {
  252. if (view is null)
  253. {
  254. continue;
  255. }
  256. if (view is Adornment adornmentView)
  257. {
  258. Point frameLoc = adornmentView.ScreenToFrame (mouseEvent.Position);
  259. if (adornmentView.Parent is { } && !adornmentView.Contains (frameLoc))
  260. {
  261. ViewsUnderMouse.Remove (view);
  262. view.NewMouseLeaveEvent (me);
  263. }
  264. }
  265. else
  266. {
  267. Point viewportLocation = view.ScreenToViewport (mouseEvent.Position);
  268. if (!view.Contains (viewportLocation))
  269. {
  270. ViewsUnderMouse.Remove (view);
  271. view.NewMouseLeaveEvent (me);
  272. }
  273. }
  274. }
  275. // Tell any views that are now under the mouse (viewsUnderMouse) that the mouse has entered and add them to the list
  276. foreach (View? view in viewsUnderMouse)
  277. {
  278. if (view is null)
  279. {
  280. continue;
  281. }
  282. Point viewportLocation = view.ScreenToViewport (mouseEvent.Position);
  283. if (view is Adornment adornmentView)
  284. {
  285. if (adornmentView.Parent is { } && !adornmentView.Contains (viewportLocation))
  286. {
  287. ViewsUnderMouse.Add (view);
  288. view.NewMouseEnterEvent (me);
  289. }
  290. }
  291. else if (view.Contains (viewportLocation))
  292. {
  293. ViewsUnderMouse.Add (view);
  294. view.NewMouseEnterEvent (me);
  295. }
  296. }
  297. WantContinuousButtonPressedView = deepestViewUnderMouse.WantContinuousButtonPressed ? deepestViewUnderMouse : null;
  298. //Debug.WriteLine ($"OnMouseEvent: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags}");
  299. if (deepestViewUnderMouse.Id == "mouseDemo")
  300. {
  301. }
  302. while (deepestViewUnderMouse.NewMouseEvent (me) is not true && MouseGrabView is not { })
  303. {
  304. if (deepestViewUnderMouse is Adornment adornmentView)
  305. {
  306. deepestViewUnderMouse = adornmentView.Parent!.SuperView;
  307. }
  308. else
  309. {
  310. deepestViewUnderMouse = deepestViewUnderMouse.SuperView;
  311. }
  312. if (deepestViewUnderMouse is null)
  313. {
  314. break;
  315. }
  316. Point boundsPoint = deepestViewUnderMouse.ScreenToViewport (mouseEvent.Position);
  317. me = new ()
  318. {
  319. Position = boundsPoint,
  320. Flags = mouseEvent.Flags,
  321. ScreenPosition = mouseEvent.Position,
  322. View = deepestViewUnderMouse
  323. };
  324. }
  325. ApplicationOverlapped.BringOverlappedTopToFront ();
  326. }
  327. #endregion Mouse handling
  328. }