View.Mouse.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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 _hovering;
  112. private ColorScheme? _savedNonHoverColorScheme;
  113. /// <summary>
  114. /// INTERNAL Called by <see cref="Application.OnMouseEvent"/> when the mouse moves over the View's <see cref="Frame"/>.
  115. /// <see cref="MouseLeave"/> will
  116. /// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
  117. /// that View will also receive MouseEnter/Leave events.
  118. /// </summary>
  119. /// <param name="eventArgs"></param>
  120. /// <returns>
  121. /// <see langword="true"/> if the event was canceled, <see langword="false"/> if not, <see langword="null"/> if the
  122. /// view is not visible. Cancelling the event
  123. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  124. /// </returns>
  125. internal bool? NewMouseEnterEvent (CancelEventArgs eventArgs)
  126. {
  127. if (!CanBeVisible (this))
  128. {
  129. return null;
  130. }
  131. if (OnMouseEnter (eventArgs))
  132. {
  133. return true;
  134. }
  135. MouseEnter?.Invoke (this, eventArgs);
  136. _hovering = !eventArgs.Cancel;
  137. if (eventArgs.Cancel)
  138. {
  139. return true;
  140. }
  141. if ((HighlightStyle.HasFlag (HighlightStyle.Hover) || Diagnostics.HasFlag (ViewDiagnosticFlags.Hover)))
  142. {
  143. HighlightStyle copy = HighlightStyle;
  144. HighlightStyle hover = HighlightStyle.Hover;
  145. CancelEventArgs<HighlightStyle> args = new (ref copy, ref hover);
  146. if (RaiseHighlight (args) || args.Cancel)
  147. {
  148. return args.Cancel;
  149. }
  150. ColorScheme cs = ColorScheme;
  151. if (cs is null)
  152. {
  153. cs = new ();
  154. }
  155. _savedNonHoverColorScheme = cs;
  156. ColorScheme = ColorScheme.GetHighlightColorScheme ();
  157. }
  158. return false;
  159. }
  160. /// <summary>
  161. /// Called when the mouse moves over the View's <see cref="Frame"/> and no other non-Subview occludes it. <see cref="MouseLeave"/> will
  162. /// be raised when the mouse is no longer over the <see cref="Frame"/>.
  163. /// </summary>
  164. /// <remarks>
  165. /// <para>
  166. /// A view must be visible to receive Enter events (Leave events are always received).
  167. /// </para>
  168. /// <para>
  169. /// If the event is cancelled, the mouse event will not be propagated to other views and <see cref="MouseEnter"/>
  170. /// will not be raised.
  171. /// </para>
  172. /// <para>
  173. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  174. /// </para>
  175. /// <para>
  176. /// See <see cref="SetHighlight"/> for more information.
  177. /// </para>
  178. /// </remarks>
  179. /// <param name="eventArgs"></param>
  180. /// <returns>
  181. /// <see langword="true"/> if the event was canceled, <see langword="false"/> if not. Cancelling the event
  182. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  183. /// </returns>
  184. protected virtual bool OnMouseEnter (CancelEventArgs eventArgs) { return false; }
  185. /// <summary>
  186. /// Raised when the mouse moves over the View's <see cref="Frame"/>. <see cref="MouseLeave"/> will
  187. /// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
  188. /// that View will also receive MouseEnter/Leave events.
  189. /// </summary>
  190. /// <remarks>
  191. /// <para>
  192. /// A view must be visible to receive Enter events (Leave events are always received).
  193. /// </para>
  194. /// <para>
  195. /// If the event is cancelled, the mouse event will not be propagated to other views.
  196. /// </para>
  197. /// <para>
  198. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  199. /// </para>
  200. /// <para>
  201. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> if the event was canceled,
  202. /// <see langword="false"/> if not. Cancelling the event
  203. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  204. /// </para>
  205. /// <para>
  206. /// See <see cref="SetHighlight"/> for more information.
  207. /// </para>
  208. /// </remarks>
  209. public event EventHandler<CancelEventArgs>? MouseEnter;
  210. /// <summary>
  211. /// INTERNAL Called by <see cref="Application.OnMouseEvent"/> when the mouse leaves <see cref="Frame"/>, or is occluded by another non-SubView.
  212. /// </summary>
  213. /// <remarks>
  214. /// <para>
  215. /// This method calls <see cref="OnMouseLeave"/> and raises the <see cref="MouseLeave"/> event.
  216. /// </para>
  217. /// <para>
  218. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  219. /// </para>
  220. /// <para>
  221. /// See <see cref="SetHighlight"/> for more information.
  222. /// </para>
  223. /// </remarks>
  224. internal void NewMouseLeaveEvent ()
  225. {
  226. OnMouseLeave ();
  227. MouseLeave?.Invoke (this, EventArgs.Empty);
  228. _hovering = false;
  229. if ((HighlightStyle.HasFlag (HighlightStyle.Hover) || Diagnostics.HasFlag (ViewDiagnosticFlags.Hover)))
  230. {
  231. HighlightStyle copy = HighlightStyle;
  232. HighlightStyle hover = HighlightStyle.None;
  233. RaiseHighlight (new (ref copy, ref hover));
  234. if (_savedNonHoverColorScheme is { })
  235. {
  236. ColorScheme = _savedNonHoverColorScheme;
  237. _savedNonHoverColorScheme = null;
  238. }
  239. }
  240. }
  241. /// <summary>
  242. /// Called 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. protected virtual void OnMouseLeave () { }
  253. /// <summary>
  254. /// Raised when the mouse moves outside View's <see cref="Frame"/>, or is occluded by another non-SubView.
  255. /// </summary>
  256. /// <remarks>
  257. /// <para>
  258. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  259. /// </para>
  260. /// <para>
  261. /// See <see cref="SetHighlight"/> for more information.
  262. /// </para>
  263. /// </remarks>
  264. public event EventHandler? MouseLeave;
  265. #endregion MouseEnterLeave
  266. /// <summary>Called when a mouse event occurs within the view's <see cref="Viewport"/>.</summary>
  267. /// <remarks>
  268. /// <para>
  269. /// The coordinates are relative to <see cref="View.Viewport"/>.
  270. /// </para>
  271. /// </remarks>
  272. /// <param name="mouseEvent"></param>
  273. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  274. protected internal virtual bool OnMouseEvent (MouseEvent mouseEvent)
  275. {
  276. var args = new MouseEventEventArgs (mouseEvent);
  277. MouseEvent?.Invoke (this, args);
  278. return args.Handled;
  279. }
  280. /// <summary>Invokes the MouseClick event.</summary>
  281. /// <remarks>
  282. /// <para>
  283. /// Called when the mouse is either clicked or double-clicked. Check
  284. /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
  285. /// </para>
  286. /// </remarks>
  287. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  288. protected bool OnMouseClick (MouseEventEventArgs args)
  289. {
  290. if (!Enabled)
  291. {
  292. // QUESTION: Is this right? Should a disabled view eat mouse clicks?
  293. return args.Handled = false;
  294. }
  295. MouseClick?.Invoke (this, args);
  296. if (args.Handled)
  297. {
  298. return true;
  299. }
  300. if (!HasFocus && CanFocus)
  301. {
  302. args.Handled = true;
  303. SetFocus ();
  304. }
  305. return args.Handled;
  306. }
  307. /// <summary>
  308. /// For cases where the view is grabbed and the mouse is clicked, this method handles the click event (typically
  309. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  310. /// </summary>
  311. /// <remarks>
  312. /// Marked internal just to support unit tests
  313. /// </remarks>
  314. /// <param name="mouseEvent"></param>
  315. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  316. internal bool HandleClicked (MouseEvent mouseEvent)
  317. {
  318. if (Application.MouseGrabView == this
  319. && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
  320. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
  321. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
  322. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)))
  323. {
  324. // We're grabbed. Clicked event comes after the last Release. This is our signal to ungrab
  325. Application.UngrabMouse ();
  326. if (SetHighlight (HighlightStyle.None))
  327. {
  328. return true;
  329. }
  330. // If mouse is still in bounds, click
  331. if (!WantContinuousButtonPressed && Viewport.Contains (mouseEvent.Position))
  332. {
  333. return OnMouseClick (new (mouseEvent));
  334. }
  335. return mouseEvent.Handled = true;
  336. }
  337. return false;
  338. }
  339. /// <summary>
  340. /// For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
  341. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  342. /// </summary>
  343. /// <remarks>
  344. /// Marked internal just to support unit tests
  345. /// </remarks>
  346. /// <param name="mouseEvent"></param>
  347. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  348. internal bool HandleReleased (MouseEvent mouseEvent)
  349. {
  350. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released)
  351. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Released)
  352. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Released)
  353. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Released))
  354. {
  355. if (Application.MouseGrabView == this)
  356. {
  357. SetHighlight (HighlightStyle.None);
  358. }
  359. return mouseEvent.Handled = true;
  360. }
  361. return false;
  362. }
  363. #region Highlight
  364. /// <summary>
  365. /// Gets or sets whether the <see cref="View"/> will be highlighted visually while the mouse button is
  366. /// pressed.
  367. /// </summary>
  368. public HighlightStyle HighlightStyle { get; set; }
  369. private bool RaiseHighlight (CancelEventArgs<HighlightStyle> args)
  370. {
  371. if (OnHighlight (args) == true)
  372. {
  373. return true;
  374. }
  375. Highlight?.Invoke (this, args);
  376. return args.Cancel;
  377. }
  378. /// <summary>
  379. /// Called when the view is to be highlighted. See <see cref="HighlightStyle"/>.
  380. /// </summary>
  381. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  382. protected virtual bool OnHighlight (CancelEventArgs<HighlightStyle> args)
  383. {
  384. return false;
  385. }
  386. /// <summary>
  387. /// Fired when the view is highlighted. See <see cref="HighlightStyle"/>.
  388. /// </summary>
  389. public event EventHandler<CancelEventArgs<HighlightStyle>>? Highlight;
  390. /// <summary>
  391. /// Enables the highlight for the view when the mouse is pressed. Called from OnMouseEvent.
  392. /// </summary>
  393. /// <remarks>
  394. /// <para>
  395. /// Set <see cref="HighlightStyle"/> to <see cref="HighlightStyle.Pressed"/> to enable.
  396. /// </para>
  397. /// <para>
  398. /// Calls <see cref="OnHighlight"/> which fires the <see cref="Highlight"/> event.
  399. /// </para>
  400. /// <para>
  401. /// Marked internal just to support unit tests
  402. /// </para>
  403. /// </remarks>
  404. /// <returns><see langword="true"/>, if the Highlight event was handled, <see langword="false"/> otherwise.</returns>
  405. internal bool SetHighlight (HighlightStyle newHighlightStyle)
  406. {
  407. // TODO: Make the highlight colors configurable
  408. if (!CanFocus)
  409. {
  410. return false;
  411. }
  412. // Enable override via virtual method and/or event
  413. HighlightStyle copy = HighlightStyle;
  414. CancelEventArgs<HighlightStyle> args = new (ref copy, ref newHighlightStyle);
  415. if (RaiseHighlight (args))
  416. {
  417. return true;
  418. }
  419. // For Shadow
  420. Margin?.RaiseHighlight (args);
  421. if (args.NewValue.HasFlag (HighlightStyle.Pressed) || args.NewValue.HasFlag (HighlightStyle.PressedOutside))
  422. {
  423. if (_savedHighlightColorScheme is null && ColorScheme is { })
  424. {
  425. _savedHighlightColorScheme ??= ColorScheme;
  426. if (CanFocus)
  427. {
  428. var cs = new ColorScheme (ColorScheme)
  429. {
  430. // Highlight the foreground focus color
  431. Focus = new (ColorScheme.Focus.Foreground.GetHighlightColor (), ColorScheme.Focus.Background.GetHighlightColor ())
  432. };
  433. ColorScheme = cs;
  434. }
  435. else
  436. {
  437. var cs = new ColorScheme (ColorScheme)
  438. {
  439. // Invert Focus color foreground/background. We can do this because we know the view is not going to be focused.
  440. Normal = new (ColorScheme.Focus.Background, ColorScheme.Normal.Foreground)
  441. };
  442. ColorScheme = cs;
  443. }
  444. }
  445. // Return false since we don't want to eat the event
  446. return false;
  447. }
  448. if (args.NewValue == HighlightStyle.None)
  449. {
  450. // Unhighlight
  451. if (_savedHighlightColorScheme is { })
  452. {
  453. ColorScheme = _savedHighlightColorScheme;
  454. _savedHighlightColorScheme = null;
  455. }
  456. }
  457. return false;
  458. }
  459. #endregion
  460. /// <summary>
  461. /// For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
  462. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  463. /// </summary>
  464. /// <remarks>
  465. /// <para>
  466. /// Marked internal just to support unit tests
  467. /// </para>
  468. /// </remarks>
  469. /// <param name="mouseEvent"></param>
  470. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  471. private bool HandlePressed (MouseEvent mouseEvent)
  472. {
  473. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)
  474. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed)
  475. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed)
  476. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Pressed))
  477. {
  478. // The first time we get pressed event, grab the mouse and set focus
  479. if (Application.MouseGrabView != this)
  480. {
  481. Application.GrabMouse (this);
  482. if (!HasFocus && CanFocus)
  483. {
  484. // Set the focus, but don't invoke Accept
  485. SetFocus ();
  486. }
  487. mouseEvent.Handled = true;
  488. }
  489. if (Viewport.Contains (mouseEvent.Position))
  490. {
  491. if (this is not Adornment
  492. && SetHighlight (HighlightStyle.HasFlag (HighlightStyle.Pressed) ? HighlightStyle.Pressed : HighlightStyle.None))
  493. {
  494. return true;
  495. }
  496. }
  497. else
  498. {
  499. if (this is not Adornment
  500. && SetHighlight (HighlightStyle.HasFlag (HighlightStyle.PressedOutside) ? HighlightStyle.PressedOutside : HighlightStyle.None))
  501. {
  502. return true;
  503. }
  504. }
  505. if (WantContinuousButtonPressed && Application.MouseGrabView == this)
  506. {
  507. // If this is not the first pressed event, click
  508. return OnMouseClick (new (mouseEvent));
  509. }
  510. return mouseEvent.Handled = true;
  511. }
  512. return false;
  513. }
  514. /// <summary>
  515. /// INTERNAL: Gets the Views that are under the mouse at <paramref name="location"/>, including Adornments.
  516. /// </summary>
  517. /// <param name="location"></param>
  518. /// <returns></returns>
  519. internal static List<View?> GetViewsUnderMouse (in Point location)
  520. {
  521. List<View?> viewsUnderMouse = new ();
  522. View? start = Application.Current ?? Application.Top;
  523. Point currentLocation = location;
  524. while (start is { Visible: true } && start.Contains (currentLocation))
  525. {
  526. viewsUnderMouse.Add (start);
  527. Adornment? found = null;
  528. if (start.Margin.Contains (currentLocation))
  529. {
  530. found = start.Margin;
  531. }
  532. else if (start.Border.Contains (currentLocation))
  533. {
  534. found = start.Border;
  535. }
  536. else if (start.Padding.Contains (currentLocation))
  537. {
  538. found = start.Padding;
  539. }
  540. Point viewportOffset = start.GetViewportOffsetFromFrame ();
  541. if (found is { })
  542. {
  543. start = found;
  544. viewsUnderMouse.Add (start);
  545. viewportOffset = found.Parent?.Frame.Location ?? Point.Empty;
  546. }
  547. int startOffsetX = currentLocation.X - (start.Frame.X + viewportOffset.X);
  548. int startOffsetY = currentLocation.Y - (start.Frame.Y + viewportOffset.Y);
  549. View? subview = null;
  550. for (int i = start.InternalSubviews.Count - 1; i >= 0; i--)
  551. {
  552. if (start.InternalSubviews [i].Visible
  553. && start.InternalSubviews [i].Contains (new (startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y)))
  554. {
  555. subview = start.InternalSubviews [i];
  556. currentLocation.X = startOffsetX + start.Viewport.X;
  557. currentLocation.Y = startOffsetY + start.Viewport.Y;
  558. // start is the deepest subview under the mouse; stop searching the subviews
  559. break;
  560. }
  561. }
  562. if (subview is null)
  563. {
  564. // No subview was found that's under the mouse, so we're done
  565. return viewsUnderMouse;
  566. }
  567. // We found a subview of start that's under the mouse, continue...
  568. start = subview;
  569. }
  570. return viewsUnderMouse;
  571. }
  572. }