View.Mouse.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. using System.ComponentModel;
  2. using System.Diagnostics;
  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, App?.TimedEvents, App?.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="IMouse.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="IMouse.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 mouse event for this view. This is the main entry point for mouse input handling,
  201. /// called by <see cref="IMouse.RaiseMouseEvent"/> when the mouse interacts with this view.
  202. /// </summary>
  203. /// <remarks>
  204. /// <para>
  205. /// This method orchestrates the complete mouse event handling pipeline:
  206. /// </para>
  207. /// <list type="number">
  208. /// <item>
  209. /// <description>
  210. /// Validates pre-conditions (view must be enabled and visible)
  211. /// </description>
  212. /// </item>
  213. /// <item>
  214. /// <description>
  215. /// Raises <see cref="MouseEvent"/> for low-level handling via <see cref="OnMouseEvent"/>
  216. /// and event subscribers
  217. /// </description>
  218. /// </item>
  219. /// <item>
  220. /// <description>
  221. /// Handles mouse grab scenarios when <see cref="HighlightStates"/> or
  222. /// <see cref="WantContinuousButtonPressed"/> are set (press/release/click)
  223. /// </description>
  224. /// </item>
  225. /// <item>
  226. /// <description>
  227. /// Invokes commands bound to mouse clicks via <see cref="MouseBindings"/>
  228. /// (default: <see cref="Command.Select"/> → <see cref="Selecting"/> event)
  229. /// </description>
  230. /// </item>
  231. /// <item>
  232. /// <description>
  233. /// Handles mouse wheel events via <see cref="OnMouseWheel"/> and <see cref="MouseWheel"/>
  234. /// </description>
  235. /// </item>
  236. /// </list>
  237. /// <para>
  238. /// <strong>Continuous Button Press:</strong> When <see cref="WantContinuousButtonPressed"/> is
  239. /// <see langword="true"/> and the user holds a mouse button down, this method is repeatedly called
  240. /// with <see cref="MouseFlags.Button1Pressed"/> (or Button2-4) events, enabling repeating button
  241. /// behavior (e.g., scroll buttons).
  242. /// </para>
  243. /// <para>
  244. /// <strong>Mouse Grab:</strong> Views with <see cref="HighlightStates"/> or
  245. /// <see cref="WantContinuousButtonPressed"/> enabled automatically grab the mouse on button press,
  246. /// receiving all subsequent mouse events until the button is released, even if the mouse moves
  247. /// outside the view's <see cref="Viewport"/>.
  248. /// </para>
  249. /// <para>
  250. /// Most views should handle mouse clicks by subscribing to the <see cref="Selecting"/> event or
  251. /// overriding <see cref="OnSelecting"/> rather than overriding this method. Override this method
  252. /// only for custom low-level mouse handling (e.g., drag-and-drop).
  253. /// </para>
  254. /// </remarks>
  255. /// <param name="mouseEvent">
  256. /// The mouse event to process. Coordinates in <see cref="MouseEventArgs.Position"/> are relative
  257. /// to the view's <see cref="Viewport"/>.
  258. /// </param>
  259. /// <returns>
  260. /// <see langword="true"/> if the event was handled and should not be propagated;
  261. /// <see langword="false"/> if the event was not handled and should continue propagating;
  262. /// <see langword="null"/> if the view declined to handle the event (e.g., disabled or not visible).
  263. /// </returns>
  264. /// <seealso cref="MouseEvent"/>
  265. /// <seealso cref="OnMouseEvent"/>
  266. /// <seealso cref="MouseBindings"/>
  267. /// <seealso cref="Selecting"/>
  268. /// <seealso cref="WantContinuousButtonPressed"/>
  269. /// <seealso cref="HighlightStates"/>
  270. public bool? NewMouseEvent (MouseEventArgs mouseEvent)
  271. {
  272. // Pre-conditions
  273. if (!Enabled)
  274. {
  275. // A disabled view should not eat mouse events
  276. return false;
  277. }
  278. if (!CanBeVisible (this))
  279. {
  280. return false;
  281. }
  282. if (!WantMousePositionReports && mouseEvent.Flags == MouseFlags.ReportMousePosition)
  283. {
  284. return false;
  285. }
  286. // Cancellable event
  287. if (RaiseMouseEvent (mouseEvent) || mouseEvent.Handled)
  288. {
  289. return true;
  290. }
  291. // Post-Conditions
  292. if (HighlightStates != MouseState.None || WantContinuousButtonPressed)
  293. {
  294. if (WhenGrabbedHandlePressed (mouseEvent))
  295. {
  296. // If we raised Clicked/Activated on the grabbed view, we are done
  297. // regardless of whether the event was handled.
  298. return true;
  299. }
  300. WhenGrabbedHandleReleased (mouseEvent);
  301. if (WhenGrabbedHandleClicked (mouseEvent))
  302. {
  303. return mouseEvent.Handled;
  304. }
  305. }
  306. // We get here if the view did not handle the mouse event via OnMouseEvent/MouseEvent, and
  307. // it did not handle the press/release/clicked events via HandlePress/HandleRelease/HandleClicked
  308. if (mouseEvent.IsSingleDoubleOrTripleClicked)
  309. {
  310. // Logging.Debug ($"{mouseEvent.Flags};{mouseEvent.Position}");
  311. return RaiseCommandsBoundToMouse (mouseEvent);
  312. }
  313. if (mouseEvent.IsWheel)
  314. {
  315. return RaiseMouseWheelEvent (mouseEvent);
  316. }
  317. return false;
  318. }
  319. /// <summary>
  320. /// Raises the <see cref="RaiseMouseEvent"/>/<see cref="MouseEvent"/> event.
  321. /// </summary>
  322. /// <param name="mouseEvent"></param>
  323. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  324. public bool RaiseMouseEvent (MouseEventArgs mouseEvent)
  325. {
  326. // TODO: probably this should be moved elsewhere, please advise
  327. if (WantContinuousButtonPressed && MouseHeldDown != null)
  328. {
  329. if (mouseEvent.IsPressed)
  330. {
  331. MouseHeldDown.Start ();
  332. }
  333. else
  334. {
  335. MouseHeldDown.Stop ();
  336. }
  337. }
  338. if (OnMouseEvent (mouseEvent) || mouseEvent.Handled)
  339. {
  340. return true;
  341. }
  342. MouseEvent?.Invoke (this, mouseEvent);
  343. return mouseEvent.Handled;
  344. }
  345. /// <summary>Called when a mouse event occurs within the view's <see cref="Viewport"/>.</summary>
  346. /// <remarks>
  347. /// <para>
  348. /// The coordinates are relative to <see cref="View.Viewport"/>.
  349. /// </para>
  350. /// </remarks>
  351. /// <param name="mouseEvent"></param>
  352. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  353. protected virtual bool OnMouseEvent (MouseEventArgs mouseEvent) { return false; }
  354. /// <summary>Raised when a mouse event occurs.</summary>
  355. /// <remarks>
  356. /// <para>
  357. /// The coordinates are relative to <see cref="View.Viewport"/>.
  358. /// </para>
  359. /// </remarks>
  360. public event EventHandler<MouseEventArgs>? MouseEvent;
  361. #endregion Low Level Mouse Events
  362. #region WhenGrabbed Handlers
  363. /// <summary>
  364. /// INTERNAL: For cases where the view is grabbed and the mouse is pressed, this method handles the pressed events from
  365. /// the driver.
  366. /// When <see cref="WantContinuousButtonPressed"/> is set, this method will raise the Clicked/Selecting event
  367. /// via <see cref="Command.Select"/> each time it is called (after the first time the mouse is pressed).
  368. /// </summary>
  369. /// <param name="mouseEvent"></param>
  370. /// <returns><see langword="true"/>, if processing should stop, <see langword="false"/> otherwise.</returns>
  371. private bool WhenGrabbedHandlePressed (MouseEventArgs mouseEvent)
  372. {
  373. if (!mouseEvent.IsPressed)
  374. {
  375. return false;
  376. }
  377. Debug.Assert (!mouseEvent.Handled);
  378. mouseEvent.Handled = false;
  379. // If the user has just pressed the mouse, grab the mouse and set focus
  380. if (App is null || App.Mouse.MouseGrabView != this)
  381. {
  382. App?.Mouse.GrabMouse (this);
  383. if (!HasFocus && CanFocus)
  384. {
  385. // Set the focus, but don't invoke Accept
  386. SetFocus ();
  387. }
  388. // This prevents raising Clicked/Selecting the first time the mouse is pressed.
  389. mouseEvent.Handled = true;
  390. }
  391. if (Viewport.Contains (mouseEvent.Position))
  392. {
  393. //Logging.Debug ($"{Id} - Inside Viewport: {MouseState}");
  394. // The mouse is inside.
  395. if (HighlightStates.HasFlag (MouseState.Pressed))
  396. {
  397. MouseState |= MouseState.Pressed;
  398. }
  399. // Always clear PressedOutside when the mouse is pressed inside the Viewport
  400. MouseState &= ~MouseState.PressedOutside;
  401. }
  402. else
  403. {
  404. // Logging.Debug ($"{Id} - Outside Viewport: {MouseState}");
  405. // The mouse is outside.
  406. // When WantContinuousButtonPressed is set we want to keep the mouse state as pressed (e.g. a repeating button).
  407. // This shows the user that the button is doing something, even if the mouse is outside the Viewport.
  408. if (HighlightStates.HasFlag (MouseState.PressedOutside) && !WantContinuousButtonPressed)
  409. {
  410. MouseState |= MouseState.PressedOutside;
  411. }
  412. }
  413. if (!mouseEvent.Handled && WantContinuousButtonPressed && App?.Mouse.MouseGrabView == this)
  414. {
  415. // Ignore the return value here, because the semantics of WhenGrabbedHandlePressed is the return
  416. // value indicates whether processing should stop or not.
  417. RaiseCommandsBoundToMouse (mouseEvent);
  418. return true;
  419. }
  420. return mouseEvent.Handled = true;
  421. }
  422. /// <summary>
  423. /// INTERNAL: For cases where the view is grabbed, this method handles the released events from the driver
  424. /// (typically
  425. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStates"/> are set).
  426. /// </summary>
  427. /// <param name="mouseEvent"></param>
  428. internal void WhenGrabbedHandleReleased (MouseEventArgs mouseEvent)
  429. {
  430. if (App is { } && App.Mouse.MouseGrabView == this)
  431. {
  432. //Logging.Debug ($"{Id} - {MouseState}");
  433. MouseState &= ~MouseState.Pressed;
  434. MouseState &= ~MouseState.PressedOutside;
  435. }
  436. }
  437. /// <summary>
  438. /// INTERNAL: For cases where the view is grabbed, this method handles the click events from the driver
  439. /// (typically
  440. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStates"/> are set).
  441. /// </summary>
  442. /// <param name="mouseEvent"></param>
  443. /// <returns><see langword="true"/>, if processing should stop; <see langword="false"/> otherwise.</returns>
  444. internal bool WhenGrabbedHandleClicked (MouseEventArgs mouseEvent)
  445. {
  446. if (App is null || App.Mouse.MouseGrabView != this || !mouseEvent.IsSingleClicked)
  447. {
  448. return false;
  449. }
  450. // Logging.Debug ($"{mouseEvent.Flags};{mouseEvent.Position}");
  451. // We're grabbed. Clicked event comes after the last Release. This is our signal to ungrab
  452. App?.Mouse.UngrabMouse ();
  453. // TODO: Prove we need to unset MouseState.Pressed and MouseState.PressedOutside here
  454. // TODO: There may be perf gains if we don't unset these flags here
  455. MouseState &= ~MouseState.Pressed;
  456. MouseState &= ~MouseState.PressedOutside;
  457. // If mouse is still in bounds, return false to indicate a click should be raised.
  458. return WantMousePositionReports || !Viewport.Contains (mouseEvent.Position);
  459. }
  460. #endregion WhenGrabbed Handlers
  461. #region Mouse Click Events
  462. /// <summary>
  463. /// INTERNAL API: Converts mouse click events into <see cref="Command"/>s by invoking the commands bound
  464. /// to the mouse button via <see cref="MouseBindings"/>. By default, all mouse clicks are bound to
  465. /// <see cref="Command.Select"/> which raises the <see cref="Selecting"/> event.
  466. /// </summary>
  467. protected bool RaiseCommandsBoundToMouse (MouseEventArgs args)
  468. {
  469. // Pre-conditions
  470. if (!Enabled)
  471. {
  472. // QUESTION: Is this right? Should a disabled view eat mouse clicks?
  473. return args.Handled = false;
  474. }
  475. Debug.Assert (!args.Handled);
  476. // Logging.Debug ($"{args.Flags};{args.Position}");
  477. MouseEventArgs clickedArgs = new ();
  478. clickedArgs.Flags = args.IsPressed
  479. ? args.Flags switch
  480. {
  481. MouseFlags.Button1Pressed => MouseFlags.Button1Clicked,
  482. MouseFlags.Button2Pressed => MouseFlags.Button2Clicked,
  483. MouseFlags.Button3Pressed => MouseFlags.Button3Clicked,
  484. MouseFlags.Button4Pressed => MouseFlags.Button4Clicked,
  485. _ => clickedArgs.Flags
  486. }
  487. : args.Flags;
  488. clickedArgs.Position = args.Position;
  489. clickedArgs.ScreenPosition = args.ScreenPosition;
  490. clickedArgs.View = args.View;
  491. // By default, this will raise Activating/OnActivating - Subclasses can override this via
  492. // ReplaceCommand (Command.Activate ...).
  493. args.Handled = InvokeCommandsBoundToMouse (clickedArgs) == true;
  494. return args.Handled;
  495. }
  496. #endregion Mouse Click Events
  497. #region Mouse Wheel Events
  498. /// <summary>Raises the <see cref="OnMouseWheel"/>/<see cref="MouseWheel"/> event.</summary>
  499. /// <remarks>
  500. /// </remarks>
  501. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  502. protected bool RaiseMouseWheelEvent (MouseEventArgs args)
  503. {
  504. // Pre-conditions
  505. if (!Enabled)
  506. {
  507. // QUESTION: Is this right? Should a disabled view eat mouse?
  508. return args.Handled = false;
  509. }
  510. // Cancellable event
  511. if (OnMouseWheel (args) || args.Handled)
  512. {
  513. return args.Handled;
  514. }
  515. MouseWheel?.Invoke (this, args);
  516. if (args.Handled)
  517. {
  518. return true;
  519. }
  520. args.Handled = InvokeCommandsBoundToMouse (args) == true;
  521. return args.Handled;
  522. }
  523. /// <summary>
  524. /// Called when a mouse wheel event occurs. Check <see cref="MouseEventArgs.Flags"/> to see which wheel was moved was
  525. /// clicked.
  526. /// </summary>
  527. /// <remarks>
  528. /// </remarks>
  529. /// <param name="args"></param>
  530. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  531. protected virtual bool OnMouseWheel (MouseEventArgs args) { return false; }
  532. /// <summary>Raised when a mouse wheel event occurs.</summary>
  533. /// <remarks>
  534. /// </remarks>
  535. public event EventHandler<MouseEventArgs>? MouseWheel;
  536. #endregion Mouse Wheel Events
  537. #region MouseState Handling
  538. private MouseState _mouseState;
  539. /// <summary>
  540. /// Gets the state of the mouse relative to the View. When changed, the <see cref="MouseStateChanged"/>/
  541. /// <see cref="OnMouseStateChanged"/>
  542. /// event will be raised.
  543. /// </summary>
  544. public MouseState MouseState
  545. {
  546. get => _mouseState;
  547. internal set
  548. {
  549. if (_mouseState == value)
  550. {
  551. return;
  552. }
  553. EventArgs<MouseState> args = new (value);
  554. RaiseMouseStateChanged (args);
  555. _mouseState = value;
  556. }
  557. }
  558. /// <summary>
  559. /// Gets or sets which <see cref="MouseState"/> changes should cause the View to change its appearance.
  560. /// </summary>
  561. /// <remarks>
  562. /// <para>
  563. /// <see cref="MouseState.In"/> is set by default, which means the View will be highlighted when the
  564. /// mouse is over it. The default behavior of <see cref="SetAttributeForRole"/>
  565. /// is to use the <see cref="Drawing.VisualRole.Highlight"/> role for the highlight Attribute.
  566. /// </para>
  567. /// <para>
  568. /// <see cref="MouseState.Pressed"/> means the View will be highlighted when the mouse is pressed over it.
  569. /// <see cref="Border"/>'s default behavior is to use
  570. /// the <see cref="VisualRole.Highlight"/> role when the Border is pressed for Arrangement.
  571. /// <see cref="Margin"/>'s default behavior, when shadows are enabled, is to move the shadow providing
  572. /// a pressed effect.
  573. /// </para>
  574. /// <para>
  575. /// <see cref="MouseState.PressedOutside"/> means the View will be highlighted when the mouse was pressed
  576. /// inside it and then moved outside of it, unless <see cref="WantContinuousButtonPressed"/> is set to
  577. /// <see langword="true"/>, in which case the flag has no effect.
  578. /// </para>
  579. /// </remarks>
  580. public MouseState HighlightStates { get; set; }
  581. /// <summary>
  582. /// INTERNAL Raises the <see cref="MouseStateChanged"/> event.
  583. /// </summary>
  584. /// <param name="args"></param>
  585. private void RaiseMouseStateChanged (EventArgs<MouseState> args)
  586. {
  587. //Logging.Debug ($"{Id} - {args.Value} -> {args.Value}");
  588. OnMouseStateChanged (args);
  589. MouseStateChanged?.Invoke (this, args);
  590. }
  591. /// <summary>
  592. /// Called when <see cref="MouseState"/> has changed, indicating the View should be highlighted or not. The
  593. /// <see cref="MouseState"/> passed in the event
  594. /// indicates the highlight style that will be applied.
  595. /// </summary>
  596. protected virtual void OnMouseStateChanged (EventArgs<MouseState> args) { }
  597. /// <summary>
  598. /// Raised when <see cref="MouseState"/> has changed, indicating the View should be highlighted or not. The
  599. /// <see cref="MouseState"/> passed in the event
  600. /// indicates the highlight style that will be applied.
  601. /// </summary>
  602. public event EventHandler<EventArgs<MouseState>>? MouseStateChanged;
  603. #endregion MouseState Handling
  604. private void DisposeMouse ()
  605. {
  606. if (App?.Mouse.MouseGrabView == this)
  607. {
  608. App.Mouse.UngrabMouse ();
  609. }
  610. }
  611. }