use std::{cell::RefCell, rc::Rc};
use dioxus::html::point_interaction::InteractionLocation;
use dioxus::{html::PointerData};
use crate::state::gestures::pointer::{IncrementalOffsetPointer, InitialPointer, OffsetPointer};
#[derive(Clone)]
pub struct Drag {
pub on_start: Option<Rc<RefCell<dyn FnMut(DragStartData)>>>,
pub on_update: Option<Rc<RefCell<dyn FnMut(DragUpdateData)>>>,
pub on_end: Option<Rc<RefCell<dyn FnMut(DragEndData)>>>,
pub on_cancel: Option<Rc<RefCell<dyn FnMut(DragCancelData)>>>,
pub has_started: Rc<dyn Fn([&PointerData; 2]) -> bool>,
}
impl Drag {
pub fn on_start(mut self, handler: impl FnMut(DragStartData) + 'static) -> Self {
self.on_start = Some(Rc::new(RefCell::new(handler)));
self
}
pub fn on_update(mut self, handler: impl FnMut(DragUpdateData) + 'static) -> Self {
self.on_update = Some(Rc::new(RefCell::new(handler)));
self
}
pub fn on_end(mut self, handler: impl FnMut(DragEndData) + 'static) -> Self {
self.on_end = Some(Rc::new(RefCell::new(handler)));
self
}
pub fn on_cancel(mut self, handler: impl FnMut(DragCancelData) + 'static) -> Self {
self.on_cancel = Some(Rc::new(RefCell::new(handler)));
self
}
pub fn has_started(mut self, predicate: impl Fn([&PointerData; 2]) -> bool + 'static) -> Self {
self.has_started = Rc::new(predicate);
self
}
}
pub struct DragStartData {
pub pointer: InitialPointer,
}
pub struct DragUpdateData {
pub pointer: IncrementalOffsetPointer,
}
pub struct DragEndData {
pub pointer: OffsetPointer,
}
pub struct DragCancelData {
pub pointer: OffsetPointer,
}
impl Default for Drag {
fn default() -> Self {
Self {
on_start: Default::default(),
on_update: Default::default(),
on_end: Default::default(),
on_cancel: Default::default(),
has_started: Rc::new(|[initial, current]| {
(current.client_coordinates() - initial.client_coordinates()).length() >= 5.0
}),
}
}
}