View.Mouse.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. using System.ComponentModel;
  2. namespace Terminal.Gui.ViewBase;
  3. public partial class View // Mouse APIs
  4. {
  5. /// <summary>
  6. /// Handles <see cref="WantContinuousButtonPressed"/>, we have detected a button
  7. /// down in the view and have grabbed the mouse.
  8. /// </summary>
  9. public IMouseHeldDown? MouseHeldDown { get; set; }
  10. /// <summary>Gets the mouse bindings for this view.</summary>
  11. public MouseBindings MouseBindings { get; internal set; } = null!;
  12. private void SetupMouse ()
  13. {
  14. MouseHeldDown = new MouseHeldDown (this, App?.TimedEvents, App?.Mouse);
  15. MouseBindings = new ();
  16. // TODO: Should the default really work with any button or just button1?
  17. MouseBindings.Add (MouseFlags.Button1Clicked, Command.Select);
  18. MouseBindings.Add (MouseFlags.Button2Clicked, Command.Select);
  19. MouseBindings.Add (MouseFlags.Button3Clicked, Command.Select);
  20. MouseBindings.Add (MouseFlags.Button4Clicked, Command.Select);
  21. MouseBindings.Add (MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl, Command.Select);
  22. }
  23. /// <summary>
  24. /// Invokes the Commands bound to the MouseFlags specified by <paramref name="mouseEventArgs"/>.
  25. /// <para>See <see href="../docs/mouse.md">for an overview of Terminal.Gui mouse APIs.</see></para>
  26. /// </summary>
  27. /// <param name="mouseEventArgs">The mouse event passed.</param>
  28. /// <returns>
  29. /// <see langword="null"/> if no command was invoked; input processing should continue.
  30. /// <see langword="false"/> if at least one command was invoked and was not handled (or cancelled); input processing
  31. /// should continue.
  32. /// <see langword="true"/> if at least one command was invoked and handled (or cancelled); input processing should
  33. /// stop.
  34. /// </returns>
  35. protected bool? InvokeCommandsBoundToMouse (MouseEventArgs mouseEventArgs)
  36. {
  37. if (!MouseBindings.TryGet (mouseEventArgs.Flags, out MouseBinding binding))
  38. {
  39. return null;
  40. }
  41. binding.MouseEventArgs = mouseEventArgs;
  42. return InvokeCommands (binding.Commands, binding);
  43. }
  44. #region MouseEnterLeave
  45. /// <summary>
  46. /// INTERNAL Called by <see cref="IMouse.RaiseMouseEvent"/> when the mouse moves over the View's
  47. /// <see cref="Frame"/>.
  48. /// <see cref="MouseLeave"/> will
  49. /// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
  50. /// that View will also receive MouseEnter/Leave events.
  51. /// </summary>
  52. /// <param name="eventArgs"></param>
  53. /// <returns>
  54. /// <see langword="true"/> if the event was canceled, <see langword="false"/> if not, <see langword="null"/> if the
  55. /// view is not visible. Cancelling the event
  56. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  57. /// </returns>
  58. internal bool? NewMouseEnterEvent (CancelEventArgs eventArgs)
  59. {
  60. // Pre-conditions
  61. if (!CanBeVisible (this))
  62. {
  63. return null;
  64. }
  65. // Cancellable event
  66. if (OnMouseEnter (eventArgs))
  67. {
  68. return true;
  69. }
  70. MouseEnter?.Invoke (this, eventArgs);
  71. if (eventArgs.Cancel)
  72. {
  73. return true;
  74. }
  75. MouseState |= MouseState.In;
  76. if (HighlightStates != MouseState.None)
  77. {
  78. SetNeedsDraw ();
  79. }
  80. return false;
  81. }
  82. /// <summary>
  83. /// Called when the mouse moves over the View's <see cref="Frame"/> and no other non-SubView occludes it.
  84. /// <see cref="MouseLeave"/> will
  85. /// be raised when the mouse is no longer over the <see cref="Frame"/>.
  86. /// </summary>
  87. /// <remarks>
  88. /// <para>
  89. /// A view must be visible to receive Enter events (Leave events are always received).
  90. /// </para>
  91. /// <para>
  92. /// If the event is cancelled, the mouse event will not be propagated to other views and <see cref="MouseEnter"/>
  93. /// will not be raised.
  94. /// </para>
  95. /// <para>
  96. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  97. /// </para>
  98. /// <para>
  99. /// See <see cref="MouseState"/> for more information.
  100. /// </para>
  101. /// </remarks>
  102. /// <param name="eventArgs"></param>
  103. /// <returns>
  104. /// <see langword="true"/> if the event was canceled, <see langword="false"/> if not. Cancelling the event
  105. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  106. /// </returns>
  107. protected virtual bool OnMouseEnter (CancelEventArgs eventArgs) { return false; }
  108. /// <summary>
  109. /// Raised when the mouse moves over the View's <see cref="Frame"/>. <see cref="MouseLeave"/> will
  110. /// be raised when the mouse is no longer over the <see cref="Frame"/>. If another View occludes this View, the
  111. /// that View will also receive MouseEnter/Leave events.
  112. /// </summary>
  113. /// <remarks>
  114. /// <para>
  115. /// A view must be visible to receive Enter events (Leave events are always received).
  116. /// </para>
  117. /// <para>
  118. /// If the event is cancelled, the mouse event will not be propagated to other views.
  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. /// Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/> if the event was canceled,
  125. /// <see langword="false"/> if not. Cancelling the event
  126. /// prevents Views higher in the visible hierarchy from receiving Enter/Leave events.
  127. /// </para>
  128. /// <para>
  129. /// See <see cref="MouseState"/> for more information.
  130. /// </para>
  131. /// </remarks>
  132. public event EventHandler<CancelEventArgs>? MouseEnter;
  133. /// <summary>
  134. /// INTERNAL Called by <see cref="IMouse.RaiseMouseEvent"/> when the mouse leaves <see cref="Frame"/>, or is
  135. /// occluded
  136. /// by another non-SubView.
  137. /// </summary>
  138. /// <remarks>
  139. /// <para>
  140. /// This method calls <see cref="OnMouseLeave"/> and raises the <see cref="MouseLeave"/> event.
  141. /// </para>
  142. /// <para>
  143. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  144. /// </para>
  145. /// <para>
  146. /// See <see cref="MouseState"/> for more information.
  147. /// </para>
  148. /// </remarks>
  149. internal void NewMouseLeaveEvent ()
  150. {
  151. // Pre-conditions
  152. // Non-cancellable event
  153. OnMouseLeave ();
  154. MouseLeave?.Invoke (this, EventArgs.Empty);
  155. MouseState &= ~MouseState.In;
  156. // TODO: Should we also MouseState &= ~MouseState.Pressed; ??
  157. if (HighlightStates != MouseState.None)
  158. {
  159. SetNeedsDraw ();
  160. }
  161. }
  162. /// <summary>
  163. /// Called when the mouse moves outside View's <see cref="Frame"/>, or is occluded by another non-SubView.
  164. /// </summary>
  165. /// <remarks>
  166. /// <para>
  167. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  168. /// </para>
  169. /// <para>
  170. /// See <see cref="MouseState"/> for more information.
  171. /// </para>
  172. /// </remarks>
  173. protected virtual void OnMouseLeave () { }
  174. /// <summary>
  175. /// Raised when the mouse moves outside View's <see cref="Frame"/>, or is occluded by another non-SubView.
  176. /// </summary>
  177. /// <remarks>
  178. /// <para>
  179. /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's <see cref="Thickness"/>.
  180. /// </para>
  181. /// <para>
  182. /// See <see cref="MouseState"/> for more information.
  183. /// </para>
  184. /// </remarks>
  185. public event EventHandler? MouseLeave;
  186. #endregion MouseEnterLeave
  187. #region Low Level Mouse Events
  188. /// <summary>
  189. /// Gets or sets whether the <see cref="View"/> wants continuous button pressed events. When set to
  190. /// <see langword="true"/>,
  191. /// and the user presses and holds the mouse button, <see cref="NewMouseEvent"/> will be
  192. /// repeatedly called with the same <see cref="MouseFlags"/> for as long as the mouse button remains pressed.
  193. /// </summary>
  194. public bool WantContinuousButtonPressed { get; set; }
  195. /// <summary>Gets or sets whether the <see cref="View"/> wants mouse position reports.</summary>
  196. /// <value><see langword="true"/> if mouse position reports are wanted; otherwise, <see langword="false"/>.</value>
  197. public bool WantMousePositionReports { get; set; }
  198. /// <summary>
  199. /// Processes a new <see cref="MouseEvent"/>. This method is called by <see cref="IMouse.RaiseMouseEvent"/> when a
  200. /// mouse
  201. /// event occurs.
  202. /// </summary>
  203. /// <remarks>
  204. /// <para>
  205. /// A view must be both enabled and visible to receive mouse events.
  206. /// </para>
  207. /// <para>
  208. /// This method raises <see cref="RaiseMouseEvent"/>/<see cref="MouseEvent"/>; if not handled, and one of the
  209. /// mouse buttons was clicked, the <see cref="RaiseMouseClickEvent"/>/<see cref="MouseClick"/> event will be raised
  210. /// </para>
  211. /// <para>
  212. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, and the user presses and holds the
  213. /// mouse button, <see cref="NewMouseEvent"/> will be repeatedly called with the same <see cref="MouseFlags"/> for
  214. /// as long as the mouse button remains pressed.
  215. /// </para>
  216. /// </remarks>
  217. /// <param name="mouseEvent"></param>
  218. /// <returns><see langword="true"/> if the event was handled, <see langword="false"/> otherwise.</returns>
  219. public bool? NewMouseEvent (MouseEventArgs mouseEvent)
  220. {
  221. // Pre-conditions
  222. if (!Enabled)
  223. {
  224. // A disabled view should not eat mouse events
  225. return false;
  226. }
  227. if (!CanBeVisible (this))
  228. {
  229. return false;
  230. }
  231. if (!WantMousePositionReports && mouseEvent.Flags == MouseFlags.ReportMousePosition)
  232. {
  233. return false;
  234. }
  235. // Cancellable event
  236. if (RaiseMouseEvent (mouseEvent) || mouseEvent.Handled)
  237. {
  238. return true;
  239. }
  240. // Post-Conditions
  241. if (HighlightStates != MouseState.None || WantContinuousButtonPressed)
  242. {
  243. if (WhenGrabbedHandlePressed (mouseEvent))
  244. {
  245. return mouseEvent.Handled;
  246. }
  247. if (WhenGrabbedHandleReleased (mouseEvent))
  248. {
  249. return mouseEvent.Handled;
  250. }
  251. if (WhenGrabbedHandleClicked (mouseEvent))
  252. {
  253. return mouseEvent.Handled;
  254. }
  255. }
  256. // We get here if the view did not handle the mouse event via OnMouseEvent/MouseEvent, and
  257. // it did not handle the press/release/clicked events via HandlePress/HandleRelease/HandleClicked
  258. if (mouseEvent.IsSingleDoubleOrTripleClicked)
  259. {
  260. return RaiseMouseClickEvent (mouseEvent);
  261. }
  262. if (mouseEvent.IsWheel)
  263. {
  264. return RaiseMouseWheelEvent (mouseEvent);
  265. }
  266. return false;
  267. }
  268. /// <summary>
  269. /// Raises the <see cref="RaiseMouseEvent"/>/<see cref="MouseEvent"/> event.
  270. /// </summary>
  271. /// <param name="mouseEvent"></param>
  272. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  273. public bool RaiseMouseEvent (MouseEventArgs mouseEvent)
  274. {
  275. // TODO: probably this should be moved elsewhere, please advise
  276. if (WantContinuousButtonPressed && MouseHeldDown != null)
  277. {
  278. if (mouseEvent.IsPressed)
  279. {
  280. MouseHeldDown.Start ();
  281. }
  282. else
  283. {
  284. MouseHeldDown.Stop ();
  285. }
  286. }
  287. if (OnMouseEvent (mouseEvent) || mouseEvent.Handled)
  288. {
  289. return true;
  290. }
  291. MouseEvent?.Invoke (this, mouseEvent);
  292. return mouseEvent.Handled;
  293. }
  294. /// <summary>Called when a mouse event occurs within the view's <see cref="Viewport"/>.</summary>
  295. /// <remarks>
  296. /// <para>
  297. /// The coordinates are relative to <see cref="View.Viewport"/>.
  298. /// </para>
  299. /// </remarks>
  300. /// <param name="mouseEvent"></param>
  301. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  302. protected virtual bool OnMouseEvent (MouseEventArgs mouseEvent) { return false; }
  303. /// <summary>Raised when a mouse event occurs.</summary>
  304. /// <remarks>
  305. /// <para>
  306. /// The coordinates are relative to <see cref="View.Viewport"/>.
  307. /// </para>
  308. /// </remarks>
  309. public event EventHandler<MouseEventArgs>? MouseEvent;
  310. #endregion Low Level Mouse Events
  311. #region Mouse Pressed Events
  312. /// <summary>
  313. /// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event
  314. /// (typically
  315. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStates"/> are set).
  316. /// </summary>
  317. /// <remarks>
  318. /// Marked internal just to support unit tests
  319. /// </remarks>
  320. /// <param name="mouseEvent"></param>
  321. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  322. internal bool WhenGrabbedHandleReleased (MouseEventArgs mouseEvent)
  323. {
  324. mouseEvent.Handled = false;
  325. if (mouseEvent.IsReleased)
  326. {
  327. if (App?.Mouse.MouseGrabView == this)
  328. {
  329. //Logging.Debug ($"{Id} - {MouseState}");
  330. MouseState &= ~MouseState.Pressed;
  331. MouseState &= ~MouseState.PressedOutside;
  332. }
  333. return mouseEvent.Handled = true;
  334. }
  335. return false;
  336. }
  337. /// <summary>
  338. /// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the released event
  339. /// (typically
  340. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStates"/> 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 (App?.Mouse.MouseGrabView != this)
  356. {
  357. App?.Mouse.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. //Logging.Debug ($"{Id} - Inside Viewport: {MouseState}");
  368. // The mouse is inside.
  369. if (HighlightStates.HasFlag (MouseState.Pressed))
  370. {
  371. MouseState |= MouseState.Pressed;
  372. }
  373. // Always clear PressedOutside when the mouse is pressed inside the Viewport
  374. MouseState &= ~MouseState.PressedOutside;
  375. }
  376. if (!Viewport.Contains (mouseEvent.Position))
  377. {
  378. // Logging.Debug ($"{Id} - Outside Viewport: {MouseState}");
  379. // The mouse is outside.
  380. // When WantContinuousButtonPressed is set we want to keep the mouse state as pressed (e.g. a repeating button).
  381. // This shows the user that the button is doing something, even if the mouse is outside the Viewport.
  382. if (HighlightStates.HasFlag (MouseState.PressedOutside) && !WantContinuousButtonPressed)
  383. {
  384. MouseState |= MouseState.PressedOutside;
  385. }
  386. }
  387. return mouseEvent.Handled = true;
  388. }
  389. return false;
  390. }
  391. #endregion Mouse Pressed Events
  392. #region Mouse Click Events
  393. /// <summary>Raises the <see cref="OnMouseClick"/>/<see cref="MouseClick"/> event.</summary>
  394. /// <remarks>
  395. /// <para>
  396. /// Called when the mouse is either clicked or double-clicked.
  397. /// </para>
  398. /// <para>
  399. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be invoked on every mouse event
  400. /// where
  401. /// the mouse button is pressed.
  402. /// </para>
  403. /// </remarks>
  404. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  405. protected bool RaiseMouseClickEvent (MouseEventArgs args)
  406. {
  407. // Pre-conditions
  408. if (!Enabled)
  409. {
  410. // QUESTION: Is this right? Should a disabled view eat mouse clicks?
  411. return args.Handled = false;
  412. }
  413. // Cancellable event
  414. if (OnMouseClick (args) || args.Handled)
  415. {
  416. return args.Handled;
  417. }
  418. MouseClick?.Invoke (this, args);
  419. if (args.Handled)
  420. {
  421. return true;
  422. }
  423. // Post-conditions
  424. // By default, this will raise Selecting/OnSelecting - Subclasses can override this via AddCommand (Command.Select ...).
  425. args.Handled = InvokeCommandsBoundToMouse (args) == true;
  426. return args.Handled;
  427. }
  428. /// <summary>
  429. /// Called when a mouse click occurs. Check <see cref="MouseEventArgs.Flags"/> to see which button was clicked.
  430. /// </summary>
  431. /// <remarks>
  432. /// <para>
  433. /// Called when the mouse is either clicked or double-clicked.
  434. /// </para>
  435. /// <para>
  436. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be called on every mouse event
  437. /// where
  438. /// the mouse button is pressed.
  439. /// </para>
  440. /// </remarks>
  441. /// <param name="args"></param>
  442. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  443. protected virtual bool OnMouseClick (MouseEventArgs args) { return false; }
  444. /// <summary>Raised when a mouse click occurs.</summary>
  445. /// <remarks>
  446. /// <para>
  447. /// Raised when the mouse is either clicked or double-clicked.
  448. /// </para>
  449. /// <para>
  450. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, will be raised on every mouse event
  451. /// where
  452. /// the mouse button is pressed.
  453. /// </para>
  454. /// </remarks>
  455. public event EventHandler<MouseEventArgs>? MouseClick;
  456. /// <summary>
  457. /// INTERNAL For cases where the view is grabbed and the mouse is clicked, this method handles the click event
  458. /// (typically
  459. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStates"/> are set).
  460. /// </summary>
  461. /// <remarks>
  462. /// Marked internal just to support unit tests
  463. /// </remarks>
  464. /// <param name="mouseEvent"></param>
  465. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  466. internal bool WhenGrabbedHandleClicked (MouseEventArgs mouseEvent)
  467. {
  468. mouseEvent.Handled = false;
  469. if (App?.Mouse.MouseGrabView == this && mouseEvent.IsSingleClicked)
  470. {
  471. // We're grabbed. Clicked event comes after the last Release. This is our signal to ungrab
  472. App?.Mouse.UngrabMouse ();
  473. // TODO: Prove we need to unset MouseState.Pressed and MouseState.PressedOutside here
  474. // TODO: There may be perf gains if we don't unset these flags here
  475. MouseState &= ~MouseState.Pressed;
  476. MouseState &= ~MouseState.PressedOutside;
  477. // If mouse is still in bounds, generate a click
  478. if (!WantMousePositionReports && Viewport.Contains (mouseEvent.Position))
  479. {
  480. return RaiseMouseClickEvent (mouseEvent);
  481. }
  482. return mouseEvent.Handled = true;
  483. }
  484. return false;
  485. }
  486. #endregion Mouse Clicked Events
  487. #region Mouse Wheel Events
  488. /// <summary>Raises the <see cref="OnMouseWheel"/>/<see cref="MouseWheel"/> event.</summary>
  489. /// <remarks>
  490. /// </remarks>
  491. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  492. protected bool RaiseMouseWheelEvent (MouseEventArgs args)
  493. {
  494. // Pre-conditions
  495. if (!Enabled)
  496. {
  497. // QUESTION: Is this right? Should a disabled view eat mouse?
  498. return args.Handled = false;
  499. }
  500. // Cancellable event
  501. if (OnMouseWheel (args) || args.Handled)
  502. {
  503. return args.Handled;
  504. }
  505. MouseWheel?.Invoke (this, args);
  506. if (args.Handled)
  507. {
  508. return true;
  509. }
  510. args.Handled = InvokeCommandsBoundToMouse (args) == true;
  511. return args.Handled;
  512. }
  513. /// <summary>
  514. /// Called when a mouse wheel event occurs. Check <see cref="MouseEventArgs.Flags"/> to see which wheel was moved was
  515. /// clicked.
  516. /// </summary>
  517. /// <remarks>
  518. /// </remarks>
  519. /// <param name="args"></param>
  520. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  521. protected virtual bool OnMouseWheel (MouseEventArgs args) { return false; }
  522. /// <summary>Raised when a mouse wheel event occurs.</summary>
  523. /// <remarks>
  524. /// </remarks>
  525. public event EventHandler<MouseEventArgs>? MouseWheel;
  526. #endregion Mouse Wheel Events
  527. #region MouseState Handling
  528. private MouseState _mouseState;
  529. /// <summary>
  530. /// Gets the state of the mouse relative to the View. When changed, the <see cref="MouseStateChanged"/>/
  531. /// <see cref="OnMouseStateChanged"/>
  532. /// event will be raised.
  533. /// </summary>
  534. public MouseState MouseState
  535. {
  536. get => _mouseState;
  537. internal set
  538. {
  539. if (_mouseState == value)
  540. {
  541. return;
  542. }
  543. EventArgs<MouseState> args = new (value);
  544. RaiseMouseStateChanged (args);
  545. _mouseState = value;
  546. }
  547. }
  548. /// <summary>
  549. /// Gets or sets which <see cref="MouseState"/> changes should cause the View to change its appearance.
  550. /// </summary>
  551. /// <remarks>
  552. /// <para>
  553. /// <see cref="MouseState.In"/> is set by default, which means the View will be highlighted when the
  554. /// mouse is over it. The default behavior of <see cref="SetAttributeForRole"/>
  555. /// is to use the <see cref="Drawing.VisualRole.Highlight"/> role for the highlight Attribute.
  556. /// </para>
  557. /// <para>
  558. /// <see cref="MouseState.Pressed"/> means the View will be highlighted when the mouse is pressed over it.
  559. /// <see cref="Border"/>'s default behavior is to use
  560. /// the <see cref="VisualRole.Highlight"/> role when the Border is pressed for Arrangement.
  561. /// <see cref="Margin"/>'s default behavior, when shadows are enabled, is to move the shadow providing
  562. /// a pressed effect.
  563. /// </para>
  564. /// <para>
  565. /// <see cref="MouseState.PressedOutside"/> means the View will be highlighted when the mouse was pressed
  566. /// inside it and then moved outside of it, unless <see cref="WantContinuousButtonPressed"/> is set to
  567. /// <see langword="true"/>, in which case the flag has no effect.
  568. /// </para>
  569. /// </remarks>
  570. public MouseState HighlightStates { get; set; }
  571. /// <summary>
  572. /// INTERNAL Raises the <see cref="MouseStateChanged"/> event.
  573. /// </summary>
  574. /// <param name="args"></param>
  575. private void RaiseMouseStateChanged (EventArgs<MouseState> args)
  576. {
  577. //Logging.Debug ($"{Id} - {args.Value} -> {args.Value}");
  578. OnMouseStateChanged (args);
  579. MouseStateChanged?.Invoke (this, args);
  580. }
  581. /// <summary>
  582. /// Called when <see cref="MouseState"/> has changed, indicating the View should be highlighted or not. The <see cref="MouseState"/> passed in the event
  583. /// indicates the highlight style that will be applied.
  584. /// </summary>
  585. protected virtual void OnMouseStateChanged (EventArgs<MouseState> args) { }
  586. /// <summary>
  587. /// RaisedCalled when <see cref="MouseState"/> has changed, indicating the View should be highlighted or not. The <see cref="MouseState"/> passed in the event
  588. /// indicates the highlight style that will be applied.
  589. /// </summary>
  590. public event EventHandler<EventArgs<MouseState>>? MouseStateChanged;
  591. #endregion MouseState Handling
  592. private void DisposeMouse () { }
  593. }