From dd6062f29207c59cd7ee84299c5bd92bf4881d33 Mon Sep 17 00:00:00 2001 From: Taylor Holliday Date: Sun, 10 Dec 2023 09:57:46 -0800 Subject: [PATCH] Remove DragS --- src/modifiers.rs | 8 +- src/views/drag.rs | 191 +++++++++------------------------------------- 2 files changed, 40 insertions(+), 159 deletions(-) diff --git a/src/modifiers.rs b/src/modifiers.rs index 19bad92..d00c948 100644 --- a/src/modifiers.rs +++ b/src/modifiers.rs @@ -1,5 +1,7 @@ + use crate::*; use accesskit::Role; +use std::marker::PhantomData; /// Modifiers common to all views. pub trait Modifiers: View + Sized { @@ -51,10 +53,10 @@ pub trait Modifiers: View + Sized { F: Fn(&mut T, LocalOffset, GestureState, Option) + 'static, >( self, - s: B, + b: B, f: F, - ) -> DragS { - DragS::new(self, s, f) + ) -> Drag> { + Drag::new(self, DragFuncS { f, b, phantom: PhantomData::default() }) } /// Calls a function in response to a mouse hovering. diff --git a/src/views/drag.rs b/src/views/drag.rs index c78d954..72c4aac 100644 --- a/src/views/drag.rs +++ b/src/views/drag.rs @@ -1,5 +1,6 @@ use crate::*; use std::any::Any; +use std::marker::PhantomData; #[derive(Clone, Copy, Eq, PartialEq, Debug)] pub enum GestureState { @@ -60,6 +61,37 @@ impl { + pub f: F, + pub b: B, + pub phantom: PhantomData, +} + +impl< + F: Fn(&mut T, LocalOffset, GestureState, Option) -> A + 'static, + B: Binding, + A: 'static, + T: 'static, + > DragFn for DragFuncS +{ + fn call( + &self, + cx: &mut Context, + _pt: LocalPoint, + delta: LocalOffset, + state: GestureState, + button: Option, + actions: &mut Vec>, + ) { + actions.push(Box::new((self.f)( + self.b.get_mut(cx), + delta, + state, + button, + ))) + } +} + /// Struct for the `drag` and `drag_p` gestures. pub struct Drag { child: V, @@ -80,10 +112,10 @@ where } } - pub fn grab_cursor(v: V, f: F) -> Self { + pub fn grab_cursor(self) -> Self { Self { - child: v, - func: f, + child: self.child, + func: self.func, grab: true, } } @@ -209,159 +241,6 @@ where impl private::Sealed for Drag {} -/// Struct for the `drag` gesture. -pub struct DragS { - child: V, - func: F, - binding: B, - phantom: std::marker::PhantomData, - grab: bool, -} - -impl DragS -where - V: View, - F: Fn(&mut T, LocalOffset, GestureState, Option) -> A + 'static, - B: Binding, - A: 'static, - T: 'static, -{ - pub fn new(v: V, b: B, f: F) -> Self { - Self { - child: v, - func: f, - binding: b, - phantom: std::marker::PhantomData::default(), - grab: false, - } - } - - pub fn grab_cursor(self) -> Self { - Self { - child: self.child, - func: self.func, - binding: self.binding, - phantom: std::marker::PhantomData::default(), - grab: true, - } - } -} - -impl View for DragS -where - V: View, - F: Fn(&mut T, LocalOffset, GestureState, Option) -> A + 'static, - B: Binding, - A: 'static, - T: '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 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; - } - } - Event::TouchMove { - id, - position, - delta, - } => { - if cx.touches[*id] == vid { - let button = cx.mouse_button; - actions.push(Box::new((self.func)( - self.binding.get_mut(cx), - *delta, - GestureState::Changed, - button, - ))); - cx.previous_position[*id] = *position; - } - } - Event::TouchEnd { id, .. } => { - if cx.touches[*id] == vid { - cx.touches[*id] = ViewId::default(); - cx.grab_cursor = false; - let button = cx.mouse_button; - actions.push(Box::new((self.func)( - self.binding.get_mut(cx), - LocalOffset::zero(), - GestureState::Ended, - button, - ))); - } - } - _ => { - path.push(0); - self.child.process(event, path, cx, actions); - path.pop(); - } - } - } - - 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 DragS {} - #[cfg(test)] mod tests {