From d87c4f97b0025ddc3126222905ed22c0c7849d6e Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Sun, 10 Dec 2023 09:36:46 -0800 Subject: [PATCH] Remove DragP --- src/modifiers.rs | 8 +- src/views/drag.rs | 92 ++++++++++++++++++---- src/views/drag_p.rs | 184 -------------------------------------------- src/views/mod.rs | 2 - 4 files changed, 79 insertions(+), 207 deletions(-) delete mode 100644 src/views/drag_p.rs diff --git a/src/modifiers.rs b/src/modifiers.rs index 32b0ff5..19bad92 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -32,16 +32,16 @@ pub trait Modifiers: View + Sized { fn drag) + 'static>( self, f: F, - ) -> Drag { - Drag::new(self, f) + ) -> Drag> { + Drag::new(self, DragFunc { f }) } /// Calls a function in response to a drag. Version which passes the position. fn drag_p) + 'static>( self, f: F, - ) -> DragP> { - DragP::new(self, DragFuncP { f }) + ) -> Drag> { + Drag::new(self, DragFuncP { f }) } /// Calls a function in response to a drag. Version which passes in a binding. diff --git a/src/views/drag.rs b/src/views/drag.rs index 9350002..c78d954 100644 --- a/src/views/drag.rs +++ b/src/views/drag.rs @@ -8,17 +8,69 @@ pub enum GestureState { Ended, } -/// Struct for the `drag` gesture. +pub trait DragFn { + fn call( + &self, + cx: &mut Context, + pt: LocalPoint, + delta: LocalOffset, + state: GestureState, + button: Option, + actions: &mut Vec>, + ); +} + +pub struct DragFunc { + pub f: F, +} + +impl) -> A> DragFn + for DragFunc +{ + fn call( + &self, + cx: &mut Context, + _pt: LocalPoint, + delta: LocalOffset, + state: GestureState, + button: Option, + actions: &mut Vec>, + ) { + actions.push(Box::new((self.f)(cx, delta, state, button))) + } +} + +pub struct DragFuncP { + pub f: F, +} + +impl) -> A> DragFn + for DragFuncP +{ + fn call( + &self, + cx: &mut Context, + pt: LocalPoint, + _delta: LocalOffset, + state: GestureState, + button: Option, + actions: &mut Vec>, + ) { + actions.push(Box::new((self.f)(cx, pt, state, button))) + } +} + +/// Struct for the `drag` and `drag_p` gestures. pub struct Drag { child: V, func: F, grab: bool, } -impl Drag +impl Drag where V: View, - F: Fn(&mut Context, LocalOffset, GestureState, Option) -> A + 'static, + F: DragFn + 'static, { pub fn new(v: V, f: F) -> Self { Self { @@ -28,20 +80,19 @@ where } } - pub fn grab_cursor(self) -> Self { + pub fn grab_cursor(v: V, f: F) -> Self { Self { - child: self.child, - func: self.func, + child: v, + func: f, grab: true, } } } -impl View for Drag +impl View for Drag where V: View, - F: Fn(&mut Context, LocalOffset, GestureState, Option) -> A + 'static, - A: 'static, + F: DragFn + 'static, { fn process( &self, @@ -59,12 +110,14 @@ where cx.previous_position[*id] = *position; cx.grab_cursor = self.grab; - actions.push(Box::new((self.func)( + self.func.call( cx, - [0.0, 0.0].into(), + *position, + LocalOffset::zero(), GestureState::Began, cx.mouse_button, - ))); + actions, + ); } } Event::TouchMove { @@ -73,25 +126,30 @@ where delta, } => { if cx.touches[*id] == vid { - actions.push(Box::new((self.func)( + self.func.call( cx, + *position, *delta, GestureState::Changed, cx.mouse_button, - ))); + actions, + ); cx.previous_position[*id] = *position; } } - Event::TouchEnd { id, .. } => { + Event::TouchEnd { id, position } => { if cx.touches[*id] == vid { cx.touches[*id] = ViewId::default(); cx.grab_cursor = false; - actions.push(Box::new((self.func)( + + self.func.call( cx, + *position, LocalOffset::zero(), GestureState::Ended, cx.mouse_button, - ))); + actions, + ); } } _ => (), diff --git a/src/views/drag_p.rs b/src/views/drag_p.rs deleted file mode 100644 index 060af01..0000000 --- a/src/views/drag_p.rs +++ /dev/null @@ -1,184 +0,0 @@ -pub use super::drag::GestureState; -use crate::*; -use std::any::Any; - -pub trait DragFn { - fn call( - &self, - cx: &mut Context, - pt: LocalPoint, - delta: LocalOffset, - 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, - _delta: LocalOffset, - 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, - func: F, - grab: bool, -} - -impl DragP -where - V: View, - F: DragFn + 'static, -{ - pub fn new(v: V, f: F) -> Self { - Self { - child: v, - func: f, - grab: false, - } - } - - pub fn grab_cursor(v: V, f: F) -> Self { - Self { - child: v, - func: f, - grab: true, - } - } -} - -impl View for DragP -where - V: View, - F: DragFn + 'static, -{ - fn process( - &self, - event: &Event, - path: &mut IdPath, - cx: &mut Context, - actions: &mut Vec>, - ) { - let vid = cx.view_id(path); - match &event { - Event::TouchBegin { id, position } => { - if cx.touches[*id].is_default() && self.hittest(path, *position, cx).is_some() { - cx.touches[*id] = vid; - cx.starts[*id] = *position; - cx.previous_position[*id] = *position; - cx.grab_cursor = self.grab; - - self.func.call( - cx, - *position, - LocalOffset::zero(), - GestureState::Began, - cx.mouse_button, - actions, - ); - } - } - Event::TouchMove { - id, - position, - delta, - } => { - if cx.touches[*id] == vid { - self.func.call( - cx, - *position, - *delta, - GestureState::Changed, - cx.mouse_button, - actions, - ); - cx.previous_position[*id] = *position; - } - } - Event::TouchEnd { id, position } => { - if cx.touches[*id] == vid { - cx.touches[*id] = ViewId::default(); - cx.grab_cursor = false; - - self.func.call( - cx, - *position, - LocalOffset::zero(), - GestureState::Ended, - cx.mouse_button, - actions, - ); - } - } - _ => (), - } - } - - fn draw(&self, path: &mut IdPath, args: &mut DrawArgs) { - path.push(0); - self.child.draw(path, args); - path.pop(); - } - - fn layout(&self, path: &mut IdPath, args: &mut LayoutArgs) -> LocalSize { - path.push(0); - let sz = self.child.layout(path, args); - path.pop(); - sz - } - - fn dirty(&self, path: &mut IdPath, xform: LocalToWorld, cx: &mut Context) { - path.push(0); - self.child.dirty(path, xform, cx); - path.pop(); - } - - fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option { - path.push(0); - let id = self.child.hittest(path, pt, cx); - path.pop(); - id - } - - fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec) { - path.push(0); - self.child.commands(path, cx, cmds); - path.pop(); - } - - fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec) { - path.push(0); - self.child.gc(path, cx, map); - path.pop(); - } - - fn access( - &self, - path: &mut IdPath, - cx: &mut Context, - nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>, - ) -> Option { - path.push(0); - let node_id = self.child.access(path, cx, nodes); - path.pop(); - node_id - } -} - -impl private::Sealed for DragP {} diff --git a/src/views/mod.rs b/src/views/mod.rs index 5067431..6efcac7 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -16,8 +16,6 @@ mod cond; pub use cond::*; mod drag; pub use drag::*; -mod drag_p; -pub use drag_p::*; mod emptyview; pub use emptyview::*; mod env;