Skip to content

Commit

Permalink
Reduce duplicate code for tap gesture (#58)
Browse files Browse the repository at this point in the history
* Try use TapP for both tap and tap_p

* Fix compilation

* Remove unused code

* Move code

* Rename

* Eliminate TapA

* Fix warning
  • Loading branch information
wtholliday authored Dec 10, 2023
1 parent f64d803 commit 49d60af
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 200 deletions.
12 changes: 6 additions & 6 deletions src/modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,22 @@ pub trait Modifiers: View + Sized {
}

/// Calls a function in response to a tap.
fn tap<A: 'static, F: Fn(&mut Context) -> A + 'static>(self, f: F) -> Tap<Self, F> {
Tap::new(self, f)
fn tap<A: 'static, F: Fn(&mut Context) -> A + 'static>(self, f: F) -> Tap<Self, TapAdapter::<F>, A> {
Tap::new(self, TapAdapter::<F>{f})
}

/// Version of `tap` which takes an action type instead
/// of a function.
fn tap_a<A: 'static>(self, action: A) -> TapA<Self, A> {
TapA::new(self, action)
fn tap_a<A: Clone + 'static>(self, action: A) -> Tap<Self, TapActionAdapter::<A>, A> {
Tap::new(self, TapActionAdapter::<A>{action})
}

/// Version of `tap` which passes the tap position and mouse button.
fn tap_p<A: 'static, F: Fn(&mut Context, LocalPoint, Option<MouseButton>) -> A + 'static>(
self,
f: F,
) -> TapP<Self, F> {
TapP::new(self, f)
) -> Tap<Self, TapFunc::<F>, A> {
Tap::new(self, TapFunc::<F>{f})
}

/// Specify the title of the window.
Expand Down
2 changes: 0 additions & 2 deletions src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
130 changes: 36 additions & 94 deletions src/views/tap.rs
Original file line number Diff line number Diff line change
@@ -1,124 +1,66 @@
use crate::*;
use std::any::Any;

/// Struct for the `tap` gesture.
pub struct Tap<V: View, F> {
/// Child view tree.
child: V,

/// Called when a tap occurs.
func: F,
pub trait TapFn<A> {
fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option<MouseButton>) -> A;
}

impl<V, F, A> Tap<V, F>
where
V: View,
F: Fn(&mut Context) -> A + 'static,
{
pub fn new(v: V, f: F) -> Self {
Self { child: v, func: f }
}
pub struct TapFunc<F> {
pub f: F
}

impl<V, F, A> View for Tap<V, F>
where
V: View,
F: Fn(&mut Context) -> A + 'static,
A: 'static,
{
fn process(
&self,
event: &Event,
path: &mut IdPath,
cx: &mut Context,
actions: &mut Vec<Box<dyn Any>>,
) {
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)(cx)))
}
}
_ => (),
}
}

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
impl<A: 'static, F: Fn(&mut Context, LocalPoint, Option<MouseButton>) -> A> TapFn<A> for TapFunc<F> {
fn call(&self, cx: &mut Context, pt: LocalPoint, button: Option<MouseButton>) -> A {
(self.f)(cx, pt, button)
}
}

fn hittest(&self, path: &mut IdPath, pt: LocalPoint, cx: &mut Context) -> Option<ViewId> {
path.push(0);
let id = self.child.hittest(path, pt, cx);
path.pop();
id
}
pub struct TapAdapter<F> {
pub f: F
}

fn commands(&self, path: &mut IdPath, cx: &mut Context, cmds: &mut Vec<CommandInfo>) {
path.push(0);
self.child.commands(path, cx, cmds);
path.pop();
impl<A: 'static, F: Fn(&mut Context) -> A> TapFn<A> for TapAdapter<F> {
fn call(&self, cx: &mut Context, _pt: LocalPoint, _button: Option<MouseButton>) -> A {
(self.f)(cx)
}
}

fn gc(&self, path: &mut IdPath, cx: &mut Context, map: &mut Vec<ViewId>) {
path.push(0);
self.child.gc(path, cx, map);
path.pop();
}
pub struct TapActionAdapter<A> {
pub action: A
}

fn access(
&self,
path: &mut IdPath,
cx: &mut Context,
nodes: &mut Vec<(accesskit::NodeId, accesskit::Node)>,
) -> Option<accesskit::NodeId> {
path.push(0);
let node_id = self.child.access(path, cx, nodes);
path.pop();
node_id
impl<A: Clone + 'static> TapFn<A> for TapActionAdapter<A> {
fn call(&self, _cx: &mut Context, _pt: LocalPoint, _button: Option<MouseButton>) -> A {
self.action.clone()
}
}

impl<V, F> private::Sealed for Tap<V, F> where V: View {}

/// Struct for the `tap_a` gesture.
pub struct TapA<V: View, A> {
/// Struct for the `tap` gesture.
pub struct Tap<V: View, F, A> {
/// Child view tree.
child: V,

/// Called when a tap occurs.
action: A,
func: F,

phantom_a: std::marker::PhantomData<A>
}

impl<V, A> TapA<V, A>
impl<V, F, A> Tap<V, F, A>
where
V: View,
F: TapFn<A> + 'static,
{
pub fn new(child: V, action: A) -> Self {
Self { child, action }
pub fn new(v: V, f: F) -> Self {
Self { child: v, func: f, phantom_a: std::marker::PhantomData::default() }
}
}

impl<V, A> View for TapA<V, A>
impl<V, F, A> View for Tap<V, F, A>
where
V: View,
A: Clone + 'static,
F: TapFn<A> + 'static,
A: 'static,
{
fn process(
&self,
Expand All @@ -134,10 +76,10 @@ where
cx.touches[*id] = vid;
}
}
Event::TouchEnd { id, position: _ } => {
Event::TouchEnd { id, position } => {
if cx.touches[*id] == vid {
cx.touches[*id] = ViewId::default();
actions.push(Box::new(self.action.clone()))
actions.push(Box::new(self.func.call(cx, *position, cx.mouse_button)))
}
}
_ => (),
Expand Down Expand Up @@ -189,4 +131,4 @@ where
}
}

impl<V, F> private::Sealed for TapA<V, F> where V: View {}
impl<V, F, A> private::Sealed for Tap<V, F, A> where V: View {}
98 changes: 0 additions & 98 deletions src/views/tap_p.rs

This file was deleted.

0 comments on commit 49d60af

Please sign in to comment.