View.Mouse.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 && WantMousePositionReports))
  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.Flags.HasFlag (MouseFlags.Button1Clicked)
  236. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
  237. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
  238. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)
  239. || mouseEvent.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  240. || mouseEvent.Flags.HasFlag (MouseFlags.Button2DoubleClicked)
  241. || mouseEvent.Flags.HasFlag (MouseFlags.Button3DoubleClicked)
  242. || mouseEvent.Flags.HasFlag (MouseFlags.Button4DoubleClicked)
  243. || mouseEvent.Flags.HasFlag (MouseFlags.Button1TripleClicked)
  244. || mouseEvent.Flags.HasFlag (MouseFlags.Button2TripleClicked)
  245. || mouseEvent.Flags.HasFlag (MouseFlags.Button3TripleClicked)
  246. || mouseEvent.Flags.HasFlag (MouseFlags.Button4TripleClicked)
  247. )
  248. {
  249. // If it's a click, and we didn't handle it, then we need to generate a click event
  250. // We get here if the view did not handle the mouse event via OnMouseEvent/MouseEvent and
  251. // it did not handle the press/release/clicked events via HandlePress/HandleRelease/HandleClicked
  252. return RaiseMouseClickEvent (mouseEvent);
  253. }
  254. return false;
  255. }
  256. /// <summary>
  257. /// Raises the <see cref="RaiseMouseEvent"/>/<see cref="MouseEvent"/> event.
  258. /// </summary>
  259. /// <param name="mouseEvent"></param>
  260. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  261. public bool RaiseMouseEvent (MouseEventArgs mouseEvent)
  262. {
  263. if (OnMouseEvent (mouseEvent) || mouseEvent.Handled == true)
  264. {
  265. return true;
  266. }
  267. MouseEvent?.Invoke (this, mouseEvent);
  268. return mouseEvent.Handled;
  269. }
  270. /// <summary>Called when a mouse event occurs within the view's <see cref="Viewport"/>.</summary>
  271. /// <remarks>
  272. /// <para>
  273. /// The coordinates are relative to <see cref="View.Viewport"/>.
  274. /// </para>
  275. /// </remarks>
  276. /// <param name="mouseEvent"></param>
  277. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  278. protected virtual bool OnMouseEvent (MouseEventArgs mouseEvent)
  279. {
  280. return false;
  281. }
  282. /// <summary>Raised when a mouse event occurs.</summary>
  283. /// <remarks>
  284. /// <para>
  285. /// The coordinates are relative to <see cref="View.Viewport"/>.
  286. /// </para>
  287. /// </remarks>
  288. public event EventHandler<MouseEventArgs>? MouseEvent;
  289. #endregion Low Level Mouse Events
  290. #region Mouse Click Events
  291. /// <summary>Raises the <see cref="OnMouseClick"/>/<see cref="MouseClick"/> event.</summary>
  292. /// <remarks>
  293. /// <para>
  294. /// Called when the mouse is either clicked or double-clicked.
  295. /// </para>
  296. /// <para>
  297. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be invoked on every mouse event where
  298. /// the mouse button is pressed.
  299. /// </para>
  300. /// </remarks>
  301. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  302. protected bool RaiseMouseClickEvent (MouseEventArgs args)
  303. {
  304. // Pre-conditions
  305. if (!Enabled)
  306. {
  307. // QUESTION: Is this right? Should a disabled view eat mouse clicks?
  308. return args.Handled = false;
  309. }
  310. // Cancellable event
  311. if (OnMouseClick (args) || args.Handled)
  312. {
  313. return args.Handled;
  314. }
  315. MouseClick?.Invoke (this, args);
  316. if (args.Handled)
  317. {
  318. return true;
  319. }
  320. // Post-conditions
  321. // Always invoke Select command on MouseClick
  322. // By default, this will raise Selecting/OnSelecting - Subclasses can override this via AddCommand (Command.Select ...).
  323. args.Handled = InvokeCommand (Command.Select, ctx: new (Command.Select, key: null, data: args)) == true;
  324. return args.Handled;
  325. }
  326. /// <summary>
  327. /// Called when a mouse click occurs. Check <see cref="MouseEventArgs.Flags"/> to see which button was clicked.
  328. /// </summary>
  329. /// <remarks>
  330. /// <para>
  331. /// Called when the mouse is either clicked or double-clicked.
  332. /// </para>
  333. /// <para>
  334. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be called on every mouse event where
  335. /// the mouse button is pressed.
  336. /// </para>
  337. /// </remarks>
  338. /// <param name="args"></param>
  339. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  340. protected virtual bool OnMouseClick (MouseEventArgs args) { return false; }
  341. /// <summary>Raised when a mouse click occurs.</summary>
  342. /// <remarks>
  343. /// <para>
  344. /// Raised when the mouse is either clicked or double-clicked.
  345. /// </para>
  346. /// <para>
  347. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be raised on every mouse event where
  348. /// the mouse button is pressed.
  349. /// </para>
  350. /// </remarks>
  351. public event EventHandler<MouseEventArgs>? MouseClick;
  352. /// <summary>
  353. /// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the click event (typically
  354. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  355. /// </summary>
  356. /// <remarks>
  357. /// Marked internal just to support unit tests
  358. /// </remarks>
  359. /// <param name="mouseEvent"></param>
  360. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  361. internal bool WhenGrabbedHandleClicked (MouseEventArgs mouseEvent)
  362. {
  363. mouseEvent.Handled = false;
  364. if (Application.MouseGrabView == this
  365. && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
  366. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
  367. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
  368. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)))
  369. {
  370. // We're grabbed. Clicked event comes after the last Release. This is our signal to ungrab
  371. Application.UngrabMouse ();
  372. if (SetPressedHighlight (HighlightStyle.None))
  373. {
  374. return true;
  375. }
  376. // If mouse is still in bounds, generate a click
  377. if (!WantMousePositionReports && Viewport.Contains (mouseEvent.Position))
  378. {
  379. return RaiseMouseClickEvent (mouseEvent);
  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. /// Marked internal just to support unit tests
  391. /// </remarks>
  392. /// <param name="mouseEvent"></param>
  393. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  394. internal bool WhenGrabbedHandleReleased (MouseEventArgs mouseEvent)
  395. {
  396. mouseEvent.Handled = false;
  397. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released)
  398. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Released)
  399. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Released)
  400. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Released))
  401. {
  402. if (Application.MouseGrabView == this)
  403. {
  404. SetPressedHighlight (HighlightStyle.None);
  405. }
  406. return mouseEvent.Handled = true;
  407. }
  408. return false;
  409. }
  410. /// <summary>
  411. /// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
  412. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  413. /// </summary>
  414. /// <remarks>
  415. /// <para>
  416. /// Marked internal just to support unit tests
  417. /// </para>
  418. /// </remarks>
  419. /// <param name="mouseEvent"></param>
  420. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  421. private bool WhenGrabbedHandlePressed (MouseEventArgs mouseEvent)
  422. {
  423. mouseEvent.Handled = false;
  424. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)
  425. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed)
  426. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed)
  427. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Pressed))
  428. {
  429. // The first time we get pressed event, grab the mouse and set focus
  430. if (Application.MouseGrabView != this)
  431. {
  432. Application.GrabMouse (this);
  433. if (!HasFocus && CanFocus)
  434. {
  435. // Set the focus, but don't invoke Accept
  436. SetFocus ();
  437. }
  438. }
  439. if (Viewport.Contains (mouseEvent.Position))
  440. {
  441. if (this is not Adornment
  442. && SetPressedHighlight (HighlightStyle.HasFlag (HighlightStyle.Pressed) ? HighlightStyle.Pressed : HighlightStyle.None))
  443. {
  444. return true;
  445. }
  446. }
  447. else
  448. {
  449. if (this is not Adornment
  450. && SetPressedHighlight (HighlightStyle.HasFlag (HighlightStyle.PressedOutside) ? HighlightStyle.PressedOutside : HighlightStyle.None))
  451. {
  452. return true;
  453. }
  454. }
  455. if (WantContinuousButtonPressed && Application.MouseGrabView == this)
  456. {
  457. // If this is not the first pressed event, generate a click
  458. return RaiseMouseClickEvent (mouseEvent);
  459. }
  460. return mouseEvent.Handled = true;
  461. }
  462. return false;
  463. }
  464. #endregion Mouse Click Events
  465. #region Highlight Handling
  466. // Used for Pressed highlighting
  467. private ColorScheme? _savedHighlightColorScheme;
  468. /// <summary>
  469. /// Gets or sets whether the <see cref="View"/> will be highlighted visually by mouse interaction.
  470. /// </summary>
  471. public HighlightStyle HighlightStyle { get; set; }
  472. /// <summary>
  473. /// INTERNAL Raises the <see cref="Highlight"/> event. Returns <see langword="true"/> if the event was handled,
  474. /// <see langword="false"/> otherwise.
  475. /// </summary>
  476. /// <param name="args"></param>
  477. /// <returns></returns>
  478. private bool RaiseHighlight (CancelEventArgs<HighlightStyle> args)
  479. {
  480. if (OnHighlight (args))
  481. {
  482. return true;
  483. }
  484. Highlight?.Invoke (this, args);
  485. return args.Cancel;
  486. }
  487. /// <summary>
  488. /// Called when the view is to be highlighted. The <see cref="HighlightStyle"/> passed in the event indicates the
  489. /// highlight style that will be applied. The view can modify the highlight style by setting the
  490. /// <see cref="CancelEventArgs{T}.NewValue"/> property.
  491. /// </summary>
  492. /// <param name="args">
  493. /// Set the <see cref="CancelEventArgs{T}.NewValue"/> property to <see langword="true"/>, to cancel, indicating custom
  494. /// highlighting.
  495. /// </param>
  496. /// <returns><see langword="true"/>, to cancel, indicating custom highlighting.</returns>
  497. protected virtual bool OnHighlight (CancelEventArgs<HighlightStyle> args) { return false; }
  498. /// <summary>
  499. /// Raised when the view is to be highlighted. The <see cref="HighlightStyle"/> passed in the event indicates the
  500. /// highlight style that will be applied. The view can modify the highlight style by setting the
  501. /// <see cref="CancelEventArgs{T}.NewValue"/> property.
  502. /// Set to <see langword="true"/>, to cancel, indicating custom highlighting.
  503. /// </summary>
  504. public event EventHandler<CancelEventArgs<HighlightStyle>>? Highlight;
  505. /// <summary>
  506. /// INTERNAL Enables the highlight for the view when the mouse is pressed. Called from OnMouseEvent.
  507. /// </summary>
  508. /// <remarks>
  509. /// <para>
  510. /// Set <see cref="HighlightStyle"/> to <see cref="HighlightStyle.Pressed"/> and/or
  511. /// <see cref="HighlightStyle.PressedOutside"/> to enable.
  512. /// </para>
  513. /// <para>
  514. /// Calls <see cref="OnHighlight"/> and raises the <see cref="Highlight"/> event.
  515. /// </para>
  516. /// <para>
  517. /// Marked internal just to support unit tests
  518. /// </para>
  519. /// </remarks>
  520. /// <returns><see langword="true"/>, if the Highlight event was handled, <see langword="false"/> otherwise.</returns>
  521. internal bool SetPressedHighlight (HighlightStyle newHighlightStyle)
  522. {
  523. // TODO: Make the highlight colors configurable
  524. if (!CanFocus)
  525. {
  526. return false;
  527. }
  528. HighlightStyle copy = HighlightStyle;
  529. CancelEventArgs<HighlightStyle> args = new (ref copy, ref newHighlightStyle);
  530. if (RaiseHighlight (args) || args.Cancel)
  531. {
  532. return true;
  533. }
  534. // For 3D Pressed Style - Note we don't care about canceling the event here
  535. Margin?.RaiseHighlight (args);
  536. args.Cancel = false; // Just in case
  537. if (args.NewValue.HasFlag (HighlightStyle.Pressed) || args.NewValue.HasFlag (HighlightStyle.PressedOutside))
  538. {
  539. if (_savedHighlightColorScheme is null && ColorScheme is { })
  540. {
  541. _savedHighlightColorScheme ??= ColorScheme;
  542. if (CanFocus)
  543. {
  544. var cs = new ColorScheme (ColorScheme)
  545. {
  546. // Highlight the foreground focus color
  547. Focus = new (ColorScheme.Focus.Foreground.GetHighlightColor (), ColorScheme.Focus.Background.GetHighlightColor ())
  548. };
  549. ColorScheme = cs;
  550. }
  551. else
  552. {
  553. var cs = new ColorScheme (ColorScheme)
  554. {
  555. // Invert Focus color foreground/background. We can do this because we know the view is not going to be focused.
  556. Normal = new (ColorScheme.Focus.Background, ColorScheme.Normal.Foreground)
  557. };
  558. ColorScheme = cs;
  559. }
  560. }
  561. // Return false since we don't want to eat the event
  562. return false;
  563. }
  564. if (args.NewValue == HighlightStyle.None)
  565. {
  566. // Unhighlight
  567. if (_savedHighlightColorScheme is { })
  568. {
  569. ColorScheme = _savedHighlightColorScheme;
  570. _savedHighlightColorScheme = null;
  571. }
  572. }
  573. return false;
  574. }
  575. #endregion Highlight Handling
  576. /// <summary>
  577. /// INTERNAL: Gets the Views that are under the mouse at <paramref name="location"/>, including Adornments.
  578. /// </summary>
  579. /// <param name="location"></param>
  580. /// <returns></returns>
  581. internal static List<View?> GetViewsUnderMouse (in Point location)
  582. {
  583. List<View?> viewsUnderMouse = new ();
  584. View? start = Application.Top;
  585. Point currentLocation = location;
  586. while (start is { Visible: true } && start.Contains (currentLocation))
  587. {
  588. viewsUnderMouse.Add (start);
  589. Adornment? found = null;
  590. if (start.Margin.Contains (currentLocation))
  591. {
  592. found = start.Margin;
  593. }
  594. else if (start.Border.Contains (currentLocation))
  595. {
  596. found = start.Border;
  597. }
  598. else if (start.Padding.Contains (currentLocation))
  599. {
  600. found = start.Padding;
  601. }
  602. Point viewportOffset = start.GetViewportOffsetFromFrame ();
  603. if (found is { })
  604. {
  605. start = found;
  606. viewsUnderMouse.Add (start);
  607. viewportOffset = found.Parent?.Frame.Location ?? Point.Empty;
  608. }
  609. int startOffsetX = currentLocation.X - (start.Frame.X + viewportOffset.X);
  610. int startOffsetY = currentLocation.Y - (start.Frame.Y + viewportOffset.Y);
  611. View? subview = null;
  612. for (int i = start.InternalSubviews.Count - 1; i >= 0; i--)
  613. {
  614. if (start.InternalSubviews [i].Visible
  615. && start.InternalSubviews [i].Contains (new (startOffsetX + start.Viewport.X, startOffsetY + start.Viewport.Y)))
  616. {
  617. subview = start.InternalSubviews [i];
  618. currentLocation.X = startOffsetX + start.Viewport.X;
  619. currentLocation.Y = startOffsetY + start.Viewport.Y;
  620. // start is the deepest subview under the mouse; stop searching the subviews
  621. break;
  622. }
  623. }
  624. if (subview is null)
  625. {
  626. // No subview was found that's under the mouse, so we're done
  627. return viewsUnderMouse;
  628. }
  629. // We found a subview of start that's under the mouse, continue...
  630. start = subview;
  631. }
  632. return viewsUnderMouse;
  633. }
  634. }