cranpose-ui 0.1.116

UI primitives for Cranpose
Documentation
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Fling animation driver for scroll containers.
//!
//! Drives decay animation using the runtime's frame callback system.

use std::{
    cell::{Cell, RefCell},
    rc::Rc,
};

use cranpose_animation::{
    ExponentialDecaySpec, FloatDecayAnimationSpec, IOS_DECELERATION_RATE_NORMAL,
};
use cranpose_core::{
    RuntimeHandle,
    internal::{FrameCallbackRegistration, FrameClock},
};

/// Minimum release velocity (in points/sec) for `UIScrollView` to start
/// decelerating at all; below this a release just stops. Measured on the iOS
/// 26.5 Simulator: releases consistently below ~260pt/s never decelerate,
/// releases consistently above ~350pt/s always do, with a noisy transition
/// between (real touch-velocity noise near a threshold, not a measurement
/// artifact — see `ios_fling_measurement.rs`). 300 sits in that band.
pub const MIN_FLING_VELOCITY: f32 = 300.0;

const BOUNDARY_EPSILON: f32 = 0.5;

fn schedule_next_frame<F, G>(
    state: Rc<RefCell<Option<FlingAnimationState>>>,
    frame_clock: FrameClock,
    on_scroll: F,
    on_end: G,
) where
    F: Fn(f32) -> f32 + 'static,
    G: FnOnce() + 'static,
{
    let state_for_closure = state.clone();
    let frame_clock_for_closure = frame_clock.clone();
    let on_end = RefCell::new(Some(on_end));

    let registration = frame_clock.with_frame_nanos(move |frame_time_nanos| {
        let should_continue = {
            let state_guard = state_for_closure.borrow();
            let Some(anim_state) = state_guard.as_ref() else {
                return;
            };

            if !anim_state.is_running.get() {
                return;
            }

            let start_time = match anim_state.start_frame_time_nanos.get() {
                Some(value) => value,
                None => {
                    anim_state
                        .start_frame_time_nanos
                        .set(Some(frame_time_nanos));
                    frame_time_nanos
                }
            };

            let play_time_nanos = frame_time_nanos.saturating_sub(start_time) as i64;

            let new_value = anim_state.decay_spec.get_value_from_nanos(
                play_time_nanos,
                anim_state.initial_value,
                anim_state.initial_velocity,
            );

            let last = anim_state.last_value.get();
            let delta = new_value - last;
            anim_state.last_value.set(new_value);
            anim_state
                .total_delta
                .set(anim_state.total_delta.get() + delta);

            let duration_nanos = anim_state
                .decay_spec
                .get_duration_nanos(anim_state.initial_value, anim_state.initial_velocity);

            let current_velocity = anim_state.decay_spec.get_velocity_from_nanos(
                play_time_nanos,
                anim_state.initial_value,
                anim_state.initial_velocity,
            );

            let is_finished = play_time_nanos >= duration_nanos
                || current_velocity.abs() < anim_state.decay_spec.abs_velocity_threshold();

            if is_finished {
                anim_state.is_running.set(false);
            }

            let consumed = if delta.abs() > 0.001 {
                on_scroll(delta)
            } else {
                0.0
            };

            let boundary_hit = (delta - consumed).abs() > BOUNDARY_EPSILON;
            if boundary_hit {
                anim_state.is_running.set(false);
            }

            !is_finished && !boundary_hit
        };

        if should_continue {
            if let Some(on_end_fn) = on_end.borrow_mut().take() {
                schedule_next_frame(
                    state_for_closure.clone(),
                    frame_clock_for_closure.clone(),
                    on_scroll,
                    on_end_fn,
                );
            }
        } else if let Some(end_fn) = on_end.borrow_mut().take() {
            end_fn();
        }
    });

    if let Some(anim_state) = state.borrow_mut().as_mut() {
        anim_state.registration = Some(registration);
    }
}

struct FlingAnimationState {
    initial_value: f32,
    last_value: Cell<f32>,
    initial_velocity: f32,
    start_frame_time_nanos: Cell<Option<u64>>,
    decay_spec: ExponentialDecaySpec,
    registration: Option<FrameCallbackRegistration>,
    is_running: Cell<bool>,
    total_delta: Cell<f32>,
}

/// Drives a fling (decay) animation on a scroll target.
///
/// Each frame, it calculates the scroll DELTA based on the decay curve
/// and applies it to the scroll target via the provided callback.
pub struct FlingAnimation {
    state: Rc<RefCell<Option<FlingAnimationState>>>,
    frame_clock: FrameClock,
}

