xrack_layout/
swipe_gesture.rs1use crate::{Affine, Interaction, LayoutNode, Point, scale_rotate::gesture_target_live};
2use xrack_runtime::{Component, Length, Node, RowKey, SwipeHandle, SwipeSample};
3
4pub(crate) fn has_swipe_directions(node: &Node) -> bool {
5 [
6 "handle-swipe-left",
7 "handle-swipe-right",
8 "handle-swipe-up",
9 "handle-swipe-down",
10 ]
11 .into_iter()
12 .any(|name| node.boolean(name, false))
13}
14
15#[derive(Clone, Debug)]
16pub(crate) struct SwipeTarget {
17 path: Vec<usize>,
18 handle: SwipeHandle,
19 row: Option<RowKey>,
20 to_local: Affine,
21 origin: Point,
22 sample: SwipeSample,
23}
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub(crate) enum SwipePhase {
27 Moved,
28 Swiped,
29 Cancelled,
30}
31
32#[derive(Clone, Debug)]
33pub struct SwipeDelivery {
34 path: Vec<usize>,
35 handle: SwipeHandle,
36 row: Option<RowKey>,
37 sample: SwipeSample,
38 phase: SwipePhase,
39}
40
41impl SwipeDelivery {
42 pub fn matches(&self, scene: &Node) -> bool {
43 scene.row_key_at(&self.path) == self.row
44 && scene
45 .at_path(&self.path)
46 .and_then(|node| node.swipe_handle.as_ref())
47 == Some(&self.handle)
48 }
49
50 pub fn apply(&self, component: &mut impl Component, scene: &Node) {
51 if self.phase == SwipePhase::Moved
52 && (self.handle.cancel_requested() || !self.matches(scene))
53 {
54 return;
55 }
56 let event = match self.phase {
57 SwipePhase::Moved => "moved",
58 SwipePhase::Swiped if !self.handle.cancel_requested() => "swiped",
59 _ => "cancelled",
60 };
61 self.handle
62 .publish(self.sample, self.phase == SwipePhase::Moved);
63 if self.matches(scene) {
64 component.dispatch_at(scene, &self.path, event);
65 }
66 }
67}
68
69impl SwipeTarget {
70 pub fn new(scene: &Node, layout: &LayoutNode, path: &[usize], point: Point) -> Option<Self> {
71 let node = scene.interactive_at_path(path)?;
72 let handle = node.swipe_handle.clone()?;
73 if !has_swipe_directions(node) {
74 return None;
75 }
76 let item = layout.at_path(path)?;
77 let to_local = item.world_transform().inverse()?;
78 let local = to_local.map_point(point);
79 if !item.visible
80 || !item.enabled
81 || !item.bounds.contains(local)
82 || !item.contains_clip_point(point)
83 {
84 return None;
85 }
86 let origin = Point {
87 x: item.bounds.x,
88 y: item.bounds.y,
89 };
90 let x = Length(f64::from(local.x - origin.x));
91 let y = Length(f64::from(local.y - origin.y));
92 Some(Self {
93 path: path.to_vec(),
94 handle,
95 row: scene.row_key_at(path),
96 to_local,
97 origin,
98 sample: SwipeSample {
99 pressed_x: x,
100 pressed_y: y,
101 current_x: x,
102 current_y: y,
103 },
104 })
105 }
106
107 pub fn accepts(
108 &self,
109 scene: &Node,
110 layout: &LayoutNode,
111 input: &Interaction,
112 point: Point,
113 ) -> bool {
114 if !self.is_live(scene, layout, input) {
115 return false;
116 }
117 let [x, y] = self.coordinates(point);
118 let dx = x.0 - self.sample.pressed_x.0;
119 let dy = y.0 - self.sample.pressed_y.0;
120 if !dx.is_finite() || !dy.is_finite() || dx == 0.0 && dy == 0.0 {
121 return false;
122 }
123 let direction = if dx.abs() >= dy.abs() {
124 if dx < 0.0 {
125 "handle-swipe-left"
126 } else {
127 "handle-swipe-right"
128 }
129 } else if dy < 0.0 {
130 "handle-swipe-up"
131 } else {
132 "handle-swipe-down"
133 };
134 scene
135 .at_path(&self.path)
136 .is_some_and(|node| node.boolean(direction, false))
137 }
138
139 pub fn is_live(&self, scene: &Node, layout: &LayoutNode, input: &Interaction) -> bool {
140 gesture_target_live(scene, layout, input, &self.path, self.row)
141 && scene
142 .interactive_at_path(&self.path)
143 .and_then(|node| node.swipe_handle.as_ref())
144 == Some(&self.handle)
145 }
146
147 pub fn cancel_requested(&self) -> bool {
148 self.handle.cancel_requested()
149 }
150
151 pub fn update(&mut self, point: Point) -> Option<SwipeDelivery> {
152 let [x, y] = self.coordinates(point);
153 if !x.0.is_finite()
154 || !y.0.is_finite()
155 || (x == self.sample.current_x && y == self.sample.current_y)
156 {
157 return None;
158 }
159 self.sample.current_x = x;
160 self.sample.current_y = y;
161 Some(self.delivery(SwipePhase::Moved))
162 }
163
164 pub fn delivery(&self, phase: SwipePhase) -> SwipeDelivery {
165 SwipeDelivery {
166 path: self.path.clone(),
167 handle: self.handle.clone(),
168 row: self.row,
169 sample: self.sample,
170 phase,
171 }
172 }
173
174 fn coordinates(&self, point: Point) -> [Length; 2] {
175 let local = self.to_local.map_point(point);
177 [
178 Length(f64::from(local.x - self.origin.x)),
179 Length(f64::from(local.y - self.origin.y)),
180 ]
181 }
182}