nsh-core 0.6.1

Platform-independent core: shadow tree, styles, layout and mount instructions.
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
//! Keyframes, staggering and motions: what is played on animated values
//! described as data, turned into [`Composite`] trees the graph runs like
//! any other (so every platform plays them, with no code of its own).
//!
//! - [`keyframes`]: one value through keyframes, as CSS `@keyframes` and
//!   the Web Animations API mean them: offsets (missing ones spread
//!   evenly), an easing per segment, iterations, a direction, a delay.
//! - [`stagger_delay`]: the delay of each item of a staggered group,
//!   counted from the first, the last, the center or any item.
//! - [`MotionSpec`]: several channels of a view ([`MotionChannel`]: opacity and
//!   the parts of its transform) through keyframes with one timing, or each
//!   on one spring to where its keyframes end, and the ready-made ones
//!   ([`MotionPreset`]: entrances, exits, attention seekers and loops). The
//!   values are the app's: a motion has no look, it only moves what the app
//!   styled.

use super::ops::{Composite, Driver, Easing, NodeId};
use crate::{TransformKind, element::Spring};

/// One keyframe of a value: where it is at `offset` (0..=1 of an
/// iteration) and the easing of the segment that starts there.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct Keyframe {
    /// Where it is in the iteration, 0..=1; `None` spreads it evenly
    /// between its neighbors (the first is 0 and the last 1 by default).
    pub offset: Option<f32>,
    /// The value there.
    pub value: f32,
    /// The curve to the next keyframe (`None`: the timing's).
    pub easing: Option<Easing>,
}

impl Default for Keyframe {
    fn default() -> Self {
        Self {
            offset: None,
            value: 0.0,
            easing: None,
        }
    }
}

impl Keyframe {
    /// `value`, spread evenly with the keyframes around it.
    pub const fn new(value: f32) -> Self {
        Self {
            offset: None,
            value,
            easing: None,
        }
    }

    /// `value` at `offset` (0..=1).
    pub const fn at(offset: f32, value: f32) -> Self {
        Self {
            offset: Some(offset),
            value,
            easing: None,
        }
    }

    /// With `easing` for the segment that starts here.
    pub const fn ease(mut self, easing: Easing) -> Self {
        self.easing = Some(easing);
        self
    }
}

/// Which way the iterations of keyframes play (CSS `animation-direction`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PlaybackDirection {
    /// From the first keyframe to the last, each iteration.
    #[default]
    Normal,
    /// From the last to the first.
    Reverse,
    /// Forwards, then backwards, and so on.
    Alternate,
    /// Backwards, then forwards, and so on.
    AlternateReverse,
}

impl PlaybackDirection {
    /// Parses a CSS `animation-direction`.
    pub fn parse(name: &str) -> Option<PlaybackDirection> {
        Some(match name {
            "normal" => PlaybackDirection::Normal,
            "reverse" => PlaybackDirection::Reverse,
            "alternate" => PlaybackDirection::Alternate,
            "alternate-reverse" => PlaybackDirection::AlternateReverse,
            _ => return None,
        })
    }

    /// Its CSS name.
    pub fn name(self) -> &'static str {
        match self {
            PlaybackDirection::Normal => "normal",
            PlaybackDirection::Reverse => "reverse",
            PlaybackDirection::Alternate => "alternate",
            PlaybackDirection::AlternateReverse => "alternate-reverse",
        }
    }

    /// Whether iteration `index` (from 0) plays backwards.
    fn reversed(self, index: u32) -> bool {
        match self {
            PlaybackDirection::Normal => false,
            PlaybackDirection::Reverse => true,
            PlaybackDirection::Alternate => !index.is_multiple_of(2),
            PlaybackDirection::AlternateReverse => index.is_multiple_of(2),
        }
    }

    fn alternates(self) -> bool {
        matches!(
            self,
            PlaybackDirection::Alternate | PlaybackDirection::AlternateReverse
        )
    }
}

/// How keyframes play (CSS `animation-duration`, `-delay`,
/// `-timing-function`, `-iteration-count` and `-direction`).
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct KeyframeTiming {
    /// One iteration, in milliseconds.
    pub duration_ms: f32,
    /// Before the first iteration, in milliseconds.
    pub delay_ms: f32,
    /// The curve of each segment without one of its own (CSS `ease` by
    /// default; as in CSS it shapes every segment, not the whole).
    pub easing: Easing,
    /// How many iterations; `None` repeats until stopped.
    pub iterations: Option<u32>,
    /// Which way the iterations play.
    pub direction: PlaybackDirection,
    /// The highest frame rate it needs, in Hz ([`super::Driver::frame_rate`]);
    /// `None`: the screen's highest.
    pub frame_rate: Option<f32>,
}

impl Default for KeyframeTiming {
    fn default() -> Self {
        Self {
            duration_ms: 300.0,
            delay_ms: 0.0,
            easing: Easing::EASE,
            iterations: Some(1),
            direction: PlaybackDirection::Normal,
            frame_rate: None,
        }
    }
}

impl KeyframeTiming {
    /// One iteration of `duration_ms` with the defaults otherwise.
    pub fn new(duration_ms: f32) -> Self {
        Self {
            duration_ms,
            ..Self::default()
        }
    }

    /// With `easing` for segments without one.
    pub fn easing(mut self, easing: Easing) -> Self {
        self.easing = easing;
        self
    }

    /// Waiting `delay_ms` before the first iteration.
    pub fn delay(mut self, delay_ms: f32) -> Self {
        self.delay_ms = delay_ms;
        self
    }

    /// `iterations` times (`None`: until stopped).
    pub fn iterations(mut self, iterations: Option<u32>) -> Self {
        self.iterations = iterations;
        self
    }

    /// Repeating until stopped.
    pub fn infinite(self) -> Self {
        self.iterations(None)
    }

    /// Playing in `direction`.
    pub fn direction(mut self, direction: PlaybackDirection) -> Self {
        self.direction = direction;
        self
    }

    /// Needing at most `hz` frames per second.
    pub fn frame_rate(mut self, hz: f32) -> Self {
        self.frame_rate = Some(hz);
        self
    }
}