impl FlingAnimation {
    /// Creates a new fling animation driver.
    pub fn new(runtime: RuntimeHandle) -> Self {
        Self {
            state: Rc::new(RefCell::new(None)),
            frame_clock: runtime.frame_clock(),
        }
    }

    /// Starts a fling animation with the given velocity.
    ///
    /// # Arguments
    /// * `initial_value` - Current scroll position (used as reference)
    /// * `velocity` - Initial velocity in px/sec (from VelocityTracker)
    /// * `on_scroll` - Callback invoked each frame with scroll DELTA (not absolute position)
    /// * `on_end` - Callback invoked when animation completes
    pub fn start_fling<F, G>(&self, initial_value: f32, velocity: f32, on_scroll: F, on_end: G)
    where
        F: Fn(f32) -> f32 + 'static,
        G: FnOnce() + 'static,
    {
        self.cancel();

        if velocity.abs() < MIN_FLING_VELOCITY {
            on_end();
            return;
        }

        let decay_spec = ExponentialDecaySpec::new(IOS_DECELERATION_RATE_NORMAL);

        let anim_state = FlingAnimationState {
            initial_value,
            last_value: Cell::new(initial_value),
            initial_velocity: velocity,
            start_frame_time_nanos: Cell::new(None),
            decay_spec,
            registration: None,
            is_running: Cell::new(true),
            total_delta: Cell::new(0.0),
        };

        *self.state.borrow_mut() = Some(anim_state);

        schedule_next_frame(
            self.state.clone(),
            self.frame_clock.clone(),
            on_scroll,
            on_end,
        );
    }

    pub fn cancel(&self) {
        if let Some(state) = self.state.borrow_mut().take() {
            state.is_running.set(false);
            drop(state.registration);
        }
    }

    /// Returns true if a fling animation is currently running.
    pub fn is_running(&self) -> bool {
        self.state
            .borrow()
            .as_ref()
            .is_some_and(|s| s.is_running.get())
    }
}

impl Clone for FlingAnimation {
    fn clone(&self) -> Self {
        Self {
            state: self.state.clone(),
            frame_clock: self.frame_clock.clone(),
        }
    }
}

/// Predicts where a fling starting at `initial_value` with `velocity` would
/// naturally come to rest, using the same decay physics as
/// [`FlingAnimation::start_fling`]. Settle policies remap this proposed rest
/// position before the deceleration starts (the `UIScrollView
/// targetContentOffset` analog).
pub fn fling_rest_position(initial_value: f32, velocity: f32) -> f32 {
    if velocity.abs() < MIN_FLING_VELOCITY {
        return initial_value;
    }
    let spec = ExponentialDecaySpec::new(IOS_DECELERATION_RATE_NORMAL);
    spec.get_target_value(initial_value, velocity)
}

/// Damped-spring parameters for a [`SettleAnimation`]. `advance_spring`
/// already generalizes over damping ratio, so the two settle use cases in
/// this crate share one scheduler and differ only in these two numbers.
#[derive(Debug, Clone, Copy)]
pub struct SpringParams {
    pub stiffness: f32,
    pub damping_ratio: f32,
}

impl SpringParams {
    /// Settle-policy remapping (e.g. the liquid nav bar's title-collapse
    /// snap): critically damped, settling in ≈0.35s, the iOS large-title
    /// snap feel.
    pub const SETTLE_POLICY: Self = Self {
        stiffness: 300.0,
        damping_ratio: 1.0,
    };

    /// Overscroll bounce-back: the spring a scroll container's rubber-banded
    /// offset relaxes through once the finger releases (or a fling's own
    /// velocity decays) while still past the edge. Fit to a `UIScrollView`
    /// bounce-back trace recorded on the iOS 26.5 Simulator (drag past the
    /// top edge, hold to zero velocity, release: -177pt to rest in 667ms) —
    /// see `ios_fling_measurement.rs`. Overdamped rather than critical: a
    /// critically-damped ζ=1 spring at the same stiffness overshoots the
    /// measured curve's shape by 15-20x; this (stiffness, damping_ratio) pair
    /// was fit by least squares against the recorded trace, residual ≤2.9pt
    /// (mean 0.79pt) against a 177pt swing.
    pub const OVERSCROLL_BOUNCE: Self = Self {
        stiffness: 1909.69,
        damping_ratio: 2.71,
    };
}

