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
//! A continuous slider: capsule track with a filled portion and a round
//! draggable thumb. Touching the thumb lifts it into a magnifying glass
//! lens (the reference "seek" thumb liquifies under the finger and rides
//! the drag); releasing lets the lens decay back into the white thumb.
use crate::material::{Glass, GlassDynamics, GlassMorph, LiquidModifierExt, LiquidShape};
use cranpose_animation::{animateFloatAsState, spring};
use cranpose_core::{mutableStateOf, remember};
use cranpose_foundation::{PointerEventKind, PointerId};
use cranpose_macros::composable;
use cranpose_ui::widgets::{Box, BoxSpec, BoxWithConstraints, BoxWithConstraintsScope};
use cranpose_ui::{Modifier, Size};
use cranpose_ui_graphics::{Brush, Color, CornerRadii, GraphicsLayer};
use std::cell::Cell;
use std::rc::Rc;
use crate::theme::liquid_colors;
const TRACK_HEIGHT: f32 = 6.0;
const THUMB_SIZE: f32 = 24.0;
const SLIDER_HEIGHT: f32 = 32.0;
/// The pressed lens circle riding the thumb (the reference seek thumb grows
/// ~1.4× and magnifies the track through itself).
const LENS_SIZE: f32 = 34.0;
/// Glass node span beyond the lens shape (rim glow + bulge live here).
const LENS_PAD: f32 = 10.0;
fn slider_deformation(pose: crate::dynamics::LiquidPose) -> crate::material::GlassDeformation {
pose.deformation()
}
/// A 0..=1 slider. The caller owns `value`; `on_change` streams new values
/// while dragging or on tap.
#[composable]
#[allow(non_snake_case)]
pub fn LiquidSlider(modifier: Modifier, value: f32, on_change: impl Fn(f32) + 'static) {
let colors = liquid_colors();
let value = value.clamp(0.0, 1.0);
let on_change = Rc::new(on_change);
let pressed = remember(|| mutableStateOf(false)).with(|s| *s);
let active_pointer = remember(|| Rc::new(Cell::new(Option::<PointerId>::None))).with(Rc::clone);
// Lens presence: fast on press, slow decay after release (the lens
// lingers briefly, like the toggle's).
let lens_progress = animateFloatAsState(
if pressed.get() { 1.0 } else { 0.0 },
if pressed.get() {
spring(0.9, 1400.0)
} else {
spring(1.0, 170.0)
},
"slider-lens",
);
Box(
Modifier::empty().height(SLIDER_HEIGHT).then(modifier),
BoxSpec::default(),
move || {
let on_change = Rc::clone(&on_change);
let active_pointer = Rc::clone(&active_pointer);
BoxWithConstraints(Modifier::empty(), move |scope| {
let width = scope.constraints().max_width.max(THUMB_SIZE);
let usable = (width - THUMB_SIZE).max(1.0);
let controlled_x = usable * value;
let lens_axis = crate::motion::remember_liquid_drag_axis(controlled_x);
lens_axis.settle_to(controlled_x, crate::motion::LiquidMotion::snappy());
let thumb_x = lens_axis.value();
let liquid_pose = lens_axis.liquid_pose();
// Interactive surface spanning the whole control: tap or drag
// anywhere on the track.
let on_drag = Rc::clone(&on_change);
let active_pointer = Rc::clone(&active_pointer);
let gesture_axis = Rc::clone(&lens_axis);
let surface = Modifier::empty()
.size(Size::new(width, SLIDER_HEIGHT))
.pointer_input((width.to_bits(), value.to_bits()), {
move |pointer_scope| {
let on_drag = Rc::clone(&on_drag);
let active_pointer = Rc::clone(&active_pointer);
let lens_axis = Rc::clone(&gesture_axis);
async move {
pointer_scope
.await_pointer_event_scope(|await_scope| async move {
loop {
let event = await_scope.await_pointer_event().await;
let fraction = ((event.position.x - THUMB_SIZE * 0.5)
/ usable)
.clamp(0.0, 1.0);
match event.kind {
PointerEventKind::Down
if active_pointer.get().is_none() =>
{
active_pointer.set(Some(event.id));
lens_axis
.begin(usable * fraction, event.time_ms);
pressed.set(true);
event.consume();
on_drag(fraction);
}
PointerEventKind::Move
if active_pointer.get() == Some(event.id) =>
{
lens_axis
.move_to(usable * fraction, event.time_ms);
event.consume();
on_drag(fraction);
}
PointerEventKind::Up
if active_pointer.get() == Some(event.id) =>
{
lens_axis.finish_at(
usable * fraction,
event.time_ms,
);
on_drag(fraction);
event.consume();
active_pointer.set(None);
pressed.set(false);
}
PointerEventKind::Cancel
if active_pointer.get() == Some(event.id) =>
{
lens_axis.release_to(
controlled_x,
event.time_ms,
crate::motion::LiquidMotion::snappy(),
);
event.consume();
active_pointer.set(None);
pressed.set(false);
}
_ => {}
}
}
})
.await;
}
}
});
Box(surface, BoxSpec::default(), move || {
// Track.
let track_fill = colors.fill;
let track = Modifier::empty()
.size(Size::new(width, TRACK_HEIGHT))
.offset(0.0, (SLIDER_HEIGHT - TRACK_HEIGHT) * 0.5)
.draw_behind(move |scope| {
scope.draw_round_rect(
Brush::solid(track_fill),
CornerRadii::uniform(TRACK_HEIGHT * 0.5),
);
});
Box(track, BoxSpec::default(), || {});
// Filled portion up to the thumb.
let accent = colors.accent;
let filled_width = (thumb_x + THUMB_SIZE * 0.5).max(TRACK_HEIGHT);
let filled = Modifier::empty()
.size(Size::new(filled_width, TRACK_HEIGHT))
.offset(0.0, (SLIDER_HEIGHT - TRACK_HEIGHT) * 0.5)
.draw_behind(move |scope| {
scope.draw_round_rect(
Brush::solid(accent),
CornerRadii::uniform(TRACK_HEIGHT * 0.5),
);
});
Box(filled, BoxSpec::default(), || {});
// Thumb: a plain white circle that dissolves into the
// glass as soon as the lens is up (the lens takes over
// the thumb's role while touched).
let lens_for_thumb = lens_progress;
let thumb = Modifier::empty()
.size(Size::new(THUMB_SIZE, THUMB_SIZE))
.offset(0.0, (SLIDER_HEIGHT - THUMB_SIZE) * 0.5)
.graphics_layer(move || {
let lens = lens_for_thumb.get();
GraphicsLayer {
translation_x: thumb_x,
alpha: ((0.30 - lens) / 0.22).clamp(0.0, 1.0),
..Default::default()
}
})
.drop_shadow(
cranpose_ui_graphics::LayerShape::Rounded(
cranpose_ui_graphics::RoundedCornerShape::uniform(THUMB_SIZE * 0.5),
),
|scope| {
scope.radius = 6.0;
scope.offset.y = 2.0;
scope.color = Color::BLACK.with_alpha(0.16);
},
)
.draw_behind(move |scope| {
scope.draw_round_rect(
Brush::solid(Color::WHITE),
CornerRadii::uniform(THUMB_SIZE * 0.5),
);
});
Box(thumb, BoxSpec::default(), || {});
// The interaction lens riding the thumb: the SDF circle
// inflates from thumb-size to the lens with a viscous
// bulge along the travel direction.
let node = LENS_SIZE + LENS_PAD * 2.0;
let lens_for_layer = lens_progress;
let lens = Modifier::empty()
// required_size: the lens node exceeds the 32dp
// control; the fixed-height host keeps layout put.
.required_size(Size::new(node, node))
.offset(0.0, (SLIDER_HEIGHT - node) * 0.5)
.graphics_layer(move || GraphicsLayer {
translation_x: thumb_x + (THUMB_SIZE - node) * 0.5,
alpha: (lens_for_layer.get() * 2.5).clamp(0.0, 1.0),
..Default::default()
})
.glass_effect_with(
Glass::lens()
.shape(LiquidShape::Circle)
.tint(Color::WHITE.with_alpha(0.05))
.highlight(0.52)
.no_clip(),
move || {
let grow = lens_for_layer.get().clamp(0.0, 1.2);
let d = THUMB_SIZE + (LENS_SIZE - THUMB_SIZE) * grow;
// Droplet law over the thumb ride
// (crate::dynamics): drag speed stretches
// the circle into a travel-axis oval,
// braking swells its leading edge.
let pose = liquid_pose;
GlassDynamics {
activity: Some(grow.clamp(0.0, 1.0)),
morph: Some(GlassMorph {
node_size: (node, node),
primary: (node * 0.5, node * 0.5, d, d, -1.0),
shapes: Vec::new(),
glue: 0.0,
wobble_amplitude: 0.0,
wobble_phase: 0.0,
bulge_amplitude: pose
.bulge_amplitude
.max(2.5 * pose.energy())
.min(4.0),
bulge_direction: pose.bulge_direction,
ellipse_blend: 0.0,
deformation: Some(slider_deformation(pose)),
zoom_anchor: (0.0, 0.0),
}),
..Default::default()
}
},
);
Box(lens, BoxSpec::default(), || {});
});
});
},
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slider_strain_uses_the_shared_incompressible_pose() {
let pose = crate::dynamics::LiquidPose {
stretch: 1.05,
ortho: 1.0 / 1.05,
..Default::default()
};
let deformation = slider_deformation(pose);
assert!((deformation.along() - pose.stretch).abs() < 1e-6);
assert!((deformation.along() * deformation.across() - 1.0).abs() < 1e-6);
}
#[test]
fn slider_strain_preserves_acceleration_axis_compression() {
let pose = crate::dynamics::LiquidPose {
stretch: 0.95,
ortho: 1.0 / 0.95,
..Default::default()
};
let deformation = slider_deformation(pose);
assert!((deformation.along() - pose.stretch).abs() < 1e-6);
assert!(deformation.across() > 1.0);
}
}