/// Keyframes with their offsets resolved (increasing, within 0..=1), as
/// the Web Animations API spaces them: the first defaults to 0 and the
/// last to 1 (a single one is the end), the ones between spread evenly
/// between their neighbors with an offset.
fn resolve(frames: &[Keyframe]) -> Vec<(f32, f32, Option<Easing>)> {
    let n = frames.len();
    let mut offsets: Vec<Option<f32>> = frames
        .iter()
        .map(|f| {
            f.offset
                .filter(|o| o.is_finite())
                .map(|o| o.clamp(0.0, 1.0))
        })
        .collect();
    if n == 1 {
        offsets[0].get_or_insert(1.0);
    } else if n > 1 {
        offsets[0].get_or_insert(0.0);
        offsets[n - 1].get_or_insert(1.0);
    }
    let mut i = 0;
    while i < n {
        if offsets[i].is_some() {
            i += 1;
            continue;
        }
        let start = i - 1;
        let mut end = i;
        while offsets[end].is_none() {
            end += 1;
        }
        let (a, b) = (offsets[start].unwrap_or(0.0), offsets[end].unwrap_or(1.0));
        for (k, slot) in offsets.iter_mut().enumerate().take(end).skip(i) {
            *slot = Some(a + (b - a) * (k - start) as f32 / (end - start) as f32);
        }
        i = end;
    }
    // Offsets go up: one below the one before it is held there.
    let mut last = 0.0f32;
    frames
        .iter()
        .zip(offsets)
        .map(|(f, o)| {
            let o = o.unwrap_or(1.0).max(last);
            last = o;
            (o, f.value, f.easing)
        })
        .collect()
}

/// One iteration over `frames` (resolved), forwards or backwards.
fn iteration(
    node: NodeId,
    frames: &[(f32, f32, Option<Easing>)],
    timing: &KeyframeTiming,
    backwards: bool,
) -> Composite {
    let duration = timing.duration_ms.max(0.0);
    // Backwards, offsets mirror and each segment runs its curve reversed.
    let oriented: Vec<(f32, f32, Easing)> = if backwards {
        let mut out = Vec::with_capacity(frames.len());
        for i in (0..frames.len()).rev() {
            let easing = match i {
                0 => timing.easing,
                _ => frames[i - 1].2.unwrap_or(timing.easing).reversed(),
            };
            out.push((1.0 - frames[i].0, frames[i].1, easing));
        }
        out
    } else {
        frames
            .iter()
            .map(|&(o, v, e)| (o, v, e.unwrap_or(timing.easing)))
            .collect()
    };
    let timing_to = |to: f32, ms: f32, easing: Easing| Composite::Run {
        node,
        driver: Driver::Timing {
            to,
            duration_ms: ms,
            easing,
            delay_ms: 0.0,
            frame_rate: timing.frame_rate,
        },
    };
    let mut steps = Vec::with_capacity(oriented.len() + 1);
    let Some(&(first_offset, first_value, _)) = oriented.first() else {
        return Composite::Sequence(steps);
    };
    // Without a keyframe at 0 the value goes from where it is, as CSS
    // goes from the underlying value.
    steps.push(timing_to(
        first_value,
        first_offset * duration,
        timing.easing,
    ));
    for pair in oriented.windows(2) {
        let ((a, _, easing), (b, value, _)) = (pair[0], pair[1]);
        steps.push(timing_to(value, (b - a) * duration, easing));
    }
    let last = oriented.last().map_or(1.0, |f| f.0);
    if last < 1.0 {
        steps.push(Composite::Delay((1.0 - last) * duration));
    }
    Composite::Sequence(steps)
}

/// The value `node` through `frames` as `timing` says: a tree of timing
/// steps the graph runs natively, each starting at the exact end of the
/// one before. Each iteration starts at its first keyframe (a jump, as in
/// CSS), so a loop of keyframes that end where they start never jumps;
/// a delay holds that first keyframe (CSS `animation-fill-mode:
/// backwards`). The value stays where the last iteration leaves it.
pub fn keyframes(node: NodeId, frames: &[Keyframe], timing: &KeyframeTiming) -> Composite {
    let frames = resolve(frames);
    let pass = |index| iteration(node, &frames, timing, timing.direction.reversed(index));
    let body = match (timing.iterations, timing.direction.alternates()) {
        (Some(0), _) => Composite::Sequence(Vec::new()),
        (Some(1), _) => pass(0),
        (iterations, false) => Composite::Loop {
            child: Box::new(pass(0)),
            iterations,
            reset: false,
        },
        (None, true) => Composite::Loop {
            child: Box::new(Composite::Sequence(vec![pass(0), pass(1)])),
            iterations: None,
            reset: false,
        },
        (Some(n), true) => {
            let pairs = Composite::Loop {
                child: Box::new(Composite::Sequence(vec![pass(0), pass(1)])),
                iterations: Some(n / 2),
                reset: false,
            };
            match n % 2 {
                0 => pairs,
                _ => Composite::Sequence(vec![pairs, pass(0)]),
            }
        }
    };
    if timing.delay_ms <= 0.0 {
        return body;
    }
    // During the delay the value already holds the first keyframe (CSS
    // `animation-fill-mode: backwards`), so a delayed entrance does not
    // show its view at rest first.
    let mut steps = Vec::with_capacity(3);
    let first = if timing.direction.reversed(0) {
        frames.last().map(|f| (1.0 - f.0, f.1))
    } else {
        frames.first().map(|f| (f.0, f.1))
    };
    if let Some((0.0, value)) = first {
        steps.push(Composite::Run {
            node,
            driver: Driver::Timing {
                to: value,
                duration_ms: 0.0,
                easing: Easing::Linear,
                delay_ms: 0.0,
                frame_rate: timing.frame_rate,
            },
        });
    }
    steps.push(Composite::Delay(timing.delay_ms));
    steps.push(body);
    Composite::Sequence(steps)
}

/// Where the value of `frames` is when keyframes played as `timing` says
/// begin and when they end: `(start, end)`. `None` without keyframes.
pub fn keyframe_ends(frames: &[Keyframe], timing: &KeyframeTiming) -> Option<(f32, f32)> {
    let (first, last) = (frames.first()?.value, frames.last()?.value);
    let at = |backwards| if backwards { last } else { first };
    let start = at(timing.direction.reversed(0));
    let end = match timing.iterations {
        Some(0) => start,
        Some(n) => {
            if timing.direction.reversed(n - 1) {
                first
            } else {
                last
            }
        }
        // Stopped at some point; where a single iteration ends.
        None => {
            if timing.direction.reversed(0) {
                first
            } else {
                last
            }
        }
    };
    Some((start, end))
}

