use std::rc::Rc;
use cranpose_ui_layout::Axis;
use super::{
ScrollTarget,
drag_gesture::{DragGesture, drag_gesture_input},
};
use crate::{
anchored_draggable::AnchoredDraggableState,
draggable::DraggableState,
local_layout_direction,
modifier::Modifier,
scroll::{ScrollMotionContextKey, scroll_motion_context_for_key},
};
impl Modifier {
pub fn draggable(self, axis: Axis, state: DraggableState) -> Self {
self.then(draggable_impl(axis, state.identity(), state, false, None))
}
pub fn draggable_guarded(
self,
axis: Axis,
state: DraggableState,
guard: impl Fn() -> bool + 'static,
) -> Self {
self.then(draggable_impl(
axis,
state.identity(),
state,
false,
Some(Rc::new(guard)),
))
}
pub fn anchored_draggable<T: Copy + PartialEq + 'static>(
self,
axis: Axis,
state: AnchoredDraggableState<T>,
) -> Self {
let reverse = axis == Axis::Horizontal && composed_right_to_left();
self.then(draggable_impl(axis, state.identity(), state, reverse, None))
}
}
fn composed_right_to_left() -> bool {
cranpose_core::composer_context::current_composer().is_some()
&& local_layout_direction().current().is_rtl()
}
fn draggable_impl<S: ScrollTarget + 'static>(
axis: Axis,
identity: usize,
state: S,
reverse_input: bool,
guard: Option<Rc<dyn Fn() -> bool>>,
) -> Modifier {
drag_gesture_input(
(identity, axis, reverse_input),
DragGesture {
target: state,
axis,
reverse_input,
motion_context: scroll_motion_context_for_key(ScrollMotionContextKey::Draggable {
state_identity: identity,
axis,
}),
guard,
},
)
}