2
0

Application.Mouse.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.ComponentModel;
  2. namespace Terminal.Gui.App;
  3. public static partial class Application // Mouse handling
  4. {
  5. /// <summary>
  6. /// Gets the most recent position of the mouse.
  7. /// </summary>
  8. public static Point? GetLastMousePosition () { return Mouse.GetLastMousePosition (); }
  9. /// <summary>Disable or enable the mouse. The mouse is enabled by default.</summary>
  10. [ConfigurationProperty (Scope = typeof (SettingsScope))]
  11. public static bool IsMouseDisabled
  12. {
  13. get => Mouse.IsMouseDisabled;
  14. set => Mouse.IsMouseDisabled = value;
  15. }
  16. /// <summary>
  17. /// Gets the <see cref="IMouse"/> instance that manages mouse event handling and state.
  18. /// </summary>
  19. /// <remarks>
  20. /// <para>
  21. /// This property provides access to mouse-related functionality in a way that supports
  22. /// parallel test execution by avoiding static state.
  23. /// </para>
  24. /// <para>
  25. /// New code should use <c>Application.Mouse</c> instead of the static properties and methods
  26. /// for better testability. Legacy static properties like <see cref="IsMouseDisabled"/> and
  27. /// <see cref="GetLastMousePosition"/> are retained for backward compatibility.
  28. /// </para>
  29. /// </remarks>
  30. public static IMouse Mouse => ApplicationImpl.Instance.Mouse;
  31. #pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
  32. /// <summary>
  33. /// Raised when a mouse event occurs. Can be cancelled by setting <see cref="HandledEventArgs.Handled"/> to
  34. /// <see langword="true"/>.
  35. /// </summary>
  36. /// <remarks>
  37. /// <para>
  38. /// <see cref="MouseEventArgs.ScreenPosition"/> coordinates are screen-relative.
  39. /// </para>
  40. /// <para>
  41. /// <see cref="MouseEventArgs.View"/> will be the deepest view under the mouse.
  42. /// </para>
  43. /// <para>
  44. /// <see cref="MouseEventArgs.Position"/> coordinates are view-relative. Only valid if
  45. /// <see cref="MouseEventArgs.View"/> is set.
  46. /// </para>
  47. /// <para>
  48. /// Use this even to handle mouse events at the application level, before View-specific handling.
  49. /// </para>
  50. /// </remarks>
  51. public static event EventHandler<MouseEventArgs>? MouseEvent
  52. {
  53. add => Mouse.MouseEvent += value;
  54. remove => Mouse.MouseEvent -= value;
  55. }
  56. #pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
  57. /// <summary>
  58. /// INTERNAL: Holds the non-<see cref="ViewportSettingsFlags.TransparentMouse"/> views that are currently under the
  59. /// mouse.
  60. /// </summary>
  61. internal static List<View?> CachedViewsUnderMouse => Mouse.CachedViewsUnderMouse;
  62. /// <summary>
  63. /// INTERNAL API: Holds the last mouse position.
  64. /// </summary>
  65. internal static Point? LastMousePosition
  66. {
  67. get => Mouse.LastMousePosition;
  68. set => Mouse.LastMousePosition = value;
  69. }
  70. /// <summary>
  71. /// INTERNAL: Raises the MouseEnter and MouseLeave events for the views that are under the mouse.
  72. /// </summary>
  73. /// <param name="screenPosition">The position of the mouse.</param>
  74. /// <param name="currentViewsUnderMouse">The most recent result from GetViewsUnderLocation().</param>
  75. internal static void RaiseMouseEnterLeaveEvents (Point screenPosition, List<View?> currentViewsUnderMouse)
  76. {
  77. Mouse.RaiseMouseEnterLeaveEvents (screenPosition, currentViewsUnderMouse);
  78. }
  79. /// <summary>
  80. /// INTERNAL API: Called when a mouse event is raised by the driver. Determines the view under the mouse and
  81. /// calls the appropriate View mouse event handlers.
  82. /// </summary>
  83. /// <remarks>This method can be used to simulate a mouse event, e.g. in unit tests.</remarks>
  84. /// <param name="mouseEvent">The mouse event with coordinates relative to the screen.</param>
  85. internal static void RaiseMouseEvent (MouseEventArgs mouseEvent)
  86. {
  87. Mouse.RaiseMouseEvent (mouseEvent);
  88. }
  89. }