ViewMouse.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. using System.ComponentModel;
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Describes the highlight style of a view.
  5. /// </summary>
  6. [Flags]
  7. public enum HighlightStyle
  8. {
  9. /// <summary>
  10. /// No highlight.
  11. /// </summary>
  12. None = 0,
  13. #if HOVER
  14. /// <summary>
  15. /// The mouse is hovering over the view.
  16. /// </summary>
  17. Hover = 1,
  18. #endif
  19. /// <summary>
  20. /// The mouse is pressed within the <see cref="View.Bounds"/>.
  21. /// </summary>
  22. Pressed = 2,
  23. /// <summary>
  24. /// The mouse is pressed but moved outside the <see cref="View.Bounds"/>.
  25. /// </summary>
  26. PressedOutside = 4
  27. }
  28. /// <summary>
  29. /// Event arguments for the <see cref="View.Highlight"/> event.
  30. /// </summary>
  31. public class HighlightEventArgs : CancelEventArgs
  32. {
  33. public HighlightEventArgs (HighlightStyle style)
  34. {
  35. HighlightStyle = style;
  36. }
  37. /// <summary>
  38. /// The highlight style.
  39. /// </summary>
  40. public HighlightStyle HighlightStyle { get; }
  41. }
  42. public partial class View
  43. {
  44. /// <summary>
  45. /// Gets or sets whether the <see cref="View"/> will be highlighted visually while the mouse button is
  46. /// pressed.
  47. /// </summary>
  48. public HighlightStyle HighlightStyle { get; set; }
  49. /// <summary>Gets or sets whether the <see cref="View"/> wants continuous button pressed events.</summary>
  50. public virtual bool WantContinuousButtonPressed { get; set; }
  51. /// <summary>Gets or sets whether the <see cref="View"/> wants mouse position reports.</summary>
  52. /// <value><see langword="true"/> if mouse position reports are wanted; otherwise, <see langword="false"/>.</value>
  53. public virtual bool WantMousePositionReports { get; set; }
  54. /// <summary>
  55. /// Called by <see cref="Application.OnMouseEvent"/> when the mouse enters <see cref="Bounds"/>. The view will
  56. /// then receive mouse events until <see cref="NewMouseLeaveEvent"/> is called indicating the mouse has left
  57. /// the view.
  58. /// </summary>
  59. /// <remarks>
  60. /// <para>
  61. /// A view must be both enabled and visible to receive mouse events.
  62. /// </para>
  63. /// <para>
  64. /// This method calls <see cref="OnMouseEnter"/> to fire the event.
  65. /// </para>
  66. /// <para>
  67. /// See <see cref="SetHighlight"/> for more information.
  68. /// </para>
  69. /// </remarks>
  70. /// <param name="mouseEvent"></param>
  71. /// <returns><see langword="true"/> if the event was handled, <see langword="false"/> otherwise.</returns>
  72. internal bool? NewMouseEnterEvent (MouseEvent mouseEvent)
  73. {
  74. if (!Enabled)
  75. {
  76. return true;
  77. }
  78. if (!CanBeVisible (this))
  79. {
  80. return false;
  81. }
  82. if (OnMouseEnter (mouseEvent) == true)
  83. {
  84. return true;
  85. }
  86. #if HOVER
  87. if (HighlightStyle.HasFlag(HighlightStyle.Hover))
  88. {
  89. if (SetHighlight (HighlightStyle.Hover))
  90. {
  91. return true;
  92. }
  93. }
  94. #endif
  95. return false;
  96. }
  97. /// <summary>
  98. /// Called by <see cref="NewMouseEvent"/> when the mouse enters <see cref="Bounds"/>. The view will
  99. /// then receive mouse events until <see cref="OnMouseLeave"/> is called indicating the mouse has left
  100. /// the view.
  101. /// </summary>
  102. /// <remarks>
  103. /// <para>
  104. /// Override this method or subscribe to <see cref="MouseEnter"/> to change the default enter behavior.
  105. /// </para>
  106. /// <para>
  107. /// The coordinates are relative to <see cref="View.Bounds"/>.
  108. /// </para>
  109. /// </remarks>
  110. /// <param name="mouseEvent"></param>
  111. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  112. protected internal virtual bool? OnMouseEnter (MouseEvent mouseEvent)
  113. {
  114. var args = new MouseEventEventArgs (mouseEvent);
  115. MouseEnter?.Invoke (this, args);
  116. return args.Handled;
  117. }
  118. /// <summary>Event fired when the mouse moves into the View's <see cref="Bounds"/>.</summary>
  119. public event EventHandler<MouseEventEventArgs> MouseEnter;
  120. /// <summary>
  121. /// Called by <see cref="Application.OnMouseEvent"/> when the mouse leaves <see cref="Bounds"/>. The view will
  122. /// then no longer receive mouse events.
  123. /// </summary>
  124. /// <remarks>
  125. /// <para>
  126. /// A view must be both enabled and visible to receive mouse events.
  127. /// </para>
  128. /// <para>
  129. /// This method calls <see cref="OnMouseLeave"/> to fire the event.
  130. /// </para>
  131. /// <para>
  132. /// See <see cref="SetHighlight"/> for more information.
  133. /// </para>
  134. /// </remarks>
  135. /// <param name="mouseEvent"></param>
  136. /// <returns><see langword="true"/> if the event was handled, <see langword="false"/> otherwise.</returns>
  137. internal bool? NewMouseLeaveEvent (MouseEvent mouseEvent)
  138. {
  139. if (!Enabled)
  140. {
  141. return true;
  142. }
  143. if (!CanBeVisible (this))
  144. {
  145. return false;
  146. }
  147. if (OnMouseLeave (mouseEvent) == true)
  148. {
  149. return true;
  150. }
  151. #if HOVER
  152. if (HighlightStyle.HasFlag (HighlightStyle.Hover))
  153. {
  154. SetHighlight (HighlightStyle.None);
  155. }
  156. #endif
  157. return false;
  158. }
  159. /// <summary>
  160. /// Called by <see cref="NewMouseEvent"/> when a mouse leaves <see cref="Bounds"/>. The view will
  161. /// no longer receive mouse events.
  162. /// </summary>
  163. /// <remarks>
  164. /// <para>
  165. /// Override this method or subscribe to <see cref="MouseEnter"/> to change the default leave behavior.
  166. /// </para>
  167. /// <para>
  168. /// The coordinates are relative to <see cref="View.Bounds"/>.
  169. /// </para>
  170. /// </remarks>
  171. /// <param name="mouseEvent"></param>
  172. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  173. protected internal virtual bool OnMouseLeave (MouseEvent mouseEvent)
  174. {
  175. if (!Enabled)
  176. {
  177. return true;
  178. }
  179. if (!CanBeVisible (this))
  180. {
  181. return false;
  182. }
  183. var args = new MouseEventEventArgs (mouseEvent);
  184. MouseLeave?.Invoke (this, args);
  185. return args.Handled;
  186. }
  187. /// <summary>Event fired when the mouse leaves the View's <see cref="Bounds"/>.</summary>
  188. public event EventHandler<MouseEventEventArgs> MouseLeave;
  189. /// <summary>
  190. /// Processes a <see cref="MouseEvent"/>. This method is called by <see cref="Application.OnMouseEvent"/> when a mouse
  191. /// event occurs.
  192. /// </summary>
  193. /// <remarks>
  194. /// <para>
  195. /// A view must be both enabled and visible to receive mouse events.
  196. /// </para>
  197. /// <para>
  198. /// This method calls <see cref="OnMouseEvent"/> to process the event. If the event is not handled, and one of the
  199. /// mouse buttons was clicked, it calls <see cref="OnMouseClick"/> to process the click.
  200. /// </para>
  201. /// <para>
  202. /// See <see cref="SetHighlight"/> and <see cref="DisableHighlight"/> for more information.
  203. /// </para>
  204. /// <para>
  205. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, the <see cref="OnMouseClick"/> event
  206. /// will be invoked repeatedly while the button is pressed.
  207. /// </para>
  208. /// </remarks>
  209. /// <param name="mouseEvent"></param>
  210. /// <returns><see langword="true"/> if the event was handled, <see langword="false"/> otherwise.</returns>
  211. public bool? NewMouseEvent (MouseEvent mouseEvent)
  212. {
  213. if (!Enabled)
  214. {
  215. // A disabled view should not eat mouse events
  216. return false;
  217. }
  218. if (!CanBeVisible (this))
  219. {
  220. return false;
  221. }
  222. if (OnMouseEvent (mouseEvent))
  223. {
  224. // Technically mouseEvent.Handled should already be true if implementers of OnMouseEvent
  225. // follow the rules. But we'll update it just in case.
  226. return mouseEvent.Handled = true;
  227. }
  228. if (HighlightStyle != Gui.HighlightStyle.None || WantContinuousButtonPressed)
  229. {
  230. if (HandlePressed (mouseEvent))
  231. {
  232. return mouseEvent.Handled;
  233. }
  234. if (HandleReleased (mouseEvent))
  235. {
  236. return mouseEvent.Handled;
  237. }
  238. if (HandleClicked (mouseEvent))
  239. {
  240. return mouseEvent.Handled;
  241. }
  242. }
  243. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
  244. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
  245. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
  246. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)
  247. || mouseEvent.Flags.HasFlag (MouseFlags.Button1DoubleClicked)
  248. || mouseEvent.Flags.HasFlag (MouseFlags.Button2DoubleClicked)
  249. || mouseEvent.Flags.HasFlag (MouseFlags.Button3DoubleClicked)
  250. || mouseEvent.Flags.HasFlag (MouseFlags.Button4DoubleClicked)
  251. || mouseEvent.Flags.HasFlag (MouseFlags.Button1TripleClicked)
  252. || mouseEvent.Flags.HasFlag (MouseFlags.Button2TripleClicked)
  253. || mouseEvent.Flags.HasFlag (MouseFlags.Button3TripleClicked)
  254. || mouseEvent.Flags.HasFlag (MouseFlags.Button4TripleClicked)
  255. )
  256. {
  257. // If it's a click, and we didn't handle it, then we'll call OnMouseClick
  258. // We get here if the view did not handle the mouse event via OnMouseEvent/MouseEvent and
  259. // it did not handle the press/release/clicked events via HandlePress/HandleRelease/HandleClicked
  260. return OnMouseClick (new (mouseEvent));
  261. }
  262. return false;
  263. }
  264. /// <summary>
  265. /// For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
  266. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  267. /// </summary>
  268. /// <remarks>
  269. /// <para>
  270. /// Marked internal just to support unit tests
  271. /// </para>
  272. /// </remarks>
  273. /// <param name="mouseEvent"></param>
  274. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  275. private bool HandlePressed (MouseEvent mouseEvent)
  276. {
  277. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)
  278. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed)
  279. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed)
  280. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Pressed))
  281. {
  282. // The first time we get pressed event, grab the mouse and set focus
  283. if (Application.MouseGrabView != this)
  284. {
  285. Application.GrabMouse (this);
  286. if (CanFocus)
  287. {
  288. // Set the focus, but don't invoke Accept
  289. SetFocus ();
  290. }
  291. }
  292. if (Bounds.Contains (mouseEvent.X, mouseEvent.Y))
  293. {
  294. if (SetHighlight (HighlightStyle.HasFlag (HighlightStyle.Pressed) ? HighlightStyle.Pressed : HighlightStyle.None) == true)
  295. {
  296. return true;
  297. }
  298. }
  299. else
  300. {
  301. if (SetHighlight (HighlightStyle.HasFlag (HighlightStyle.PressedOutside) ? HighlightStyle.PressedOutside : HighlightStyle.None) == true)
  302. {
  303. return true;
  304. }
  305. }
  306. if (WantContinuousButtonPressed && Application.MouseGrabView == this)
  307. {
  308. // If this is not the first pressed event, click
  309. return OnMouseClick (new (mouseEvent));
  310. }
  311. return mouseEvent.Handled = true;
  312. }
  313. return false;
  314. }
  315. /// <summary>
  316. /// For cases where the view is grabbed and the mouse is clicked, this method handles the released event (typically
  317. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  318. /// </summary>
  319. /// <remarks>
  320. /// Marked internal just to support unit tests
  321. /// </remarks>
  322. /// <param name="mouseEvent"></param>
  323. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  324. internal bool HandleReleased (MouseEvent mouseEvent)
  325. {
  326. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released)
  327. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Released)
  328. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Released)
  329. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Released))
  330. {
  331. if (Application.MouseGrabView == this)
  332. {
  333. SetHighlight (HighlightStyle.None);
  334. }
  335. return mouseEvent.Handled = true;
  336. }
  337. return false;
  338. }
  339. /// <summary>
  340. /// For cases where the view is grabbed and the mouse is clicked, this method handles the click event (typically
  341. /// when <see cref="WantContinuousButtonPressed"/> or <see cref="HighlightStyle"/> are set).
  342. /// </summary>
  343. /// <remarks>
  344. /// Marked internal just to support unit tests
  345. /// </remarks>
  346. /// <param name="mouseEvent"></param>
  347. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  348. internal bool HandleClicked (MouseEvent mouseEvent)
  349. {
  350. if (Application.MouseGrabView == this
  351. && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
  352. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
  353. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
  354. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)))
  355. {
  356. // We're grabbed. Clicked event comes after the last Release. This is our signal to ungrab
  357. Application.UngrabMouse ();
  358. if (SetHighlight (HighlightStyle.None))
  359. {
  360. return true;
  361. }
  362. // If mouse is still in bounds, click
  363. if (!WantContinuousButtonPressed && Bounds.Contains (mouseEvent.X, mouseEvent.Y))
  364. {
  365. return OnMouseClick (new (mouseEvent));
  366. }
  367. return mouseEvent.Handled = true;
  368. }
  369. return false;
  370. }
  371. [CanBeNull]
  372. private ColorScheme _savedHighlightColorScheme;
  373. /// <summary>
  374. /// Enables the highlight for the view when the mouse is pressed. Called from OnMouseEvent.
  375. /// </summary>
  376. /// <remarks>
  377. /// <para>
  378. /// Set <see cref="HighlightStyle"/> to have the view highlighted based on the mouse.
  379. /// </para>
  380. /// <para>
  381. /// Calls <see cref="OnHighlight"/> which fires the <see cref="Highlight"/> event.
  382. /// </para>
  383. /// <para>
  384. /// Marked internal just to support unit tests
  385. /// </para>
  386. /// </remarks>
  387. /// <returns><see langword="true"/>, if the Highlight event was handled, <see langword="false"/> otherwise.</returns>
  388. internal bool SetHighlight (HighlightStyle style)
  389. {
  390. // TODO: Make the highlight colors configurable
  391. // Enable override via virtual method and/or event
  392. if (OnHighlight (style) == true)
  393. {
  394. return true;
  395. }
  396. #if HOVER
  397. if (style.HasFlag (HighlightStyle.Hover))
  398. {
  399. if (_savedHighlightColorScheme is null && ColorScheme is { })
  400. {
  401. _savedHighlightColorScheme ??= ColorScheme;
  402. var cs = new ColorScheme (ColorScheme)
  403. {
  404. Normal = GetFocusColor (),
  405. HotNormal = ColorScheme.HotFocus
  406. };
  407. ColorScheme = cs;
  408. }
  409. return true;
  410. }
  411. #endif
  412. if (style.HasFlag (HighlightStyle.Pressed) || style.HasFlag (HighlightStyle.PressedOutside))
  413. {
  414. if (_savedHighlightColorScheme is null && ColorScheme is { })
  415. {
  416. _savedHighlightColorScheme ??= ColorScheme;
  417. if (CanFocus)
  418. {
  419. var cs = new ColorScheme (ColorScheme)
  420. {
  421. Focus = new (ColorScheme.Focus.Foreground.GetHighlightColor (), ColorScheme.Focus.Background.GetHighlightColor ()),
  422. };
  423. ColorScheme = cs;
  424. }
  425. else
  426. {
  427. var cs = new ColorScheme (ColorScheme)
  428. {
  429. // For Buttons etc... that can't focus (like up/down).
  430. Normal = new (ColorScheme.Focus.Background, ColorScheme.Normal.Foreground)
  431. };
  432. ColorScheme = cs;
  433. }
  434. return true;
  435. }
  436. }
  437. if (style == HighlightStyle.None)
  438. {
  439. // Unhighlight
  440. if (_savedHighlightColorScheme is { })
  441. {
  442. ColorScheme = _savedHighlightColorScheme;
  443. _savedHighlightColorScheme = null;
  444. }
  445. }
  446. return false;
  447. }
  448. /// <summary>
  449. /// Fired when the view is highlighted. Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/>
  450. /// to implement a custom highlight scheme or prevent the view from being highlighted.
  451. /// </summary>
  452. public event EventHandler<HighlightEventArgs> Highlight;
  453. /// <summary>
  454. /// Called when the view is to be highlighted.
  455. /// </summary>
  456. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  457. protected virtual bool? OnHighlight (HighlightStyle highlight)
  458. {
  459. HighlightEventArgs args = new (highlight);
  460. Highlight?.Invoke (this, args);
  461. return args.Cancel;
  462. }
  463. /// <summary>Called when a mouse event occurs within the view's <see cref="Bounds"/>.</summary>
  464. /// <remarks>
  465. /// <para>
  466. /// The coordinates are relative to <see cref="View.Bounds"/>.
  467. /// </para>
  468. /// </remarks>
  469. /// <param name="mouseEvent"></param>
  470. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  471. protected internal virtual bool OnMouseEvent (MouseEvent mouseEvent)
  472. {
  473. var args = new MouseEventEventArgs (mouseEvent);
  474. MouseEvent?.Invoke (this, args);
  475. return args.Handled;
  476. }
  477. /// <summary>Event fired when a mouse event occurs.</summary>
  478. /// <remarks>
  479. /// <para>
  480. /// The coordinates are relative to <see cref="View.Bounds"/>.
  481. /// </para>
  482. /// </remarks>
  483. public event EventHandler<MouseEventEventArgs> MouseEvent;
  484. /// <summary>Invokes the MouseClick event.</summary>
  485. /// <remarks>
  486. /// <para>
  487. /// Called when the mouse is either clicked or double-clicked. Check
  488. /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
  489. /// </para>
  490. /// </remarks>
  491. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  492. protected bool OnMouseClick (MouseEventEventArgs args)
  493. {
  494. if (!Enabled)
  495. {
  496. // QUESTION: Is this right? Should a disabled view eat mouse clicks?
  497. args.Handled = true;
  498. return true;
  499. }
  500. MouseClick?.Invoke (this, args);
  501. if (args.Handled)
  502. {
  503. return true;
  504. }
  505. if (!HasFocus && CanFocus)
  506. {
  507. args.Handled = true;
  508. SetFocus ();
  509. }
  510. return args.Handled;
  511. }
  512. /// <summary>Event fired when a mouse click occurs.</summary>
  513. /// <remarks>
  514. /// <para>
  515. /// Fired when the mouse is either clicked or double-clicked. Check
  516. /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
  517. /// </para>
  518. /// <para>
  519. /// The coordinates are relative to <see cref="View.Bounds"/>.
  520. /// </para>
  521. /// </remarks>
  522. public event EventHandler<MouseEventEventArgs> MouseClick;
  523. }