Introduce FingerId (#3783)

This commit is contained in:
daxpedda
2024-08-08 00:36:36 +02:00
committed by GitHub
parent f07153b8e0
commit 9dff801f93
24 changed files with 329 additions and 113 deletions

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),
);