waft-widgets 0.6.0

Widget framework and widgets for Waft
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! Flutter counterpart: `widgets/scroll_simulation.dart`.

use std::fmt::{self, Debug};

use waft_embedder::clamp_double;
use waft_physics::{
    FrictionSimulation, ScrollSpringSimulation, Simulation, SpringDescription, Tolerance,
};

/// An implementation of scroll physics that matches iOS.
///
/// See also:
///
///  * [`ClampingScrollSimulation`], which implements Android scroll physics.
pub struct BouncingScrollSimulation {
    /// When [`x`](Simulation::x) falls below this value the simulation switches from an
    /// internal friction model to a spring model which causes
    /// [`x`](Simulation::x) to "spring" back to [`leading_extent`](Self::leading_extent).
    pub leading_extent: f64,

    /// When [`x`](Simulation::x) exceeds this value the simulation switches from an
    /// internal friction model to a spring model which causes
    /// [`x`](Simulation::x) to "spring" back to [`trailing_extent`](Self::trailing_extent).
    pub trailing_extent: f64,

    /// The spring used to return [`x`](Simulation::x) to either
    /// [`leading_extent`](Self::leading_extent) or
    /// [`trailing_extent`](Self::trailing_extent).
    pub spring: SpringDescription,

    friction_simulation: Option<FrictionSimulation>,
    spring_simulation: Option<ScrollSpringSimulation>,
    spring_time: f64,

    /// How close to the actual end of the simulation a value at a particular
    /// time must be before [`is_done`](Simulation::is_done) considers the
    /// simulation to be "done".
    pub tolerance: Tolerance,
}

impl BouncingScrollSimulation {
    /// Creates a simulation group for scrolling on iOS, with the given
    /// parameters.
    ///
    /// The position and velocity arguments must use the same units as will be
    /// expected from the [`x`](Simulation::x) and [`dx`](Simulation::dx) methods
    /// respectively (typically logical pixels and logical pixels per second
    /// respectively).
    ///
    /// The leading and trailing extents must use the unit of length, the same
    /// unit as used for the position argument and as expected from the
    /// [`x`](Simulation::x) method (typically logical pixels).
    ///
    /// The units used with the provided [`SpringDescription`] must similarly be
    /// consistent with the other arguments.
    pub fn new(
        position: f64,
        velocity: f64,
        leading_extent: f64,
        trailing_extent: f64,
        spring: SpringDescription,
    ) -> BouncingScrollSimulation {
        BouncingScrollSimulation::with_options(
            position,
            velocity,
            leading_extent,
            trailing_extent,
            spring,
            0.0,
            Tolerance::DEFAULT_TOLERANCE,
        )
    }

