mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-23 13:51:30 +11:00
Split modifier handling in all pointer events
This commit is contained in:
parent
964e342f69
commit
b4b2389d0a
|
@ -198,51 +198,65 @@ impl<T> EventLoopWindowTarget<T> {
|
|||
prevent_default,
|
||||
);
|
||||
|
||||
canvas.on_cursor_leave(
|
||||
{
|
||||
let runner = self.runner.clone();
|
||||
let has_focus = has_focus.clone();
|
||||
let modifiers = self.modifiers.clone();
|
||||
let has_focus_clone = has_focus.clone();
|
||||
canvas.on_cursor_leave(move |pointer_id, active_modifiers| {
|
||||
let modifiers_changed = (has_focus_clone.get() && modifiers.get() != active_modifiers)
|
||||
.then(|| {
|
||||
|
||||
move |active_modifiers| {
|
||||
if has_focus.get() && modifiers.get() != active_modifiers {
|
||||
modifiers.set(active_modifiers);
|
||||
Event::WindowEvent {
|
||||
runner.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(id),
|
||||
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
let runner = self.runner.clone();
|
||||
|
||||
runner.send_events(modifiers_changed.into_iter().chain(iter::once(
|
||||
Event::WindowEvent {
|
||||
move |pointer_id| {
|
||||
runner.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(id),
|
||||
event: WindowEvent::CursorLeft {
|
||||
device_id: RootDeviceId(DeviceId(pointer_id)),
|
||||
},
|
||||
},
|
||||
)));
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
canvas.on_cursor_enter(
|
||||
{
|
||||
let runner = self.runner.clone();
|
||||
let has_focus = has_focus.clone();
|
||||
let modifiers = self.modifiers.clone();
|
||||
let has_focus_clone = has_focus.clone();
|
||||
canvas.on_cursor_enter(move |pointer_id, active_modifiers| {
|
||||
let modifiers_changed = (has_focus_clone.get() && modifiers.get() != active_modifiers)
|
||||
.then(|| {
|
||||
|
||||
move |active_modifiers| {
|
||||
if has_focus.get() && modifiers.get() != active_modifiers {
|
||||
modifiers.set(active_modifiers);
|
||||
Event::WindowEvent {
|
||||
runner.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(id),
|
||||
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
let runner = self.runner.clone();
|
||||
|
||||
runner.send_events(modifiers_changed.into_iter().chain(iter::once(
|
||||
Event::WindowEvent {
|
||||
move |pointer_id| {
|
||||
runner.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(id),
|
||||
event: WindowEvent::CursorEntered {
|
||||
device_id: RootDeviceId(DeviceId(pointer_id)),
|
||||
},
|
||||
},
|
||||
)));
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
canvas.on_cursor_move(
|
||||
{
|
||||
|
@ -350,19 +364,25 @@ impl<T> EventLoopWindowTarget<T> {
|
|||
let runner = self.runner.clone();
|
||||
let modifiers = self.modifiers.clone();
|
||||
|
||||
move |pointer_id, position, button, active_modifiers| {
|
||||
let modifiers_changed = (modifiers.get() != active_modifiers).then(|| {
|
||||
move |active_modifiers| {
|
||||
if modifiers.get() != active_modifiers {
|
||||
modifiers.set(active_modifiers);
|
||||
Event::WindowEvent {
|
||||
runner.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(id),
|
||||
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
let runner = self.runner.clone();
|
||||
|
||||
move |pointer_id, position, button| {
|
||||
// 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_changed.into_iter().chain([
|
||||
runner.send_events(
|
||||
[
|
||||
Event::WindowEvent {
|
||||
window_id: RootWindowId(id),
|
||||
event: WindowEvent::CursorMoved {
|
||||
|
@ -378,7 +398,9 @@ impl<T> EventLoopWindowTarget<T> {
|
|||
button,
|
||||
},
|
||||
},
|
||||
]));
|
||||
]
|
||||
.into_iter(),
|
||||
);
|
||||
}
|
||||
},
|
||||
{
|
||||
|
@ -403,23 +425,28 @@ impl<T> EventLoopWindowTarget<T> {
|
|||
canvas.on_mouse_release(
|
||||
{
|
||||
let runner = self.runner.clone();
|
||||
let modifiers = self.modifiers.clone();
|
||||
let has_focus = has_focus.clone();
|
||||
let modifiers = self.modifiers.clone();
|
||||
|
||||
move |pointer_id, position, button, active_modifiers| {
|
||||
let modifiers_changed =
|
||||
(has_focus.get() && modifiers.get() != active_modifiers).then(|| {
|
||||
move |active_modifiers| {
|
||||
if has_focus.get() && modifiers.get() != active_modifiers {
|
||||
modifiers.set(active_modifiers);
|
||||
Event::WindowEvent {
|
||||
runner.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(id),
|
||||
event: WindowEvent::ModifiersChanged(active_modifiers.into()),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
let runner = self.runner.clone();
|
||||
|
||||
move |pointer_id, position, button| {
|
||||
// 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_changed.into_iter().chain([
|
||||
runner.send_events(
|
||||
[
|
||||
Event::WindowEvent {
|
||||
window_id: RootWindowId(id),
|
||||
event: WindowEvent::CursorMoved {
|
||||
|
@ -435,7 +462,9 @@ impl<T> EventLoopWindowTarget<T> {
|
|||
button,
|
||||
},
|
||||
},
|
||||
]));
|
||||
]
|
||||
.into_iter(),
|
||||
);
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -212,40 +212,56 @@ impl Canvas {
|
|||
));
|
||||
}
|
||||
|
||||
pub fn on_cursor_leave<F>(&mut self, handler: F)
|
||||
pub fn on_cursor_leave<MOD, M>(&mut self, modifier_handler: MOD, mouse_handler: M)
|
||||
where
|
||||
F: 'static + FnMut(i32, ModifiersState),
|
||||
{
|
||||
self.pointer_handler.on_cursor_leave(&self.common, handler)
|
||||
}
|
||||
|
||||
pub fn on_cursor_enter<F>(&mut self, handler: F)
|
||||
where
|
||||
F: 'static + FnMut(i32, ModifiersState),
|
||||
{
|
||||
self.pointer_handler.on_cursor_enter(&self.common, handler)
|
||||
}
|
||||
|
||||
pub fn on_mouse_release<M, T>(&mut self, mouse_handler: M, touch_handler: T)
|
||||
where
|
||||
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
|
||||
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
|
||||
MOD: 'static + FnMut(ModifiersState),
|
||||
M: 'static + FnMut(i32),
|
||||
{
|
||||
self.pointer_handler
|
||||
.on_mouse_release(&self.common, mouse_handler, touch_handler)
|
||||
.on_cursor_leave(&self.common, modifier_handler, mouse_handler)
|
||||
}
|
||||
|
||||
pub fn on_mouse_press<M, T>(
|
||||
pub fn on_cursor_enter<MOD, M>(&mut self, modifier_handler: MOD, mouse_handler: M)
|
||||
where
|
||||
MOD: 'static + FnMut(ModifiersState),
|
||||
M: 'static + FnMut(i32),
|
||||
{
|
||||
self.pointer_handler
|
||||
.on_cursor_enter(&self.common, modifier_handler, mouse_handler)
|
||||
}
|
||||
|
||||
pub fn on_mouse_release<MOD, M, T>(
|
||||
&mut self,
|
||||
modifier_handler: MOD,
|
||||
mouse_handler: M,
|
||||
touch_handler: T,
|
||||
) where
|
||||
MOD: 'static + FnMut(ModifiersState),
|
||||
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton),
|
||||
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
|
||||
{
|
||||
self.pointer_handler.on_mouse_release(
|
||||
&self.common,
|
||||
modifier_handler,
|
||||
mouse_handler,
|
||||
touch_handler,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn on_mouse_press<MOD, M, T>(
|
||||
&mut self,
|
||||
modifier_handler: MOD,
|
||||
mouse_handler: M,
|
||||
touch_handler: T,
|
||||
prevent_default: bool,
|
||||
) where
|
||||
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
|
||||
MOD: 'static + FnMut(ModifiersState),
|
||||
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton),
|
||||
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
|
||||
{
|
||||
self.pointer_handler.on_mouse_press(
|
||||
&self.common,
|
||||
modifier_handler,
|
||||
mouse_handler,
|
||||
touch_handler,
|
||||
prevent_default,
|
||||
|
|
|
@ -32,13 +32,20 @@ impl PointerHandler {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn on_cursor_leave<F>(&mut self, canvas_common: &Common, mut handler: F)
|
||||
where
|
||||
F: 'static + FnMut(i32, ModifiersState),
|
||||
pub fn on_cursor_leave<MOD, M>(
|
||||
&mut self,
|
||||
canvas_common: &Common,
|
||||
mut modifier_handler: MOD,
|
||||
mut mouse_handler: M,
|
||||
) where
|
||||
MOD: 'static + FnMut(ModifiersState),
|
||||
M: 'static + FnMut(i32),
|
||||
{
|
||||
self.on_cursor_leave = Some(canvas_common.add_event(
|
||||
"pointerout",
|
||||
move |event: PointerEvent| {
|
||||
modifier_handler(event::mouse_modifiers(&event));
|
||||
|
||||
// touch events are handled separately
|
||||
// handling them here would produce duplicate mouse events, inconsistent with
|
||||
// other platforms.
|
||||
|
@ -46,18 +53,25 @@ impl PointerHandler {
|
|||
return;
|
||||
}
|
||||
|
||||
handler(event.pointer_id(), event::mouse_modifiers(&event));
|
||||
mouse_handler(event.pointer_id());
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
pub fn on_cursor_enter<F>(&mut self, canvas_common: &Common, mut handler: F)
|
||||
where
|
||||
F: 'static + FnMut(i32, ModifiersState),
|
||||
pub fn on_cursor_enter<MOD, M>(
|
||||
&mut self,
|
||||
canvas_common: &Common,
|
||||
mut modifier_handler: MOD,
|
||||
mut mouse_handler: M,
|
||||
) where
|
||||
MOD: 'static + FnMut(ModifiersState),
|
||||
M: 'static + FnMut(i32),
|
||||
{
|
||||
self.on_cursor_enter = Some(canvas_common.add_event(
|
||||
"pointerover",
|
||||
move |event: PointerEvent| {
|
||||
modifier_handler(event::mouse_modifiers(&event));
|
||||
|
||||
// touch events are handled separately
|
||||
// handling them here would produce duplicate mouse events, inconsistent with
|
||||
// other platforms.
|
||||
|
@ -65,24 +79,29 @@ impl PointerHandler {
|
|||
return;
|
||||
}
|
||||
|
||||
handler(event.pointer_id(), event::mouse_modifiers(&event));
|
||||
mouse_handler(event.pointer_id());
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
pub fn on_mouse_release<M, T>(
|
||||
pub fn on_mouse_release<MOD, M, T>(
|
||||
&mut self,
|
||||
canvas_common: &Common,
|
||||
mut modifier_handler: MOD,
|
||||
mut mouse_handler: M,
|
||||
mut touch_handler: T,
|
||||
) where
|
||||
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
|
||||
MOD: 'static + FnMut(ModifiersState),
|
||||
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton),
|
||||
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
|
||||
{
|
||||
let canvas = canvas_common.raw.clone();
|
||||
self.on_pointer_release = Some(canvas_common.add_user_event(
|
||||
"pointerup",
|
||||
move |event: PointerEvent| match event.pointer_type().as_str() {
|
||||
move |event: PointerEvent| {
|
||||
modifier_handler(event::mouse_modifiers(&event));
|
||||
|
||||
match event.pointer_type().as_str() {
|
||||
"touch" => touch_handler(
|
||||
event.pointer_id(),
|
||||
event::touch_position(&event, &canvas).to_physical(super::scale_factor()),
|
||||
|
@ -92,21 +111,23 @@ impl PointerHandler {
|
|||
event.pointer_id(),
|
||||
event::mouse_position(&event).to_physical(super::scale_factor()),
|
||||
event::mouse_button(&event).expect("no mouse button released"),
|
||||
event::mouse_modifiers(&event),
|
||||
),
|
||||
_ => (),
|
||||
}
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
pub fn on_mouse_press<M, T>(
|
||||
pub fn on_mouse_press<MOD, M, T>(
|
||||
&mut self,
|
||||
canvas_common: &Common,
|
||||
mut modifier_handler: MOD,
|
||||
mut mouse_handler: M,
|
||||
mut touch_handler: T,
|
||||
prevent_default: bool,
|
||||
) where
|
||||
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
|
||||
MOD: 'static + FnMut(ModifiersState),
|
||||
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton),
|
||||
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
|
||||
{
|
||||
let canvas = canvas_common.raw.clone();
|
||||
|
@ -120,6 +141,8 @@ impl PointerHandler {
|
|||
let _ = canvas.focus();
|
||||
}
|
||||
|
||||
modifier_handler(event::mouse_modifiers(&event));
|
||||
|
||||
match event.pointer_type().as_str() {
|
||||
"touch" => {
|
||||
touch_handler(
|
||||
|
@ -134,7 +157,6 @@ impl PointerHandler {
|
|||
event.pointer_id(),
|
||||
event::mouse_position(&event).to_physical(super::scale_factor()),
|
||||
event::mouse_button(&event).expect("no mouse button pressed"),
|
||||
event::mouse_modifiers(&event),
|
||||
);
|
||||
|
||||
// Error is swallowed here since the error would occur every time the mouse is
|
||||
|
|
Loading…
Reference in a new issue