1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//! Foundation slider with caller-owned state and composable visual content.
#![allow(non_snake_case)]
use std::{cell::RefCell, rc::Rc};
use cranpose_core::{NodeId, State, rememberMutableStateOf, rememberUpdatedState};
use cranpose_foundation::{PointerEventKind, PointerId};
use crate::{
Modifier, MutableInteractionSource, composable,
widgets::{Box, BoxSpec, BoxWithConstraints, scopes::BoxWithConstraintsScope},
};
/// Main axis used by a [`Slider`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum SliderOrientation {
/// Values increase from start to end.
#[default]
Horizontal,
/// Values increase from top to bottom unless reversed.
Vertical,
}
/// Input and geometry policy for a [`Slider`].
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SliderSpec {
pub orientation: SliderOrientation,
pub reverse_direction: bool,
pub thumb_extent: f32,
pub enabled: bool,
pub rotary_step: f32,
}
impl SliderSpec {
pub fn new() -> Self {
Self::default()
}
pub fn orientation(mut self, orientation: SliderOrientation) -> Self {
self.orientation = orientation;
self
}
pub fn reverse_direction(mut self, reverse_direction: bool) -> Self {
self.reverse_direction = reverse_direction;
self
}
pub fn thumb_extent(mut self, thumb_extent: f32) -> Self {
self.thumb_extent = thumb_extent.max(0.0);
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn rotary_step(mut self, rotary_step: f32) -> Self {
self.rotary_step = rotary_step.abs();
self
}
}
impl Default for SliderSpec {
fn default() -> Self {
Self {
orientation: SliderOrientation::Horizontal,
reverse_direction: false,
thumb_extent: 0.0,
enabled: true,
rotary_step: 0.05,
}
}
}
/// Values available to custom slider track and thumb content.
#[derive(Clone)]
pub struct SliderScope {
value: f32,
track_extent: f32,
thumb_offset: f32,
dragging: State<bool>,
interaction_source: MutableInteractionSource,
}
impl SliderScope {
/// Current caller-owned value, clamped to `0..=1`.
pub fn value(&self) -> f32 {
self.value
}
/// Main-axis space through which the thumb can travel.
pub fn track_extent(&self) -> f32 {
self.track_extent
}
/// Main-axis offset for the leading edge of the thumb.
pub fn thumb_offset(&self) -> f32 {
self.thumb_offset
}
/// Whether direct pointer input is currently changing the value.
pub fn is_dragging(&self) -> bool {
self.dragging.get()
}
/// Interaction source shared by the slider surface.
pub fn interaction_source(&self) -> MutableInteractionSource {
self.interaction_source
}
}
/// A `0..=1` slider whose visuals are ordinary composables supplied by
/// `content`. The framework owns pointer capture, cancellation, rotary input,
/// pressed interactions, value mapping, and completion delivery.
#[composable]
pub fn Slider<F>(
modifier: Modifier,
value: f32,
on_value_change: impl Fn(f32) + 'static,
on_value_change_finished: impl Fn() + 'static,
spec: SliderSpec,
content: F,
) -> NodeId
where
F: FnMut(SliderScope) + 'static,
{
let value = value.clamp(0.0, 1.0);
let current_value = rememberUpdatedState(value);
let on_value_change: Rc<dyn Fn(f32)> = Rc::new(on_value_change);
let on_value_change = rememberUpdatedState(on_value_change);
let on_value_change_finished: Rc<dyn Fn()> = Rc::new(on_value_change_finished);
let on_value_change_finished = rememberUpdatedState(on_value_change_finished);
let dragging = rememberMutableStateOf(|| false);
let interaction_source = crate::rememberMutableInteractionSource();
let content = Rc::new(RefCell::new(content));
BoxWithConstraints(modifier, move |constraints_scope| {
let constraints = constraints_scope.constraints();
let extent = match spec.orientation {
SliderOrientation::Horizontal => constraints.max_width,
SliderOrientation::Vertical => constraints.max_height,
}
.max(0.0);
let track_extent = (extent - spec.thumb_extent).max(0.0);
let logical_value = if spec.reverse_direction {
1.0 - value
} else {
value
};
let slider_scope = SliderScope {
value,
track_extent,
thumb_offset: track_extent * logical_value,
dragging: dragging.as_state(),
interaction_source,
};
let interaction = interaction_source;
let content = Rc::clone(&content);
let input = Modifier::empty()
.fill_max_size()
.semantics(move |config| {
config.enabled = spec.enabled;
config.state_description = Some(format!("{}%", (value * 100.0).round() as u32));
})
.pointer_input(
(
spec.orientation,
spec.reverse_direction,
spec.thumb_extent.to_bits(),
spec.enabled,
spec.rotary_step.to_bits(),
extent.to_bits(),
),
move |pointer_scope| {
let interaction = interaction;
async move {
pointer_scope
.await_pointer_event_scope(|await_scope| async move {
let mut active_pointer: Option<PointerId> = None;
let mut active_press = None;
loop {
let event = await_scope.await_pointer_event().await;
match event.kind {
PointerEventKind::Down
if spec.enabled && active_pointer.is_none() =>
{
active_pointer = Some(event.id);
dragging.set(true);
active_press = Some(interaction.press(event.position));
let next = value_for_position(
axis_position(
event.position.x,
event.position.y,
spec,
),
extent,
spec.thumb_extent,
spec.reverse_direction,
);
(on_value_change.value())(next);
event.consume();
}
PointerEventKind::Move
if active_pointer == Some(event.id) =>
{
let next = value_for_position(
axis_position(
event.position.x,
event.position.y,
spec,
),
extent,
spec.thumb_extent,
spec.reverse_direction,
);
(on_value_change.value())(next);
event.consume();
}
PointerEventKind::Up
if active_pointer == Some(event.id) =>
{
let next = value_for_position(
axis_position(
event.position.x,
event.position.y,
spec,
),
extent,
spec.thumb_extent,
spec.reverse_direction,
);
(on_value_change.value())(next);
if let Some(press) = active_press.take() {
interaction.release(press);
}
dragging.set(false);
active_pointer = None;
(on_value_change_finished.value())();
event.consume();
}
PointerEventKind::Cancel
if active_pointer == Some(event.id) =>
{
if let Some(press) = active_press.take() {
interaction.cancel(press);
}
dragging.set(false);
active_pointer = None;
(on_value_change_finished.value())();
event.consume();
}
PointerEventKind::RotaryScroll if spec.enabled => {
let delta = event.scroll_delta.y;
if delta != 0.0 {
let direction =
if spec.reverse_direction { 1.0 } else { -1.0 };
let next = (current_value.value()
+ direction
* delta.signum()
* spec.rotary_step)
.clamp(0.0, 1.0);
(on_value_change.value())(next);
(on_value_change_finished.value())();
event.consume();
}
}
_ => {}
}
}
})
.await;
}
},
);
Box(input, BoxSpec::default(), move || {
(content.borrow_mut())(slider_scope.clone())
});
})
}
fn axis_position(x: f32, y: f32, spec: SliderSpec) -> f32 {
match spec.orientation {
SliderOrientation::Horizontal => x,
SliderOrientation::Vertical => y,
}
}
fn value_for_position(position: f32, extent: f32, thumb_extent: f32, reverse: bool) -> f32 {
let travel = (extent - thumb_extent).max(0.0);
let mut value = if travel > 0.0 {
((position - thumb_extent * 0.5) / travel).clamp(0.0, 1.0)
} else {
0.0
};
if reverse {
value = 1.0 - value;
}
value
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn spec_builders_define_orientation_and_input_policy() {
let spec = SliderSpec::new()
.orientation(SliderOrientation::Vertical)
.reverse_direction(true)
.thumb_extent(11.0)
.enabled(false)
.rotary_step(-0.2);
assert_eq!(spec.orientation, SliderOrientation::Vertical);
assert!(spec.reverse_direction);
assert_eq!(spec.thumb_extent, 11.0);
assert!(!spec.enabled);
assert_eq!(spec.rotary_step, 0.2);
}
#[test]
fn pointer_position_tracks_thumb_centre_and_reverse_direction() {
assert_eq!(value_for_position(5.0, 110.0, 10.0, false), 0.0);
assert_eq!(value_for_position(105.0, 110.0, 10.0, false), 1.0);
assert_eq!(value_for_position(55.0, 110.0, 10.0, false), 0.5);
assert_eq!(value_for_position(5.0, 110.0, 10.0, true), 1.0);
}
#[test]
fn zero_travel_is_stable() {
assert_eq!(value_for_position(0.0, 0.0, 0.0, false), 0.0);
assert_eq!(value_for_position(0.0, 0.0, 0.0, true), 1.0);
}
}