/// Where the items of a staggered group start counting from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum StaggerFrom {
    /// The first item starts at once, the others one interval after the
    /// one before.
    #[default]
    First,
    /// The last item starts at once.
    Last,
    /// The middle item starts at once, the others spread out from it.
    Center,
    /// The item at this index starts at once.
    Index(usize),
}

/// How many intervals item `index` of `count` waits, counted from `from`.
pub fn stagger_delay(index: usize, count: usize, from: StaggerFrom) -> f32 {
    if count == 0 {
        return 0.0;
    }
    let index = index.min(count - 1);
    match from {
        StaggerFrom::First => index as f32,
        StaggerFrom::Last => (count - 1 - index) as f32,
        // Even counts have two middle items; both start at once.
        StaggerFrom::Center => (index as f32 - (count - 1) as f32 / 2.0).abs().floor(),
        StaggerFrom::Index(origin) => index.abs_diff(origin.min(count - 1)) as f32,
    }
}

/// What a motion moves: the opacity of a view and the parts of its
/// transform, applied in this order (translations first, then rotations,
/// then scales, about the view's transform origin).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MotionChannel {
    /// `opacity` (multiplies the view's), 0..1.
    Opacity,
    /// `translateX`, points.
    TranslateX,
    /// `translateY`, points.
    TranslateY,
    /// `rotate`, degrees clockwise.
    Rotate,
    /// `rotateX`, degrees.
    RotateX,
    /// `rotateY`, degrees.
    RotateY,
    /// `scale`.
    Scale,
    /// `scaleX`.
    ScaleX,
    /// `scaleY`.
    ScaleY,
}

impl MotionChannel {
    /// Every channel, in the order a motion applies them.
    pub const ALL: [MotionChannel; 9] = [
        MotionChannel::Opacity,
        MotionChannel::TranslateX,
        MotionChannel::TranslateY,
        MotionChannel::Rotate,
        MotionChannel::RotateX,
        MotionChannel::RotateY,
        MotionChannel::Scale,
        MotionChannel::ScaleX,
        MotionChannel::ScaleY,
    ];

    /// Its position in [`Self::ALL`].
    pub fn index(self) -> usize {
        self as usize
    }

    /// Its name in styles (`opacity`, `translateX`, ...).
    pub fn name(self) -> &'static str {
        match self {
            MotionChannel::Opacity => "opacity",
            MotionChannel::TranslateX => "translateX",
            MotionChannel::TranslateY => "translateY",
            MotionChannel::Rotate => "rotate",
            MotionChannel::RotateX => "rotateX",
            MotionChannel::RotateY => "rotateY",
            MotionChannel::Scale => "scale",
            MotionChannel::ScaleX => "scaleX",
            MotionChannel::ScaleY => "scaleY",
        }
    }

    /// The channel named `name`.
    pub fn parse(name: &str) -> Option<MotionChannel> {
        MotionChannel::ALL.into_iter().find(|c| c.name() == name)
    }

    /// Its value at rest (what changes nothing).
    pub fn identity(self) -> f32 {
        match self {
            MotionChannel::Opacity
            | MotionChannel::Scale
            | MotionChannel::ScaleX
            | MotionChannel::ScaleY => 1.0,
            _ => 0.0,
        }
    }

    /// The transform part it binds to (`None`: the opacity).
    pub fn transform(self) -> Option<TransformKind> {
        Some(match self {
            MotionChannel::Opacity => return None,
            MotionChannel::TranslateX => TransformKind::TranslateX,
            MotionChannel::TranslateY => TransformKind::TranslateY,
            MotionChannel::Rotate => TransformKind::Rotate,
            MotionChannel::RotateX => TransformKind::RotateX,
            MotionChannel::RotateY => TransformKind::RotateY,
            MotionChannel::Scale => TransformKind::Scale,
            MotionChannel::ScaleX => TransformKind::ScaleX,
            MotionChannel::ScaleY => TransformKind::ScaleY,
        })
    }
}

/// Several channels of a view through keyframes, played with one timing
/// or one spring: a preset ([`MotionPreset`]) or the app's own.
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct MotionSpec {
    /// The keyframes of each channel it moves.
    pub tracks: Vec<(MotionChannel, Vec<Keyframe>)>,
    /// How they play.
    pub timing: KeyframeTiming,
    /// Plays each channel on this spring instead of through its keyframes:
    /// from where the value is, keeping its velocity, to where the
    /// keyframes end ([`keyframe_ends`] of one iteration), after the
    /// timing's delay. The first keyframe is where the motion starts a
    /// view that enters ([`MotionSpec::start_values`]); the keyframes
    /// between, the duration, easings and iterations are not used, nor the
    /// spring's own velocity (each channel keeps its own).
    pub spring: Option<Spring>,
}

impl MotionSpec {
    /// A motion over `timing` with no tracks yet.
    pub fn new(timing: KeyframeTiming) -> Self {
        Self {
            tracks: Vec::new(),
            timing,
            spring: None,
        }
    }

    /// With `channel` through `frames` (replacing its track, if any).
    pub fn track(
        mut self,
        channel: MotionChannel,
        frames: impl IntoIterator<Item = Keyframe>,
    ) -> Self {
        let frames: Vec<Keyframe> = frames.into_iter().collect();
        match self.tracks.iter_mut().find(|(c, _)| *c == channel) {
            Some((_, f)) => *f = frames,
            None => self.tracks.push((channel, frames)),
        }
        self
    }

    /// With `channel` through `values`, spread evenly.
    pub fn values(self, channel: MotionChannel, values: impl IntoIterator<Item = f32>) -> Self {
        self.track(channel, values.into_iter().map(Keyframe::new))
    }

    /// With its timing changed by `f`.
    pub fn timing(mut self, f: impl FnOnce(KeyframeTiming) -> KeyframeTiming) -> Self {
        self.timing = f(self.timing);
        self
    }

    /// Playing each channel on `spring` ([`MotionSpec::spring`]).
    pub fn spring(mut self, spring: Spring) -> Self {
        self.spring = Some(spring);
        self
    }

