View.Mouse.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui;
  4. public partial class View // Mouse APIs
  5. {
  6. private ColorScheme? _savedHighlightColorScheme;
  7. /// <summary>
  8. /// Fired when the view is highlighted. Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/>
  9. /// to implement a custom highlight scheme or prevent the view from being highlighted.
  10. /// </summary>
  11. public event EventHandler<CancelEventArgs<HighlightStyle>>? Highlight;
  12. /// <summary>
  13. /// Gets or sets whether the <see cref="View"/> will be highlighted visually while the mouse button is
  14. /// pressed.
  15. /// </summary>
  16. public HighlightStyle HighlightStyle { get; set; }
  17. /// <summary>Event fired when a mouse click occurs.</summary>
  18. /// <remarks>
  19. /// <para>
  20. /// Fired when the mouse is either clicked or double-clicked. Check
  21. /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
  22. /// </para>
  23. /// <para>
  24. /// The coordinates are relative to <see cref="View.Viewport"/>.
  25. /// </para>
  26. /// </remarks>
  27. public event EventHandler<MouseEventEventArgs>? MouseClick;
  28. /// <summary>Event fired when a mouse event occurs.</summary>
  29. /// <remarks>
  30. /// <para>
  31. /// The coordinates are relative to <see cref="View.Viewport"/>.
  32. /// </para>
  33. /// </remarks>
  34. public event EventHandler<MouseEventEventArgs>? MouseEvent;
  35. /// <summary>
  36. /// Processes a <see cref="MouseEvent"/>. This method is called by <see cref="Application.OnMouseEvent"/> when a mouse
  37. /// event occurs.
  38. /// </summary>
  39. /// <remarks>
  40. /// <para>
  41. /// A view must be both enabled and visible to receive mouse events.
  42. /// </para>
  43. /// <para>
  44. /// This method calls <see cref="OnMouseEvent"/> to process the event. If the event is not handled, and one of the
  45. /// mouse buttons was clicked, it calls <see cref="OnMouseClick"/> to process the click.
  46. /// </para>
  47. /// <para>
  48. /// See <see cref="SetHighlight"/> for more information.
  49. /// </para>
  50. /// <para>
  51. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, the <see cref="OnMouseClick"/> event
  52. /// will be invoked repeatedly while the button is pressed.
  53. /// </para>
  54. /// </remarks>
  55. /// <param name="mouseEvent"></param>
  56. /// <returns><see langword="true"/> if the event was handled, <see langword="false"/> otherwise.</returns>
  57. public bool? NewMouseEvent (MouseEvent mouseEvent)
  58. {
  59. if (!Enabled)
  60. {
  61. // A disabled view should not eat mouse events
  62. return false;
  63. }
  64. if (!CanBeVisible (this))
  65. {
  66. return false;
  67. }
  68. if (!WantMousePositionReports && mouseEvent.Flags == MouseFlags.ReportMousePosition)
  69. {
  70. return false;
  71. }
  72. if (OnMouseEvent (mouseEvent))
  73. {
  74. // Technically mouseEvent.Handled should already be true if implementers of OnMouseEvent
  75. // follow the rules. But we'll update it just in case.
  76. return mouseEvent.Handled = true;
  77. }
  78. if (HighlightStyle != HighlightStyle.None || (WantContinuousButtonPressed && WantMousePositionReports))
  79. {
  80. if (HandlePressed (mouseEvent))
  81. {
  82. return mouseEvent.Handled;
  83. }
  84. if (HandleReleased (mouseEvent))
  85. {
  86. return mouseEvent.Handled;
  87. }
  88. if (HandleClicked (mouseEvent))
  89. {
  90. return mouseEvent.Handled;
  91. }
  92. }
  93. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
  94. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
  95. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
  96. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)
  97. || mouseEvent.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  98. || mouseEvent.Flags.HasFlag (MouseFlags.Button2DoubleClicked)
  99. || mouseEvent.Flags.HasFlag (MouseFlags.Button3DoubleClicked)
  100. || mouseEvent.Flags.HasFlag (MouseFlags.Button4DoubleClicked)
  101. || mouseEvent.Flags.HasFlag (MouseFlags.Button1TripleClicked)
  102. || mouseEvent.Flags.HasFlag (MouseFlags.Button2TripleClicked)
  103. || mouseEvent.Flags.HasFlag (MouseFlags.Button3TripleClicked)
  104. || mouseEvent.Flags.HasFlag (MouseFlags.Button4TripleClicked)
  105. )
  106. {
  107. // If it's a click, and we didn't handle it, then we'll call OnMouseClick
  108. // We get here if the view did not handle the mouse event via OnMouseEvent/MouseEvent and
  109. // it did not handle the press/release/clicked events via HandlePress/HandleRelease/HandleClicked
  110. return OnMouseClick (new (mouseEvent));
  111. }
  112. return false;
  113. }
  114. /// <summary>Gets or sets whether the <see cref="View"/> wants continuous button pressed events.</summary>
  115. public virtual bool WantContinuousButtonPressed { get; set; }
  116. /// <summary>Gets or sets whether the <see cref="View"/> wants mouse position reports.</summary>
  117. /// <value><see langword="true"/> if mouse position reports are wanted; otherwise, <see langword="false"/>.</value>
  118. public virtual bool WantMousePositionReports { get; set; }
  119. #region MouseEnterLeave
  120. /// <summary>
  121. /// INTERNAL Called by <see cref="Application.OnMouseEvent"/> when the mouse moves over the View's <see cref="Frame"/>.
  122. /// <see cref="MouseLeave"/> will
  123. /// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
  124. /// that View will also receive MouseEnter/Leave events.
  125. /// </summary>
  126. /// <param name="eventArgs"></param>
  127. /// <returns>
  128. /// <see langword="true"/> if the event was canceled, <see langword="false"/> if not, <see langword="null"/> if the
  129. /// view is not visible. Cancelling the event
  130. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  131. /// </returns>
  132. internal bool? NewMouseEnterEvent (CancelEventArgs eventArgs)
  133. {
  134. if (!CanBeVisible (this))
  135. {
  136. return null;
  137. }
  138. if (OnMouseEnter (eventArgs))
  139. {
  140. return true;
  141. }
  142. MouseEnter?.Invoke (this, eventArgs);
  143. #if HOVER
  144. if (HighlightStyle.HasFlag(HighlightStyle.Hover))
  145. {
  146. if (SetHighlight (HighlightStyle.Hover))
  147. {
  148. return true;
  149. }
  150. }
  151. #endif
  152. return eventArgs.Cancel;
  153. }
  154. /// <summary>
  155. /// Called when the mouse moves over the View's <see cref="Frame"/> and no other non-Subview occludes it. <see cref="MouseLeave"/> will
  156. /// be raised when the mouse is no longer over the <see cref="Frame"/>.
  157. /// </summary>
  158. /// <remarks>
  159. /// <para>
  160. /// A view must be visible to receive Enter events (Leave events are always received).
  161. /// </para>
  162. /// <para>
  163. /// If the event is cancelled, the mouse event will not be propagated to other views and <see cref="MouseEnter"/>
  164. /// will not be raised.
  165. /// </para>
  166. /// <para>
  167. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  168. /// </para>
  169. /// <para>
  170. /// See <see cref="SetHighlight"/> for more information.
  171. /// </para>
  172. /// </remarks>
  173. /// <param name="eventArgs"></param>
  174. /// <returns>
  175. /// <see langword="true"/> if the event was canceled, <see langword="false"/> if not. Cancelling the event
  176. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  177. /// </returns>
  178. protected virtual bool OnMouseEnter (CancelEventArgs eventArgs) { return false; }
  179. /// <summary>
  180. /// Raised when the mouse moves over the View's <see cref="Frame"/>. <see cref="MouseLeave"/> will
  181. /// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
  182. /// that View will also receive MouseEnter/Leave events.
  183. /// </summary>
  184. /// <remarks>
  185. /// <para>
  186. /// A view must be visible to receive Enter events (Leave events are always received).
  187. /// </para>
  188. /// <para>
  189. /// If the event is cancelled, the mouse event will not be propagated to other views.
  190. /// </para>
  191. /// <para>
  192. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  193. /// </para>
  194. /// <para>
  195. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> if the event was canceled,
  196. /// <see langword="false"/> if not. Cancelling the event
  197. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  198. /// </para>
  199. /// <para>
  200. /// See <see cref="SetHighlight"/> for more information.
  201. /// </para>
  202. /// </remarks>
  203. public event EventHandler<CancelEventArgs>? MouseEnter;
  204. /// <summary>
  205. /// INTERNAL Called by <see cref="Application.OnMouseEvent"/> when the mouse leaves <see cref="Frame"/>, or is occluded by another non-SubView.
  206. /// </summary>
  207. /// <remarks>
  208. /// <para>
  209. /// This method calls <see cref="OnMouseLeave"/> and raises the <see cref="MouseLeave"/> event.
  210. /// </para>
  211. /// <para>
  212. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  213. /// </para>
  214. /// <para>
  215. /// See <see cref="SetHighlight"/> for more information.
  216. /// </para>
  217. /// </remarks>
  218. internal void NewMouseLeaveEvent ()
  219. {
  220. OnMouseLeave ();
  221. MouseLeave?.Invoke (this, EventArgs.Empty);
  222. #if HOVER
  223. if (HighlightStyle.HasFlag (HighlightStyle.Hover))
  224. {
  225. SetHighlight (HighlightStyle.None);
  226. }
  227. #endif
  228. }
  229. /// <summary>
  230. /// Called when the mouse moves outside View's <see cref="Frame"/>, or is occluded by another non-SubView.
  231. /// </summary>
  232. /// <remarks>
  233. /// <para>
  234. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  235. /// </para>
  236. /// <para>
  237. /// See <see cref="SetHighlight"/> for more information.
  238. /// </para>
  239. /// </remarks>
  240. protected virtual void OnMouseLeave () { }
  241. /// <summary>
  242. /// Raised when the mouse moves outside View's <see cref="Frame"/>, or is occluded by another non-SubView.
  243. /// </summary>
  244. /// <remarks>
  245. /// <para>
  246. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  247. /// </para>
  248. /// <para>
  249. /// See <see cref="SetHighlight"/> for more information.
  250. /// </para>
  251. /// </remarks>
  252. public event EventHandler? MouseLeave;
  253. #endregion MouseEnterLeave
  254. /// <summary>Called when a mouse event occurs within the view's <see cref="Viewport"/>.</summary>
  255. /// <remarks>
  256. /// <para>
  257. /// The coordinates are relative to <see cref="View.Viewport"/>.
  258. /// </para>
  259. /// </remarks>
  260. /// <param name="mouseEvent"></param>
  261. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  262. protected internal virtual bool OnMouseEvent (MouseEvent mouseEvent)
  263. {
  264. var args = new MouseEventEventArgs (mouseEvent);
  265. MouseEvent?.Invoke (this, args);
  266. return args.Handled;
  267. }
  268. /// <summary>
  269. /// Called when the view is to be highlighted.
  270. /// </summary>
  271. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  272. protected virtual bool? OnHighlight (CancelEventArgs<HighlightStyle> args)
  273. {
  274. Highlight?.Invoke (this, args);
  275. if (args.Cancel)
  276. {
  277. return true;
  278. }
  279. Margin?.Highlight?.Invoke (this, args);
  280. //args = new (highlight);
  281. //Border?.Highlight?.Invoke (this, args);
  282. //args = new (highlight);
  283. //Padding?.Highlight?.Invoke (this, args);
  284. return args.Cancel;
  285. }
  286. /// <summary>Invokes the MouseClick event.</summary>
  287. /// <remarks>
  288. /// <para>
  289. /// Called when the mouse is either clicked or double-clicked. Check
  290. /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
  291. /// </para>
  292. /// </remarks>
  293. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  294. protected bool OnMouseClick (MouseEventEventArgs args)
  295. {
  296. if (!Enabled)
  297. {
  298. // QUESTION: Is this right? Should a disabled view eat mouse clicks?
  299. return args.Handled = false;
  300. }
  301. MouseClick?.Invoke (this, args);
  302. if (args.Handled)
  303. {
  304. return true;
  305. }
  306. if (!HasFocus && CanFocus)
  307. {
  308. args.Handled = true;
  309. SetFocus ();
  310. }
  311. return args.Handled;
  312. }
  313. /// <summary>
  314. /// For cases where the view is grabbed and the mouse is clicked, this method handles the click event (typically
  315. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  316. /// </summary>
  317. /// <remarks>
  318. /// Marked internal just to support unit tests
  319. /// </remarks>
  320. /// <param name="mouseEvent"></param>
  321. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  322. internal bool HandleClicked (MouseEvent mouseEvent)
  323. {
  324. if (Application.MouseGrabView == this
  325. && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
  326. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
  327. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
  328. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)))
  329. {
  330. // We're grabbed. Clicked event comes after the last Release. This is our signal to ungrab
  331. Application.UngrabMouse ();
  332. if (SetHighlight (HighlightStyle.None))
  333. {
  334. return true;
  335. }
  336. // If mouse is still in bounds, click
  337. if (!WantContinuousButtonPressed && Viewport.Contains (mouseEvent.Position))
  338. {
  339. return OnMouseClick (new (mouseEvent));
  340. }
  341. return mouseEvent.Handled = true;
  342. }
  343. return false;
  344. }
  345. /// <summary>
  346. /// For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
  347. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  348. /// </summary>
  349. /// <remarks>
  350. /// Marked internal just to support unit tests
  351. /// </remarks>
  352. /// <param name="mouseEvent"></param>
  353. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  354. internal bool HandleReleased (MouseEvent mouseEvent)
  355. {
  356. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released)
  357. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Released)
  358. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Released)
  359. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Released))
  360. {
  361. if (Application.MouseGrabView == this)
  362. {
  363. SetHighlight (HighlightStyle.None);
  364. }
  365. return mouseEvent.Handled = true;
  366. }
  367. return false;
  368. }
  369. /// <summary>
  370. /// Enables the highlight for the view when the mouse is pressed. Called from OnMouseEvent.
  371. /// </summary>
  372. /// <remarks>
  373. /// <para>
  374. /// Set <see cref="HighlightStyle"/> to have the view highlighted based on the mouse.
  375. /// </para>
  376. /// <para>
  377. /// Calls <see cref="OnHighlight"/> which fires the <see cref="Highlight"/> event.
  378. /// </para>
  379. /// <para>
  380. /// Marked internal just to support unit tests
  381. /// </para>
  382. /// </remarks>
  383. /// <returns><see langword="true"/>, if the Highlight event was handled, <see langword="false"/> otherwise.</returns>
  384. internal bool SetHighlight (HighlightStyle newHighlightStyle)
  385. {
  386. // TODO: Make the highlight colors configurable
  387. if (!CanFocus)
  388. {
  389. return false;
  390. }
  391. // Enable override via virtual method and/or event
  392. HighlightStyle copy = HighlightStyle;
  393. CancelEventArgs<HighlightStyle> args = new (ref copy, ref newHighlightStyle);
  394. if (OnHighlight (args) == true)
  395. {
  396. return true;
  397. }
  398. #if HOVER
  399. if (style.HasFlag (HighlightStyle.Hover))
  400. {
  401. if (_savedHighlightColorScheme is null && ColorScheme is { })
  402. {
  403. _savedHighlightColorScheme ??= ColorScheme;
  404. var cs = new ColorScheme (ColorScheme)
  405. {
  406. Normal = GetFocusColor (),
  407. HotNormal = ColorScheme.HotFocus
  408. };
  409. ColorScheme = cs;
  410. }
  411. return true;
  412. }
  413. #endif
  414. if (args.NewValue.HasFlag (HighlightStyle.Pressed) || args.NewValue.HasFlag (HighlightStyle.PressedOutside))
  415. {
  416. if (_savedHighlightColorScheme is null && ColorScheme is { })
  417. {
  418. _savedHighlightColorScheme ??= ColorScheme;
  419. if (CanFocus)
  420. {
  421. var cs = new ColorScheme (ColorScheme)
  422. {
  423. // Highlight the foreground focus color
  424. Focus = new (ColorScheme.Focus.Foreground.GetHighlightColor (), ColorScheme.Focus.Background.GetHighlightColor ())
  425. };
  426. ColorScheme = cs;
  427. }
  428. else
  429. {
  430. var cs = new ColorScheme (ColorScheme)
  431. {
  432. // Invert Focus color foreground/background. We can do this because we know the view is not going to be focused.
  433. Normal = new (ColorScheme.Focus.Background, ColorScheme.Normal.Foreground)
  434. };
  435. ColorScheme = cs;
  436. }
  437. }
  438. // Return false since we don't want to eat the event
  439. return false;
  440. }
  441. if (args.NewValue == HighlightStyle.None)
  442. {
  443. // Unhighlight
  444. if (_savedHighlightColorScheme is { })
  445. {
  446. ColorScheme = _savedHighlightColorScheme;
  447. _savedHighlightColorScheme = null;
  448. }
  449. }
  450. return false;
  451. }
  452. /// <summary>
  453. /// For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
  454. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  455. /// </summary>
  456. /// <remarks>
  457. /// <para>
  458. /// Marked internal just to support unit tests
  459. /// </para>
  460. /// </remarks>
  461. /// <param name="mouseEvent"></param>
  462. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  463. private bool HandlePressed (MouseEvent mouseEvent)
  464. {
  465. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)
  466. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed)
  467. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed)
  468. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Pressed))
  469. {
  470. // The first time we get pressed event, grab the mouse and set focus
  471. if (Application.MouseGrabView != this)
  472. {
  473. Application.GrabMouse (this);
  474. if (!HasFocus && CanFocus)
  475. {
  476. // Set the focus, but don't invoke Accept
  477. SetFocus ();
  478. }
  479. mouseEvent.Handled = true;
  480. }
  481. if (Viewport.Contains (mouseEvent.Position))
  482. {
  483. if (this is not Adornment
  484. && SetHighlight (HighlightStyle.HasFlag (HighlightStyle.Pressed) ? HighlightStyle.Pressed : HighlightStyle.None))
  485. {
  486. return true;
  487. }
  488. }
  489. else
  490. {
  491. if (this is not Adornment
  492. && SetHighlight (HighlightStyle.HasFlag (HighlightStyle.PressedOutside) ? HighlightStyle.PressedOutside : HighlightStyle.None))
  493. {
  494. return true;
  495. }
  496. }
  497. if (WantContinuousButtonPressed && Application.MouseGrabView == this)
  498. {
  499. // If this is not the first pressed event, click
  500. return OnMouseClick (new (mouseEvent));
  501. }
  502. return mouseEvent.Handled = true;
  503. }
  504. return false;
  505. }
  506. /// <summary>
  507. /// INTERNAL: Gets the Views that are under the mouse at <paramref name="location"/>, including Adornments.
  508. /// </summary>
  509. /// <param name="location"></param>
  510. /// <returns></returns>
  511. internal static List<View?> GetViewsUnderMouse (in Point location)
  512. {
  513. List<View?> viewsUnderMouse = new ();
  514. View? start = Application.Current ?? Application.Top;
  515. Point currentLocation = location;
  516. while (start is { Visible: true } && start.Contains (currentLocation))
  517. {
  518. viewsUnderMouse.Add (start);
  519. Adornment? found = null;
  520. if (start.Margin.Contains (currentLocation))
  521. {
  522. found = start.Margin;
  523. }
  524. else if (start.Border.Contains (currentLocation))
  525. {
  526. found = start.Border;
  527. }
  528. else if (start.Padding.Contains (currentLocation))
  529. {
  530. found = start.Padding;
  531. }
  532. Point viewportOffset = start.GetViewportOffsetFromFrame ();
  533. if (found is { })
  534. {
  535. start = found;
  536. viewsUnderMouse.Add (start);
  537. viewportOffset = found.Parent?.Frame.Location ?? Point.Empty;
  538. }
  539. int startOffsetX = currentLocation.X - (start.Frame.X + viewportOffset.X);
  540. int startOffsetY = currentLocation.Y - (start.Frame.Y + viewportOffset.Y);
  541. View? subview = null;
  542. for (int i = start.InternalSubviews.Count - 1; i >= 0; i--)
  543. {
  544. if (start.InternalSubviews [i].Visible
  545. && start.InternalSubviews [i].Contains (new (startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y)))
  546. {
  547. subview = start.InternalSubviews [i];
  548. currentLocation.X = startOffsetX + start.Viewport.X;
  549. currentLocation.Y = startOffsetY + start.Viewport.Y;
  550. // start is the deepest subview under the mouse; stop searching the subviews
  551. break;
  552. }
  553. }
  554. if (subview is null)
  555. {
  556. // No subview was found that's under the mouse, so we're done
  557. return viewsUnderMouse;
  558. }
  559. // We found a subview of start that's under the mouse, continue...
  560. start = subview;
  561. }
  562. return viewsUnderMouse;
  563. }
  564. }