1
1
#nullable enable
2
2
using System . ComponentModel ;
3
- using System . Diagnostics ;
4
3
5
4
namespace Terminal . Gui ;
6
5
@@ -45,12 +44,12 @@ public static partial class Application // Mouse handling
45
44
/// <param name="view">View that will receive all mouse events until <see cref="UngrabMouse"/> is invoked.</param>
46
45
public static void GrabMouse ( View ? view )
47
46
{
48
- if ( view is null || OnGrabbingMouse ( view ) )
47
+ if ( view is null || RaiseGrabbingMouseEvent ( view ) )
49
48
{
50
49
return ;
51
50
}
52
51
53
- OnGrabbedMouse ( view ) ;
52
+ RaiseGrabbedMouseEvent ( view ) ;
54
53
MouseGrabView = view ;
55
54
}
56
55
@@ -66,16 +65,16 @@ public static void UngrabMouse ()
66
65
ObjectDisposedException . ThrowIf ( MouseGrabView . WasDisposed , MouseGrabView ) ;
67
66
#endif
68
67
69
- if ( ! OnUnGrabbingMouse ( MouseGrabView ) )
68
+ if ( ! RaiseUnGrabbingMouseEvent ( MouseGrabView ) )
70
69
{
71
70
View view = MouseGrabView ;
72
71
MouseGrabView = null ;
73
- OnUnGrabbedMouse ( view ) ;
72
+ RaiseUnGrabbedMouseEvent ( view ) ;
74
73
}
75
74
}
76
75
77
76
/// <exception cref="Exception">A delegate callback throws an exception.</exception>
78
- private static bool OnGrabbingMouse ( View ? view )
77
+ private static bool RaiseGrabbingMouseEvent ( View ? view )
79
78
{
80
79
if ( view is null )
81
80
{
@@ -89,7 +88,7 @@ private static bool OnGrabbingMouse (View? view)
89
88
}
90
89
91
90
/// <exception cref="Exception">A delegate callback throws an exception.</exception>
92
- private static bool OnUnGrabbingMouse ( View ? view )
91
+ private static bool RaiseUnGrabbingMouseEvent ( View ? view )
93
92
{
94
93
if ( view is null )
95
94
{
@@ -103,7 +102,7 @@ private static bool OnUnGrabbingMouse (View? view)
103
102
}
104
103
105
104
/// <exception cref="Exception">A delegate callback throws an exception.</exception>
106
- private static void OnGrabbedMouse ( View ? view )
105
+ private static void RaiseGrabbedMouseEvent ( View ? view )
107
106
{
108
107
if ( view is null )
109
108
{
@@ -114,7 +113,7 @@ private static void OnGrabbedMouse (View? view)
114
113
}
115
114
116
115
/// <exception cref="Exception">A delegate callback throws an exception.</exception>
117
- private static void OnUnGrabbedMouse ( View ? view )
116
+ private static void RaiseUnGrabbedMouseEvent ( View ? view )
118
117
{
119
118
if ( view is null )
120
119
{
@@ -124,20 +123,14 @@ private static void OnUnGrabbedMouse (View? view)
124
123
UnGrabbedMouse ? . Invoke ( view , new ( view ) ) ;
125
124
}
126
125
127
- /// <summary>Event fired when a mouse move or click occurs. Coordinates are screen relative.</summary>
128
- /// <remarks>
129
- /// <para>
130
- /// Use this event to receive mouse events in screen coordinates. Use <see cref="MouseEvent"/> to
131
- /// receive mouse events relative to a <see cref="View.Viewport"/>.
132
- /// </para>
133
- /// <para>The <see cref="MouseEvent.View"/> will contain the <see cref="View"/> that contains the mouse coordinates.</para>
134
- /// </remarks>
135
- public static event EventHandler < MouseEvent > ? MouseEvent ;
136
126
137
- /// <summary>Called when a mouse event is raised by the driver.</summary>
127
+ /// <summary>
128
+ /// INTERNAL API: Called when a mouse event is raised by the driver. Determines the view under the mouse and
129
+ /// calls the appropriate View mouse event handlers.
130
+ /// </summary>
138
131
/// <remarks>This method can be used to simulate a mouse event, e.g. in unit tests.</remarks>
139
132
/// <param name="mouseEvent">The mouse event with coordinates relative to the screen.</param>
140
- internal static void OnMouseEvent ( MouseEvent mouseEvent )
133
+ internal static void RaiseMouseEvent ( MouseEventArgs mouseEvent )
141
134
{
142
135
_lastMousePosition = mouseEvent . ScreenPosition ;
143
136
@@ -177,9 +170,6 @@ internal static void OnMouseEvent (MouseEvent mouseEvent)
177
170
return ;
178
171
}
179
172
180
- // We can combine this into the switch expression to reduce cognitive complexity even more and likely
181
- // avoid one or two of these checks in the process, as well.
182
-
183
173
WantContinuousButtonPressedView = deepestViewUnderMouse switch
184
174
{
185
175
{ WantContinuousButtonPressed : true } => deepestViewUnderMouse ,
@@ -194,7 +184,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent)
194
184
}
195
185
196
186
// Create a view-relative mouse event to send to the view that is under the mouse.
197
- MouseEvent ? viewMouseEvent ;
187
+ MouseEventArgs ? viewMouseEvent ;
198
188
199
189
if ( deepestViewUnderMouse is Adornment adornment )
200
190
{
@@ -208,7 +198,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent)
208
198
View = deepestViewUnderMouse
209
199
} ;
210
200
}
211
- else if ( deepestViewUnderMouse . ViewportToScreen ( Rectangle . Empty with { Size = deepestViewUnderMouse . Viewport . Size } ) . Contains ( mouseEvent . Position ) )
201
+ else if ( deepestViewUnderMouse . ViewportToScreen ( Rectangle . Empty with { Size = deepestViewUnderMouse . Viewport . Size } ) . Contains ( mouseEvent . ScreenPosition ) )
212
202
{
213
203
Point viewportLocation = deepestViewUnderMouse . ScreenToViewport ( mouseEvent . ScreenPosition ) ;
214
204
@@ -224,7 +214,7 @@ internal static void OnMouseEvent (MouseEvent mouseEvent)
224
214
{
225
215
// The mouse was outside any View's Viewport.
226
216
227
- // Debug.Fail ("This should never happen. If it does please file an Issue!!");
217
+ // Debug.Fail ("This should never happen. If it does please file an Issue!!");
228
218
229
219
return ;
230
220
}
@@ -261,7 +251,29 @@ internal static void OnMouseEvent (MouseEvent mouseEvent)
261
251
}
262
252
}
263
253
264
- internal static bool HandleMouseGrab ( View ? deepestViewUnderMouse , MouseEvent mouseEvent )
254
+
255
+ #pragma warning disable CS1574 // XML comment has cref attribute that could not be resolved
256
+ /// <summary>
257
+ /// Raised when a mouse event occurs. Can be cancelled by setting <see cref="MouseEventArgs.Handled"/> to <see langword="true"/>.
258
+ /// </summary>
259
+ /// <remarks>
260
+ /// <para>
261
+ /// <see cref="MouseEventArgs.ScreenPosition"/> coordinates are screen-relative.
262
+ /// </para>
263
+ /// <para>
264
+ /// <see cref="MouseEventArgs.View"/> will be the deepest view under the under the mouse.
265
+ /// </para>
266
+ /// <para>
267
+ /// <see cref="MouseEventArgs.Position"/> coordinates are view-relative. Only valid if <see cref="MouseEventArgs.View"/> is set.
268
+ /// </para>
269
+ /// <para>
270
+ /// Use this evento to handle mouse events at the application level, before View-specific handling.
271
+ /// </para>
272
+ /// </remarks>
273
+ public static event EventHandler < MouseEventArgs > ? MouseEvent ;
274
+ #pragma warning restore CS1574 // XML comment has cref attribute that could not be resolved
275
+
276
+ internal static bool HandleMouseGrab ( View ? deepestViewUnderMouse , MouseEventArgs mouseEvent )
265
277
{
266
278
if ( MouseGrabView is { } )
267
279
{
@@ -276,7 +288,7 @@ internal static bool HandleMouseGrab (View? deepestViewUnderMouse, MouseEvent mo
276
288
// The coordinates are relative to the Bounds of the view that grabbed the mouse.
277
289
Point frameLoc = MouseGrabView . ScreenToViewport ( mouseEvent . ScreenPosition ) ;
278
290
279
- var viewRelativeMouseEvent = new MouseEvent
291
+ var viewRelativeMouseEvent = new MouseEventArgs
280
292
{
281
293
Position = frameLoc ,
282
294
Flags = mouseEvent . Flags ,
@@ -303,7 +315,6 @@ internal static bool HandleMouseGrab (View? deepestViewUnderMouse, MouseEvent mo
303
315
304
316
internal static readonly List < View ? > _cachedViewsUnderMouse = new ( ) ;
305
317
306
- // TODO: Refactor MouseEnter/LeaveEvents to not take MouseEvent param.
307
318
/// <summary>
308
319
/// INTERNAL: Raises the MouseEnter and MouseLeave events for the views that are under the mouse.
309
320
/// </summary>
0 commit comments