View.Mouse.cs 23 KB

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