1use crate::{ControlInput, LayoutNode, Point, Rect, Size, Target, TextMeasurer, linear};
2use xrack_runtime::{ControlAction, Node, RowKey, SwipeSide};
3
4#[derive(Clone, Copy, Debug, PartialEq)]
5pub struct SwipeDelegateGeometry {
6 bounds: Rect,
7 widths: [f32; 2],
8 pub side: SwipeSide,
9 offset: f32,
10 preview: bool,
11}
12
13pub(crate) struct Region {
14 pub(crate) bounds: Rect,
15 pub(crate) clip: Rect,
16 pub(crate) enabled: bool,
17}
18
19impl SwipeDelegateGeometry {
20 pub(crate) fn new(node: &Node, bounds: Rect, text: &mut impl TextMeasurer) -> Self {
21 let available = Size::new(bounds.width, bounds.height);
22 let limit = (bounds.width - 24.0).max(0.0);
23 let widths = [SwipeSide::Left, SwipeSide::Right].map(|side| {
24 node.swipe_panel(side).map_or(0.0, |panel| {
25 linear::measure(panel, available, text)
26 .x
27 .preferred
28 .min(limit)
29 })
30 });
31 let mut geometry = Self {
32 bounds,
33 widths,
34 side: SwipeSide::Closed,
35 offset: 0.0,
36 preview: false,
37 };
38 let side = node.swipe_side();
39 if geometry.supports(side) {
40 geometry.side = side;
41 geometry.offset = geometry.position(side);
42 }
43 geometry
44 }
45
46 pub fn supports(&self, side: SwipeSide) -> bool {
47 side == SwipeSide::Closed || self.position(side) != 0.0
48 }
49
50 pub fn toggle(&self) -> Option<(Rect, ControlAction)> {
51 if !self.actions_available() {
52 return None;
53 }
54 let content = self.region("SwipeContent").bounds.intersect(self.bounds);
55 let width = content.width.min(28.0);
56 let height = content.height.min(32.0);
57 let rect = Rect::new(
58 content.x + content.width - width,
59 content.y + (content.height - height) * 0.5,
60 width,
61 height,
62 );
63 let side = if self.side != SwipeSide::Closed {
64 SwipeSide::Closed
65 } else if self.supports(SwipeSide::Right) {
66 SwipeSide::Right
67 } else {
68 SwipeSide::Left
69 };
70 Some((rect, ControlAction::SwipeDelegate(side)))
71 }
72
73 pub(crate) fn actions_available(&self) -> bool {
74 !self.preview && self.widths != [0.0, 0.0]
75 }
76
77 fn position(&self, side: SwipeSide) -> f32 {
78 match side {
79 SwipeSide::Closed => 0.0,
80 SwipeSide::Left => self.widths[0],
81 SwipeSide::Right => -self.widths[1],
82 }
83 }
84
85 pub(crate) fn set_preview(&mut self, offset: f32) {
86 self.offset = offset.clamp(-self.widths[1], self.widths[0]);
87 self.preview = true;
88 }
89
90 pub(crate) fn region(&self, kind: &str) -> Region {
91 let bounds = self.bounds;
92 let (bounds, clip, enabled) = match kind {
93 "SwipeContent" => (
94 Rect {
95 x: bounds.x + self.offset,
96 ..bounds
97 },
98 bounds,
99 true,
100 ),
101 "SwipeLeft" => (
102 Rect {
103 width: self.widths[0],
104 ..bounds
105 },
106 Rect {
107 width: self.offset.max(0.0),
108 ..bounds
109 },
110 self.side == SwipeSide::Left && !self.preview,
111 ),
112 "SwipeRight" => {
113 let right = bounds.x + bounds.width;
114 (
115 Rect {
116 x: right - self.widths[1],
117 width: self.widths[1],
118 ..bounds
119 },
120 Rect {
121 x: right + self.offset.min(0.0),
122 width: (-self.offset).max(0.0),
123 ..bounds
124 },
125 self.side == SwipeSide::Right && !self.preview,
126 )
127 }
128 _ => (Rect::default(), Rect::default(), false),
129 };
130 Region {
131 bounds,
132 clip,
133 enabled,
134 }
135 }
136}
137
138#[derive(Clone, Debug)]
139pub(crate) struct SwipeDrag {
140 pub(crate) target: Target,
141 start: Point,
142 initial: SwipeSide,
143 geometry: SwipeDelegateGeometry,
144 offset: Option<f32>,
145}
146
147impl SwipeDrag {
148 pub(crate) fn new(
149 target: Target,
150 node: &Node,
151 item: &LayoutNode,
152 start: Point,
153 ) -> Option<Self> {
154 let geometry = *item.swipe_delegate.as_deref()?;
155 (geometry.widths != [0.0, 0.0]).then_some(Self {
156 target,
157 start,
158 initial: node.swipe_side(),
159 geometry,
160 offset: None,
161 })
162 }
163
164 fn offset_at(&self, point: Point) -> f32 {
165 (self.geometry.position(self.geometry.side) + point.x - self.start.x)
166 .clamp(-self.geometry.widths[1], self.geometry.widths[0])
167 }
168
169 pub(crate) fn accepts_direction(&self, point: Point) -> bool {
170 (point.x - self.start.x).abs() > (point.y - self.start.y).abs()
171 && self.offset_at(point) != self.geometry.position(self.geometry.side)
172 }
173
174 fn matches(&self, node: &Node, geometry: &SwipeDelegateGeometry, row: Option<RowKey>) -> bool {
175 node.kind == "SwipeDelegate"
176 && self.target.matches_node(node)
177 && node.swipe_side() == self.initial
178 && row == self.target.row_key
179 && self.geometry.bounds == geometry.bounds
180 && self.geometry.widths == geometry.widths
181 }
182
183 fn is_current(&self, scene: &Node, layout: &LayoutNode) -> bool {
184 let path = &self.target.path;
185 scene
186 .interactive_at_path(path)
187 .zip(layout.at_path(path))
188 .is_some_and(|(node, item)| {
189 item.enabled
190 && item.visible
191 && item.swipe_delegate.as_deref().is_some_and(|geometry| {
192 self.matches(node, geometry, scene.row_key_at(path))
193 })
194 })
195 }
196
197 pub(crate) fn update(&mut self, scene: &Node, layout: &LayoutNode, point: Point) -> bool {
198 let offset = self
199 .is_current(scene, layout)
200 .then(|| self.offset_at(point));
201 let changed = self.offset != offset;
202 self.offset = offset;
203 changed
204 }
205
206 pub(crate) fn preview(
207 &self,
208 path: &[usize],
209 node: &Node,
210 geometry: &SwipeDelegateGeometry,
211 row: Option<RowKey>,
212 ) -> Option<f32> {
213 self.offset
214 .filter(|_| self.target.path == path && self.matches(node, geometry, row))
215 }
216
217 pub(crate) fn has_preview(&self) -> bool {
218 self.offset.is_some()
219 }
220
221 pub(crate) fn preview_state(&self) -> Option<(&[usize], Option<RowKey>, f32)> {
222 Some((&self.target.path, self.target.row_key, self.offset?))
223 }
224
225 pub(crate) fn finish(&self, scene: &Node, layout: &LayoutNode) -> Option<ControlInput> {
226 let offset = self.offset?;
227 let side = if offset > 0.0 && offset >= self.geometry.widths[0] * 0.5 {
228 SwipeSide::Left
229 } else if offset < 0.0 && -offset >= self.geometry.widths[1] * 0.5 {
230 SwipeSide::Right
231 } else {
232 SwipeSide::Closed
233 };
234 (side != self.initial && self.is_current(scene, layout)).then(|| ControlInput {
235 path: self.target.path.clone(),
236 action: ControlAction::SwipeDelegate(side),
237 })
238 }
239}