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