Skip to content

Commit

Permalink
Add DragFn
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Dec 10, 2023
1 parent bed6fb2 commit 53c92e5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub trait Modifiers: View + Sized {
fn drag_p<F: Fn(&mut Context, LocalPoint, GestureState, Option<MouseButton>) + 'static>(
self,
f: F,
) -> DragP<Self, F> {
DragP::new(self, f)
) -> DragP<Self, DragFuncP<F>> {
DragP::new(self, DragFuncP{f})
}

/// Calls a function in response to a drag. Version which passes in a binding.
Expand Down
62 changes: 39 additions & 23 deletions src/views/drag_p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,47 @@ 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<MouseButton>,
actions: &mut Vec<Box<dyn Any>>,
);
}

pub struct DragFuncP<F> {
pub f: F,
}

impl<A: 'static, F: Fn(&mut Context, LocalPoint, GestureState, Option<MouseButton>) -> A> DragFn
for DragFuncP<F>
{
fn call(
&self,
cx: &mut Context,
pt: LocalPoint,
state: GestureState,
button: Option<MouseButton>,
actions: &mut Vec<Box<dyn Any>>,
) {
actions.push(Box::new((self.f)(cx, pt, state, button)))
}
}

/// Struct for the `drag_p` gesture.
pub struct DragP<V, F> {
child: V,
func: F,
grab: bool,
}

impl<V, F, A> DragP<V, F>
impl<V, F> DragP<V, F>
where
V: View,
F: Fn(&mut Context, LocalPoint, GestureState, Option<MouseButton>) -> A + 'static,
F: DragFn + 'static,
{
pub fn new(v: V, f: F) -> Self {
Self {
Expand All @@ -31,11 +61,10 @@ where
}
}

impl<V, F, A> View for DragP<V, F>
impl<V, F> View for DragP<V, F>
where
V: View,
F: Fn(&mut Context, LocalPoint, GestureState, Option<MouseButton>) -> A + 'static,
A: 'static,
F: DragFn + 'static,
{
fn process(
&self,
Expand All @@ -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 {
Expand All @@ -67,25 +91,17 @@ 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;
}
}
Event::TouchEnd { id, position } => {
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);
}
}
_ => (),
Expand Down

0 comments on commit 53c92e5

Please sign in to comment.