View.Mouse.cs 20 KB

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