View.Mouse.cs 29 KB

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