2
0

View.Mouse.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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.OnMouseEvent"/> 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.OnMouseEvent"/> 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>Event fired when a mouse event occurs.</summary>
  172. /// <remarks>
  173. /// <para>
  174. /// The coordinates are relative to <see cref="View.Viewport"/>.
  175. /// </para>
  176. /// </remarks>
  177. public event EventHandler<MouseEventEventArgs>? MouseEvent;
  178. /// <summary>
  179. /// Processes a <see cref="MouseEvent"/>. This method is called by <see cref="Application.OnMouseEvent"/> when a mouse
  180. /// event occurs.
  181. /// </summary>
  182. /// <remarks>
  183. /// <para>
  184. /// A view must be both enabled and visible to receive mouse events.
  185. /// </para>
  186. /// <para>
  187. /// This method calls <see cref="OnMouseEvent"/> to process the event. If the event is not handled, and one of the
  188. /// mouse buttons was clicked, it calls <see cref="OnMouseClick"/> to process the click.
  189. /// </para>
  190. /// <para>
  191. /// See <see cref="SetPressedHighlight"/> for more information.
  192. /// </para>
  193. /// <para>
  194. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, the <see cref="OnMouseClick"/> event
  195. /// will be invoked repeatedly while the button is pressed.
  196. /// </para>
  197. /// </remarks>
  198. /// <param name="mouseEvent"></param>
  199. /// <returns><see langword="true"/> if the event was handled, <see langword="false"/> otherwise.</returns>
  200. public bool? NewMouseEvent (MouseEvent mouseEvent)
  201. {
  202. // Pre-conditions
  203. if (!Enabled)
  204. {
  205. // A disabled view should not eat mouse events
  206. return false;
  207. }
  208. if (!CanBeVisible (this))
  209. {
  210. return false;
  211. }
  212. if (!WantMousePositionReports && mouseEvent.Flags == MouseFlags.ReportMousePosition)
  213. {
  214. return false;
  215. }
  216. // Cancellable event
  217. if (OnMouseEvent (mouseEvent))
  218. {
  219. // Technically mouseEvent.Handled should already be true if implementers of OnMouseEvent
  220. // follow the rules. But we'll update it just in case.
  221. return mouseEvent.Handled = true;
  222. }
  223. // BUGBUG: MouseEvent should be fired from here. Fix this in https://github.com/gui-cs/Terminal.Gui/issues/3029
  224. // Post-Conditions
  225. if (HighlightStyle != HighlightStyle.None || (WantContinuousButtonPressed && WantMousePositionReports))
  226. {
  227. if (WhenGrabbedHandlePressed (mouseEvent))
  228. {
  229. return mouseEvent.Handled;
  230. }
  231. if (WhenGrabbedHandleReleased (mouseEvent))
  232. {
  233. return mouseEvent.Handled;
  234. }
  235. if (WhenGrabbedHandleClicked (mouseEvent))
  236. {
  237. return mouseEvent.Handled;
  238. }
  239. }
  240. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
  241. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
  242. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
  243. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)
  244. || mouseEvent.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  245. || mouseEvent.Flags.HasFlag (MouseFlags.Button2DoubleClicked)
  246. || mouseEvent.Flags.HasFlag (MouseFlags.Button3DoubleClicked)
  247. || mouseEvent.Flags.HasFlag (MouseFlags.Button4DoubleClicked)
  248. || mouseEvent.Flags.HasFlag (MouseFlags.Button1TripleClicked)
  249. || mouseEvent.Flags.HasFlag (MouseFlags.Button2TripleClicked)
  250. || mouseEvent.Flags.HasFlag (MouseFlags.Button3TripleClicked)
  251. || mouseEvent.Flags.HasFlag (MouseFlags.Button4TripleClicked)
  252. )
  253. {
  254. // If it's a click, and we didn't handle it, then we need to generate a click event
  255. // We get here if the view did not handle the mouse event via OnMouseEvent/MouseEvent and
  256. // it did not handle the press/release/clicked events via HandlePress/HandleRelease/HandleClicked
  257. return OnMouseClick (new (mouseEvent));
  258. }
  259. return false;
  260. }
  261. /// <summary>Gets or sets whether the <see cref="View"/> wants continuous button pressed events.</summary>
  262. public virtual bool WantContinuousButtonPressed { get; set; }
  263. /// <summary>Gets or sets whether the <see cref="View"/> wants mouse position reports.</summary>
  264. /// <value><see langword="true"/> if mouse position reports are wanted; otherwise, <see langword="false"/>.</value>
  265. public virtual bool WantMousePositionReports { get; set; }
  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. #endregion Low Level Mouse Events
  281. #region Mouse Click Events
  282. /// <summary>Event fired when a mouse click occurs.</summary>
  283. ///
  284. /// <remarks>
  285. /// <para>
  286. /// Fired when the mouse is either clicked or double-clicked. Check
  287. /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
  288. /// </para>
  289. /// <para>
  290. /// The coordinates are relative to <see cref="View.Viewport"/>.
  291. /// </para>
  292. /// </remarks>
  293. public event EventHandler<MouseEventEventArgs>? MouseClick;
  294. /// <summary>Invokes the MouseClick event.</summary>
  295. /// <remarks>
  296. /// <para>
  297. /// Called when the mouse is either clicked or double-clicked. Check
  298. /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
  299. /// </para>
  300. /// </remarks>
  301. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  302. protected bool OnMouseClick (MouseEventEventArgs args)
  303. {
  304. // BUGBUG: This should be named NewMouseClickEvent. Fix this in https://github.com/gui-cs/Terminal.Gui/issues/3029
  305. // Pre-conditions
  306. if (!Enabled)
  307. {
  308. // QUESTION: Is this right? Should a disabled view eat mouse clicks?
  309. return args.Handled = false;
  310. }
  311. // Cancellable event
  312. // BUGBUG: There should be a call to a protected virtual OnMouseClick here. Fix this in https://github.com/gui-cs/Terminal.Gui/issues/3029
  313. MouseClick?.Invoke (this, args);
  314. if (args.Handled)
  315. {
  316. return true;
  317. }
  318. // Post-conditions
  319. if (!HasFocus && CanFocus)
  320. {
  321. args.Handled = true;
  322. SetFocus ();
  323. }
  324. return args.Handled;
  325. }
  326. /// <summary>
  327. /// INTERNAL 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 WhenGrabbedHandleClicked (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 (SetPressedHighlight (HighlightStyle.None))
  346. {
  347. return true;
  348. }
  349. // If mouse is still in bounds, generate a click
  350. if (!WantContinuousButtonPressed && Viewport.Contains (mouseEvent.Position))
  351. {
  352. var meea = new MouseEventEventArgs (mouseEvent);
  353. // We can ignore the return value of OnMouseClick; if the click is handled
  354. // meea.Handled and meea.MouseEvent.Handled will be true
  355. OnMouseClick (meea);
  356. }
  357. return mouseEvent.Handled = true;
  358. }
  359. return false;
  360. }
  361. /// <summary>
  362. /// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
  363. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  364. /// </summary>
  365. /// <remarks>
  366. /// Marked internal just to support unit tests
  367. /// </remarks>
  368. /// <param name="mouseEvent"></param>
  369. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  370. internal bool WhenGrabbedHandleReleased (MouseEvent mouseEvent)
  371. {
  372. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released)
  373. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Released)
  374. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Released)
  375. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Released))
  376. {
  377. if (Application.MouseGrabView == this)
  378. {
  379. SetPressedHighlight (HighlightStyle.None);
  380. }
  381. return mouseEvent.Handled = true;
  382. }
  383. return false;
  384. }
  385. /// <summary>
  386. /// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
  387. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  388. /// </summary>
  389. /// <remarks>
  390. /// <para>
  391. /// Marked internal just to support unit tests
  392. /// </para>
  393. /// </remarks>
  394. /// <param name="mouseEvent"></param>
  395. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  396. private bool WhenGrabbedHandlePressed (MouseEvent mouseEvent)
  397. {
  398. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)
  399. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed)
  400. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed)
  401. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Pressed))
  402. {
  403. // The first time we get pressed event, grab the mouse and set focus
  404. if (Application.MouseGrabView != this)
  405. {
  406. Application.GrabMouse (this);
  407. if (!HasFocus && CanFocus)
  408. {
  409. // Set the focus, but don't invoke Accept
  410. SetFocus ();
  411. }
  412. mouseEvent.Handled = true;
  413. }
  414. if (Viewport.Contains (mouseEvent.Position))
  415. {
  416. if (this is not Adornment
  417. && SetPressedHighlight (HighlightStyle.HasFlag (HighlightStyle.Pressed) ? HighlightStyle.Pressed : HighlightStyle.None))
  418. {
  419. return true;
  420. }
  421. }
  422. else
  423. {
  424. if (this is not Adornment
  425. && SetPressedHighlight (HighlightStyle.HasFlag (HighlightStyle.PressedOutside) ? HighlightStyle.PressedOutside : HighlightStyle.None))
  426. {
  427. return true;
  428. }
  429. }
  430. if (WantContinuousButtonPressed && Application.MouseGrabView == this)
  431. {
  432. // If this is not the first pressed event, generate a click
  433. return OnMouseClick (new (mouseEvent));
  434. }
  435. return mouseEvent.Handled = true;
  436. }
  437. return false;
  438. }
  439. #endregion Mouse Click Events
  440. #region Highlight Handling
  441. // Used for Pressed highlighting
  442. private ColorScheme? _savedHighlightColorScheme;
  443. /// <summary>
  444. /// Gets or sets whether the <see cref="View"/> will be highlighted visually by mouse interaction.
  445. /// </summary>
  446. public HighlightStyle HighlightStyle { get; set; }
  447. /// <summary>
  448. /// INTERNAL Raises the <see cref="Highlight"/> event. Returns <see langword="true"/> if the event was handled,
  449. /// <see langword="false"/> otherwise.
  450. /// </summary>
  451. /// <param name="args"></param>
  452. /// <returns></returns>
  453. private bool RaiseHighlight (CancelEventArgs<HighlightStyle> args)
  454. {
  455. if (OnHighlight (args))
  456. {
  457. return true;
  458. }
  459. Highlight?.Invoke (this, args);
  460. return args.Cancel;
  461. }
  462. /// <summary>
  463. /// Called when the view is to be highlighted. The <see cref="HighlightStyle"/> passed in the event indicates the
  464. /// highlight style that will be applied. The view can modify the highlight style by setting the
  465. /// <see cref="CancelEventArgs{T}.NewValue"/> property.
  466. /// </summary>
  467. /// <param name="args">
  468. /// Set the <see cref="CancelEventArgs{T}.NewValue"/> property to <see langword="true"/>, to cancel, indicating custom
  469. /// highlighting.
  470. /// </param>
  471. /// <returns><see langword="true"/>, to cancel, indicating custom highlighting.</returns>
  472. protected virtual bool OnHighlight (CancelEventArgs<HighlightStyle> args) { return false; }
  473. /// <summary>
  474. /// Raised when the view is to be highlighted. The <see cref="HighlightStyle"/> passed in the event indicates the
  475. /// highlight style that will be applied. The view can modify the highlight style by setting the
  476. /// <see cref="CancelEventArgs{T}.NewValue"/> property.
  477. /// Set to <see langword="true"/>, to cancel, indicating custom highlighting.
  478. /// </summary>
  479. public event EventHandler<CancelEventArgs<HighlightStyle>>? Highlight;
  480. /// <summary>
  481. /// INTERNAL Enables the highlight for the view when the mouse is pressed. Called from OnMouseEvent.
  482. /// </summary>
  483. /// <remarks>
  484. /// <para>
  485. /// Set <see cref="HighlightStyle"/> to <see cref="HighlightStyle.Pressed"/> and/or
  486. /// <see cref="HighlightStyle.PressedOutside"/> to enable.
  487. /// </para>
  488. /// <para>
  489. /// Calls <see cref="OnHighlight"/> and raises the <see cref="Highlight"/> event.
  490. /// </para>
  491. /// <para>
  492. /// Marked internal just to support unit tests
  493. /// </para>
  494. /// </remarks>
  495. /// <returns><see langword="true"/>, if the Highlight event was handled, <see langword="false"/> otherwise.</returns>
  496. internal bool SetPressedHighlight (HighlightStyle newHighlightStyle)
  497. {
  498. // TODO: Make the highlight colors configurable
  499. if (!CanFocus)
  500. {
  501. return false;
  502. }
  503. HighlightStyle copy = HighlightStyle;
  504. CancelEventArgs<HighlightStyle> args = new (ref copy, ref newHighlightStyle);
  505. if (RaiseHighlight (args) || args.Cancel)
  506. {
  507. return true;
  508. }
  509. // For 3D Pressed Style - Note we don't care about canceling the event here
  510. Margin?.RaiseHighlight (args);
  511. args.Cancel = false; // Just in case
  512. if (args.NewValue.HasFlag (HighlightStyle.Pressed) || args.NewValue.HasFlag (HighlightStyle.PressedOutside))
  513. {
  514. if (_savedHighlightColorScheme is null && ColorScheme is { })
  515. {
  516. _savedHighlightColorScheme ??= ColorScheme;
  517. if (CanFocus)
  518. {
  519. var cs = new ColorScheme (ColorScheme)
  520. {
  521. // Highlight the foreground focus color
  522. Focus = new (ColorScheme.Focus.Foreground.GetHighlightColor (), ColorScheme.Focus.Background.GetHighlightColor ())
  523. };
  524. ColorScheme = cs;
  525. }
  526. else
  527. {
  528. var cs = new ColorScheme (ColorScheme)
  529. {
  530. // Invert Focus color foreground/background. We can do this because we know the view is not going to be focused.
  531. Normal = new (ColorScheme.Focus.Background, ColorScheme.Normal.Foreground)
  532. };
  533. ColorScheme = cs;
  534. }
  535. }
  536. // Return false since we don't want to eat the event
  537. return false;
  538. }
  539. if (args.NewValue == HighlightStyle.None)
  540. {
  541. // Unhighlight
  542. if (_savedHighlightColorScheme is { })
  543. {
  544. ColorScheme = _savedHighlightColorScheme;
  545. _savedHighlightColorScheme = null;
  546. }
  547. }
  548. return false;
  549. }
  550. #endregion Highlight Handling
  551. /// <summary>
  552. /// INTERNAL: Gets the Views that are under the mouse at <paramref name="location"/>, including Adornments.
  553. /// </summary>
  554. /// <param name="location"></param>
  555. /// <returns></returns>
  556. internal static List<View?> GetViewsUnderMouse (in Point location)
  557. {
  558. List<View?> viewsUnderMouse = new ();
  559. View? start = Application.Top;
  560. Point currentLocation = location;
  561. while (start is { Visible: true } && start.Contains (currentLocation))
  562. {
  563. viewsUnderMouse.Add (start);
  564. Adornment? found = null;
  565. if (start.Margin.Contains (currentLocation))
  566. {
  567. found = start.Margin;
  568. }
  569. else if (start.Border.Contains (currentLocation))
  570. {
  571. found = start.Border;
  572. }
  573. else if (start.Padding.Contains (currentLocation))
  574. {
  575. found = start.Padding;
  576. }
  577. Point viewportOffset = start.GetViewportOffsetFromFrame ();
  578. if (found is { })
  579. {
  580. start = found;
  581. viewsUnderMouse.Add (start);
  582. viewportOffset = found.Parent?.Frame.Location ?? Point.Empty;
  583. }
  584. int startOffsetX = currentLocation.X - (start.Frame.X + viewportOffset.X);
  585. int startOffsetY = currentLocation.Y - (start.Frame.Y + viewportOffset.Y);
  586. View? subview = null;
  587. for (int i = start.InternalSubviews.Count - 1; i >= 0; i--)
  588. {
  589. if (start.InternalSubviews [i].Visible
  590. && start.InternalSubviews [i].Contains (new (startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y)))
  591. {
  592. subview = start.InternalSubviews [i];
  593. currentLocation.X = startOffsetX + start.Viewport.X;
  594. currentLocation.Y = startOffsetY + start.Viewport.Y;
  595. // start is the deepest subview under the mouse; stop searching the subviews
  596. break;
  597. }
  598. }
  599. if (subview is null)
  600. {
  601. // No subview was found that's under the mouse, so we're done
  602. return viewsUnderMouse;
  603. }
  604. // We found a subview of start that's under the mouse, continue...
  605. start = subview;
  606. }
  607. return viewsUnderMouse;
  608. }
  609. }