    /// Where a channel through `frames` lands.
    fn end(&self, frames: &[Keyframe]) -> Option<f32> {
        let timing = match self.spring {
            Some(_) => KeyframeTiming {
                iterations: Some(1),
                ..self.timing
            },
            None => self.timing,
        };
        keyframe_ends(frames, &timing).map(|(_, end)| end)
    }

    /// The tree that plays it on the channels' value nodes (`nodes`, by
    /// [`MotionChannel::index`]). `reduced`: the user asked for less motion,
    /// so it lands where it ends at once, without moving.
    pub fn composite(&self, nodes: &[NodeId; 9], reduced: bool) -> Composite {
        let mut children = Vec::with_capacity(self.tracks.len());
        for (channel, frames) in &self.tracks {
            let node = nodes[channel.index()];
            if reduced {
                let Some(end) = self.end(frames) else {
                    continue;
                };
                children.push(Composite::Run {
                    node,
                    driver: Driver::Timing {
                        to: end,
                        duration_ms: 0.0,
                        easing: Easing::Linear,
                        delay_ms: 0.0,
                        frame_rate: self.timing.frame_rate,
                    },
                });
            } else if let Some(spring) = self.spring {
                let Some(to) = self.end(frames) else {
                    continue;
                };
                children.push(Composite::Run {
                    node,
                    driver: Driver::Spring {
                        to,
                        stiffness: spring.stiffness,
                        damping: spring.damping,
                        mass: spring.mass,
                        velocity: None,
                        frame_rate: spring.frame_rate.or(self.timing.frame_rate),
                    },
                });
            } else {
                children.push(keyframes(node, frames, &self.timing));
            }
        }
        let all = Composite::Parallel {
            children,
            stop_together: true,
        };
        // The delay of keyframes is theirs (it holds their first frame); a
        // spring waits with the values left where they are.
        match self.spring {
            Some(_) if !reduced && self.timing.delay_ms > 0.0 => {
                Composite::Sequence(vec![Composite::Delay(self.timing.delay_ms), all])
            }
            _ => all,
        }
    }

    /// Where each channel it moves starts: a motion that brings a view in
    /// starts from there, set before its first frame shows.
    pub fn start_values(&self) -> impl Iterator<Item = (MotionChannel, f32)> + '_ {
        self.tracks.iter().filter_map(|(channel, frames)| {
            Some((*channel, keyframe_ends(frames, &self.timing)?.0))
        })
    }
}

/// What a preset is for.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MotionKind {
    /// Brings a view in: it starts hidden or away and ends at rest.
    Enter,
    /// Takes a view out: it starts at rest and ends hidden or away.
    Exit,
    /// Draws the eye: it starts and ends at rest.
    Attention,
    /// Repeats until stopped.
    Loop,
}

impl MotionKind {
    /// Its name (`enter`, `exit`, `attention`, `loop`).
    pub fn name(self) -> &'static str {
        match self {
            MotionKind::Enter => "enter",
            MotionKind::Exit => "exit",
            MotionKind::Attention => "attention",
            MotionKind::Loop => "loop",
        }
    }
}

