View.Mouse.cs 29 KB

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