Introduce FingerId (#3783)

This commit is contained in:
daxpedda 2024-08-08 00:36:36 +02:00 committed by GitHub
parent f07153b8e0
commit 9dff801f93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 329 additions and 113 deletions

View file

@ -1,8 +0,0 @@
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId(pub i32);
impl DeviceId {
pub const fn dummy() -> Self {
Self(0)
}
}

View file

@ -0,0 +1,32 @@
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId(i32);
impl DeviceId {
pub fn new(pointer_id: i32) -> Self {
Self(pointer_id)
}
pub const fn dummy() -> Self {
Self(-1)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FingerId {
pointer_id: i32,
primary: bool,
}
impl FingerId {
pub fn new(pointer_id: i32, primary: bool) -> Self {
Self { pointer_id, primary }
}
pub const fn dummy() -> Self {
Self { pointer_id: -1, primary: false }
}
pub fn is_primary(self) -> bool {
self.primary
}
}

View file

@ -1,4 +1,4 @@
use super::{backend, device, window, HasMonitorPermissionFuture, MonitorPermissionFuture};
use super::{backend, event, window, HasMonitorPermissionFuture, MonitorPermissionFuture};
use crate::application::ApplicationHandler;
use crate::error::{EventLoopError, NotSupportedError};
use crate::event::Event;

View file

@ -287,7 +287,7 @@ impl Shared {
}
// chorded button event
let device_id = RootDeviceId(DeviceId(event.pointer_id()));
let device_id = RootDeviceId(DeviceId::new(event.pointer_id()));
if let Some(button) = backend::event::mouse_button(&event) {
let state = if backend::event::mouse_buttons(&event).contains(button.into()) {
@ -328,7 +328,7 @@ impl Shared {
if let Some(delta) = backend::event::mouse_scroll_delta(&window, &event) {
runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(0)),
device_id: RootDeviceId(DeviceId::dummy()),
event: DeviceEvent::MouseWheel { delta },
});
}
@ -345,7 +345,7 @@ impl Shared {
let button = backend::event::mouse_button(&event).expect("no mouse button pressed");
runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(event.pointer_id())),
device_id: RootDeviceId(DeviceId::new(event.pointer_id())),
event: DeviceEvent::Button {
button: button.to_id(),
state: ElementState::Pressed,
@ -364,7 +364,7 @@ impl Shared {
let button = backend::event::mouse_button(&event).expect("no mouse button pressed");
runner.send_event(Event::DeviceEvent {
device_id: RootDeviceId(DeviceId(event.pointer_id())),
device_id: RootDeviceId(DeviceId::new(event.pointer_id())),
event: DeviceEvent::Button {
button: button.to_id(),
state: ElementState::Released,

View file

@ -8,13 +8,14 @@ use web_sys::Element;
use super::super::monitor::MonitorPermissionFuture;
use super::super::{lock, KeyEventExtra};
use super::device::DeviceId;
use super::event::DeviceId;
use super::runner::{EventWrapper, WeakShared};
use super::window::WindowId;
use super::{backend, runner, EventLoopProxy};
use crate::error::{ExternalError, NotSupportedError};
use crate::event::{
DeviceId as RootDeviceId, ElementState, Event, KeyEvent, Touch, TouchPhase, WindowEvent,
DeviceId as RootDeviceId, ElementState, Event, FingerId as RootFingerId, KeyEvent, Touch,
TouchPhase, WindowEvent,
};
use crate::event_loop::{
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents,
@ -220,11 +221,9 @@ impl ActiveEventLoop {
}
});
let pointer = pointer_id.map(|pointer_id| Event::WindowEvent {
let pointer = pointer_id.map(|device_id| Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorLeft {
device_id: RootDeviceId(DeviceId(pointer_id)),
},
event: WindowEvent::CursorLeft { device_id: RootDeviceId(device_id) },
});
if focus.is_some() || pointer.is_some() {
@ -247,11 +246,9 @@ impl ActiveEventLoop {
}
});
let pointer = pointer_id.map(|pointer_id| Event::WindowEvent {
let pointer = pointer_id.map(|device_id| Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::CursorEntered {
device_id: RootDeviceId(DeviceId(pointer_id)),
},
event: WindowEvent::CursorEntered { device_id: RootDeviceId(device_id) },
});
if focus.is_some() || pointer.is_some() {
@ -277,7 +274,7 @@ impl ActiveEventLoop {
});
runner.send_events(modifiers.into_iter().chain(events.flat_map(|position| {
let device_id = RootDeviceId(DeviceId(pointer_id));
let device_id = RootDeviceId(pointer_id);
iter::once(Event::WindowEvent {
window_id: RootWindowId(id),
@ -291,7 +288,7 @@ impl ActiveEventLoop {
let has_focus = has_focus.clone();
let modifiers = self.modifiers.clone();
move |active_modifiers, device_id, events| {
move |active_modifiers, device_id, finger_id, events| {
let modifiers =
(has_focus.get() && modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
@ -305,8 +302,8 @@ impl ActiveEventLoop {
|(location, force)| Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Touch(Touch {
id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)),
finger_id: RootFingerId(finger_id),
device_id: RootDeviceId(device_id),
phase: TouchPhase::Moved,
force: Some(force),
location,
@ -321,7 +318,7 @@ impl ActiveEventLoop {
let modifiers = self.modifiers.clone();
move |active_modifiers,
pointer_id,
device_id,
position: crate::dpi::PhysicalPosition<f64>,
buttons,
button| {
@ -334,7 +331,7 @@ impl ActiveEventLoop {
}
});
let device_id = RootDeviceId(DeviceId(pointer_id));
let device_id = RootDeviceId(device_id);
let state = if buttons.contains(button.into()) {
ElementState::Pressed
@ -373,7 +370,7 @@ impl ActiveEventLoop {
}
});
let device_id: RootDeviceId = RootDeviceId(DeviceId(pointer_id));
let device_id: RootDeviceId = RootDeviceId(pointer_id);
// A mouse down event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the
@ -398,7 +395,7 @@ impl ActiveEventLoop {
let runner = self.runner.clone();
let modifiers = self.modifiers.clone();
move |active_modifiers, device_id, location, force| {
move |active_modifiers, device_id, finger_id, location, force| {
let modifiers = (modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
Event::WindowEvent {
@ -411,8 +408,8 @@ impl ActiveEventLoop {
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Touch(Touch {
id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)),
finger_id: RootFingerId(finger_id),
device_id: RootDeviceId(device_id),
phase: TouchPhase::Started,
force: Some(force),
location,
@ -439,7 +436,7 @@ impl ActiveEventLoop {
}
});
let device_id: RootDeviceId = RootDeviceId(DeviceId(pointer_id));
let device_id: RootDeviceId = RootDeviceId(pointer_id);
// A mouse up event may come in without any prior CursorMoved events,
// therefore we should send a CursorMoved event to make sure that the
@ -465,7 +462,7 @@ impl ActiveEventLoop {
let has_focus = has_focus.clone();
let modifiers = self.modifiers.clone();
move |active_modifiers, device_id, location, force| {
move |active_modifiers, device_id, finger_id, location, force| {
let modifiers =
(has_focus.get() && modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
@ -479,8 +476,8 @@ impl ActiveEventLoop {
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Touch(Touch {
id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)),
finger_id: RootFingerId(finger_id),
device_id: RootDeviceId(device_id),
phase: TouchPhase::Ended,
force: Some(force),
location,
@ -493,7 +490,7 @@ impl ActiveEventLoop {
let runner = self.runner.clone();
let modifiers = self.modifiers.clone();
canvas.on_mouse_wheel(move |pointer_id, delta, active_modifiers| {
canvas.on_mouse_wheel(move |delta, active_modifiers| {
let modifiers_changed =
(has_focus.get() && modifiers.get() != active_modifiers).then(|| {
modifiers.set(active_modifiers);
@ -507,7 +504,7 @@ impl ActiveEventLoop {
Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::MouseWheel {
device_id: RootDeviceId(DeviceId(pointer_id)),
device_id: RootDeviceId(DeviceId::dummy()),
delta,
phase: TouchPhase::Moved,
},
@ -516,12 +513,12 @@ impl ActiveEventLoop {
});
let runner = self.runner.clone();
canvas.on_touch_cancel(move |device_id, location, force| {
canvas.on_touch_cancel(move |device_id, finger_id, location, force| {
runner.send_event(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Touch(Touch {
id: device_id as u64,
device_id: RootDeviceId(DeviceId(device_id)),
finger_id: RootFingerId(finger_id),
device_id: RootDeviceId(device_id),
phase: TouchPhase::Cancelled,
force: Some(force),
location,

View file

@ -22,8 +22,8 @@
mod r#async;
mod cursor;
mod device;
mod error;
mod event;
mod event_loop;
mod keyboard;
mod lock;
@ -37,8 +37,8 @@ pub(crate) use cursor::{
CustomCursorSource as PlatformCustomCursorSource,
};
pub use self::device::DeviceId;
pub use self::error::OsError;
pub use self::event::{DeviceId, FingerId};
pub(crate) use self::event_loop::{
ActiveEventLoop, EventLoop, EventLoopProxy, OwnedDisplayHandle,
PlatformSpecificEventLoopAttributes,

View file

@ -12,6 +12,7 @@ use web_sys::{
};
use super::super::cursor::CursorHandler;
use super::super::event::{DeviceId, FingerId};
use super::super::main_thread::MainThreadMarker;
use super::super::WindowId;
use super::animation_frame::AnimationFrameHandler;
@ -329,22 +330,22 @@ impl Canvas {
pub fn on_cursor_leave<F>(&self, handler: F)
where
F: 'static + FnMut(ModifiersState, Option<i32>),
F: 'static + FnMut(ModifiersState, Option<DeviceId>),
{
self.handlers.borrow_mut().pointer_handler.on_cursor_leave(&self.common, handler)
}
pub fn on_cursor_enter<F>(&self, handler: F)
where
F: 'static + FnMut(ModifiersState, Option<i32>),
F: 'static + FnMut(ModifiersState, Option<DeviceId>),
{
self.handlers.borrow_mut().pointer_handler.on_cursor_enter(&self.common, handler)
}
pub fn on_mouse_release<M, T>(&self, mouse_handler: M, touch_handler: T)
where
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
M: 'static + FnMut(ModifiersState, DeviceId, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, DeviceId, FingerId, PhysicalPosition<f64>, Force),
{
self.handlers.borrow_mut().pointer_handler.on_mouse_release(
&self.common,
@ -355,8 +356,8 @@ impl Canvas {
pub fn on_mouse_press<M, T>(&self, mouse_handler: M, touch_handler: T)
where
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
M: 'static + FnMut(ModifiersState, DeviceId, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, DeviceId, FingerId, PhysicalPosition<f64>, Force),
{
self.handlers.borrow_mut().pointer_handler.on_mouse_press(
&self.common,
@ -368,10 +369,17 @@ impl Canvas {
pub fn on_cursor_move<M, T, B>(&self, mouse_handler: M, touch_handler: T, button_handler: B)
where
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
M: 'static
+ FnMut(ModifiersState, DeviceId, &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),
+ FnMut(
ModifiersState,
DeviceId,
FingerId,
&mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>,
),
B: 'static
+ FnMut(ModifiersState, DeviceId, PhysicalPosition<f64>, ButtonsState, MouseButton),
{
self.handlers.borrow_mut().pointer_handler.on_cursor_move(
&self.common,
@ -384,14 +392,14 @@ impl Canvas {
pub fn on_touch_cancel<F>(&self, handler: F)
where
F: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
F: 'static + FnMut(DeviceId, FingerId, PhysicalPosition<f64>, Force),
{
self.handlers.borrow_mut().pointer_handler.on_touch_cancel(&self.common, handler)
}
pub fn on_mouse_wheel<F>(&self, mut handler: F)
where
F: 'static + FnMut(i32, MouseScrollDelta, ModifiersState),
F: 'static + FnMut(MouseScrollDelta, ModifiersState),
{
let window = self.common.window.clone();
let prevent_default = Rc::clone(&self.prevent_default);
@ -403,7 +411,7 @@ impl Canvas {
if let Some(delta) = event::mouse_scroll_delta(&window, &event) {
let modifiers = event::mouse_modifiers(&event);
handler(0, delta, modifiers);
handler(delta, modifiers);
}
}));
}

View file

@ -4,6 +4,7 @@ use std::rc::Rc;
use event::ButtonsState;
use web_sys::PointerEvent;
use super::super::event::{DeviceId, FingerId};
use super::canvas::Common;
use super::event;
use super::event_handle::EventListenerHandle;
@ -35,7 +36,7 @@ impl PointerHandler {
pub fn on_cursor_leave<F>(&mut self, canvas_common: &Common, mut handler: F)
where
F: 'static + FnMut(ModifiersState, Option<i32>),
F: 'static + FnMut(ModifiersState, Option<DeviceId>),
{
self.on_cursor_leave =
Some(canvas_common.add_event("pointerout", move |event: PointerEvent| {
@ -44,15 +45,16 @@ impl PointerHandler {
// touch events are handled separately
// handling them here would produce duplicate mouse events, inconsistent with
// other platforms.
let pointer_id = (event.pointer_type() != "touch").then(|| event.pointer_id());
let device_id =
(event.pointer_type() != "touch").then(|| DeviceId::new(event.pointer_id()));
handler(modifiers, pointer_id);
handler(modifiers, device_id);
}));
}
pub fn on_cursor_enter<F>(&mut self, canvas_common: &Common, mut handler: F)
where
F: 'static + FnMut(ModifiersState, Option<i32>),
F: 'static + FnMut(ModifiersState, Option<DeviceId>),
{
self.on_cursor_enter =
Some(canvas_common.add_event("pointerover", move |event: PointerEvent| {
@ -61,9 +63,10 @@ impl PointerHandler {
// touch events are handled separately
// handling them here would produce duplicate mouse events, inconsistent with
// other platforms.
let pointer_id = (event.pointer_type() != "touch").then(|| event.pointer_id());
let device_id =
(event.pointer_type() != "touch").then(|| DeviceId::new(event.pointer_id()));
handler(modifiers, pointer_id);
handler(modifiers, device_id);
}));
}
@ -73,8 +76,8 @@ impl PointerHandler {
mut mouse_handler: M,
mut touch_handler: T,
) where
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
M: 'static + FnMut(ModifiersState, DeviceId, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, DeviceId, FingerId, PhysicalPosition<f64>, Force),
{
let window = canvas_common.window.clone();
self.on_pointer_release =
@ -82,15 +85,19 @@ impl PointerHandler {
let modifiers = event::mouse_modifiers(&event);
match event.pointer_type().as_str() {
"touch" => touch_handler(
modifiers,
event.pointer_id(),
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
Force::Normalized(event.pressure() as f64),
),
"touch" => {
let pointer_id = event.pointer_id();
touch_handler(
modifiers,
DeviceId::new(pointer_id),
FingerId::new(pointer_id, event.is_primary()),
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
Force::Normalized(event.pressure() as f64),
)
},
_ => mouse_handler(
modifiers,
event.pointer_id(),
DeviceId::new(event.pointer_id()),
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
event::mouse_button(&event).expect("no mouse button released"),
),
@ -105,8 +112,8 @@ impl PointerHandler {
mut touch_handler: T,
prevent_default: Rc<Cell<bool>>,
) where
M: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, i32, PhysicalPosition<f64>, Force),
M: 'static + FnMut(ModifiersState, DeviceId, PhysicalPosition<f64>, MouseButton),
T: 'static + FnMut(ModifiersState, DeviceId, FingerId, PhysicalPosition<f64>, Force),
{
let window = canvas_common.window.clone();
let canvas = canvas_common.raw().clone();
@ -124,17 +131,21 @@ impl PointerHandler {
match pointer_type.as_str() {
"touch" => {
let pointer_id = event.pointer_id();
touch_handler(
modifiers,
event.pointer_id(),
DeviceId::new(pointer_id),
FingerId::new(pointer_id, event.is_primary()),
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
Force::Normalized(event.pressure() as f64),
);
},
_ => {
let pointer_id = event.pointer_id();
mouse_handler(
modifiers,
event.pointer_id(),
DeviceId::new(pointer_id),
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
event::mouse_button(&event).expect("no mouse button pressed"),
);
@ -145,7 +156,7 @@ impl PointerHandler {
// grabbed, and there is probably not a
// situation where this could fail, that we
// care if it fails.
let _e = canvas.set_pointer_capture(event.pointer_id());
let _e = canvas.set_pointer_capture(pointer_id);
}
},
}
@ -160,18 +171,25 @@ impl PointerHandler {
mut button_handler: B,
prevent_default: Rc<Cell<bool>>,
) where
M: 'static + FnMut(ModifiersState, i32, &mut dyn Iterator<Item = PhysicalPosition<f64>>),
M: 'static
+ FnMut(ModifiersState, DeviceId, &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),
+ FnMut(
ModifiersState,
DeviceId,
FingerId,
&mut dyn Iterator<Item = (PhysicalPosition<f64>, Force)>,
),
B: 'static
+ FnMut(ModifiersState, DeviceId, PhysicalPosition<f64>, ButtonsState, MouseButton),
{
let window = canvas_common.window.clone();
let canvas = canvas_common.raw().clone();
self.on_cursor_move =
Some(canvas_common.add_event("pointermove", move |event: PointerEvent| {
let modifiers = event::mouse_modifiers(&event);
let id = event.pointer_id();
let pointer_id = event.pointer_id();
let device_id = DeviceId::new(pointer_id);
// chorded button event
if let Some(button) = event::mouse_button(&event) {
@ -184,7 +202,7 @@ impl PointerHandler {
button_handler(
modifiers,
id,
device_id,
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
event::mouse_buttons(&event),
button,
@ -198,7 +216,8 @@ impl PointerHandler {
match event.pointer_type().as_str() {
"touch" => touch_handler(
modifiers,
id,
device_id,
FingerId::new(pointer_id, event.is_primary()),
&mut event::pointer_move_event(event).map(|event| {
(
event::mouse_position(&event).to_physical(scale),
@ -208,7 +227,7 @@ impl PointerHandler {
),
_ => mouse_handler(
modifiers,
id,
device_id,
&mut event::pointer_move_event(event)
.map(|event| event::mouse_position(&event).to_physical(scale)),
),
@ -218,14 +237,16 @@ impl PointerHandler {
pub fn on_touch_cancel<F>(&mut self, canvas_common: &Common, mut handler: F)
where
F: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
F: 'static + FnMut(DeviceId, FingerId, PhysicalPosition<f64>, Force),
{
let window = canvas_common.window.clone();
self.on_touch_cancel =
Some(canvas_common.add_event("pointercancel", move |event: PointerEvent| {
if event.pointer_type() == "touch" {
let pointer_id = event.pointer_id();
handler(
event.pointer_id(),
DeviceId::new(pointer_id),
FingerId::new(pointer_id, event.is_primary()),
event::mouse_position(&event).to_physical(super::scale_factor(&window)),
Force::Normalized(event.pressure() as f64),
);