View.Mouse.cs 25 KB

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