cranpose_ui/modifier/scroll/draggable.rs
1use std::rc::Rc;
2
3use cranpose_ui_layout::Axis;
4
5use super::{
6 ScrollTarget,
7 drag_gesture::{DragGesture, drag_gesture_input},
8};
9use crate::{
10 anchored_draggable::AnchoredDraggableState,
11 draggable::DraggableState,
12 local_layout_direction,
13 modifier::Modifier,
14 scroll::{ScrollMotionContextKey, scroll_motion_context_for_key},
15};
16
17impl Modifier {
18 /// Drags along `axis`, reporting each delta to `state`.
19 ///
20 /// The gesture is the one the scroll containers run — touch slop before a
21 /// drag begins, axis locking so a mostly-vertical drag never steals a
22 /// horizontal one, the primary pointer only, and yielding to whoever
23 /// consumed the event first — so a dragged control and a scrolled list feel
24 /// the same under a finger. Unlike a scroll, a release does not fling: a
25 /// control the user placed stays where it was let go.
26 ///
27 /// Deltas are logical pixels, positive to the right for [`Axis::Horizontal`]
28 /// and downwards for [`Axis::Vertical`].
29 pub fn draggable(self, axis: Axis, state: DraggableState) -> Self {
30 self.then(draggable_impl(axis, state.identity(), state, false, None))
31 }
32
33 /// Drags along `axis` only while `guard` says so.
34 ///
35 /// The guard is asked per event, so a control can stop accepting a drag
36 /// half way through one and the gesture ends rather than being applied to
37 /// a control that has since been disabled.
38 pub fn draggable_guarded(
39 self,
40 axis: Axis,
41 state: DraggableState,
42 guard: impl Fn() -> bool + 'static,
43 ) -> Self {
44 self.then(draggable_impl(
45 axis,
46 state.identity(),
47 state,
48 false,
49 Some(Rc::new(guard)),
50 ))
51 }
52
53 /// Drags `state` along `axis` between its anchors and settles it at one
54 /// when the finger lifts: Compose's `Modifier.anchoredDraggable`. The
55 /// drag takes the slop and the axis lock of a scroll, so a horizontal
56 /// row inside a vertical list leaves the list its scroll. In a
57 /// right-to-left layout a horizontal drag runs from the right edge, so
58 /// the anchors of `state` measure from the start edge as
59 /// [`Modifier::offset_with`] places them.
60 pub fn anchored_draggable<T: Copy + PartialEq + 'static>(
61 self,
62 axis: Axis,
63 state: AnchoredDraggableState<T>,
64 ) -> Self {
65 let reverse = axis == Axis::Horizontal && composed_right_to_left();
66 self.then(draggable_impl(axis, state.identity(), state, reverse, None))
67 }
68}
69
70// Whether the composition building this modifier lays out right to left.
71fn composed_right_to_left() -> bool {
72 cranpose_core::composer_context::current_composer().is_some()
73 && local_layout_direction().current().is_rtl()
74}
75
76// The drag of `state` along `axis`, keyed by its `identity`. A
77// `reverse_input` drag hands `state` the opposite of the pointer's deltas;
78// the key holds it, so a changed direction restarts the gesture.
79fn draggable_impl<S: ScrollTarget + 'static>(
80 axis: Axis,
81 identity: usize,
82 state: S,
83 reverse_input: bool,
84 guard: Option<Rc<dyn Fn() -> bool>>,
85) -> Modifier {
86 drag_gesture_input(
87 (identity, axis, reverse_input),
88 DragGesture {
89 target: state,
90 axis,
91 reverse_input,
92 motion_context: scroll_motion_context_for_key(ScrollMotionContextKey::Draggable {
93 state_identity: identity,
94 axis,
95 }),
96 guard,
97 },
98 )
99}