Skip to content

Commit

Permalink
Remove DragP
Browse files Browse the repository at this point in the history
  • Loading branch information
wtholliday committed Dec 10, 2023
1 parent 6f374b6 commit d87c4f9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 207 deletions.
8 changes: 4 additions & 4 deletions src/modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ pub trait Modifiers: View + Sized {
fn drag<F: Fn(&mut Context, LocalOffset, GestureState, Option<MouseButton>) + 'static>(
self,
f: F,
) -> Drag<Self, F> {
Drag::new(self, f)
) -> Drag<Self, DragFunc<F>> {
Drag::new(self, DragFunc { f })
}

/// Calls a function in response to a drag. Version which passes the position.
fn drag_p<F: Fn(&mut Context, LocalPoint, GestureState, Option<MouseButton>) + 'static>(
self,
f: F,
) -> DragP<Self, DragFuncP<F>> {
DragP::new(self, DragFuncP { f })
) -> Drag<Self, DragFuncP<F>> {
Drag::new(self, DragFuncP { f })
}

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

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

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

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,
_delta: LocalOffset,
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` and `drag_p` gestures.
pub struct Drag<V, F> {
child: V,
func: F,
grab: bool,
}

impl<V, F, A> Drag<V, F>
impl<V, F> Drag<V, F>
where
V: View,
F: Fn(&mut Context, LocalOffset, GestureState, Option<MouseButton>) -> A + 'static,
F: DragFn + 'static,
{
pub fn new(v: V, f: F) -> Self {
Self {
Expand All @@ -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<V, F, A> View for Drag<V, F>
impl<V, F> View for Drag<V, F>
where
V: View,
F: Fn(&mut Context, LocalOffset, GestureState, Option<MouseButton>) -> A + 'static,
A: 'static,
F: DragFn + 'static,
{
fn process(
&self,
Expand All @@ -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 {
Expand All @@ -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,
);
}
}
_ => (),
Expand Down
184 changes: 0 additions & 184 deletions src/views/drag_p.rs

This file was deleted.

2 changes: 0 additions & 2 deletions src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d87c4f9

Please sign in to comment.