ViewMouse.cs 20 KB

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