2
0

ViewMouse.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System.ComponentModel;
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. public partial class View
  5. {
  6. /// <summary>
  7. /// Gets or sets whether the <see cref="View"/> will be highlighted visually while the mouse button is
  8. /// pressed.
  9. /// </summary>
  10. public bool HighlightOnPress { get; set; }
  11. /// <summary>Gets or sets whether the <see cref="View"/> wants continuous button pressed events.</summary>
  12. public virtual bool WantContinuousButtonPressed { get; set; }
  13. /// <summary>Gets or sets whether the <see cref="View"/> wants mouse position reports.</summary>
  14. /// <value><see langword="true"/> if mouse position reports are wanted; otherwise, <see langword="false"/>.</value>
  15. public virtual bool WantMousePositionReports { get; set; }
  16. /// <summary>
  17. /// Called when the mouse enters the View's <see cref="Viewport"/>. The view will now receive mouse events until the mouse leaves
  18. /// the view. At which time, <see cref="OnMouseLeave(Gui.MouseEvent)"/> will be called.
  19. /// </summary>
  20. /// <remarks>
  21. /// The coordinates are relative to <see cref="Viewport"/>.
  22. /// </remarks>
  23. /// <param name="mouseEvent"></param>
  24. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  25. protected internal virtual bool OnMouseEnter (MouseEvent mouseEvent)
  26. {
  27. if (!Enabled)
  28. {
  29. return true;
  30. }
  31. if (!CanBeVisible (this))
  32. {
  33. return false;
  34. }
  35. var args = new MouseEventEventArgs (mouseEvent);
  36. MouseEnter?.Invoke (this, args);
  37. return args.Handled;
  38. }
  39. /// <summary>Event fired when the mouse moves into the View's <see cref="Bounds"/>.</summary>
  40. public event EventHandler<MouseEventEventArgs> MouseEnter;
  41. /// <summary>
  42. /// Called when the mouse has moved out of the View's <see cref="Viewport"/>. The view will no longer receive mouse events (until the
  43. /// mouse moves within the view again and <see cref="OnMouseEnter(Gui.MouseEvent)"/> is called).
  44. /// </summary>
  45. /// <remarks>
  46. /// The coordinates are relative to <see cref="Viewport"/>.
  47. /// </remarks>
  48. /// <param name="mouseEvent"></param>
  49. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  50. protected internal virtual bool OnMouseLeave (MouseEvent mouseEvent)
  51. {
  52. if (!Enabled)
  53. {
  54. return true;
  55. }
  56. if (!CanBeVisible (this))
  57. {
  58. return false;
  59. }
  60. var args = new MouseEventEventArgs (mouseEvent);
  61. MouseLeave?.Invoke (this, args);
  62. return args.Handled;
  63. }
  64. /// <summary>Event fired when the mouse leaves the View's <see cref="Bounds"/>.</summary>
  65. public event EventHandler<MouseEventEventArgs> MouseLeave;
  66. /// <summary>
  67. /// Processes a <see cref="MouseEvent"/>. This method is called by <see cref="Application.OnMouseEvent"/> when a mouse
  68. /// event occurs.
  69. /// </summary>
  70. /// <remarks>
  71. /// <para>
  72. /// A view must be both enabled and visible to receive mouse events.
  73. /// </para>
  74. /// <para>
  75. /// This method calls <see cref="OnMouseEvent"/> to process the event. If the event is not handled, and one of the
  76. /// mouse buttons was clicked, it calls <see cref="OnMouseClick"/> to process the click.
  77. /// </para>
  78. /// <para>
  79. /// If <see cref="HighlightOnPress"/> is <see langword="true"/>, the view will be highlighted when the mouse is
  80. /// pressed.
  81. /// See <see cref="EnableHighlight"/> and <see cref="DisableHighlight"/> for more information.
  82. /// </para>
  83. /// <para>
  84. /// If <see cref="WantContinuousButtonPressed"/> is <see langword="true"/>, the <see cref="OnMouseClick"/> event
  85. /// will be invoked repeatedly while the button is pressed.
  86. /// </para>
  87. /// </remarks>
  88. /// <param name="mouseEvent"></param>
  89. /// <returns><see langword="true"/> if the event was handled, <see langword="false"/> otherwise.</returns>
  90. public bool? NewMouseEvent (MouseEvent mouseEvent)
  91. {
  92. if (!Enabled)
  93. {
  94. // A disabled view should not eat mouse events
  95. return false;
  96. }
  97. if (!CanBeVisible (this))
  98. {
  99. return false;
  100. }
  101. if (OnMouseEvent (mouseEvent))
  102. {
  103. // Technically mouseEvent.Handled should already be true if implementers of OnMouseEvent
  104. // follow the rules. But we'll update it just in case.
  105. return mouseEvent.Handled = true;
  106. }
  107. if ((HighlightOnPress || WantContinuousButtonPressed) && Highlight (mouseEvent))
  108. {
  109. Debug.Assert (mouseEvent.Handled);
  110. return mouseEvent.Handled;
  111. }
  112. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
  113. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
  114. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
  115. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked))
  116. {
  117. return OnMouseClick (new (mouseEvent));
  118. }
  119. return false;
  120. }
  121. /// <summary>
  122. /// Highlight the view when the mouse is pressed.
  123. /// </summary>
  124. /// <remarks>
  125. /// <para>
  126. /// Set <see cref="HighlightOnPress"/> to <see langword="true"/> to have the view highlighted when the mouse is
  127. /// pressed.
  128. /// </para>
  129. /// <para>
  130. /// Calls <see cref="OnEnablingHighlight"/> which fires the <see cref="EnablingHighlight"/> event.
  131. /// </para>
  132. /// <para>
  133. /// Calls <see cref="OnDisablingHighlight"/> which fires the <see cref="DisablingHighlight"/> event.
  134. /// </para>
  135. /// </remarks>
  136. /// <param name="mouseEvent"></param>
  137. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  138. private bool Highlight (MouseEvent mouseEvent)
  139. {
  140. if (Application.MouseGrabView == this
  141. && (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)
  142. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Clicked)
  143. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)
  144. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Clicked)))
  145. {
  146. // We're grabbed. Clicked event comes after the last Release. This is our signal to ungrab
  147. Application.UngrabMouse ();
  148. DisableHighlight ();
  149. // If mouse is still in bounds, click
  150. if (!WantContinuousButtonPressed && Viewport.Contains (mouseEvent.X, mouseEvent.Y))
  151. {
  152. return OnMouseClick (mouseEvent);
  153. }
  154. return mouseEvent.Handled = true;
  155. }
  156. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed)
  157. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Pressed)
  158. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Pressed)
  159. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Pressed))
  160. {
  161. // The first time we get pressed event, grab the mouse and set focus
  162. if (Application.MouseGrabView != this)
  163. {
  164. Application.GrabMouse (this);
  165. if (CanFocus)
  166. {
  167. // Set the focus, but don't invoke Accept
  168. SetFocus ();
  169. }
  170. args.Handled = true;
  171. }
  172. if (Viewport.Contains (mouseEvent.X, mouseEvent.Y))
  173. {
  174. EnableHighlight ();
  175. }
  176. else
  177. {
  178. DisableHighlight ();
  179. }
  180. if (WantContinuousButtonPressed && Application.MouseGrabView == this)
  181. {
  182. // If this is not the first pressed event, click
  183. return OnMouseClick (new (mouseEvent));
  184. }
  185. return mouseEvent.Handled = true;
  186. }
  187. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released)
  188. || mouseEvent.Flags.HasFlag (MouseFlags.Button2Released)
  189. || mouseEvent.Flags.HasFlag (MouseFlags.Button3Released)
  190. || mouseEvent.Flags.HasFlag (MouseFlags.Button4Released))
  191. {
  192. if (Application.MouseGrabView == this)
  193. {
  194. DisableHighlight ();
  195. }
  196. return mouseEvent.Handled = true;
  197. }
  198. return mouseEvent.Handled;
  199. }
  200. [CanBeNull]
  201. private ColorScheme _savedHighlightColorScheme;
  202. /// <summary>
  203. /// Enables the highlight for the view. Called from OnMouseEvent.
  204. /// </summary>
  205. public void EnableHighlight ()
  206. {
  207. if (OnEnablingHighlight () == true)
  208. {
  209. return;
  210. }
  211. if (_savedHighlightColorScheme is null && ColorScheme is { })
  212. {
  213. _savedHighlightColorScheme ??= ColorScheme;
  214. if (CanFocus)
  215. {
  216. // TODO: Make the inverted color configurable
  217. var cs = new ColorScheme (ColorScheme)
  218. {
  219. // For Buttons etc...
  220. Focus = new (ColorScheme.Normal.Foreground, ColorScheme.Focus.Background),
  221. // For Adornments
  222. Normal = new (ColorScheme.Focus.Foreground, ColorScheme.Normal.Background)
  223. };
  224. ColorScheme = cs;
  225. }
  226. else
  227. {
  228. var cs = new ColorScheme (ColorScheme)
  229. {
  230. // For Buttons etc... that can't focus (like up/down).
  231. Normal = new (ColorScheme.Focus.Background, ColorScheme.Normal.Foreground)
  232. };
  233. ColorScheme = cs;
  234. }
  235. }
  236. }
  237. /// <summary>
  238. /// Fired when the view is highlighted. Set <see cref="CancelEventArgs.Cancel"/> to <see langword="true"/>
  239. /// to implement a custom highlight scheme or prevent the view from being highlighted.
  240. /// </summary>
  241. public event EventHandler<CancelEventArgs> EnablingHighlight;
  242. /// <summary>
  243. /// Called when the view is to be highlighted.
  244. /// </summary>
  245. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  246. protected virtual bool? OnEnablingHighlight ()
  247. {
  248. CancelEventArgs args = new ();
  249. EnablingHighlight?.Invoke (this, args);
  250. return args.Cancel;
  251. }
  252. /// <summary>
  253. /// Disables the highlight for the view. Called from OnMouseEvent.
  254. /// </summary>
  255. public void DisableHighlight ()
  256. {
  257. if (OnDisablingHighlight () == true)
  258. {
  259. return;
  260. }
  261. // Unhighlight
  262. if (_savedHighlightColorScheme is { })
  263. {
  264. ColorScheme = _savedHighlightColorScheme;
  265. _savedHighlightColorScheme = null;
  266. }
  267. }
  268. /// <summary>
  269. /// Fired when the view is no longer to be highlighted. Set <see cref="CancelEventArgs.Cancel"/> to
  270. /// <see langword="true"/>
  271. /// to implement a custom highlight scheme or prevent the view from being highlighted.
  272. /// </summary>
  273. public event EventHandler<CancelEventArgs> DisablingHighlight;
  274. /// <summary>
  275. /// Called when the view is no longer to be highlighted.
  276. /// </summary>
  277. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  278. protected virtual bool? OnDisablingHighlight ()
  279. {
  280. CancelEventArgs args = new ();
  281. DisablingHighlight?.Invoke (this, args);
  282. return args.Cancel;
  283. }
  284. /// <summary>Called when a mouse event occurs within the view's <see cref="Bounds"/>.</summary>
  285. /// <remarks>
  286. /// <para>
  287. /// The coordinates are relative to <see cref="View.Bounds"/>.
  288. /// </para>
  289. /// </remarks>
  290. /// <param name="mouseEvent"></param>
  291. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  292. protected internal virtual bool OnMouseEvent (MouseEvent mouseEvent)
  293. {
  294. var args = new MouseEventEventArgs (mouseEvent);
  295. MouseEvent?.Invoke (this, args);
  296. return args.Handled;
  297. }
  298. /// <summary>Event fired when a mouse event occurs.</summary>
  299. /// <remarks>
  300. /// <para>
  301. /// The coordinates are relative to <see cref="View.Bounds"/>.
  302. /// </para>
  303. /// </remarks>
  304. public event EventHandler<MouseEventEventArgs> MouseEvent;
  305. /// <summary>Invokes the MouseClick event.</summary>
  306. /// <remarks>
  307. /// <para>
  308. /// Called when the mouse is either clicked or double-clicked. Check
  309. /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
  310. /// </para>
  311. /// </remarks>
  312. /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  313. protected bool OnMouseClick (MouseEventEventArgs args)
  314. {
  315. if (!Enabled)
  316. {
  317. // QUESTION: Is this right? Should a disabled view eat mouse clicks?
  318. return args.Handled = true;
  319. }
  320. MouseClick?.Invoke (this, args);
  321. if (args.Handled)
  322. {
  323. return true;
  324. }
  325. if (!HasFocus && CanFocus)
  326. {
  327. args.Handled = true;
  328. SetFocus ();
  329. }
  330. return args.Handled;
  331. }
  332. /// <summary>Event fired when a mouse click occurs.</summary>
  333. /// <remarks>
  334. /// <para>
  335. /// Fired when the mouse is either clicked or double-clicked. Check
  336. /// <see cref="MouseEvent.Flags"/> to see which button was clicked.
  337. /// </para>
  338. /// <para>
  339. /// The coordinates are relative to <see cref="View.Bounds"/>.
  340. /// </para>
  341. /// </remarks>
  342. public event EventHandler<MouseEventEventArgs> MouseClick;
  343. }