2023-06-04 01:44:53 +02:00
|
|
|
use super::canvas::Common;
|
2020-08-29 21:34:33 +08:00
|
|
|
use super::event;
|
2023-06-04 01:44:53 +02:00
|
|
|
use super::event_handle::EventListenerHandle;
|
2020-08-29 21:34:33 +08:00
|
|
|
use crate::dpi::PhysicalPosition;
|
2023-05-28 20:02:59 +02:00
|
|
|
use crate::event::{Force, MouseButton};
|
|
|
|
|
use crate::keyboard::ModifiersState;
|
2020-08-29 21:34:33 +08:00
|
|
|
|
2023-06-02 11:37:23 +02:00
|
|
|
use event::ButtonsState;
|
2023-06-02 14:23:09 +02:00
|
|
|
use wasm_bindgen::prelude::wasm_bindgen;
|
|
|
|
|
use wasm_bindgen::{JsCast, JsValue};
|
2020-08-29 21:34:33 +08:00
|
|
|
use web_sys::PointerEvent;
|
|
|
|
|
|
2021-03-30 21:27:32 +02:00
|
|
|
#[allow(dead_code)]
|
2020-08-29 21:34:33 +08:00
|
|
|
pub(super) struct PointerHandler {
|
2020-09-21 06:42:07 +08:00
|
|
|
on_cursor_leave: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>,
|
|
|
|
|
on_cursor_enter: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>,
|
|
|
|
|
on_cursor_move: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>,
|
|
|
|
|
on_pointer_press: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>,
|
|
|
|
|
on_pointer_release: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>,
|
2022-12-23 14:55:22 +09:00
|
|
|
on_touch_cancel: Option<EventListenerHandle<dyn FnMut(PointerEvent)>>,
|
2020-08-29 21:34:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PointerHandler {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
on_cursor_leave: None,
|
|
|
|
|
on_cursor_enter: None,
|
|
|
|
|
on_cursor_move: None,
|
|
|
|
|
on_pointer_press: None,
|
|
|
|
|
on_pointer_release: None,
|
2022-12-23 14:55:22 +09:00
|
|
|
on_touch_cancel: None,
|
2020-08-29 21:34:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 01:44:53 +02:00
|
|
|
pub fn on_cursor_leave<F>(&mut self, canvas_common: &Common, mut handler: F)
|
2020-08-29 21:34:33 +08:00
|
|
|
where
|
2023-05-28 20:02:59 +02:00
|
|
|
F: 'static + FnMut(i32, ModifiersState),
|
2020-08-29 21:34:33 +08:00
|
|
|
{
|
|
|
|
|
self.on_cursor_leave = Some(canvas_common.add_event(
|
|
|
|
|
"pointerout",
|
|
|
|
|
move |event: PointerEvent| {
|
2022-12-23 14:55:22 +09:00
|
|
|
// touch events are handled separately
|
|
|
|
|
// handling them here would produce duplicate mouse events, inconsistent with
|
|
|
|
|
// other platforms.
|
2023-06-02 13:16:04 +02:00
|
|
|
if event.pointer_type() != "mouse" {
|
2022-12-23 14:55:22 +09:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 11:37:23 +02:00
|
|
|
handler(event.pointer_id(), event::mouse_modifiers(&event));
|
2020-08-29 21:34:33 +08:00
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 01:44:53 +02:00
|
|
|
pub fn on_cursor_enter<F>(&mut self, canvas_common: &Common, mut handler: F)
|
2020-08-29 21:34:33 +08:00
|
|
|
where
|
2023-05-28 20:02:59 +02:00
|
|
|
F: 'static + FnMut(i32, ModifiersState),
|
2020-08-29 21:34:33 +08:00
|
|
|
{
|
|
|
|
|
self.on_cursor_enter = Some(canvas_common.add_event(
|
|
|
|
|
"pointerover",
|
|
|
|
|
move |event: PointerEvent| {
|
2022-12-23 14:55:22 +09:00
|
|
|
// touch events are handled separately
|
|
|
|
|
// handling them here would produce duplicate mouse events, inconsistent with
|
|
|
|
|
// other platforms.
|
2023-06-02 13:16:04 +02:00
|
|
|
if event.pointer_type() != "mouse" {
|
2022-12-23 14:55:22 +09:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 11:37:23 +02:00
|
|
|
handler(event.pointer_id(), event::mouse_modifiers(&event));
|
2020-08-29 21:34:33 +08:00
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 14:55:22 +09:00
|
|
|
pub fn on_mouse_release<M, T>(
|
|
|
|
|
&mut self,
|
2023-06-04 01:44:53 +02:00
|
|
|
canvas_common: &Common,
|
2022-12-23 14:55:22 +09:00
|
|
|
mut mouse_handler: M,
|
|
|
|
|
mut touch_handler: T,
|
|
|
|
|
) where
|
2023-06-04 01:08:47 +02:00
|
|
|
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
|
2022-12-23 14:55:22 +09:00
|
|
|
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
|
2020-08-29 21:34:33 +08:00
|
|
|
{
|
2022-12-23 14:55:22 +09:00
|
|
|
let canvas = canvas_common.raw.clone();
|
2020-08-29 21:34:33 +08:00
|
|
|
self.on_pointer_release = Some(canvas_common.add_user_event(
|
|
|
|
|
"pointerup",
|
2023-06-04 01:44:53 +02:00
|
|
|
move |event: PointerEvent| match event.pointer_type().as_str() {
|
|
|
|
|
"touch" => touch_handler(
|
|
|
|
|
event.pointer_id(),
|
|
|
|
|
event::touch_position(&event, &canvas).to_physical(super::scale_factor()),
|
|
|
|
|
Force::Normalized(event.pressure() as f64),
|
|
|
|
|
),
|
|
|
|
|
"mouse" => mouse_handler(
|
|
|
|
|
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),
|
|
|
|
|
),
|
|
|
|
|
_ => (),
|
2020-08-29 21:34:33 +08:00
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 14:55:22 +09:00
|
|
|
pub fn on_mouse_press<M, T>(
|
|
|
|
|
&mut self,
|
2023-06-04 01:44:53 +02:00
|
|
|
canvas_common: &Common,
|
2022-12-23 14:55:22 +09:00
|
|
|
mut mouse_handler: M,
|
|
|
|
|
mut touch_handler: T,
|
2023-06-04 01:50:30 +02:00
|
|
|
prevent_default: bool,
|
2022-12-23 14:55:22 +09:00
|
|
|
) where
|
|
|
|
|
M: 'static + FnMut(i32, PhysicalPosition<f64>, MouseButton, ModifiersState),
|
|
|
|
|
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
|
2020-08-29 21:34:33 +08:00
|
|
|
{
|
|
|
|
|
let canvas = canvas_common.raw.clone();
|
|
|
|
|
self.on_pointer_press = Some(canvas_common.add_user_event(
|
|
|
|
|
"pointerdown",
|
|
|
|
|
move |event: PointerEvent| {
|
2023-06-04 01:50:30 +02:00
|
|
|
if prevent_default {
|
|
|
|
|
// prevent text selection
|
|
|
|
|
event.prevent_default();
|
|
|
|
|
// but still focus element
|
|
|
|
|
let _ = canvas.focus();
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 13:16:04 +02:00
|
|
|
match event.pointer_type().as_str() {
|
|
|
|
|
"touch" => {
|
|
|
|
|
touch_handler(
|
|
|
|
|
event.pointer_id(),
|
|
|
|
|
event::touch_position(&event, &canvas)
|
2023-06-04 01:44:53 +02:00
|
|
|
.to_physical(super::scale_factor()),
|
2023-06-02 13:16:04 +02:00
|
|
|
Force::Normalized(event.pressure() as f64),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
"mouse" => {
|
|
|
|
|
mouse_handler(
|
|
|
|
|
event.pointer_id(),
|
2023-06-04 01:44:53 +02:00
|
|
|
event::mouse_position(&event).to_physical(super::scale_factor()),
|
2023-06-02 13:16:04 +02:00
|
|
|
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
|
|
|
|
|
// clicked when the cursor is 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());
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
2022-12-23 14:55:22 +09:00
|
|
|
}
|
2020-08-29 21:34:33 +08:00
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 01:08:03 +02:00
|
|
|
pub fn on_cursor_move<MOD, M, T, B>(
|
2022-09-04 13:45:30 +10:00
|
|
|
&mut self,
|
2023-06-04 01:44:53 +02:00
|
|
|
canvas_common: &Common,
|
2023-06-04 01:08:03 +02:00
|
|
|
mut modifier_handler: MOD,
|
2022-12-23 14:55:22 +09:00
|
|
|
mut mouse_handler: M,
|
|
|
|
|
mut touch_handler: T,
|
2023-06-04 01:08:03 +02:00
|
|
|
mut button_handler: B,
|
2022-09-04 13:45:30 +10:00
|
|
|
prevent_default: bool,
|
|
|
|
|
) where
|
2023-06-04 01:08:03 +02:00
|
|
|
MOD: 'static + FnMut(ModifiersState),
|
|
|
|
|
M: 'static + FnMut(i32, PhysicalPosition<f64>, PhysicalPosition<f64>),
|
2022-12-23 14:55:22 +09:00
|
|
|
T: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
|
2023-06-04 01:08:03 +02:00
|
|
|
B: 'static + FnMut(i32, PhysicalPosition<f64>, ButtonsState, MouseButton),
|
2020-08-29 21:34:33 +08:00
|
|
|
{
|
2022-12-23 14:55:22 +09:00
|
|
|
let canvas = canvas_common.raw.clone();
|
2020-08-29 21:34:33 +08:00
|
|
|
self.on_cursor_move = Some(canvas_common.add_event(
|
|
|
|
|
"pointermove",
|
|
|
|
|
move |event: PointerEvent| {
|
2023-06-02 14:23:09 +02:00
|
|
|
// coalesced events are not available on Safari
|
|
|
|
|
#[wasm_bindgen]
|
|
|
|
|
extern "C" {
|
|
|
|
|
#[wasm_bindgen(extends = PointerEvent)]
|
|
|
|
|
type PointerEventExt;
|
|
|
|
|
|
|
|
|
|
#[wasm_bindgen(method, getter, js_name = getCoalescedEvents)]
|
|
|
|
|
fn has_get_coalesced_events(this: &PointerEventExt) -> JsValue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 01:08:03 +02:00
|
|
|
modifier_handler(event::mouse_modifiers(&event));
|
|
|
|
|
|
|
|
|
|
let pointer_type = event.pointer_type();
|
|
|
|
|
|
|
|
|
|
match pointer_type.as_str() {
|
2023-06-02 13:16:04 +02:00
|
|
|
"touch" => {
|
|
|
|
|
if prevent_default {
|
|
|
|
|
// prevent scroll on mobile web
|
|
|
|
|
event.prevent_default();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
"mouse" => (),
|
|
|
|
|
_ => return,
|
2023-06-02 14:23:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let id = event.pointer_id();
|
2023-06-04 01:08:03 +02:00
|
|
|
|
|
|
|
|
// chorded button event
|
|
|
|
|
if let Some(button) = event::mouse_button(&event) {
|
|
|
|
|
debug_assert_eq!(
|
|
|
|
|
pointer_type, "mouse",
|
|
|
|
|
"expect pointer type of a chorded button event to be a mouse"
|
|
|
|
|
);
|
|
|
|
|
|
2023-06-04 01:50:30 +02:00
|
|
|
if prevent_default {
|
|
|
|
|
// prevent text selection
|
|
|
|
|
event.prevent_default();
|
|
|
|
|
// but still focus element
|
|
|
|
|
let _ = canvas.focus();
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 01:08:03 +02:00
|
|
|
button_handler(
|
|
|
|
|
id,
|
2023-06-04 01:44:53 +02:00
|
|
|
event::mouse_position(&event).to_physical(super::scale_factor()),
|
2023-06-02 11:37:23 +02:00
|
|
|
event::mouse_buttons(&event),
|
2023-06-04 01:08:03 +02:00
|
|
|
button,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pointer move event
|
|
|
|
|
|
|
|
|
|
let event: PointerEventExt = event.unchecked_into();
|
2023-06-02 14:23:09 +02:00
|
|
|
|
|
|
|
|
// store coalesced events to extend it's lifetime
|
|
|
|
|
let events = (!event.has_get_coalesced_events().is_undefined())
|
2023-06-04 01:08:03 +02:00
|
|
|
.then(|| event.get_coalesced_events());
|
2023-06-02 14:23:09 +02:00
|
|
|
|
|
|
|
|
// make a single iterator depending on the availability of coalesced events
|
|
|
|
|
let events = if let Some(events) = &events {
|
|
|
|
|
None.into_iter().chain(
|
|
|
|
|
Some(events.iter().map(PointerEventExt::unchecked_from_js))
|
|
|
|
|
.into_iter()
|
|
|
|
|
.flatten(),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
Some(event).into_iter().chain(None.into_iter().flatten())
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for event in events {
|
2023-06-04 01:08:03 +02:00
|
|
|
match pointer_type.as_str() {
|
|
|
|
|
"mouse" => mouse_handler(
|
2023-06-02 14:23:09 +02:00
|
|
|
id,
|
2023-06-04 01:44:53 +02:00
|
|
|
event::mouse_position(&event).to_physical(super::scale_factor()),
|
|
|
|
|
event::mouse_delta(&event).to_physical(super::scale_factor()),
|
2023-06-04 01:08:03 +02:00
|
|
|
),
|
|
|
|
|
"touch" => touch_handler(
|
2023-06-02 14:23:09 +02:00
|
|
|
id,
|
|
|
|
|
event::touch_position(&event, &canvas)
|
2023-06-04 01:44:53 +02:00
|
|
|
.to_physical(super::scale_factor()),
|
2023-06-02 14:23:09 +02:00
|
|
|
Force::Normalized(event.pressure() as f64),
|
2023-06-04 01:08:03 +02:00
|
|
|
),
|
|
|
|
|
_ => unreachable!("didn't return early before"),
|
2023-06-02 14:23:09 +02:00
|
|
|
}
|
2022-12-23 14:55:22 +09:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-04 01:44:53 +02:00
|
|
|
pub fn on_touch_cancel<F>(&mut self, canvas_common: &Common, mut handler: F)
|
2022-12-23 14:55:22 +09:00
|
|
|
where
|
|
|
|
|
F: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
|
|
|
|
|
{
|
|
|
|
|
let canvas = canvas_common.raw.clone();
|
|
|
|
|
self.on_touch_cancel = Some(canvas_common.add_event(
|
|
|
|
|
"pointercancel",
|
|
|
|
|
move |event: PointerEvent| {
|
|
|
|
|
if event.pointer_type() == "touch" {
|
|
|
|
|
handler(
|
|
|
|
|
event.pointer_id(),
|
2023-06-04 01:44:53 +02:00
|
|
|
event::touch_position(&event, &canvas).to_physical(super::scale_factor()),
|
2022-12-23 14:55:22 +09:00
|
|
|
Force::Normalized(event.pressure() as f64),
|
|
|
|
|
);
|
2022-09-04 13:45:30 +10:00
|
|
|
}
|
2020-08-29 21:34:33 +08:00
|
|
|
},
|
|
|
|
|
));
|
|
|
|
|
}
|
2020-09-21 06:42:07 +08:00
|
|
|
|
|
|
|
|
pub fn remove_listeners(&mut self) {
|
|
|
|
|
self.on_cursor_leave = None;
|
|
|
|
|
self.on_cursor_enter = None;
|
|
|
|
|
self.on_cursor_move = None;
|
|
|
|
|
self.on_pointer_press = None;
|
|
|
|
|
self.on_pointer_release = None;
|
2022-12-23 14:55:22 +09:00
|
|
|
self.on_touch_cancel = None;
|
2020-09-21 06:42:07 +08:00
|
|
|
}
|
2020-08-29 21:34:33 +08:00
|
|
|
}
|