    /// Dart's `constantDeceleration` / `tolerance` named arguments.
    pub fn with_options(
        position: f64,
        velocity: f64,
        leading_extent: f64,
        trailing_extent: f64,
        spring: SpringDescription,
        constant_deceleration: f64,
        tolerance: Tolerance,
    ) -> BouncingScrollSimulation {
        debug_assert!(leading_extent <= trailing_extent);
        let mut simulation = BouncingScrollSimulation {
            leading_extent,
            trailing_extent,
            spring,
            friction_simulation: None,
            spring_simulation: None,
            spring_time: 0.0,
            tolerance,
        };
        // Taken from UIScrollView.decelerationRate (.normal = 0.998)
        // 0.998^1000 = ~0.135
        let friction_simulation = FrictionSimulation::with_options(
            0.135,
            position,
            velocity,
            tolerance,
            constant_deceleration,
        );
        let final_x = friction_simulation.final_x();
        // A release past an edge springs back to it only where friction would leave it past that
        // edge; one friction carries back into the content decelerates like a release within the
        // extents, as UIKit's does, where Flutter's springs back to the edge (`PORTING.md`).
        if position < leading_extent && final_x < leading_extent {
            simulation.spring_simulation =
                Some(simulation.underscroll_simulation(position, velocity));
            simulation.spring_time = f64::NEG_INFINITY;
        } else if position > trailing_extent && final_x > trailing_extent {
            simulation.spring_simulation =
                Some(simulation.overscroll_simulation(position, velocity));
            simulation.spring_time = f64::NEG_INFINITY;
        } else {
            if velocity > 0.0 && final_x > trailing_extent {
                simulation.spring_time = friction_simulation.time_at_x(trailing_extent);
                simulation.spring_simulation = Some(
                    simulation.overscroll_simulation(
                        trailing_extent,
                        friction_simulation
                            .dx(simulation.spring_time)
                            .min(BouncingScrollSimulation::MAX_SPRING_TRANSFER_VELOCITY),
                    ),
                );
                debug_assert!(simulation.spring_time.is_finite());
            } else if velocity < 0.0 && final_x < leading_extent {
                simulation.spring_time = friction_simulation.time_at_x(leading_extent);
                simulation.spring_simulation = Some(
                    simulation.underscroll_simulation(
                        leading_extent,
                        friction_simulation
                            .dx(simulation.spring_time)
                            .min(BouncingScrollSimulation::MAX_SPRING_TRANSFER_VELOCITY),
                    ),
                );
                debug_assert!(simulation.spring_time.is_finite());
            } else {
                simulation.spring_time = f64::INFINITY;
            }
            simulation.friction_simulation = Some(friction_simulation);
        }
        simulation
    }

    /// The maximum velocity that can be transferred from the inertia of a ballistic
    /// scroll into overscroll.
    pub const MAX_SPRING_TRANSFER_VELOCITY: f64 = 5000.0;

    fn underscroll_simulation(&self, x: f64, dx: f64) -> ScrollSpringSimulation {
        ScrollSpringSimulation::new(self.spring, x, self.leading_extent, dx, self.tolerance)
    }

    fn overscroll_simulation(&self, x: f64, dx: f64) -> ScrollSpringSimulation {
        ScrollSpringSimulation::new(self.spring, x, self.trailing_extent, dx, self.tolerance)
    }

    /// Dart's `_simulation`, which also stores the time offset in `_timeOffset` for the
    /// caller to subtract; that scratch field is this second return value.
    fn simulation(&self, time: f64) -> (&dyn Simulation, f64) {
        if time > self.spring_time {
            let time_offset = if self.spring_time.is_finite() {
                self.spring_time
            } else {
                0.0
            };
            let spring = self
                .spring_simulation
                .as_ref()
                .expect("a spring time below infinity means a spring simulation was built");
            (spring, time_offset)
        } else {
            let friction = self
                .friction_simulation
                .as_ref()
                .expect("a finite spring time means the friction simulation was built");
            (friction, 0.0)
        }
    }
}

impl Simulation for BouncingScrollSimulation {
    fn x(&self, time: f64) -> f64 {
        let (simulation, time_offset) = self.simulation(time);
        simulation.x(time - time_offset)
    }

    fn dx(&self, time: f64) -> f64 {
        let (simulation, time_offset) = self.simulation(time);
        simulation.dx(time - time_offset)
    }

    fn is_done(&self, time: f64) -> bool {
        let (simulation, time_offset) = self.simulation(time);
        simulation.is_done(time - time_offset)
    }

    fn tolerance(&self) -> Tolerance {
        self.tolerance
    }

    /// Dart's `_simulation` assigns this simulation's tolerance to whichever inner
    /// simulation it hands back, on every query; the inner simulations are built with it
    /// and it is pushed down here instead, which no caller can tell apart.
    fn set_tolerance(&mut self, tolerance: Tolerance) {
        self.tolerance = tolerance;
        if let Some(friction) = &mut self.friction_simulation {
            friction.set_tolerance(tolerance);
        }
        if let Some(spring) = &mut self.spring_simulation {
            spring.set_tolerance(tolerance);
        }
    }
}

