View.Mouse.cs 30 KB

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