View.Mouse.cs 31 KB

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