impl Debug for BouncingScrollSimulation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "BouncingScrollSimulation(leadingExtent: {:?}, trailingExtent: {:?})",
            self.leading_extent, self.trailing_extent
        )
    }
}

/// An implementation of scroll physics that aligns with Android.
///
/// For any value of [`velocity`](Self::velocity), this travels the same total distance as
/// the Android scroll physics.
///
/// This scroll physics has been adjusted relative to Android's in order to make
/// it ballistic, meaning that the deceleration at any moment is a function only
/// of the current velocity [`dx`](Simulation::dx) and does not depend on how long ago the
/// simulation was started. (This is required by Flutter's scrolling protocol,
/// where `ScrollActivityDelegate.goBallistic` may restart a scroll activity
/// using only its current velocity and the scroll position's own state.)
/// Compared to this scroll physics, Android's moves faster at the very
/// beginning, then slower, and it ends at the same place but a little later.
///
/// Times are measured in seconds, and positions in logical pixels.
///
/// See also:
///
///  * [`BouncingScrollSimulation`], which implements iOS scroll physics.
//
// This class is based on OverScroller.java from Android:
//   https://android.googlesource.com/platform/frameworks/base/+/android-13.0.0_r24/core/java/android/widget/OverScroller.java#738
// and in particular class SplineOverScroller (at the end of the file), starting
// at method "fling".  (A very similar algorithm is in Scroller.java in the same
// directory, but OverScroller is what's used by RecyclerView.)
//
// In the Android implementation, times are in milliseconds, positions are in
// physical pixels, but velocity is in physical pixels per whole second.
//
// The "See..." comments below refer to SplineOverScroller methods and values.
#[derive(Debug)]
pub struct ClampingScrollSimulation {
    /// The position of the particle at the beginning of the simulation, in
    /// logical pixels.
    pub position: f64,

    /// The velocity at which the particle is traveling at the beginning of the
    /// simulation, in logical pixels per second.
    pub velocity: f64,

    /// The amount of friction the particle experiences as it travels.
    ///
    /// The more friction the particle experiences, the sooner it stops and the
    /// less far it travels.
    ///
    /// The default value causes the particle to travel the same total distance
    /// as in the Android scroll physics.
    // See mFlingFriction.
    pub friction: f64,

    /// The total time the simulation will run, in seconds.
    duration: f64,

    /// The total, signed, distance the simulation will travel, in logical pixels.
    distance: f64,

    /// How close to the actual end of the simulation a value at a particular
    /// time must be before [`is_done`](Simulation::is_done) considers the
    /// simulation to be "done".
    pub tolerance: Tolerance,
}

// See DECELERATION_RATE. A `static final` in Dart; `f64::ln` is not a `const fn`.
fn k_deceleration_rate() -> f64 {
    0.78_f64.ln() / 0.9_f64.ln()
}

// See INFLEXION.
const K_INFLEXION: f64 = 0.35;

// See mPhysicalCoeff.  This has a value of 0.84 times Earth gravity,
// expressed in units of logical pixels per second^2.
const PHYSICAL_COEFF: f64 = 9.80665 // g, in meters per second^2
    * 39.37 // 1 meter / 1 inch
    * 160.0 // 1 inch / 1 logical pixel
    * 0.84; // "look and feel tuning"

impl ClampingScrollSimulation {
    /// Dart's default `friction`, which causes the particle to travel the same total
    /// distance as in the Android scroll physics.
    pub const DEFAULT_FRICTION: f64 = 0.015;

    /// Creates a scroll physics simulation that aligns with Android scrolling.
    pub fn new(position: f64, velocity: f64) -> ClampingScrollSimulation {
        ClampingScrollSimulation::with_options(
            position,
            velocity,
            ClampingScrollSimulation::DEFAULT_FRICTION,
            Tolerance::DEFAULT_TOLERANCE,
        )
    }

