View.Mouse.cs 24 KB

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