diff --git a/src/modifiers.rs b/src/modifiers.rs index a6bd7ea..fc490f0 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -40,8 +40,8 @@ pub trait Modifiers: View + Sized { fn drag_p) + 'static>( self, f: F, - ) -> DragP { - DragP::new(self, f) + ) -> DragP> { + DragP::new(self, DragFuncP{f}) } /// Calls a function in response to a drag. Version which passes in a binding. diff --git a/src/views/drag_p.rs b/src/views/drag_p.rs index bfda657..317b5df 100644 --- a/src/views/drag_p.rs +++ b/src/views/drag_p.rs @@ -2,6 +2,36 @@ pub use super::drag::GestureState; use crate::*; use std::any::Any; +pub trait DragFn { + fn call( + &self, + cx: &mut Context, + pt: LocalPoint, + state: GestureState, + button: Option, + actions: &mut Vec>, + ); +} + +pub struct DragFuncP { + pub f: F, +} + +impl) -> A> DragFn + for DragFuncP +{ + fn call( + &self, + cx: &mut Context, + pt: LocalPoint, + state: GestureState, + button: Option, + actions: &mut Vec>, + ) { + actions.push(Box::new((self.f)(cx, pt, state, button))) + } +} + /// Struct for the `drag_p` gesture. pub struct DragP { child: V, @@ -9,10 +39,10 @@ pub struct DragP { grab: bool, } -impl DragP +impl DragP where V: View, - F: Fn(&mut Context, LocalPoint, GestureState, Option) -> A + 'static, + F: DragFn + 'static, { pub fn new(v: V, f: F) -> Self { Self { @@ -31,11 +61,10 @@ where } } -impl View for DragP +impl View for DragP where V: View, - F: Fn(&mut Context, LocalPoint, GestureState, Option) -> A + 'static, - A: 'static, + F: DragFn + 'static, { fn process( &self, @@ -53,12 +82,7 @@ where cx.previous_position[*id] = *position; cx.grab_cursor = self.grab; - actions.push(Box::new((self.func)( - cx, - *position, - GestureState::Began, - cx.mouse_button, - ))); + self.func.call(cx, *position, GestureState::Began, cx.mouse_button, actions); } } Event::TouchMove { @@ -67,12 +91,8 @@ where delta: _, } => { if cx.touches[*id] == vid { - actions.push(Box::new((self.func)( - cx, - *position, - GestureState::Changed, - cx.mouse_button, - ))); + + self.func.call(cx, *position, GestureState::Changed, cx.mouse_button, actions); cx.previous_position[*id] = *position; } } @@ -80,12 +100,8 @@ where if cx.touches[*id] == vid { cx.touches[*id] = ViewId::default(); cx.grab_cursor = false; - actions.push(Box::new((self.func)( - cx, - *position, - GestureState::Ended, - cx.mouse_button, - ))); + + self.func.call(cx, *position, GestureState::Ended, cx.mouse_button, actions); } } _ => (),