    /// Dart's `friction` / `tolerance` named arguments.
    pub fn with_options(
        position: f64,
        velocity: f64,
        friction: f64,
        tolerance: Tolerance,
    ) -> ClampingScrollSimulation {
        let mut simulation = ClampingScrollSimulation {
            position,
            velocity,
            friction,
            duration: 0.0,
            distance: 0.0,
            tolerance,
        };
        simulation.duration = simulation.fling_duration();
        simulation.distance = simulation.fling_distance();
        simulation
    }

    // See getSplineFlingDuration().
    fn fling_duration(&self) -> f64 {
        // See getSplineDeceleration().  That function's value is
        // (velocity.abs() / referenceVelocity).ln().
        let reference_velocity = self.friction * PHYSICAL_COEFF / K_INFLEXION;

        // This is the value getSplineFlingDuration() would return, but in seconds.
        let android_duration =
            (self.velocity.abs() / reference_velocity).powf(1.0 / (k_deceleration_rate() - 1.0));

        // We finish a bit sooner than Android, in order to travel the
        // same total distance.
        k_deceleration_rate() * K_INFLEXION * android_duration
    }

    // See getSplineFlingDistance().  This returns the same value but with the
    // sign of [`velocity`](Self::velocity), and in logical pixels.
    fn fling_distance(&self) -> f64 {
        let distance = self.velocity * self.duration / k_deceleration_rate();
        if cfg!(debug_assertions) {
            // This is the more complicated calculation that getSplineFlingDistance()
            // actually performs, which boils down to the much simpler formula above.
            let reference_velocity = self.friction * PHYSICAL_COEFF / K_INFLEXION;
            let log_velocity = (self.velocity.abs() / reference_velocity).ln();
            let distance_again = self.friction
                * PHYSICAL_COEFF
                * (log_velocity * k_deceleration_rate() / (k_deceleration_rate() - 1.0)).exp();
            debug_assert!((distance.abs() - distance_again).abs() < self.tolerance.distance);
        }
        distance
    }
}

impl Simulation for ClampingScrollSimulation {
    fn x(&self, time: f64) -> f64 {
        let t = clamp_double(time / self.duration, 0.0, 1.0);
        self.position + self.distance * (1.0 - (1.0 - t).powf(k_deceleration_rate()))
    }

    fn dx(&self, time: f64) -> f64 {
        let t = clamp_double(time / self.duration, 0.0, 1.0);
        self.velocity * (1.0 - t).powf(k_deceleration_rate() - 1.0)
    }

    fn is_done(&self, time: f64) -> bool {
        time >= self.duration
    }

    fn tolerance(&self) -> Tolerance {
        self.tolerance
    }

