@@ -38,21 +38,22 @@ pub use web_time::Instant;
38
38
use ui_events:: {
39
39
keyboard:: KeyboardEvent ,
40
40
pointer:: {
41
- PointerButtonEvent , PointerEvent , PointerId , PointerInfo , PointerScrollEvent , PointerState ,
42
- PointerType , PointerUpdate ,
41
+ PointerButtonEvent , PointerEvent , PointerId , PointerInfo , PointerRelativeFrame ,
42
+ PointerRelativeMotion , PointerScrollEvent , PointerState , PointerType , PointerUpdate ,
43
43
} ,
44
44
ScrollDelta ,
45
45
} ;
46
46
use winit:: {
47
- event:: { ElementState , Force , MouseScrollDelta , Touch , TouchPhase , WindowEvent } ,
47
+ dpi:: PhysicalPosition ,
48
+ event:: { DeviceEvent , ElementState , Force , MouseScrollDelta , Touch , TouchPhase , WindowEvent } ,
48
49
keyboard:: ModifiersState ,
49
50
} ;
50
51
51
52
/// Manages stateful transformations of winit [`WindowEvent`].
52
53
///
53
54
/// Store a single instance of this per window, then call [`WindowEventReducer::reduce`]
54
55
/// on each [`WindowEvent`] for that window.
55
- /// Use the [`WindowEventTranslation `] value to receive [`PointerEvent`]s and [`KeyboardEvent`]s.
56
+ /// Use the [`EventTranslation `] value to receive [`PointerEvent`]s and [`KeyboardEvent`]s.
56
57
///
57
58
/// This handles:
58
59
/// - [`ModifiersChanged`][`WindowEvent::ModifiersChanged`]
@@ -78,7 +79,7 @@ pub struct WindowEventReducer {
78
79
#[ allow( clippy:: cast_possible_truncation) ]
79
80
impl WindowEventReducer {
80
81
/// Process a [`WindowEvent`].
81
- pub fn reduce ( & mut self , we : & WindowEvent ) -> Option < WindowEventTranslation > {
82
+ pub fn reduce ( & mut self , we : & WindowEvent ) -> Option < EventTranslation > {
82
83
const PRIMARY_MOUSE : PointerInfo = PointerInfo {
83
84
pointer_id : Some ( PointerId :: PRIMARY ) ,
84
85
// TODO: Maybe transmute device.
@@ -98,19 +99,19 @@ impl WindowEventReducer {
98
99
self . primary_state . modifiers = keyboard:: from_winit_modifier_state ( self . modifiers ) ;
99
100
None
100
101
}
101
- WindowEvent :: KeyboardInput { event, .. } => Some ( WindowEventTranslation :: Keyboard (
102
+ WindowEvent :: KeyboardInput { event, .. } => Some ( EventTranslation :: Keyboard (
102
103
keyboard:: from_winit_keyboard_event ( event. clone ( ) , self . modifiers ) ,
103
104
) ) ,
104
- WindowEvent :: CursorEntered { .. } => Some ( WindowEventTranslation :: Pointer (
105
+ WindowEvent :: CursorEntered { .. } => Some ( EventTranslation :: Pointer (
105
106
PointerEvent :: Enter ( PRIMARY_MOUSE ) ,
106
107
) ) ,
107
- WindowEvent :: CursorLeft { .. } => Some ( WindowEventTranslation :: Pointer (
108
- PointerEvent :: Leave ( PRIMARY_MOUSE ) ,
109
- ) ) ,
108
+ WindowEvent :: CursorLeft { .. } => Some ( EventTranslation :: Pointer ( PointerEvent :: Leave (
109
+ PRIMARY_MOUSE ,
110
+ ) ) ) ,
110
111
WindowEvent :: CursorMoved { position, .. } => {
111
112
self . primary_state . position = * position;
112
113
113
- Some ( WindowEventTranslation :: Pointer ( self . counter . attach_count (
114
+ Some ( EventTranslation :: Pointer ( self . counter . attach_count (
114
115
PointerEvent :: Move ( PointerUpdate {
115
116
pointer : PRIMARY_MOUSE ,
116
117
current : self . primary_state . clone ( ) ,
@@ -129,7 +130,7 @@ impl WindowEventReducer {
129
130
self . primary_state . buttons . insert ( button) ;
130
131
}
131
132
132
- Some ( WindowEventTranslation :: Pointer ( self . counter . attach_count (
133
+ Some ( EventTranslation :: Pointer ( self . counter . attach_count (
133
134
PointerEvent :: Down ( PointerButtonEvent {
134
135
pointer : PRIMARY_MOUSE ,
135
136
button,
@@ -147,15 +148,15 @@ impl WindowEventReducer {
147
148
self . primary_state . buttons . remove ( button) ;
148
149
}
149
150
150
- Some ( WindowEventTranslation :: Pointer ( self . counter . attach_count (
151
+ Some ( EventTranslation :: Pointer ( self . counter . attach_count (
151
152
PointerEvent :: Up ( PointerButtonEvent {
152
153
pointer : PRIMARY_MOUSE ,
153
154
button,
154
155
state : self . primary_state . clone ( ) ,
155
156
} ) ,
156
157
) ) )
157
158
}
158
- WindowEvent :: MouseWheel { delta, .. } => Some ( WindowEventTranslation :: Pointer (
159
+ WindowEvent :: MouseWheel { delta, .. } => Some ( EventTranslation :: Pointer (
159
160
PointerEvent :: Scroll ( PointerScrollEvent {
160
161
pointer : PRIMARY_MOUSE ,
161
162
delta : match * delta {
@@ -196,7 +197,7 @@ impl WindowEventReducer {
196
197
..Default :: default ( )
197
198
} ;
198
199
199
- Some ( WindowEventTranslation :: Pointer ( self . counter . attach_count (
200
+ Some ( EventTranslation :: Pointer ( self . counter . attach_count (
200
201
match phase {
201
202
Started => PointerEvent :: Down ( PointerButtonEvent {
202
203
pointer,
@@ -221,11 +222,48 @@ impl WindowEventReducer {
221
222
_ => None ,
222
223
}
223
224
}
225
+
226
+ /// Process a [`DeviceEvent`].
227
+ pub fn reduce_device_event ( & mut self , de : & DeviceEvent ) -> Option < EventTranslation > {
228
+ match de {
229
+ DeviceEvent :: MouseMotion { delta : ( x, y) } => {
230
+ const PRIMARY_MOUSE : PointerInfo = PointerInfo {
231
+ pointer_id : Some ( PointerId :: PRIMARY ) ,
232
+ persistent_device_id : None ,
233
+ pointer_type : PointerType :: Mouse ,
234
+ } ;
235
+
236
+ let time = Instant :: now ( )
237
+ . duration_since ( * self . first_instant . get_or_insert_with ( Instant :: now) )
238
+ . as_nanos ( ) as u64 ;
239
+
240
+ self . primary_state . time = time;
241
+
242
+ let frame = PointerRelativeFrame {
243
+ time,
244
+ distance : PhysicalPosition { x : * x, y : * y } ,
245
+ buttons : self . primary_state . buttons ,
246
+ modifiers : self . primary_state . modifiers ,
247
+ count : 0 ,
248
+ } ;
249
+
250
+ Some ( EventTranslation :: Pointer ( PointerEvent :: RelativeMotion (
251
+ PointerRelativeMotion {
252
+ pointer : PRIMARY_MOUSE ,
253
+ total : frame. clone ( ) ,
254
+ frames : vec ! [ frame] ,
255
+ predicted : vec ! [ ] ,
256
+ } ,
257
+ ) ) )
258
+ }
259
+ _ => None ,
260
+ }
261
+ }
224
262
}
225
263
226
264
/// Result of [`WindowEventReducer::reduce`].
227
265
#[ derive( Debug ) ]
228
- pub enum WindowEventTranslation {
266
+ pub enum EventTranslation {
229
267
/// Resulting [`KeyboardEvent`].
230
268
Keyboard ( KeyboardEvent ) ,
231
269
/// Resulting [`PointerEvent`].
@@ -345,6 +383,49 @@ impl TapCounter {
345
383
} )
346
384
}
347
385
}
386
+ PointerEvent :: RelativeMotion ( PointerRelativeMotion {
387
+ pointer,
388
+ mut total,
389
+ mut frames,
390
+ mut predicted,
391
+ } ) => {
392
+ if let Some ( TapState { count, .. } ) = self
393
+ . taps
394
+ . iter ( )
395
+ . find (
396
+ |TapState {
397
+ pointer_id,
398
+ down_time,
399
+ up_time,
400
+ ..
401
+ } | {
402
+ * pointer_id == pointer. pointer_id && down_time == up_time
403
+ } ,
404
+ )
405
+ . cloned ( )
406
+ {
407
+ total. count = count;
408
+ for event in frames. iter_mut ( ) {
409
+ event. count = count;
410
+ }
411
+ for event in predicted. iter_mut ( ) {
412
+ event. count = count;
413
+ }
414
+ PointerEvent :: RelativeMotion ( PointerRelativeMotion {
415
+ pointer,
416
+ total,
417
+ frames,
418
+ predicted,
419
+ } )
420
+ } else {
421
+ PointerEvent :: RelativeMotion ( PointerRelativeMotion {
422
+ pointer,
423
+ total,
424
+ frames,
425
+ predicted,
426
+ } )
427
+ }
428
+ }
348
429
PointerEvent :: Cancel ( p) => {
349
430
self . taps
350
431
. retain ( |TapState { pointer_id, .. } | * pointer_id != p. pointer_id ) ;
0 commit comments