const SETTLE_REST_DISTANCE: f32 = 0.1;
const SETTLE_REST_VELOCITY: f32 = 4.0;

struct SettleAnimationState {
    value: Cell<f32>,
    velocity: Cell<f32>,
    target: f32,
    params: SpringParams,
    last_frame_time_nanos: Cell<Option<u64>>,
    registration: Option<FrameCallbackRegistration>,
    is_running: Cell<bool>,
}

pub(crate) struct SettleEnd {
    pub(crate) velocity: f32,
    pub(crate) hit_boundary: bool,
}

/// Drives a damped spring toward a settle target on a scroll container,
/// taking over the gesture's release velocity so a policy-adjusted rest
/// position (or an overscroll bounce-back) still reads as one continuous
/// deceleration.
pub struct SettleAnimation {
    state: Rc<RefCell<Option<SettleAnimationState>>>,
    frame_clock: FrameClock,
    params: SpringParams,
}

impl SettleAnimation {
    pub fn new(runtime: RuntimeHandle, params: SpringParams) -> Self {
        Self {
            state: Rc::new(RefCell::new(None)),
            frame_clock: runtime.frame_clock(),
            params,
        }
    }

    pub(crate) fn start_settle<F, G>(
        &self,
        initial_value: f32,
        initial_velocity: f32,
        target: f32,
        on_scroll: F,
        on_end: G,
    ) where
        F: Fn(f32) -> f32 + 'static,
        G: FnOnce(SettleEnd) + 'static,
    {
        self.cancel();
        *self.state.borrow_mut() = Some(SettleAnimationState {
            value: Cell::new(initial_value),
            velocity: Cell::new(initial_velocity),
            target,
            params: self.params,
            last_frame_time_nanos: Cell::new(None),
            registration: None,
            is_running: Cell::new(true),
        });
        schedule_next_settle_frame(
            self.state.clone(),
            self.frame_clock.clone(),
            on_scroll,
            on_end,
        );
    }

    pub fn cancel(&self) {
        if let Some(state) = self.state.borrow_mut().take() {
            state.is_running.set(false);
            drop(state.registration);
        }
    }

    pub fn is_running(&self) -> bool {
        self.state
            .borrow()
            .as_ref()
            .is_some_and(|s| s.is_running.get())
    }
}

impl Clone for SettleAnimation {
    fn clone(&self) -> Self {
        Self {
            state: self.state.clone(),
            frame_clock: self.frame_clock.clone(),
            params: self.params,
        }
    }
}

fn schedule_next_settle_frame<F, G>(
    state: Rc<RefCell<Option<SettleAnimationState>>>,
    frame_clock: FrameClock,
    on_scroll: F,
    on_end: G,
) where
    F: Fn(f32) -> f32 + 'static,
    G: FnOnce(SettleEnd) + 'static,
{
    let state_for_closure = state.clone();
    let frame_clock_for_closure = frame_clock.clone();
    let on_end = RefCell::new(Some(on_end));
    let hit_boundary = Cell::new(false);

    let registration = frame_clock.with_frame_nanos(move |frame_time_nanos| {
        let should_continue = {
            let state_guard = state_for_closure.borrow();
            let Some(anim_state) = state_guard.as_ref() else {
                return;
            };
            if !anim_state.is_running.get() {
                return;
            }

            let dt = match anim_state.last_frame_time_nanos.get() {
                Some(last) => (frame_time_nanos.saturating_sub(last) as f32) / 1_000_000_000.0,
                None => 0.0,
            };
            anim_state.last_frame_time_nanos.set(Some(frame_time_nanos));

            let (mut next_value, next_velocity) = cranpose_animation::advance_spring(
                anim_state.value.get(),
                anim_state.velocity.get(),
                anim_state.target,
                anim_state.params.damping_ratio,
                anim_state.params.stiffness,
                dt.max(0.0),
            );

            let is_finished = (next_value - anim_state.target).abs() < SETTLE_REST_DISTANCE
                && next_velocity.abs() < SETTLE_REST_VELOCITY;
            if is_finished {
                next_value = anim_state.target;
                anim_state.is_running.set(false);
            }

            let delta = next_value - anim_state.value.get();
            anim_state.value.set(next_value);
            anim_state.velocity.set(next_velocity);

            let consumed = if delta.abs() > 0.0001 {
                on_scroll(delta)
            } else {
                delta
            };
            let boundary_hit = (delta - consumed).abs() > BOUNDARY_EPSILON;
            if boundary_hit {
                anim_state.is_running.set(false);
                hit_boundary.set(true);
            }

            !is_finished && !boundary_hit
        };

        if should_continue {
            if let Some(on_end_fn) = on_end.borrow_mut().take() {
                schedule_next_settle_frame(
                    state_for_closure.clone(),
                    frame_clock_for_closure.clone(),
                    on_scroll,
                    on_end_fn,
                );
            }
        } else if let Some(end_fn) = on_end.borrow_mut().take() {
            let state_guard = state_for_closure.borrow();
            let velocity = state_guard
                .as_ref()
                .map_or(0.0, |anim_state| anim_state.velocity.get());
            end_fn(SettleEnd {
                velocity,
                hit_boundary: hit_boundary.get(),
            });
        }
    });

    if let Some(anim_state) = state.borrow_mut().as_mut() {
        anim_state.registration = Some(registration);
    }
}

