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

@ -17,8 +17,7 @@ use super::{
window::WindowId,
};
use crate::event::{
DeviceEvent, DeviceId as RootDeviceId, ElementState, Event, KeyEvent, RawKeyEvent, Touch,
TouchPhase, WindowEvent,
DeviceId as RootDeviceId, ElementState, Event, KeyEvent, Touch, TouchPhase, WindowEvent,
};
use crate::event_loop::DeviceEvents;
use crate::keyboard::ModifiersState;
@ -140,34 +139,24 @@ impl<T> EventLoopWindowTarget<T> {
let device_id = RootDeviceId(unsafe { DeviceId::dummy() });
let device_event = runner.device_events().then_some(Event::DeviceEvent {
device_id,
event: DeviceEvent::Key(RawKeyEvent {
physical_key,
state: ElementState::Pressed,
}),
});
runner.send_events(
device_event
.into_iter()
.chain(iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::KeyboardInput {
device_id,
event: KeyEvent {
physical_key,
logical_key,
text,
location,
state: ElementState::Pressed,
repeat,
platform_specific: KeyEventExtra,
},
is_synthetic: false,
iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::KeyboardInput {
device_id,
event: KeyEvent {
physical_key,
logical_key,
text,
location,
state: ElementState::Pressed,
repeat,
platform_specific: KeyEventExtra,
},
}))
.chain(modifiers_changed),
is_synthetic: false,
},
})
.chain(modifiers_changed),
);
},
prevent_default,
@ -187,34 +176,24 @@ impl<T> EventLoopWindowTarget<T> {
let device_id = RootDeviceId(unsafe { DeviceId::dummy() });
let device_event = runner.device_events().then_some(Event::DeviceEvent {
device_id,
event: DeviceEvent::Key(RawKeyEvent {
physical_key,
state: ElementState::Pressed,
}),
});
runner.send_events(
device_event
.into_iter()
.chain(iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::KeyboardInput {
device_id,
event: KeyEvent {
physical_key,
logical_key,
text,
location,
state: ElementState::Released,
repeat,
platform_specific: KeyEventExtra,
},
is_synthetic: false,
iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::KeyboardInput {
device_id,
event: KeyEvent {
physical_key,
logical_key,
text,
location,
state: ElementState::Released,
repeat,
platform_specific: KeyEventExtra,
},
}))
.chain(modifiers_changed),
is_synthetic: false,
},
})
.chain(modifiers_changed),
)
},
prevent_default,
@ -311,48 +290,17 @@ impl<T> EventLoopWindowTarget<T> {
}
});
runner.send_events(modifiers.into_iter().chain(events.flat_map(
|(position, delta)| {
let device_id = RootDeviceId(DeviceId(pointer_id));
runner.send_events(modifiers.into_iter().chain(events.flat_map(|position| {
let device_id = RootDeviceId(DeviceId(pointer_id));
let device_events = runner.device_events().then(|| {
let x_motion = (delta.x != 0.0).then_some(Event::DeviceEvent {
device_id,
event: DeviceEvent::Motion {
axis: 0,
value: delta.x,
},
});
let y_motion = (delta.y != 0.0).then_some(Event::DeviceEvent {
device_id,
event: DeviceEvent::Motion {
axis: 1,
value: delta.y,
},
});
x_motion.into_iter().chain(y_motion).chain(iter::once(
Event::DeviceEvent {
device_id,
event: DeviceEvent::MouseMotion {
delta: (delta.x, delta.y),
},
},
))
});
device_events.into_iter().flatten().chain(iter::once(
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
device_id,
position,
},
},
))
},
)));
iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
device_id,
position,
},
})
})));
}
},
{
@ -413,18 +361,10 @@ impl<T> EventLoopWindowTarget<T> {
ElementState::Released
};
let device_event = runner.device_events().then(|| Event::DeviceEvent {
device_id,
event: DeviceEvent::Button {
button: button.to_id(),
state,
},
});
// A chorded button event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the
// user code has the correct cursor position.
runner.send_events(modifiers.into_iter().chain(device_event).chain([
runner.send_events(modifiers.into_iter().chain([
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
@ -475,18 +415,11 @@ impl<T> EventLoopWindowTarget<T> {
});
let device_id: RootDeviceId = RootDeviceId(DeviceId(pointer_id));
let device_event = runner.device_events().then(|| Event::DeviceEvent {
device_id,
event: DeviceEvent::Button {
button: button.to_id(),
state: ElementState::Pressed,
},
});
// A mouse down event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the
// user code has the correct cursor position.
runner.send_events(modifiers.into_iter().chain(device_event).chain([
runner.send_events(modifiers.into_iter().chain([
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
@ -568,18 +501,11 @@ impl<T> EventLoopWindowTarget<T> {
});
let device_id: RootDeviceId = RootDeviceId(DeviceId(pointer_id));
let device_event = runner.device_events().then(|| Event::DeviceEvent {
device_id,
event: DeviceEvent::Button {
button: button.to_id(),
state: ElementState::Pressed,
},
});
// A mouse up event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the
// user code has the correct cursor position.
runner.send_events(modifiers.into_iter().chain(device_event).chain([
runner.send_events(modifiers.into_iter().chain([
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorMoved {
@ -644,21 +570,16 @@ impl<T> EventLoopWindowTarget<T> {
}
});
let device_event = runner.device_events().then_some(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(pointer_id)),
event: DeviceEvent::MouseWheel { delta },
});
runner.send_events(modifiers_changed.into_iter().chain(device_event).chain(
iter::once(Event::WindowEvent {
runner.send_events(modifiers_changed.into_iter().chain(iter::once(
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::MouseWheel {
device_id: RootDeviceId(DeviceId(pointer_id)),
delta,
phase: TouchPhase::Moved,
},
}),
));
},
)));
},
prevent_default,
);