Application.Mouse.cs 4.0 KB

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