View.Mouse.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. #nullable enable
  2. using System.ComponentModel;
  3. namespace Terminal.Gui.ViewBase;
  4. public partial class View // Mouse APIs
  5. {
  6. /// <summary>
  7. /// Handles <see cref="WantContinuousButtonPressed"/>, we have detected a button
  8. /// down in the view and have grabbed the mouse.
  9. /// </summary>
  10. public IMouseHeldDown? MouseHeldDown { get; set; }
  11. /// <summary>Gets the mouse bindings for this view.</summary>
  12. public MouseBindings MouseBindings { get; internal set; } = null!;
  13. private void SetupMouse ()
  14. {
  15. MouseHeldDown = new MouseHeldDown (this, Application.TimedEvents,Application.Mouse);
  16. MouseBindings = new ();
  17. // TODO: Should the default really work with any button or just button1?
  18. MouseBindings.Add (MouseFlags.Button1Clicked, Command.Select);
  19. MouseBindings.Add (MouseFlags.Button2Clicked, Command.Select);
  20. MouseBindings.Add (MouseFlags.Button3Clicked, Command.Select);
  21. MouseBindings.Add (MouseFlags.Button4Clicked, Command.Select);
  22. MouseBindings.Add (MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl, Command.Select);
  23. }
  24. /// <summary>
  25. /// Invokes the Commands bound to the MouseFlags specified by <paramref name="mouseEventArgs"/>.
  26. /// <para>See <see href="../docs/mouse.md">for an overview of Terminal.Gui mouse APIs.</see></para>
  27. /// </summary>
  28. /// <param name="mouseEventArgs">The mouse event passed.</param>
  29. /// <returns>
  30. /// <see langword="null"/> if no command was invoked; input processing should continue.
  31. /// <see langword="false"/> if at least one command was invoked and was not handled (or cancelled); input processing
  32. /// should continue.
  33. /// <see langword="true"/> if at least one command was invoked and handled (or cancelled); input processing should
  34. /// stop.
  35. /// </returns>
  36. protected bool? InvokeCommandsBoundToMouse (MouseEventArgs mouseEventArgs)
  37. {
  38. if (!MouseBindings.TryGet (mouseEventArgs.Flags, out MouseBinding binding))
  39. {
  40. return null;
  41. }
  42. binding.MouseEventArgs = mouseEventArgs;
  43. return InvokeCommands (binding.Commands, binding);
  44. }
  45. #region MouseEnterLeave
  46. /// <summary>
  47. /// INTERNAL Called by <see cref="Application.RaiseMouseEvent"/> when the mouse moves over the View's
  48. /// <see cref="Frame"/>.
  49. /// <see cref="MouseLeave"/> will
  50. /// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
  51. /// that View will also receive MouseEnter/Leave events.
  52. /// </summary>
  53. /// <param name="eventArgs"></param>
  54. /// <returns>
  55. /// <see langword="true"/> if the event was canceled, <see langword="false"/> if not, <see langword="null"/> if the
  56. /// view is not visible. Cancelling the event
  57. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  58. /// </returns>
  59. internal bool? NewMouseEnterEvent (CancelEventArgs eventArgs)
  60. {
  61. // Pre-conditions
  62. if (!CanBeVisible (this))
  63. {
  64. return null;
  65. }
  66. // Cancellable event
  67. if (OnMouseEnter (eventArgs))
  68. {
  69. return true;
  70. }
  71. MouseEnter?.Invoke (this, eventArgs);
  72. if (eventArgs.Cancel)
  73. {
  74. return true;
  75. }
  76. MouseState |= MouseState.In;
  77. if (HighlightStates != MouseState.None)
  78. {
  79. SetNeedsDraw ();
  80. }
  81. return false;
  82. }
  83. /// <summary>
  84. /// Called when the mouse moves over the View's <see cref="Frame"/> and no other non-SubView occludes it.
  85. /// <see cref="MouseLeave"/> will
  86. /// be raised when the mouse is no longer over the <see cref="Frame"/>.
  87. /// </summary>
  88. /// <remarks>
  89. /// <para>
  90. /// A view must be visible to receive Enter events (Leave events are always received).
  91. /// </para>
  92. /// <para>
  93. /// If the event is cancelled, the mouse event will not be propagated to other views and <see cref="MouseEnter"/>
  94. /// will not be raised.
  95. /// </para>
  96. /// <para>
  97. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  98. /// </para>
  99. /// <para>
  100. /// See <see cref="MouseState"/> for more information.
  101. /// </para>
  102. /// </remarks>
  103. /// <param name="eventArgs"></param>
  104. /// <returns>
  105. /// <see langword="true"/> if the event was canceled, <see langword="false"/> if not. Cancelling the event
  106. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  107. /// </returns>
  108. protected virtual bool OnMouseEnter (CancelEventArgs eventArgs) { return false; }
  109. /// <summary>
  110. /// Raised when the mouse moves over the View's <see cref="Frame"/>. <see cref="MouseLeave"/> will
  111. /// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
  112. /// that View will also receive MouseEnter/Leave events.
  113. /// </summary>
  114. /// <remarks>
  115. /// <para>
  116. /// A view must be visible to receive Enter events (Leave events are always received).
  117. /// </para>
  118. /// <para>
  119. /// If the event is cancelled, the mouse event will not be propagated to other views.
  120. /// </para>
  121. /// <para>
  122. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  123. /// </para>
  124. /// <para>
  125. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> if the event was canceled,
  126. /// <see langword="false"/> if not. Cancelling the event
  127. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  128. /// </para>
  129. /// <para>
  130. /// See <see cref="MouseState"/> for more information.
  131. /// </para>
  132. /// </remarks>
  133. public event EventHandler<CancelEventArgs>? MouseEnter;
  134. /// <summary>
  135. /// INTERNAL Called by <see cref="Application.RaiseMouseEvent"/> when the mouse leaves <see cref="Frame"/>, or is
  136. /// occluded
  137. /// by another non-SubView.
  138. /// </summary>
  139. /// <remarks>
  140. /// <para>
  141. /// This method calls <see cref="OnMouseLeave"/> and raises the <see cref="MouseLeave"/> event.
  142. /// </para>
  143. /// <para>
  144. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  145. /// </para>
  146. /// <para>
  147. /// See <see cref="MouseState"/> for more information.
  148. /// </para>
  149. /// </remarks>
  150. internal void NewMouseLeaveEvent ()
  151. {
  152. // Pre-conditions
  153. // Non-cancellable event
  154. OnMouseLeave ();
  155. MouseLeave?.Invoke (this, EventArgs.Empty);
  156. MouseState &= ~MouseState.In;
  157. // TODO: Should we also MouseState &= ~MouseState.Pressed; ??
  158. if (HighlightStates != MouseState.None)
  159. {
  160. SetNeedsDraw ();
  161. }
  162. }
  163. /// <summary>
  164. /// Called when the mouse moves outside View's <see cref="Frame"/>, or is occluded by another non-SubView.
  165. /// </summary>
  166. /// <remarks>
  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="MouseState"/> for more information.
  172. /// </para>
  173. /// </remarks>
  174. protected virtual void OnMouseLeave () { }
  175. /// <summary>
  176. /// Raised when the mouse moves outside View's <see cref="Frame"/>, or is occluded by another non-SubView.
  177. /// </summary>
  178. /// <remarks>
  179. /// <para>
  180. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  181. /// </para>
  182. /// <para>
  183. /// See <see cref="MouseState"/> for more information.
  184. /// </para>
  185. /// </remarks>
  186. public event EventHandler? MouseLeave;
  187. #endregion MouseEnterLeave
  188. #region Low Level Mouse Events
  189. /// <summary>
  190. /// Gets or sets whether the <see cref="View"/> wants continuous button pressed events. When set to
  191. /// <see langword="true"/>,
  192. /// and the user presses and holds the mouse button, <see cref="NewMouseEvent"/> will be
  193. /// repeatedly called with the same <see cref="MouseFlags"/> for as long as the mouse button remains pressed.
  194. /// </summary>
  195. public bool WantContinuousButtonPressed { get; set; }
  196. /// <summary>Gets or sets whether the <see cref="View"/> wants mouse position reports.</summary>
  197. /// <value><see langword="true"/> if mouse position reports are wanted; otherwise, <see langword="false"/>.</value>
  198. public bool WantMousePositionReports { get; set; }
  199. /// <summary>
  200. /// Processes a new <see cref="MouseEvent"/>. This method is called by <see cref="Application.RaiseMouseEvent"/> when a
  201. /// mouse
  202. /// event occurs.
  203. /// </summary>
  204. /// <remarks>
  205. /// <para>
  206. /// A view must be both enabled and visible to receive mouse events.
  207. /// </para>
  208. /// <para>
  209. /// This method raises <see cref="RaiseMouseEvent"/>/<see cref="MouseEvent"/>; if not handled, and one of the
  210. /// mouse buttons was clicked, the <see cref="RaiseMouseClickEvent"/>/<see cref="MouseClick"/> event will be raised
  211. /// </para>
  212. /// <para>
  213. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, and the user presses and holds the
  214. /// mouse button, <see cref="NewMouseEvent"/> will be repeatedly called with the same <see cref="MouseFlags"/> for
  215. /// as long as the mouse button remains pressed.
  216. /// </para>
  217. /// </remarks>
  218. /// <param name="mouseEvent"></param>
  219. /// <returns><see langword="true"/> if the event was handled, <see langword="false"/> otherwise.</returns>
  220. public bool? NewMouseEvent (MouseEventArgs mouseEvent)
  221. {
  222. // Pre-conditions
  223. if (!Enabled)
  224. {
  225. // A disabled view should not eat mouse events
  226. return false;
  227. }
  228. if (!CanBeVisible (this))
  229. {
  230. return false;
  231. }
  232. if (!WantMousePositionReports && mouseEvent.Flags == MouseFlags.ReportMousePosition)
  233. {
  234. return false;
  235. }
  236. // Cancellable event
  237. if (RaiseMouseEvent (mouseEvent) || mouseEvent.Handled)
  238. {
  239. return true;
  240. }
  241. // Post-Conditions
  242. if (HighlightStates != MouseState.None || WantContinuousButtonPressed)
  243. {
  244. if (WhenGrabbedHandlePressed (mouseEvent))
  245. {
  246. return mouseEvent.Handled;
  247. }
  248. if (WhenGrabbedHandleReleased (mouseEvent))
  249. {
  250. return mouseEvent.Handled;
  251. }
  252. if (WhenGrabbedHandleClicked (mouseEvent))
  253. {
  254. return mouseEvent.Handled;
  255. }
  256. }
  257. // We get here if the view did not handle the mouse event via OnMouseEvent/MouseEvent, and
  258. // it did not handle the press/release/clicked events via HandlePress/HandleRelease/HandleClicked
  259. if (mouseEvent.IsSingleDoubleOrTripleClicked)
  260. {
  261. return RaiseMouseClickEvent (mouseEvent);
  262. }
  263. if (mouseEvent.IsWheel)
  264. {
  265. return RaiseMouseWheelEvent (mouseEvent);
  266. }
  267. return false;
  268. }
  269. /// <summary>
  270. /// Raises the <see cref="RaiseMouseEvent"/>/<see cref="MouseEvent"/> event.
  271. /// </summary>
  272. /// <param name="mouseEvent"></param>
  273. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  274. public bool RaiseMouseEvent (MouseEventArgs mouseEvent)
  275. {
  276. // TODO: probably this should be moved elsewhere, please advise
  277. if (WantContinuousButtonPressed && MouseHeldDown != null)
  278. {
  279. if (mouseEvent.IsPressed)
  280. {
  281. MouseHeldDown.Start ();
  282. }
  283. else
  284. {
  285. MouseHeldDown.Stop ();
  286. }
  287. }
  288. if (OnMouseEvent (mouseEvent) || mouseEvent.Handled)
  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) { return false; }
  304. /// <summary>Raised when a mouse event occurs.</summary>
  305. /// <remarks>
  306. /// <para>
  307. /// The coordinates are relative to <see cref="View.Viewport"/>.
  308. /// </para>
  309. /// </remarks>
  310. public event EventHandler<MouseEventArgs>? MouseEvent;
  311. #endregion Low Level Mouse Events
  312. #region Mouse Pressed Events
  313. /// <summary>
  314. /// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event
  315. /// (typically
  316. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStates"/> are set).
  317. /// </summary>
  318. /// <remarks>
  319. /// Marked internal just to support unit tests
  320. /// </remarks>
  321. /// <param name="mouseEvent"></param>
  322. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  323. internal bool WhenGrabbedHandleReleased (MouseEventArgs mouseEvent)
  324. {
  325. mouseEvent.Handled = false;
  326. if (mouseEvent.IsReleased)
  327. {
  328. if (Application.Mouse.MouseGrabView == this)
  329. {
  330. //Logging.Debug ($"{Id} - {MouseState}");
  331. MouseState &= ~MouseState.Pressed;
  332. MouseState &= ~MouseState.PressedOutside;
  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
  340. /// (typically
  341. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStates"/> are set).
  342. /// </summary>
  343. /// <remarks>
  344. /// <para>
  345. /// Marked internal just to support unit tests
  346. /// </para>
  347. /// </remarks>
  348. /// <param name="mouseEvent"></param>
  349. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  350. private bool WhenGrabbedHandlePressed (MouseEventArgs mouseEvent)
  351. {
  352. mouseEvent.Handled = false;
  353. if (mouseEvent.IsPressed)
  354. {
  355. // The first time we get pressed event, grab the mouse and set focus
  356. if (Application.Mouse.MouseGrabView != this)
  357. {
  358. Application.Mouse.GrabMouse (this);
  359. if (!HasFocus && CanFocus)
  360. {
  361. // Set the focus, but don't invoke Accept
  362. SetFocus ();
  363. }
  364. mouseEvent.Handled = true;
  365. }
  366. if (Viewport.Contains (mouseEvent.Position))
  367. {
  368. //Logging.Debug ($"{Id} - Inside Viewport: {MouseState}");
  369. // The mouse is inside.
  370. if (HighlightStates.HasFlag (MouseState.Pressed))
  371. {
  372. MouseState |= MouseState.Pressed;
  373. }
  374. // Always clear PressedOutside when the mouse is pressed inside the Viewport
  375. MouseState &= ~MouseState.PressedOutside;
  376. }
  377. if (!Viewport.Contains (mouseEvent.Position))
  378. {
  379. // Logging.Debug ($"{Id} - Outside Viewport: {MouseState}");
  380. // The mouse is outside.
  381. // When WantContinuousButtonPressed is set we want to keep the mouse state as pressed (e.g. a repeating button).
  382. // This shows the user that the button is doing something, even if the mouse is outside the Viewport.
  383. if (HighlightStates.HasFlag (MouseState.PressedOutside) && !WantContinuousButtonPressed)
  384. {
  385. MouseState |= MouseState.PressedOutside;
  386. }
  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="HighlightStates"/> 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.Mouse.MouseGrabView == this && mouseEvent.IsSingleClicked)
  471. {
  472. // We're grabbed. Clicked event comes after the last Release. This is our signal to ungrab
  473. Application.Mouse.UngrabMouse ();
  474. // TODO: Prove we need to unset MouseState.Pressed and MouseState.PressedOutside here
  475. // TODO: There may be perf gains if we don't unset these flags here
  476. MouseState &= ~MouseState.Pressed;
  477. MouseState &= ~MouseState.PressedOutside;
  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 MouseState Handling
  529. private MouseState _mouseState;
  530. /// <summary>
  531. /// Gets the state of the mouse relative to the View. When changed, the <see cref="MouseStateChanged"/>/
  532. /// <see cref="OnMouseStateChanged"/>
  533. /// event will be raised.
  534. /// </summary>
  535. public MouseState MouseState
  536. {
  537. get => _mouseState;
  538. internal set
  539. {
  540. if (_mouseState == value)
  541. {
  542. return;
  543. }
  544. EventArgs<MouseState> args = new (value);
  545. RaiseMouseStateChanged (args);
  546. _mouseState = value;
  547. }
  548. }
  549. /// <summary>
  550. /// Gets or sets which <see cref="MouseState"/> changes should cause the View to change its appearance.
  551. /// </summary>
  552. /// <remarks>
  553. /// <para>
  554. /// <see cref="MouseState.In"/> is set by default, which means the View will be highlighted when the
  555. /// mouse is over it. The default behavior of <see cref="SetAttributeForRole"/>
  556. /// is to use the <see cref="Drawing.VisualRole.Highlight"/> role for the highlight Attribute.
  557. /// </para>
  558. /// <para>
  559. /// <see cref="MouseState.Pressed"/> means the View will be highlighted when the mouse is pressed over it.
  560. /// <see cref="Border"/>'s default behavior is to use
  561. /// the <see cref="VisualRole.Highlight"/> role when the Border is pressed for Arrangement.
  562. /// <see cref="Margin"/>'s default behavior, when shadows are enabled, is to move the shadow providing
  563. /// a pressed effect.
  564. /// </para>
  565. /// <para>
  566. /// <see cref="MouseState.PressedOutside"/> means the View will be highlighted when the mouse was pressed
  567. /// inside it and then moved outside of it, unless <see cref="WantContinuousButtonPressed"/> is set to
  568. /// <see langword="true"/>, in which case the flag has no effect.
  569. /// </para>
  570. /// </remarks>
  571. public MouseState HighlightStates { get; set; }
  572. /// <summary>
  573. /// INTERNAL Raises the <see cref="MouseStateChanged"/> event.
  574. /// </summary>
  575. /// <param name="args"></param>
  576. private void RaiseMouseStateChanged (EventArgs<MouseState> args)
  577. {
  578. //Logging.Debug ($"{Id} - {args.Value} -> {args.Value}");
  579. OnMouseStateChanged (args);
  580. MouseStateChanged?.Invoke (this, args);
  581. }
  582. /// <summary>
  583. /// Called when <see cref="MouseState"/> has changed, indicating the View should be highlighted or not. The <see cref="MouseState"/> passed in the event
  584. /// indicates the highlight style that will be applied.
  585. /// </summary>
  586. protected virtual void OnMouseStateChanged (EventArgs<MouseState> args) { }
  587. /// <summary>
  588. /// RaisedCalled when <see cref="MouseState"/> has changed, indicating the View should be highlighted or not. The <see cref="MouseState"/> passed in the event
  589. /// indicates the highlight style that will be applied.
  590. /// </summary>
  591. public event EventHandler<EventArgs<MouseState>>? MouseStateChanged;
  592. #endregion MouseState Handling
  593. private void DisposeMouse () { }
  594. }