Skip to content

Commit

Permalink
Refactor add touch_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ryo33 committed May 26, 2022
1 parent 3c5635f commit 1911832
Showing 1 changed file with 30 additions and 64 deletions.
94 changes: 30 additions & 64 deletions src/platform_impl/web/web_sys/canvas/pointer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,88 +115,36 @@ impl PointerHandler {
));
}

pub fn on_touch_move<F>(&mut self, canvas_common: &super::Common, mut handler: F)
pub fn on_touch_move<F>(&mut self, canvas_common: &super::Common, handler: F)
where
F: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
{
let canvas = canvas_common.raw.clone();
self.on_touch_move = Some(canvas_common.add_event(
"pointermove",
move |event: PointerEvent| {
if event.pointer_type() != "touch" {
return;
}

handler(
event.pointer_id(),
touch_physical_position(&event, &canvas),
Force::Normalized(event.pressure() as f64),
);
},
));
self.on_touch_move =
Some(canvas_common.add_event("pointermove", touch_handler(handler, canvas_common)));
}

pub fn on_touch_down<F>(&mut self, canvas_common: &super::Common, mut handler: F)
pub fn on_touch_down<F>(&mut self, canvas_common: &super::Common, handler: F)
where
F: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
{
let canvas = canvas_common.raw.clone();
self.on_touch_down = Some(canvas_common.add_event(
"pointerdown",
move |event: PointerEvent| {
if event.pointer_type() != "touch" {
return;
}

handler(
event.pointer_id(),
touch_physical_position(&event, &canvas),
Force::Normalized(event.pressure() as f64),
);
},
));
self.on_touch_down =
Some(canvas_common.add_event("pointerdown", touch_handler(handler, canvas_common)));
}

pub fn on_touch_up<F>(&mut self, canvas_common: &super::Common, mut handler: F)
pub fn on_touch_up<F>(&mut self, canvas_common: &super::Common, handler: F)
where
F: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
{
let canvas = canvas_common.raw.clone();
self.on_touch_up = Some(canvas_common.add_event(
"pointerup",
move |event: PointerEvent| {
if event.pointer_type() != "touch" {
return;
}

handler(
event.pointer_id(),
touch_physical_position(&event, &canvas),
Force::Normalized(event.pressure() as f64),
);
},
));
self.on_touch_up =
Some(canvas_common.add_event("pointerup", touch_handler(handler, canvas_common)));
}

pub fn on_touch_cancel<F>(&mut self, canvas_common: &super::Common, mut handler: F)
pub fn on_touch_cancel<F>(&mut self, canvas_common: &super::Common, handler: F)
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" {
return;
}

handler(
event.pointer_id(),
touch_physical_position(&event, &canvas),
Force::Normalized(event.pressure() as f64),
);
},
));
self.on_touch_cancel =
Some(canvas_common.add_event("pointercancel", touch_handler(handler, canvas_common)));
}

pub fn remove_listeners(&mut self) {
Expand All @@ -212,6 +160,24 @@ impl PointerHandler {
}
}

fn touch_handler<F>(mut handler: F, canvas_common: &super::Common) -> impl FnMut(PointerEvent)
where
F: 'static + FnMut(i32, PhysicalPosition<f64>, Force),
{
let canvas = canvas_common.raw.clone();
move |event: PointerEvent| {
if event.pointer_type() != "touch" {
return;
}

handler(
event.pointer_id(),
touch_physical_position(&event, &canvas),
Force::Normalized(event.pressure() as f64),
);
}
}

fn touch_physical_position(
event: &PointerEvent,
canvas: &HtmlCanvasElement,
Expand Down

0 comments on commit 1911832

Please sign in to comment.