    fn set_tolerance(&mut self, tolerance: Tolerance) {
        self.tolerance = tolerance;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn default_spring() -> SpringDescription {
        SpringDescription::with_damping_ratio_value(0.5, 100.0, 1.1)
    }

    /// Runs the simulation to its end at 60Hz and returns the time it stopped at.
    fn run_to_completion(simulation: &dyn Simulation, deadline: f64) -> f64 {
        let mut time = 0.0;
        while !simulation.is_done(time) {
            time += 1.0 / 60.0;
            assert!(
                time < deadline,
                "the simulation must finish before {deadline}s"
            );
        }
        time
    }

    /// `scroll_simulation_test.dart`: "ClampingScrollSimulation has a stable initial
    /// conditions".
    #[test]
    fn clamping_scroll_simulation_has_stable_initial_conditions() {
        for (position, velocity) in [
            (51.0, 2866.91537),
            (584.0, 2617.294734),
            (0.0, 1831.366634),
            (-156.2, 1541.57665),
            (5469.0, 182.114534),
        ] {
            let simulation = ClampingScrollSimulation::new(position, velocity);
            assert!((simulation.x(0.0) - position).abs() < 1e-9);
            assert!((simulation.dx(0.0) - velocity).abs() < 1e-9);
        }
    }

    /// `scroll_simulation_test.dart`: "ClampingScrollSimulation only decelerates, never
    /// speeds up".
    #[test]
    fn clamping_scroll_simulation_only_decelerates() {
        let simulation = ClampingScrollSimulation::new(0.0, 8000.0);
        let mut time = 0.0;
        let mut velocity = simulation.dx(time);
        while !simulation.is_done(time) {
            assert!(time < 3.0);
            time += 1.0 / 60.0;
            let next_velocity = simulation.dx(time);
            assert!(next_velocity <= velocity);
            velocity = next_velocity;
        }
        assert!(velocity.abs() < 1e-9);
    }

    /// The fling duration and distance Android's `SplineOverScroller` produces for a
    /// 8000 logical-pixel-per-second fling.
    #[test]
    fn clamping_scroll_simulation_travels_its_fling_distance_over_its_fling_duration() {
        let simulation = ClampingScrollSimulation::new(0.0, 8000.0);
        assert!(!simulation.is_done(2.1183617));
        assert!(simulation.is_done(2.118361712));
        assert!((simulation.x(2.2) - 7186.362753988744).abs() < 1e-6);
        assert_eq!(simulation.dx(2.2), 0.0);
    }

    /// A fling that stays inside the extents runs the friction model to a stop.
    #[test]
    fn bouncing_scroll_simulation_uses_friction_inside_the_extents() {
        let simulation =
            BouncingScrollSimulation::new(0.0, 200.0, -1000.0, 1000.0, default_spring());
        assert_eq!(simulation.x(0.0), 0.0);
        assert_eq!(simulation.dx(0.0), 200.0);
        let time = run_to_completion(&simulation, 10.0);
        let resting = simulation.x(time);
        assert!(resting > 0.0 && resting < 1000.0, "stopped at {resting}");
    }

    /// A position past the leading extent starts on the spring and settles on the extent.
    #[test]
    fn bouncing_scroll_simulation_springs_back_to_the_leading_extent() {
        let simulation = BouncingScrollSimulation::new(-100.0, 0.0, 0.0, 500.0, default_spring());
        assert_eq!(simulation.x(0.0), -100.0);
        let time = run_to_completion(&simulation, 10.0);
        assert!(
            simulation.x(time).abs() < 0.1,
            "settled at {}",
            simulation.x(time)
        );
    }

    /// A fling fast enough to leave the trailing extent hands off to the spring, which
    /// brings it back to that extent.
    #[test]
    fn bouncing_scroll_simulation_hands_a_fast_fling_to_the_trailing_spring() {
        let simulation = BouncingScrollSimulation::new(0.0, 5000.0, 0.0, 100.0, default_spring());
        // The friction phase alone would carry the particle far past the extent.
        assert!(simulation.x(0.1) > 100.0);
        let time = run_to_completion(&simulation, 10.0);
        assert!(
            (simulation.x(time) - 100.0).abs() < 0.1,
            "settled at {}",
            simulation.x(time)
        );
    }

    /// `physics/newton_test.dart`: "test_kinetic_scroll".
    #[test]
    fn bouncing_scroll_simulation_switches_from_friction_to_the_spring() {
        let spring = SpringDescription::with_damping_ratio_value(1.0, 50.0, 0.5);

        let mut scroll = BouncingScrollSimulation::new(100.0, 800.0, 0.0, 300.0, spring);
        scroll.set_tolerance(Tolerance {
            velocity: 0.5,
            distance: 0.1,
            ..Tolerance::DEFAULT_TOLERANCE
        });
        assert!(!scroll.is_done(0.0));
        assert!(!scroll.is_done(0.5)); // switch from friction to spring
        assert!(scroll.is_done(3.5));

        let mut scroll2 = BouncingScrollSimulation::new(100.0, -800.0, 0.0, 300.0, spring);
        scroll2.set_tolerance(Tolerance {
            velocity: 0.5,
            distance: 0.1,
            ..Tolerance::DEFAULT_TOLERANCE
        });
        assert!(!scroll2.is_done(0.0));
        assert!(!scroll2.is_done(0.5)); // switch from friction to spring
        assert!(scroll2.is_done(3.5));
    }

    /// `physics/newton_test.dart`: "scroll_with_inf_edge_ends".
    #[test]
    fn bouncing_scroll_simulation_stays_on_friction_with_an_infinite_trailing_extent() {
        let spring = SpringDescription::with_damping_ratio_value(1.0, 50.0, 0.5);
        let mut scroll = BouncingScrollSimulation::new(100.0, 400.0, 0.0, f64::INFINITY, spring);
        scroll.set_tolerance(Tolerance {
            velocity: 1.0,
            ..Tolerance::DEFAULT_TOLERANCE
        });

        assert!(!scroll.is_done(0.0));
        assert_eq!(scroll.x(0.0), 100.0);
        assert_eq!(scroll.dx(0.0), 400.0);

        assert!((scroll.x(1.0) - 272.0).abs() < 1.0);
        assert!((scroll.dx(1.0) - 54.0).abs() < 1.0);
        assert!((scroll.dx(2.0) - 7.0).abs() < 1.0);
        assert!(scroll.dx(3.0) < 1.0);

        assert!(scroll.is_done(5.0));
        assert!((scroll.x(5.0) - 300.0).abs() < 1.0);
    }

    /// `physics/newton_test.dart`: "over/under scroll spring".
    #[test]
    fn bouncing_scroll_simulation_overscrolls_then_springs_back() {
        let spring = SpringDescription::with_damping_ratio_value(1.0, 170.0, 1.1);
        let mut scroll = BouncingScrollSimulation::new(500.0, -7500.0, 0.0, 1000.0, spring);
        scroll.set_tolerance(Tolerance {
            velocity: 45.0,
            distance: 1.5,
            ..Tolerance::DEFAULT_TOLERANCE
        });

        assert!(!scroll.is_done(0.0));
        assert!((scroll.x(0.0) - 500.0).abs() < 1e-9);
        assert!((scroll.dx(0.0) + 7500.0).abs() < 1e-9);

        // Expect to reach 0.0 at about t=.07 at which point the simulation will
        // switch from friction to the spring
        assert!(!scroll.is_done(0.065));
        assert!((scroll.x(0.065) - 42.0).abs() < 1.0);
        assert!((scroll.dx(0.065) + 6584.0).abs() < 1.0);

        // We've overscrolled (0.1 > 0.07). Trigger the underscroll
        // simulation, and reverse direction
        assert!(!scroll.is_done(0.1));
        assert!((scroll.x(0.1) + 123.0).abs() < 1.0);
        assert!((scroll.dx(0.1) + 2613.0).abs() < 1.0);

        // Headed back towards 0.0 and slowing down.
        assert!(!scroll.is_done(0.5));
        assert!((scroll.x(0.5) + 15.0).abs() < 1.0);
        assert!((scroll.dx(0.5) - 124.0).abs() < 1.0);

        // Now jump back to the beginning, because we can.
        assert!(!scroll.is_done(0.0));
        assert!((scroll.x(0.0) - 500.0).abs() < 1e-9);
        assert!((scroll.dx(0.0) + 7500.0).abs() < 1e-9);

        assert!(scroll.is_done(2.0));
        assert_eq!(scroll.x(2.0), 0.0);
        assert!(scroll.dx(2.0).abs() < 1.0);
    }

    #[test]
    fn bouncing_scroll_simulation_to_string_names_its_extents() {
        let simulation = BouncingScrollSimulation::new(0.0, 0.0, -1.0, 500.0, default_spring());
        assert_eq!(
            format!("{simulation:?}"),
            "BouncingScrollSimulation(leadingExtent: -1.0, trailingExtent: 500.0)"
        );
    }

    /// The positions a simulation passes through at 60Hz until it is done.
    fn trajectory(simulation: &dyn Simulation, deadline: f64) -> Vec<f64> {
        let mut positions = Vec::new();
        let mut time = 0.0;
        loop {
            positions.push(simulation.x(time));
            if simulation.is_done(time) {
                return positions;
            }
            time += 1.0 / 60.0;
            assert!(
                time < deadline,
                "the simulation must finish before {deadline}s"
            );
        }
    }

    /// A release past an edge moving outward springs back to that edge, as Flutter's does.
    #[test]
    fn a_release_past_an_edge_moving_outward_springs_back_to_it() {
        let simulation = BouncingScrollSimulation::new(350.0, 1000.0, 0.0, 300.0, default_spring());
        let positions = trajectory(&simulation, 10.0);
        assert!((positions.last().unwrap() - 300.0).abs() < 1.0);
        assert!(
            positions.iter().all(|x| *x > 299.0),
            "it never enters the content"
        );
    }

    /// A release past an edge at rest springs back to that edge, as Flutter's does.
    #[test]
    fn a_release_past_an_edge_at_rest_springs_back_to_it() {
        let simulation = BouncingScrollSimulation::new(-60.0, 0.0, 0.0, 300.0, default_spring());
        let positions = trajectory(&simulation, 10.0);
        assert!(positions.last().unwrap().abs() < 1.0);
        assert!(positions.iter().all(|x| *x < 1.0));
    }

    /// A release moving back into the content too slowly for friction to carry it past the
    /// edge springs back to that edge, as Flutter's does.
    #[test]
    fn a_slow_release_back_into_the_content_springs_back_to_the_edge() {
        let simulation = BouncingScrollSimulation::new(320.0, -20.0, 0.0, 300.0, default_spring());
        let positions = trajectory(&simulation, 10.0);
        assert!((positions.last().unwrap() - 300.0).abs() < 1.0);
    }

    /// A release past an edge moving back into the content fast enough decelerates through the
    /// edge and stops inside the content, as UIKit's does, with no spring.
    #[test]
    fn a_fast_release_back_into_the_content_decelerates_like_any_fling() {
        let simulation = BouncingScrollSimulation::new(320.0, -500.0, 0.0, 300.0, default_spring());
        assert_eq!(simulation.x(0.0), 320.0);
        assert_eq!(simulation.dx(0.0), -500.0);
        let positions = trajectory(&simulation, 10.0);
        let resting = *positions.last().unwrap();
        // 0.135 friction carries -500 px/s 500 / ln(1 / 0.135) px.
        assert!((resting - (320.0 - 500.0 / (1.0f64 / 0.135).ln())).abs() < 1.0);
        assert!(resting > 0.0 && resting < 300.0, "stopped at {resting}");
        let mirrored = BouncingScrollSimulation::new(-20.0, 500.0, 0.0, 300.0, default_spring());
        let resting = *trajectory(&mirrored, 10.0).last().unwrap();
        assert!(resting > 0.0 && resting < 300.0, "stopped at {resting}");
    }

    /// The lead's recording on an iPhone, touch 3: released at 377 px with the end at 303,
    /// moving back at about -7,276 px/s. UIKit carries it through the end and the whole page,
    /// springing only at the far edge; it never comes back to the end it was past.
    #[test]
    fn the_recorded_release_never_returns_to_the_edge_it_crossed() {
        let simulation =
            BouncingScrollSimulation::new(377.0, -7276.0, 0.0, 303.0, default_spring());
        let positions = trajectory(&simulation, 10.0);
        let crossed = positions
            .iter()
            .position(|x| *x < 303.0)
            .expect("it crosses the end it was past");
        assert!(
            positions[crossed..].iter().all(|x| *x < 303.0),
            "it never returns to 303 once it crossed it"
        );
        assert!(
            positions.iter().any(|x| *x < 0.0),
            "it runs past the far edge"
        );
        assert!(
            positions.last().unwrap().abs() < 1.0,
            "and settles at the far edge"
        );
    }
}