/// Keyframes of a preset's track: `(offset, value)`.
type Track = (MotionChannel, &'static [(f32, f32)]);

/// A ready-made motion, as data.
#[derive(Debug)]
pub struct PresetDef {
    /// Its name in JS (`fadeIn`, `shake`, ...).
    pub name: &'static str,
    /// What it is for.
    pub kind: MotionKind,
    /// One line about it.
    pub doc: &'static str,
    duration_ms: f32,
    easing: Easing,
    iterations: Option<u32>,
    direction: PlaybackDirection,
    tracks: &'static [Track],
}

impl PresetDef {
    /// The motion.
    pub fn spec(&self) -> MotionSpec {
        let timing = KeyframeTiming {
            duration_ms: self.duration_ms,
            easing: self.easing,
            iterations: self.iterations,
            direction: self.direction,
            ..KeyframeTiming::default()
        };
        let mut spec = MotionSpec::new(timing);
        for (channel, frames) in self.tracks {
            spec = spec.track(*channel, frames.iter().map(|&(o, v)| Keyframe::at(o, v)));
        }
        spec
    }
}

use MotionChannel::{
    Opacity as O, Rotate as R, RotateX as RX, RotateY as RY, Scale as S, ScaleX as SX,
    ScaleY as SY, TranslateX as X, TranslateY as Y,
};

/// Strong deceleration: things that arrive.
const OUT_EXPO: Easing = Easing::Bezier(0.16, 1.0, 0.3, 1.0);
/// Things that leave.
const IN_QUART: Easing = Easing::Bezier(0.5, 0.0, 0.75, 0.0);
/// A slight overshoot.
const OUT_BACK: Easing = Easing::Bezier(0.34, 1.56, 0.64, 1.0);
const IN_OUT_SINE: Easing = Easing::Bezier(0.37, 0.0, 0.63, 1.0);

macro_rules! presets {
    ($(
        $(#[doc = $doc:literal])+
        $variant:ident $name:literal $kind:ident $ms:literal $easing:expr, $iterations:expr, $direction:ident
            [$($channel:ident: [$(($o:literal, $v:literal)),* $(,)?]),* $(,)?]
    )*) => {
        /// The ready-made motions: entrances, exits, attention seekers and
        /// loops, as data ([`PresetDef`]). Distances are in points, sized
        /// for controls and cards; scale a motion's values for larger
        /// views, or write your own keyframes.
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        #[non_exhaustive]
        pub enum MotionPreset {
            $($(#[doc = $doc])+ $variant,)*
        }

        /// Every preset, in [`MotionPreset`]'s order.
        pub const PRESETS: &[PresetDef] = &[$(
            PresetDef {
                name: $name,
                kind: MotionKind::$kind,
                doc: concat!($($doc),+),
                duration_ms: $ms,
                easing: $easing,
                iterations: $iterations,
                direction: PlaybackDirection::$direction,
                tracks: &[$(($channel, &[$(($o, $v)),*])),*],
            },
        )*];

        impl MotionPreset {
            /// Every preset.
            pub const ALL: &[MotionPreset] = &[$(MotionPreset::$variant),*];
        }
    };
}

presets! {
    /// Fades in.
    FadeIn "fadeIn" Enter 300.0 Easing::EaseOut, Some(1), Normal [O: [(0.0, 0.0), (1.0, 1.0)]]
    /// Fades in, rising into place.
    FadeInUp "fadeInUp" Enter 450.0 OUT_EXPO, Some(1), Normal
        [O: [(0.0, 0.0), (1.0, 1.0)], Y: [(0.0, 24.0), (1.0, 0.0)]]
    /// Fades in, dropping into place.
    FadeInDown "fadeInDown" Enter 450.0 OUT_EXPO, Some(1), Normal
        [O: [(0.0, 0.0), (1.0, 1.0)], Y: [(0.0, -24.0), (1.0, 0.0)]]
    /// Fades in, coming from the left.
    FadeInLeft "fadeInLeft" Enter 450.0 OUT_EXPO, Some(1), Normal
        [O: [(0.0, 0.0), (1.0, 1.0)], X: [(0.0, -24.0), (1.0, 0.0)]]
    /// Fades in, coming from the right.
    FadeInRight "fadeInRight" Enter 450.0 OUT_EXPO, Some(1), Normal
        [O: [(0.0, 0.0), (1.0, 1.0)], X: [(0.0, 24.0), (1.0, 0.0)]]
    /// Fades in, growing from 90 %.
    ZoomIn "zoomIn" Enter 350.0 OUT_EXPO, Some(1), Normal
        [O: [(0.0, 0.0), (1.0, 1.0)], S: [(0.0, 0.9), (1.0, 1.0)]]
    /// Pops in: grows from half its size past its own and settles.
    PopIn "popIn" Enter 450.0 OUT_BACK, Some(1), Normal
        [O: [(0.0, 0.0), (0.4, 1.0), (1.0, 1.0)], S: [(0.0, 0.5), (1.0, 1.0)]]
    /// Bounces in, overshooting twice.
    BounceIn "bounceIn" Enter 650.0 Easing::Bezier(0.215, 0.61, 0.355, 1.0), Some(1), Normal
        [O: [(0.0, 0.0), (0.6, 1.0), (1.0, 1.0)],
         S: [(0.0, 0.3), (0.2, 1.1), (0.4, 0.9), (0.6, 1.03), (0.8, 0.97), (1.0, 1.0)]]
    /// Slides up from below, unfaded (sheets, toasts).
    SlideInUp "slideInUp" Enter 400.0 OUT_EXPO, Some(1), Normal [Y: [(0.0, 64.0), (1.0, 0.0)]]
    /// Slides down from above, unfaded.
    SlideInDown "slideInDown" Enter 400.0 OUT_EXPO, Some(1), Normal [Y: [(0.0, -64.0), (1.0, 0.0)]]
    /// Flips in about its horizontal axis.
    FlipInX "flipInX" Enter 600.0 Easing::EaseOut, Some(1), Normal
        [O: [(0.0, 0.0), (0.6, 1.0), (1.0, 1.0)], RX: [(0.0, 90.0), (0.4, -20.0), (0.6, 10.0), (0.8, -5.0), (1.0, 0.0)]]
    /// Flips in about its vertical axis.
    FlipInY "flipInY" Enter 600.0 Easing::EaseOut, Some(1), Normal
        [O: [(0.0, 0.0), (0.6, 1.0), (1.0, 1.0)], RY: [(0.0, 90.0), (0.4, -20.0), (0.6, 10.0), (0.8, -5.0), (1.0, 0.0)]]
    /// Fades out.
    FadeOut "fadeOut" Exit 250.0 Easing::EaseIn, Some(1), Normal [O: [(0.0, 1.0), (1.0, 0.0)]]
    /// Fades out, rising away.
    FadeOutUp "fadeOutUp" Exit 300.0 IN_QUART, Some(1), Normal
        [O: [(0.0, 1.0), (1.0, 0.0)], Y: [(0.0, 0.0), (1.0, -24.0)]]
    /// Fades out, dropping away.
    FadeOutDown "fadeOutDown" Exit 300.0 IN_QUART, Some(1), Normal
        [O: [(0.0, 1.0), (1.0, 0.0)], Y: [(0.0, 0.0), (1.0, 24.0)]]
    /// Fades out to the left.
    FadeOutLeft "fadeOutLeft" Exit 300.0 IN_QUART, Some(1), Normal
        [O: [(0.0, 1.0), (1.0, 0.0)], X: [(0.0, 0.0), (1.0, -24.0)]]
    /// Fades out to the right.
    FadeOutRight "fadeOutRight" Exit 300.0 IN_QUART, Some(1), Normal
        [O: [(0.0, 1.0), (1.0, 0.0)], X: [(0.0, 0.0), (1.0, 24.0)]]
    /// Fades out, shrinking to 90 %.
    ZoomOut "zoomOut" Exit 250.0 IN_QUART, Some(1), Normal
        [O: [(0.0, 1.0), (1.0, 0.0)], S: [(0.0, 1.0), (1.0, 0.9)]]
    /// Pops out: swells a little, then shrinks away.
    PopOut "popOut" Exit 300.0 Easing::EaseIn, Some(1), Normal
        [O: [(0.0, 1.0), (0.3, 1.0), (1.0, 0.0)], S: [(0.0, 1.0), (0.3, 1.08), (1.0, 0.5)]]
    /// Slides down out of view, unfaded.
    SlideOutDown "slideOutDown" Exit 300.0 IN_QUART, Some(1), Normal [Y: [(0.0, 0.0), (1.0, 64.0)]]
    /// Slides up out of view, unfaded.
    SlideOutUp "slideOutUp" Exit 300.0 IN_QUART, Some(1), Normal [Y: [(0.0, 0.0), (1.0, -64.0)]]
    /// Shakes sideways: a wrong password, an invalid field.
    Shake "shake" Attention 500.0 Easing::EaseInOut, Some(1), Normal
        [X: [(0.0, 0.0), (0.1, -8.0), (0.3, 8.0), (0.5, -8.0), (0.7, 8.0), (0.9, -4.0), (1.0, 0.0)]]
    /// Shakes its head: a gentler no.
    HeadShake "headShake" Attention 800.0 Easing::EaseInOut, Some(1), Normal
        [X: [(0.0, 0.0), (0.065, -6.0), (0.185, 5.0), (0.315, -3.0), (0.435, 2.0), (0.5, 0.0), (1.0, 0.0)],
         RY: [(0.0, 0.0), (0.065, -9.0), (0.185, 7.0), (0.315, -5.0), (0.435, 3.0), (0.5, 0.0), (1.0, 0.0)]]
    /// Wiggles about its center: something to notice.
    Wiggle "wiggle" Attention 600.0 Easing::EaseInOut, Some(1), Normal
        [R: [(0.0, 0.0), (0.15, -6.0), (0.3, 6.0), (0.45, -4.0), (0.6, 4.0), (0.75, -2.0), (1.0, 0.0)]]
    /// Swings side to side like a hanging sign (hang it from its top edge
    /// with a top transform origin).
    Swing "swing" Attention 900.0 Easing::EaseInOut, Some(1), Normal
        [R: [(0.0, 0.0), (0.2, 15.0), (0.4, -10.0), (0.6, 5.0), (0.8, -5.0), (1.0, 0.0)]]
    /// Grows a little and back: a soft emphasis.
    Pulse "pulse" Attention 500.0 Easing::EaseInOut, Some(1), Normal
        [S: [(0.0, 1.0), (0.5, 1.06), (1.0, 1.0)]]
    /// Beats twice, like a heart: a like, a favorite.
    Heartbeat "heartbeat" Attention 900.0 Easing::EaseInOut, Some(1), Normal
        [S: [(0.0, 1.0), (0.14, 1.25), (0.28, 1.0), (0.42, 1.25), (0.7, 1.0), (1.0, 1.0)]]
    /// Squashes and stretches like rubber.
    RubberBand "rubberBand" Attention 800.0 Easing::EaseInOut, Some(1), Normal
        [SX: [(0.0, 1.0), (0.3, 1.25), (0.4, 0.75), (0.5, 1.15), (0.65, 0.95), (0.75, 1.05), (1.0, 1.0)],
         SY: [(0.0, 1.0), (0.3, 0.75), (0.4, 1.25), (0.5, 0.85), (0.65, 1.05), (0.75, 0.95), (1.0, 1.0)]]
    /// Wobbles like jelly.
    Jello "jello" Attention 800.0 Easing::EaseInOut, Some(1), Normal
        [SX: [(0.0, 1.0), (0.2, 1.12), (0.4, 0.92), (0.6, 1.05), (0.8, 0.98), (1.0, 1.0)],
         SY: [(0.0, 1.0), (0.2, 0.88), (0.4, 1.08), (0.6, 0.95), (0.8, 1.02), (1.0, 1.0)]]
    /// Shrinks, then grows with a wiggle: a reward.
    Tada "tada" Attention 900.0 Easing::EaseInOut, Some(1), Normal
        [S: [(0.0, 1.0), (0.1, 0.9), (0.2, 0.9), (0.3, 1.1), (0.9, 1.1), (1.0, 1.0)],
         R: [(0.0, 0.0), (0.1, -3.0), (0.2, -3.0), (0.3, 3.0), (0.4, -3.0), (0.5, 3.0), (0.6, -3.0), (0.7, 3.0), (0.8, -3.0), (0.9, 3.0), (1.0, 0.0)]]
    /// Jumps and lands with a squash.
    Jump "jump" Attention 700.0 Easing::EaseInOut, Some(1), Normal
        [Y: [(0.0, 0.0), (0.4, -18.0), (0.55, -18.0), (0.8, 0.0), (1.0, 0.0)],
         SY: [(0.0, 1.0), (0.15, 0.9), (0.4, 1.05), (0.8, 0.92), (1.0, 1.0)]]
    /// Blinks out and in twice.
    Flash "flash" Attention 800.0 Easing::Linear, Some(1), Normal
        [O: [(0.0, 1.0), (0.25, 0.0), (0.5, 1.0), (0.75, 0.0), (1.0, 1.0)]]
    /// Turns once, forever (CSS `animate-spin`'s turn on a motion).
    Spin "spin" Loop 1000.0 Easing::Linear, None, Normal [R: [(0.0, 0.0), (1.0, 360.0)]]
    /// Floats up and down, forever.
    Float "float" Loop 1500.0 IN_OUT_SINE, None, Alternate [Y: [(0.0, 0.0), (1.0, -8.0)]]
    /// Breathes: grows and fades a little, forever.
    Breathe "breathe" Loop 1800.0 IN_OUT_SINE, None, Alternate
        [S: [(0.0, 1.0), (1.0, 1.05)], O: [(0.0, 1.0), (1.0, 0.8)]]
    /// Sways about its center, forever.
    Sway "sway" Loop 1600.0 IN_OUT_SINE, None, Alternate [R: [(0.0, -4.0), (1.0, 4.0)]]
    /// Bobs like a bouncing ball, forever (Tailwind's `animate-bounce`).
    Bob "bob" Loop 1000.0 Easing::Linear, None, Normal
        [Y: [(0.0, 0.0), (0.5, -12.0), (1.0, 0.0)]]
    /// Glows in and out, forever (Tailwind's `animate-pulse` on a motion).
    Glow "glow" Loop 2000.0 Easing::Bezier(0.4, 0.0, 0.6, 1.0), None, Normal
        [O: [(0.0, 1.0), (0.5, 0.5), (1.0, 1.0)]]
}

impl MotionPreset {
    /// Its definition.
    pub fn def(self) -> &'static PresetDef {
        &PRESETS[self as usize]
    }

    /// Its name in JS.
    pub fn name(self) -> &'static str {
        self.def().name
    }

    /// The preset named `name`.
    pub fn parse(name: &str) -> Option<MotionPreset> {
        MotionPreset::ALL.iter().copied().find(|p| p.name() == name)
    }

    /// The motion.
    pub fn spec(self) -> MotionSpec {
        self.def().spec()
    }
}

impl From<MotionPreset> for MotionSpec {
    fn from(preset: MotionPreset) -> Self {
        preset.spec()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::animation::{AnimationOp, Finished, Graph};

    fn value(g: &mut Graph, node: NodeId, start: f32) {
        g.apply(
            AnimationOp::CreateValue { node, value: start },
            &mut Vec::new(),
        );
    }

    /// Runs `spec` on node 2 from `start`, sampled at `times` (ms).
    fn run(spec: Composite, start: f32, times: &[f64]) -> (Vec<f32>, Vec<Finished>) {
        let mut g = Graph::default();
        value(&mut g, 2, start);
        let mut f = Vec::new();
        g.apply(
            AnimationOp::StartComposite {
                animation: 10,
                key: 12,
                spec,
            },
            &mut f,
        );
        // Frames every 10 ms up to each sample (a composite starts at most a
        // few steps per frame).
        let mut out = Vec::new();
        let mut now = 0.0;
        for &t in times {
            while now + 10.0 < t {
                now += 10.0;
                g.tick(now / 1000.0, &mut f);
            }
            now = t;
            g.tick(t / 1000.0, &mut f);
            out.push((g.value(2).unwrap() * 1000.0).round() / 1000.0);
        }
        (out, f)
    }

    fn linear(ms: f32) -> KeyframeTiming {
        KeyframeTiming::new(ms).easing(Easing::Linear)
    }

    #[test]
    fn offsets_spread_as_the_web_animations_api_spreads_them() {
        let frames = [
            Keyframe::new(0.0),
            Keyframe::new(1.0),
            Keyframe::at(0.8, 2.0),
            Keyframe::new(3.0),
        ];
        let resolved: Vec<f32> = resolve(&frames).iter().map(|f| f.0).collect();
        assert_eq!(resolved, [0.0, 0.4, 0.8, 1.0]);
        assert_eq!(resolve(&[Keyframe::new(5.0)])[0].0, 1.0);
        // Out of order: held at the one before.
        let held: Vec<f32> = resolve(&[Keyframe::at(0.5, 0.0), Keyframe::at(0.2, 1.0)])
            .iter()
            .map(|f| f.0)
            .collect();
        assert_eq!(held, [0.5, 0.5]);
    }

    #[test]
    fn keyframes_play_their_segments_on_time() {
        let frames = [
            Keyframe::new(0.0),
            Keyframe::at(0.25, 10.0),
            Keyframe::new(0.0),
        ];
        let spec = keyframes(2, &frames, &linear(400.0));
        // The jump to the first keyframe happens on the first frame.
        let (values, f) = run(spec, 7.0, &[0.0, 50.0, 100.0, 250.0, 400.0, 450.0]);
        assert_eq!(values, [0.0, 5.0, 10.0, 5.0, 0.0, 0.0]);
        assert!(f.iter().any(|f| f.animation == 10 && f.finished), "{f:?}");
    }

    #[test]
    fn directions_iterations_and_delays() {
        let frames = [Keyframe::new(0.0), Keyframe::new(10.0)];
        let alternate = linear(100.0)
            .direction(PlaybackDirection::Alternate)
            .iterations(Some(3));
        let (values, _) = run(
            keyframes(2, &frames, &alternate),
            0.0,
            &[0.0, 50.0, 150.0, 250.0, 300.0, 400.0],
        );
        assert_eq!(values, [0.0, 5.0, 5.0, 5.0, 10.0, 10.0]);
        let reverse = linear(100.0)
            .direction(PlaybackDirection::Reverse)
            .delay(100.0);
        let (values, _) = run(
            keyframes(2, &frames, &reverse),
            3.0,
            &[0.0, 50.0, 150.0, 200.0],
        );
        // The delay holds the first keyframe (the last, backwards).
        assert_eq!(values, [10.0, 10.0, 5.0, 0.0]);
        // Endless: still going long after.
        let (values, f) = run(
            keyframes(2, &frames, &linear(100.0).infinite()),
            0.0,
            &[0.0, 10_050.0],
        );
        assert_eq!(values[1], 5.0);
        assert!(!f.iter().any(|f| f.animation == 10), "{f:?}");
        assert_eq!(
            keyframe_ends(&frames, &alternate.iterations(Some(2))),
            Some((0.0, 0.0))
        );
    }

    #[test]
    fn a_reversed_segment_runs_its_curve_backwards() {
        let ease = Easing::Bezier(0.42, 0.0, 1.0, 1.0);
        let frames = [Keyframe::new(0.0).ease(ease), Keyframe::new(1.0)];
        let timing = KeyframeTiming::new(100.0).direction(PlaybackDirection::Reverse);
        let (backwards, _) = run(keyframes(2, &frames, &timing), 0.0, &[0.0, 25.0]);
        let (forwards, _) = run(
            keyframes(2, &frames, &KeyframeTiming::new(100.0)),
            0.0,
            &[0.0, 75.0],
        );
        assert!(
            (backwards[1] - forwards[1]).abs() < 1e-3,
            "{backwards:?} {forwards:?}"
        );
    }

    #[test]
    fn steps_jump_as_in_css() {
        use super::super::sample_easing;
        use crate::animation::StepPosition::*;
        let at = |e| sample_easing(Easing::Steps(4, e), 5);
        assert_eq!(at(JumpEnd), [0.0, 0.25, 0.5, 0.75, 1.0]);
        assert_eq!(at(JumpStart), [0.25, 0.5, 0.75, 1.0, 1.0]);
        let none = sample_easing(Easing::Steps(3, JumpNone), 4);
        assert_eq!(none, [0.0, 0.5, 1.0, 1.0]);
        let both = sample_easing(Easing::Steps(1, JumpBoth), 3);
        assert_eq!(both, [0.5, 0.5, 1.0]);
        assert_eq!(
            Easing::Steps(2, JumpStart).reversed(),
            Easing::Steps(2, JumpEnd)
        );
    }

    #[test]
    fn staggering_counts_from_its_origin() {
        let delays = |from| {
            (0..5)
                .map(|i| stagger_delay(i, 5, from))
                .collect::<Vec<_>>()
        };
        assert_eq!(delays(StaggerFrom::First), [0.0, 1.0, 2.0, 3.0, 4.0]);
        assert_eq!(delays(StaggerFrom::Last), [4.0, 3.0, 2.0, 1.0, 0.0]);
        assert_eq!(delays(StaggerFrom::Center), [2.0, 1.0, 0.0, 1.0, 2.0]);
        assert_eq!(delays(StaggerFrom::Index(1)), [1.0, 0.0, 1.0, 2.0, 3.0]);
        let even: Vec<f32> = (0..4)
            .map(|i| stagger_delay(i, 4, StaggerFrom::Center))
            .collect();
        assert_eq!(even, [1.0, 0.0, 0.0, 1.0]);
    }

    #[test]
    fn presets_are_well_formed() {
        assert_eq!(PRESETS.len(), MotionPreset::ALL.len());
        for (i, preset) in MotionPreset::ALL.iter().enumerate() {
            let def = preset.def();
            assert_eq!(def.name, PRESETS[i].name);
            assert_eq!(MotionPreset::parse(def.name), Some(*preset));
            let spec = preset.spec();
            assert!(!spec.tracks.is_empty(), "{}", def.name);
            for (channel, frames) in &spec.tracks {
                let resolved = resolve(frames);
                assert_eq!(resolved.first().map(|f| f.0), Some(0.0), "{}", def.name);
                assert_eq!(resolved.last().map(|f| f.0), Some(1.0), "{}", def.name);
                assert!(
                    resolved.windows(2).all(|w| w[0].0 <= w[1].0),
                    "{}",
                    def.name
                );
                let (start, end) = keyframe_ends(frames, &spec.timing).unwrap();
                let rest = channel.identity();
                match def.kind {
                    MotionKind::Enter => assert_eq!(end, rest, "{} ends at rest", def.name),
                    MotionKind::Exit => assert_eq!(start, rest, "{} starts at rest", def.name),
                    MotionKind::Attention => {
                        assert_eq!((start, end), (rest, rest), "{}", def.name)
                    }
                    MotionKind::Loop => assert!(spec.timing.iterations.is_none(), "{}", def.name),
                }
            }
        }
    }

    /// The spring of csp's motions: about 380 ms to settle, 2.8 % over.
    fn csp_spring() -> Spring {
        Spring {
            stiffness: 620.0,
            damping: 36.0,
            ..Spring::default()
        }
    }

    #[test]
    fn a_spring_motion_springs_each_channel_to_where_it_ends() {
        let spec = MotionPreset::FadeInUp.spec().spring(csp_spring());
        let nodes = [2, 4, 6, 8, 10, 12, 14, 16, 18];
        let Composite::Parallel { children, .. } = spec.composite(&nodes, false) else {
            panic!()
        };
        let targets: Vec<_> = children
            .iter()
            .map(|child| match child {
                Composite::Run {
                    node,
                    driver:
                        Driver::Spring {
                            to,
                            stiffness,
                            velocity: None,
                            ..
                        },
                } => (*node, *to, *stiffness),
                other => panic!("{other:?}"),
            })
            .collect();
        assert_eq!(targets, [(2, 1.0, 620.0), (6, 0.0, 620.0)]);
        // Starts from where the value is (no jump to the first keyframe),
        // passes its end a little and settles there.
        let track = MotionSpec::default()
            .values(MotionChannel::Opacity, [0.0, 10.0])
            .spring(csp_spring());
        let mut nodes = [0; 9];
        nodes[MotionChannel::Opacity.index()] = 2;
        let (values, f) = run(
            track.composite(&nodes, false),
            5.0,
            &[0.0, 120.0, 200.0, 1000.0],
        );
        assert_eq!(values[0], 5.0);
        assert!(values[1] > 5.0 && values[1] < 10.0, "{values:?}");
        assert!(values[2] > 10.0, "{values:?}");
        assert_eq!(values[3], 10.0);
        assert!(f.iter().any(|f| f.animation == 10 && f.finished), "{f:?}");
        // Its delay leaves the value where it is.
        let delayed = track.clone().timing(|t| t.delay(100.0));
        let (values, _) = run(delayed.composite(&nodes, false), 5.0, &[0.0, 90.0, 200.0]);
        assert_eq!(values[..2], [5.0, 5.0]);
        assert!(values[2] > 5.0, "{values:?}");
        // Less motion: it lands at once, as keyframes do.
        let (values, _) = run(track.composite(&nodes, true), 5.0, &[0.0]);
        assert_eq!(values, [10.0]);
    }

    #[test]
    fn an_interrupted_spring_motion_goes_on_from_its_value_and_velocity() {
        let mut nodes = [0; 9];
        nodes[MotionChannel::TranslateY.index()] = 2;
        let to = |end: f32| {
            MotionSpec::default()
                .values(MotionChannel::TranslateY, [100.0, end])
                .spring(csp_spring())
                .composite(&nodes, false)
        };
        let mut g = Graph::default();
        value(&mut g, 2, 100.0);
        let mut f = Vec::new();
        let start = |g: &mut Graph, animation, spec, f: &mut Vec<Finished>| {
            g.apply(
                AnimationOp::StartComposite {
                    animation,
                    key: 12,
                    spec,
                },
                f,
            )
        };
        start(&mut g, 10, to(0.0), &mut f);
        let mut now = 0.0;
        while now < 80.0 {
            g.tick(now / 1000.0, &mut f);
            now += 10.0;
        }
        let before = g.value(2).unwrap();
        assert!(before < 100.0 && before > 0.0, "{before}");
        // Sent back while it falls: it goes on from there, still falling
        // for a moment, rather than jumping to its first keyframe.
        start(&mut g, 11, to(100.0), &mut f);
        g.tick(now / 1000.0, &mut f);
        let at_once = g.value(2).unwrap();
        g.tick((now + 10.0) / 1000.0, &mut f);
        let next = g.value(2).unwrap();
        assert!((at_once - before).abs() < 15.0, "{before} {at_once}");
        assert!(next < before, "{before} {at_once} {next}");
        assert!(f.iter().any(|f| f.animation == 10 && !f.finished), "{f:?}");
        for _ in 0..100 {
            now += 10.0;
            g.tick(now / 1000.0, &mut f);
        }
        assert_eq!(g.value(2), Some(100.0));
        assert!(f.iter().any(|f| f.animation == 11 && f.finished), "{f:?}");
    }

    #[test]
    fn a_reduced_motion_lands_at_its_end() {
        let spec = MotionPreset::FadeInUp.spec();
        let nodes = [2, 4, 6, 8, 10, 12, 14, 16, 18];
        let Composite::Parallel { children, .. } = spec.composite(&nodes, true) else {
            panic!()
        };
        assert_eq!(children.len(), 2);
        for child in &children {
            assert!(matches!(
                child,
                Composite::Run {
                    driver: Driver::Timing {
                        duration_ms: 0.0,
                        ..
                    },
                    ..
                }
            ));
        }
        let starts: Vec<_> = spec.start_values().collect();
        assert_eq!(
            starts,
            [
                (MotionChannel::Opacity, 0.0),
                (MotionChannel::TranslateY, 24.0)
            ]
        );
    }
}