Skip to content

Commit 2428224

Browse files
daxpeddakchibisov
authored andcommitted
Enable event propagation (#3062)
1 parent 2d9b852 commit 2428224

File tree

7 files changed

+76
-177
lines changed

7 files changed

+76
-177
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ And please only add new entries to the top of this list, right below the `# Unre
1515
- On Web, never return a `MonitorHandle`.
1616
- **Breaking:** Move `Event::RedrawRequested` to `WindowEvent::RedrawRequested`.
1717
- On macOS, fix crash in `window.set_minimized(false)`.
18+
- On Web, enable event propagation and let `DeviceEvent`s appear after `WindowEvent`s.
1819

1920
# 0.29.1-beta
2021

src/platform_impl/web/event_loop/runner.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<T: 'static> Shared<T> {
201201
self.init();
202202

203203
*self.0.page_transition_event_handle.borrow_mut() = Some(backend::on_page_transition(
204-
self.window(),
204+
self.window().clone(),
205205
{
206206
let runner = self.clone();
207207
move |event: PageTransitionEvent| {
@@ -227,7 +227,7 @@ impl<T: 'static> Shared<T> {
227227
let runner = self.clone();
228228
let window = self.window().clone();
229229
*self.0.on_mouse_move.borrow_mut() = Some(EventListenerHandle::new(
230-
self.window(),
230+
self.window().clone(),
231231
"pointermove",
232232
Closure::new(move |event: PointerEvent| {
233233
if !runner.device_events() {
@@ -304,7 +304,7 @@ impl<T: 'static> Shared<T> {
304304
let runner = self.clone();
305305
let window = self.window().clone();
306306
*self.0.on_wheel.borrow_mut() = Some(EventListenerHandle::new(
307-
self.window(),
307+
self.window().clone(),
308308
"wheel",
309309
Closure::new(move |event: WheelEvent| {
310310
if !runner.device_events() {
@@ -321,7 +321,7 @@ impl<T: 'static> Shared<T> {
321321
));
322322
let runner = self.clone();
323323
*self.0.on_mouse_press.borrow_mut() = Some(EventListenerHandle::new(
324-
self.window(),
324+
self.window().clone(),
325325
"pointerdown",
326326
Closure::new(move |event: PointerEvent| {
327327
if !runner.device_events() {
@@ -344,7 +344,7 @@ impl<T: 'static> Shared<T> {
344344
));
345345
let runner = self.clone();
346346
*self.0.on_mouse_release.borrow_mut() = Some(EventListenerHandle::new(
347-
self.window(),
347+
self.window().clone(),
348348
"pointerup",
349349
Closure::new(move |event: PointerEvent| {
350350
if !runner.device_events() {
@@ -367,7 +367,7 @@ impl<T: 'static> Shared<T> {
367367
));
368368
let runner = self.clone();
369369
*self.0.on_key_press.borrow_mut() = Some(EventListenerHandle::new(
370-
self.window(),
370+
self.window().clone(),
371371
"keydown",
372372
Closure::new(move |event: KeyboardEvent| {
373373
if !runner.device_events() {
@@ -385,7 +385,7 @@ impl<T: 'static> Shared<T> {
385385
));
386386
let runner = self.clone();
387387
*self.0.on_key_release.borrow_mut() = Some(EventListenerHandle::new(
388-
self.window(),
388+
self.window().clone(),
389389
"keyup",
390390
Closure::new(move |event: KeyboardEvent| {
391391
if !runner.device_events() {
@@ -404,7 +404,7 @@ impl<T: 'static> Shared<T> {
404404
let runner = self.clone();
405405
*self.0.on_visibility_change.borrow_mut() = Some(EventListenerHandle::new(
406406
// Safari <14 doesn't support the `visibilitychange` event on `Window`.
407-
self.document(),
407+
self.document().clone(),
408408
"visibilitychange",
409409
Closure::new(move |_| {
410410
if !runner.0.suspended.get() {

src/platform_impl/web/event_loop/window_target.rs

+51-130
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use super::{
1717
window::WindowId,
1818
};
1919
use crate::event::{
20-
DeviceEvent, DeviceId as RootDeviceId, ElementState, Event, KeyEvent, RawKeyEvent, Touch,
21-
TouchPhase, WindowEvent,
20+
DeviceId as RootDeviceId, ElementState, Event, KeyEvent, Touch, TouchPhase, WindowEvent,
2221
};
2322
use crate::event_loop::DeviceEvents;
2423
use crate::keyboard::ModifiersState;
@@ -140,34 +139,24 @@ impl<T> EventLoopWindowTarget<T> {
140139

141140
let device_id = RootDeviceId(unsafe { DeviceId::dummy() });
142141

143-
let device_event = runner.device_events().then_some(Event::DeviceEvent {
144-
device_id,
145-
event: DeviceEvent::Key(RawKeyEvent {
146-
physical_key,
147-
state: ElementState::Pressed,
148-
}),
149-
});
150-
151142
runner.send_events(
152-
device_event
153-
.into_iter()
154-
.chain(iter::once(Event::WindowEvent {
155-
window_id: RootWindowId(id),
156-
event: WindowEvent::KeyboardInput {
157-
device_id,
158-
event: KeyEvent {
159-
physical_key,
160-
logical_key,
161-
text,
162-
location,
163-
state: ElementState::Pressed,
164-
repeat,
165-
platform_specific: KeyEventExtra,
166-
},
167-
is_synthetic: false,
143+
iter::once(Event::WindowEvent {
144+
window_id: RootWindowId(id),
145+
event: WindowEvent::KeyboardInput {
146+
device_id,
147+
event: KeyEvent {
148+
physical_key,
149+
logical_key,
150+
text,
151+
location,
152+
state: ElementState::Pressed,
153+
repeat,
154+
platform_specific: KeyEventExtra,
168155
},
169-
}))
170-
.chain(modifiers_changed),
156+
is_synthetic: false,
157+
},
158+
})
159+
.chain(modifiers_changed),
171160
);
172161
},
173162
prevent_default,
@@ -187,34 +176,24 @@ impl<T> EventLoopWindowTarget<T> {
187176

188177
let device_id = RootDeviceId(unsafe { DeviceId::dummy() });
189178

190-
let device_event = runner.device_events().then_some(Event::DeviceEvent {
191-
device_id,
192-
event: DeviceEvent::Key(RawKeyEvent {
193-
physical_key,
194-
state: ElementState::Pressed,
195-
}),
196-
});
197-
198179
runner.send_events(
199-
device_event
200-
.into_iter()
201-
.chain(iter::once(Event::WindowEvent {
202-
window_id: RootWindowId(id),
203-
event: WindowEvent::KeyboardInput {
204-
device_id,
205-
event: KeyEvent {
206-
physical_key,
207-
logical_key,
208-
text,
209-
location,
210-
state: ElementState::Released,
211-
repeat,
212-
platform_specific: KeyEventExtra,
213-
},
214-
is_synthetic: false,
180+
iter::once(Event::WindowEvent {
181+
window_id: RootWindowId(id),
182+
event: WindowEvent::KeyboardInput {
183+
device_id,
184+
event: KeyEvent {
185+
physical_key,
186+
logical_key,
187+
text,
188+
location,
189+
state: ElementState::Released,
190+
repeat,
191+
platform_specific: KeyEventExtra,
215192
},
216-
}))
217-
.chain(modifiers_changed),
193+
is_synthetic: false,
194+
},
195+
})
196+
.chain(modifiers_changed),
218197
)
219198
},
220199
prevent_default,
@@ -311,48 +290,17 @@ impl<T> EventLoopWindowTarget<T> {
311290
}
312291
});
313292

314-
runner.send_events(modifiers.into_iter().chain(events.flat_map(
315-
|(position, delta)| {
316-
let device_id = RootDeviceId(DeviceId(pointer_id));
317-
318-
let device_events = runner.device_events().then(|| {
319-
let x_motion = (delta.x != 0.0).then_some(Event::DeviceEvent {
320-
device_id,
321-
event: DeviceEvent::Motion {
322-
axis: 0,
323-
value: delta.x,
324-
},
325-
});
326-
327-
let y_motion = (delta.y != 0.0).then_some(Event::DeviceEvent {
328-
device_id,
329-
event: DeviceEvent::Motion {
330-
axis: 1,
331-
value: delta.y,
332-
},
333-
});
334-
335-
x_motion.into_iter().chain(y_motion).chain(iter::once(
336-
Event::DeviceEvent {
337-
device_id,
338-
event: DeviceEvent::MouseMotion {
339-
delta: (delta.x, delta.y),
340-
},
341-
},
342-
))
343-
});
344-
345-
device_events.into_iter().flatten().chain(iter::once(
346-
Event::WindowEvent {
347-
window_id: RootWindowId(id),
348-
event: WindowEvent::CursorMoved {
349-
device_id,
350-
position,
351-
},
352-
},
353-
))
354-
},
355-
)));
293+
runner.send_events(modifiers.into_iter().chain(events.flat_map(|position| {
294+
let device_id = RootDeviceId(DeviceId(pointer_id));
295+
296+
iter::once(Event::WindowEvent {
297+
window_id: RootWindowId(id),
298+
event: WindowEvent::CursorMoved {
299+
device_id,
300+
position,
301+
},
302+
})
303+
})));
356304
}
357305
},
358306
{
@@ -413,18 +361,10 @@ impl<T> EventLoopWindowTarget<T> {
413361
ElementState::Released
414362
};
415363

416-
let device_event = runner.device_events().then(|| Event::DeviceEvent {
417-
device_id,
418-
event: DeviceEvent::Button {
419-
button: button.to_id(),
420-
state,
421-
},
422-
});
423-
424364
// A chorded button event may come in without any prior CursorMoved events,
425365
// therefore we should send a CursorMoved event to make sure that the
426366
// user code has the correct cursor position.
427-
runner.send_events(modifiers.into_iter().chain(device_event).chain([
367+
runner.send_events(modifiers.into_iter().chain([
428368
Event::WindowEvent {
429369
window_id: RootWindowId(id),
430370
event: WindowEvent::CursorMoved {
@@ -475,18 +415,11 @@ impl<T> EventLoopWindowTarget<T> {
475415
});
476416

477417
let device_id: RootDeviceId = RootDeviceId(DeviceId(pointer_id));
478-
let device_event = runner.device_events().then(|| Event::DeviceEvent {
479-
device_id,
480-
event: DeviceEvent::Button {
481-
button: button.to_id(),
482-
state: ElementState::Pressed,
483-
},
484-
});
485418

486419
// A mouse down event may come in without any prior CursorMoved events,
487420
// therefore we should send a CursorMoved event to make sure that the
488421
// user code has the correct cursor position.
489-
runner.send_events(modifiers.into_iter().chain(device_event).chain([
422+
runner.send_events(modifiers.into_iter().chain([
490423
Event::WindowEvent {
491424
window_id: RootWindowId(id),
492425
event: WindowEvent::CursorMoved {
@@ -568,18 +501,11 @@ impl<T> EventLoopWindowTarget<T> {
568501
});
569502

570503
let device_id: RootDeviceId = RootDeviceId(DeviceId(pointer_id));
571-
let device_event = runner.device_events().then(|| Event::DeviceEvent {
572-
device_id,
573-
event: DeviceEvent::Button {
574-
button: button.to_id(),
575-
state: ElementState::Pressed,
576-
},
577-
});
578504

579505
// A mouse up event may come in without any prior CursorMoved events,
580506
// therefore we should send a CursorMoved event to make sure that the
581507
// user code has the correct cursor position.
582-
runner.send_events(modifiers.into_iter().chain(device_event).chain([
508+
runner.send_events(modifiers.into_iter().chain([
583509
Event::WindowEvent {
584510
window_id: RootWindowId(id),
585511
event: WindowEvent::CursorMoved {
@@ -644,21 +570,16 @@ impl<T> EventLoopWindowTarget<T> {
644570
}
645571
});
646572

647-
let device_event = runner.device_events().then_some(Event::DeviceEvent {
648-
device_id: RootDeviceId(DeviceId(pointer_id)),
649-
event: DeviceEvent::MouseWheel { delta },
650-
});
651-
652-
runner.send_events(modifiers_changed.into_iter().chain(device_event).chain(
653-
iter::once(Event::WindowEvent {
573+
runner.send_events(modifiers_changed.into_iter().chain(iter::once(
574+
Event::WindowEvent {
654575
window_id: RootWindowId(id),
655576
event: WindowEvent::MouseWheel {
656577
device_id: RootDeviceId(DeviceId(pointer_id)),
657578
delta,
658579
phase: TouchPhase::Moved,
659580
},
660-
}),
661-
));
581+
},
582+
)));
662583
},
663584
prevent_default,
664585
);

src/platform_impl/web/web_sys/canvas.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,7 @@ impl Canvas {
367367
prevent_default: bool,
368368
) where
369369
MOD: 'static + FnMut(ModifiersState),
370-
M: 'static
371-
+ FnMut(
372-
ModifiersState,
373-
i32,
374-
&mut dyn Iterator<Item = (PhysicalPosition<f64>, PhysicalPosition<f64>)>,
375-
),
370+
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
376371
T: 'static
377372
+ FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>),
378373
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
@@ -524,17 +519,13 @@ impl Common {
524519
pub fn add_event<E, F>(
525520
&self,
526521
event_name: &'static str,
527-
mut handler: F,
522+
handler: F,
528523
) -> EventListenerHandle<dyn FnMut(E)>
529524
where
530525
E: 'static + AsRef<web_sys::Event> + wasm_bindgen::convert::FromWasmAbi,
531526
F: 'static + FnMut(E),
532527
{
533-
let closure = Closure::new(move |event: E| {
534-
event.as_ref().stop_propagation();
535-
handler(event);
536-
});
537-
EventListenerHandle::new(&self.raw, event_name, closure)
528+
EventListenerHandle::new(self.raw.clone(), event_name, Closure::new(handler))
538529
}
539530

540531
// The difference between add_event and add_user_event is that the latter has a special meaning

src/platform_impl/web/web_sys/event_handle.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ pub struct EventListenerHandle<T: ?Sized> {
88
}
99

1010
impl<T: ?Sized> EventListenerHandle<T> {
11-
pub fn new<U>(target: &U, event_type: &'static str, listener: Closure<T>) -> Self
11+
pub fn new<U>(target: U, event_type: &'static str, listener: Closure<T>) -> Self
1212
where
13-
U: Clone + Into<EventTarget>,
13+
U: Into<EventTarget>,
1414
{
15-
let target = target.clone().into();
15+
let target = target.into();
1616
target
1717
.add_event_listener_with_callback(event_type, listener.as_ref().unchecked_ref())
1818
.expect("Failed to add event listener");

0 commit comments

Comments
 (0)