Enable event propagation (#3062)

This commit is contained in:
daxpedda 2023-08-28 19:18:10 +02:00 committed by GitHub
parent 7541220a41
commit 1dfca5a395
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 75 additions and 176 deletions

View file

@ -367,12 +367,7 @@ impl Canvas {
prevent_default: bool,
) where
MOD: 'static + FnMut(ModifiersState),
M: 'static
+ FnMut(
ModifiersState,
i32,
&mut dyn Iterator<Item = (PhysicalPosition<f64>, PhysicalPosition<f64>)>,
),
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
T: 'static
+ FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>),
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
@ -524,17 +519,13 @@ impl Common {
pub fn add_event<E, F>(
&self,
event_name: &'static str,
mut handler: F,
handler: F,
) -> EventListenerHandle<dyn FnMut(E)>
where
E: 'static + AsRef<web_sys::Event> + wasm_bindgen::convert::FromWasmAbi,
F: 'static + FnMut(E),
{
let closure = Closure::new(move |event: E| {
event.as_ref().stop_propagation();
handler(event);
});
EventListenerHandle::new(&self.raw, event_name, closure)
EventListenerHandle::new(self.raw.clone(), event_name, Closure::new(handler))
}
// The difference between add_event and add_user_event is that the latter has a special meaning

View file

@ -8,11 +8,11 @@ pub struct EventListenerHandle<T: ?Sized> {
}
impl<T: ?Sized> EventListenerHandle<T> {
pub fn new<U>(target: &U, event_type: &'static str, listener: Closure<T>) -> Self
pub fn new<U>(target: U, event_type: &'static str, listener: Closure<T>) -> Self
where
U: Clone + Into<EventTarget>,
U: Into<EventTarget>,
{
let target = target.clone().into();
let target = target.into();
target
.add_event_listener_with_callback(event_type, listener.as_ref().unchecked_ref())
.expect("Failed to add event listener");

View file

@ -35,14 +35,15 @@ pub struct PageTransitionEventHandle {
}
pub fn on_page_transition(
window: &web_sys::Window,
window: web_sys::Window,
show_handler: impl FnMut(PageTransitionEvent) + 'static,
hide_handler: impl FnMut(PageTransitionEvent) + 'static,
) -> PageTransitionEventHandle {
let show_closure = Closure::new(show_handler);
let hide_closure = Closure::new(hide_handler);
let show_listener = event_handle::EventListenerHandle::new(window, "pageshow", show_closure);
let show_listener =
event_handle::EventListenerHandle::new(window.clone(), "pageshow", show_closure);
let hide_listener = event_handle::EventListenerHandle::new(window, "pagehide", hide_closure);
PageTransitionEventHandle {
_show_listener: show_listener,

View file

@ -168,12 +168,7 @@ impl PointerHandler {
prevent_default: bool,
) where
MOD: 'static + FnMut(ModifiersState),
M: 'static
+ FnMut(
ModifiersState,
i32,
&mut dyn Iterator<Item = (PhysicalPosition<f64>, PhysicalPosition<f64>)>,
),
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
T: 'static
+ FnMut(ModifiersState, i32, &mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>),
B: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
@ -223,22 +218,12 @@ impl PointerHandler {
// pointer move event
let scale = super::scale_factor(&window);
match pointer_type.as_str() {
"mouse" => {
let mut delta = event::MouseDelta::init(&window, &event);
mouse_handler(
modifiers,
id,
&mut event::pointer_move_event(event).map(|event| {
let position = event::mouse_position(&event).to_physical(scale);
let delta = delta
.delta(&event)
.to_physical(super::scale_factor(&window));
(position, delta)
}),
)
}
"mouse" => mouse_handler(
modifiers,
id,
&mut event::pointer_move_event(event)
.map(|event| event::mouse_position(&event).to_physical(scale)),
),
"touch" => touch_handler(
modifiers,
id,