#[cfg(test)]
mod tests {
    use std::{cell::Cell, rc::Rc, sync::Arc};

    use cranpose_core::{DefaultScheduler, Runtime};

    use super::*;

    #[test]
    fn test_min_velocity_threshold() {
        assert_eq!(MIN_FLING_VELOCITY, 300.0);
    }

    #[test]
    fn settle_animation_springs_to_target_and_ends() {
        let runtime = Runtime::new(Arc::new(DefaultScheduler));
        let handle = runtime.handle();
        let settle = SettleAnimation::new(handle.clone(), SpringParams::SETTLE_POLICY);
        let position = Rc::new(Cell::new(30.0f32));
        let ended = Rc::new(Cell::new(false));
        let position_for_scroll = Rc::clone(&position);
        let ended_for_end = Rc::clone(&ended);
        settle.start_settle(
            30.0,
            0.0,
            52.0,
            move |delta| {
                position_for_scroll.set(position_for_scroll.get() + delta);
                delta
            },
            move |_| ended_for_end.set(true),
        );
        for frame in 0..240u64 {
            handle.drain_frame_callbacks(frame * 16_000_000);
            if ended.get() {
                break;
            }
        }
        assert!(ended.get(), "settle animation must finish");
        assert!(
            (position.get() - 52.0).abs() < 0.2,
            "settle must land on the target, got {}",
            position.get()
        );
    }

    #[test]
    fn settle_reports_reduced_velocity_when_crossing_boundary() {
        let runtime = Runtime::new(Arc::new(DefaultScheduler));
        let handle = runtime.handle();
        let settle = SettleAnimation::new(handle.clone(), SpringParams::SETTLE_POLICY);
        let position = Rc::new(Cell::new(30.0f32));
        let ended = Rc::new(Cell::new(None::<(f32, bool)>));
        let position_for_scroll = Rc::clone(&position);
        let ended_for_end = Rc::clone(&ended);
        settle.start_settle(
            30.0,
            -1_200.0,
            0.0,
            move |delta| {
                let previous = position_for_scroll.get();
                let next = (previous + delta).max(0.0);
                position_for_scroll.set(next);
                next - previous
            },
            move |end| ended_for_end.set(Some((end.velocity, end.hit_boundary))),
        );
        for frame in 0..240u64 {
            handle.drain_frame_callbacks(frame * 16_000_000);
            if ended.get().is_some() {
                break;
            }
        }

        let (velocity, hit_boundary) = ended.get().expect("settle must finish");
        assert!(hit_boundary);
        assert!(velocity < 0.0 && velocity.abs() < 1_200.0);
        assert_eq!(position.get(), 0.0);
    }

    #[test]
    fn fling_rest_position_is_beyond_start_in_fling_direction() {
        let rest = fling_rest_position(100.0, 900.0);
        assert!(rest > 100.0, "rest {rest} must be past the start");
        assert_eq!(fling_rest_position(100.0, 0.0), 100.0);
    }

    #[test]
    fn test_on_end_called_when_boundary_hit() {
        let runtime = Runtime::new(Arc::new(DefaultScheduler));
        let handle = runtime.handle();
        let fling = FlingAnimation::new(handle.clone());
        let finished = Rc::new(Cell::new(false));
        let finished_flag = Rc::clone(&finished);

        fling.start_fling(0.0, 10_000.0, |_| 0.0, move || finished_flag.set(true));

        handle.drain_frame_callbacks(0);
        handle.drain_frame_callbacks(16_000_000);

        assert!(finished.get());
    }
}