View.Mouse.cs 24 KB

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