ViewMouse.cs 21 KB

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