diff --git a/src/views/mod.rs b/src/views/mod.rs index 2667693..04968fd 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -67,8 +67,6 @@ mod state; pub use state::*; mod tap; pub use tap::*; -mod tap_p; -pub use tap_p::*; mod text_editor; pub use text_editor::*; mod text; diff --git a/src/views/tap.rs b/src/views/tap.rs index f0c78fe..5978567 100644 --- a/src/views/tap.rs +++ b/src/views/tap.rs @@ -1,6 +1,128 @@ use crate::*; use std::any::Any; +pub trait TapFn { + fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option) -> A; +} + +pub struct TapFunc { + pub f: F +} + +impl) -> A> TapFn for TapFunc { + fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option) -> A { + (self.f)(cx, pt, button) + } +} + +pub struct TapAdapter { + pub f: F +} + +impl A> TapFn for TapAdapter { + fn call(&self, cx: &mut Context, _pt: LocalPoint, _button: Option) -> A { + (self.f)(cx) + } +} + +/// Struct for the `tap` gesture. +pub struct TapP { + /// Child view tree. + child: V, + + /// Called when a tap occurs. + func: F, + + phantom_a: std::marker::PhantomData +} + +impl TapP +where + V: View, + F: TapFn + 'static, +{ + pub fn new(v: V, f: F) -> Self { + Self { child: v, func: f, phantom_a: std::marker::PhantomData::default() } + } +} + +impl View for TapP +where + V: View, + F: TapFn + 'static, + A: '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; + } + } + Event::TouchEnd { id, position } => { + if cx.touches[*id] == vid { + cx.touches[*id] = ViewId::default(); + actions.push(Box::new(self.func.call(cx, *position, cx.mouse_button))) + } + } + _ => (), + } + } + + 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 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 TapP where V: View {} + /// Struct for the `tap_a` gesture. pub struct TapA { /// Child view tree. diff --git a/src/views/tap_p.rs b/src/views/tap_p.rs deleted file mode 100644 index 6687b4f..0000000 --- a/src/views/tap_p.rs +++ /dev/null @@ -1,124 +0,0 @@ -use crate::*; -use std::any::Any; - -pub trait TapFn { - fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option) -> A; -} - -pub struct TapFunc { - pub f: F -} - -impl) -> A> TapFn for TapFunc { - fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option) -> A { - (self.f)(cx, pt, button) - } -} - -pub struct TapAdapter { - pub f: F -} - -impl A> TapFn for TapAdapter { - fn call(&self, cx: &mut Context, _pt: LocalPoint, _button: Option) -> A { - (self.f)(cx) - } -} - -/// Struct for the `tap` gesture. -pub struct TapP { - /// Child view tree. - child: V, - - /// Called when a tap occurs. - func: F, - - phantom_a: std::marker::PhantomData -} - -impl TapP -where - V: View, - F: TapFn + 'static, -{ - pub fn new(v: V, f: F) -> Self { - Self { child: v, func: f, phantom_a: std::marker::PhantomData::default() } - } -} - -impl View for TapP -where - V: View, - F: TapFn + 'static, - A: '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; - } - } - Event::TouchEnd { id, position } => { - if cx.touches[*id] == vid { - cx.touches[*id] = ViewId::default(); - actions.push(Box::new(self.func.call(cx, *position, cx.mouse_button))) - } - } - _ => (), - } - } - - 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 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 TapP where V: View {}