From 1911832f28263b1cd8742a1209a8ec58057aaf6c Mon Sep 17 00:00:00 2001 From: Ryo Hirayama Date: Thu, 26 May 2022 16:42:09 +0900 Subject: [PATCH] Refactor add touch_handler --- .../web/web_sys/canvas/pointer_handler.rs | 94 ++++++------------- 1 file changed, 30 insertions(+), 64 deletions(-) diff --git a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs index 9225ce9dfd..dc4db22474 100644 --- a/src/platform_impl/web/web_sys/canvas/pointer_handler.rs +++ b/src/platform_impl/web/web_sys/canvas/pointer_handler.rs @@ -115,88 +115,36 @@ impl PointerHandler { )); } - pub fn on_touch_move(&mut self, canvas_common: &super::Common, mut handler: F) + pub fn on_touch_move(&mut self, canvas_common: &super::Common, handler: F) where F: 'static + FnMut(i32, PhysicalPosition, 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(&mut self, canvas_common: &super::Common, mut handler: F) + pub fn on_touch_down(&mut self, canvas_common: &super::Common, handler: F) where F: 'static + FnMut(i32, PhysicalPosition, 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(&mut self, canvas_common: &super::Common, mut handler: F) + pub fn on_touch_up(&mut self, canvas_common: &super::Common, handler: F) where F: 'static + FnMut(i32, PhysicalPosition, 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(&mut self, canvas_common: &super::Common, mut handler: F) + pub fn on_touch_cancel(&mut self, canvas_common: &super::Common, handler: F) where F: 'static + FnMut(i32, PhysicalPosition, 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) { @@ -212,6 +160,24 @@ impl PointerHandler { } } +fn touch_handler(mut handler: F, canvas_common: &super::Common) -> impl FnMut(PointerEvent) +where + F: 'static + FnMut(i32, PhysicalPosition, 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,