View.Mouse.cs 26 KB

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