makeover_timing/lib.rs
1//! The time axis of the make-family design system.
2//!
3//! <!-- wiki: makeover-timing -->
4//!
5//! [`makeover`] resolves colour, `makeover-geometry` resolves distance, and
6//! `makeover-layout` names what a thing is without resolving anything. This
7//! crate answers the remaining question of the same shape: **how long**.
8//!
9//! A duration is named by what it is waiting for, and the number follows:
10//!
11//! | Moment | What it waits for | Resolves to |
12//! |---|---|---|
13//! | [`Moment::Revert`] | a control returning to its resting label after confirming | 1500ms |
14//! | [`Moment::Clear`] | a status line emptying itself | 2000ms |
15//! | [`Moment::Dismiss`] | a transient notice's lifetime | 3000ms |
16//! | [`Moment::Debounce`] | typing settling before the work starts | 150ms |
17//!
18//! A moment resolves to exactly one duration on every renderer and under every
19//! theme; there is no per-renderer table.
20//!
21//! # Out of scope
22//!
23//! A race (a wait for a write to land, a delay so a mousedown beats a blur),
24//! a severity (a message that must not be missed is not
25//! `Notice::transient`, and [`notice_lifetime`] gives it no lifetime) and a
26//! poll interval are not moments.
27//!
28//! # Three axes
29//!
30//! [`Moment`] is how long a state lasts, [`Motion`] how long a change takes,
31//! and [`Cadence`] how often a repeating mark repeats.
32//!
33//! # Reduced motion
34//!
35//! A web surface gets a `prefers-reduced-motion` block in the generated
36//! stylesheet ([`reduced_motion_css`]), which zeroes every [`Motion`] and
37//! [`Cadence`]. Another renderer passes the [`MotionPreference`] its platform
38//! reported to [`Motion::duration_for`], which is zero under
39//! [`MotionPreference::Reduced`], and to [`activity_blink`], or [`blink`] for
40//! the phase, which keep the mark lit and still. [`Motion::duration`] is the
41//! full duration under either preference. A [`Moment`] is a wait and is
42//! unaffected.
43//!
44//! # Consumers
45//!
46//! Web surfaces bake [`timing_css`] in at build time. egui and ratatui surfaces
47//! read [`Moment::duration`] and its siblings. Each value is pinned by a test.
48//!
49//! [`makeover`]: https://makenot.work/git/max/makeover
50
51#![forbid(unsafe_code)]
52
53use std::fmt::Write as _;
54use std::time::Duration;
55
56use makeover_geometry::{in_css_layer, write_declarations};
57
58/// A duration named by what it is waiting for.
59///
60/// Each member is a thing a reader is waiting through; the crate header says
61/// what is left out.
62#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
63pub enum Moment {
64 /// How long a control shows that it did something before returning to its
65 /// resting label.
66 ///
67 /// "Copied!" on a button that said "Copy link".
68 Revert,
69 /// How long a status line holds a message before emptying itself.
70 ///
71 /// The message is a receipt for something the user just did.
72 Clear,
73 /// How long a transient notice lives before it starts to leave.
74 ///
75 /// Excludes the leaving itself, which is [`Motion::Fade`]. A notice the
76 /// user must not miss is not transient and gets no lifetime at all; see
77 /// [`notice_lifetime`].
78 Dismiss,
79 /// How long input waits to settle before the work behind it starts.
80 ///
81 /// A search field that queries on every keystroke, filtered through this.
82 Debounce,
83}
84
85impl Moment {
86 /// The duration in whole milliseconds.
87 ///
88 /// [`Self::duration`] and [`Self::css`] are spellings of this number.
89 #[must_use]
90 pub const fn ms(self) -> u32 {
91 match self {
92 Self::Revert => 1500,
93 Self::Clear => 2000,
94 Self::Dismiss => 3000,
95 Self::Debounce => 150,
96 }
97 }
98
99 /// The duration as a [`Duration`], for the renderers that are not a
100 /// browser.
101 #[must_use]
102 pub const fn duration(self) -> Duration {
103 Duration::from_millis(self.ms() as u64)
104 }
105
106 /// The CSS custom property name, without the leading dashes.
107 #[must_use]
108 pub const fn token(self) -> &'static str {
109 match self {
110 Self::Revert => "timing-revert",
111 Self::Clear => "timing-clear",
112 Self::Dismiss => "timing-dismiss",
113 Self::Debounce => "timing-debounce",
114 }
115 }
116
117 /// The CSS value, in milliseconds at every rung (`1500ms`, never `1.5s`).
118 #[must_use]
119 pub fn css(self) -> String {
120 format!("{}ms", self.ms())
121 }
122
123 /// Every moment, in the order they are emitted.
124 #[must_use]
125 pub const fn all() -> &'static [Self] {
126 &[Self::Revert, Self::Clear, Self::Dismiss, Self::Debounce]
127 }
128}
129
130/// How long a change takes, as opposed to how long a state lasts.
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
132pub enum Motion {
133 /// A thing leaving: opacity to zero, then gone.
134 ///
135 /// The whole of the departure: a renderer that removes the node itself
136 /// waits this out first.
137 Fade,
138 /// A state answering a pointer or a press: a hover tone, a pressed bevel,
139 /// a disclosure turning.
140 Quick,
141}
142
143impl Motion {
144 /// The duration in whole milliseconds.
145 #[must_use]
146 pub const fn ms(self) -> u32 {
147 match self {
148 Self::Fade => 300,
149 Self::Quick => 120,
150 }
151 }
152
153 /// The full duration as a [`Duration`], under either motion preference;
154 /// [`duration_for`](Self::duration_for) answers the preference.
155 #[must_use]
156 pub const fn duration(self) -> Duration {
157 Duration::from_millis(self.ms() as u64)
158 }
159
160 /// The duration under the reader's motion preference: [`duration`](Self::duration)
161 /// in full, and zero under [`MotionPreference::Reduced`], as
162 /// [`reduced_motion_css`] sets every `--motion-*` token to `0ms`.
163 ///
164 /// The caller supplies the preference its platform reported.
165 ///
166 /// ```
167 /// # use makeover_timing::{Motion, MotionPreference};
168 /// # use std::time::Duration;
169 /// assert_eq!(Motion::Fade.duration_for(MotionPreference::Full), Motion::Fade.duration());
170 /// assert_eq!(Motion::Fade.duration_for(MotionPreference::Reduced), Duration::ZERO);
171 /// ```
172 #[must_use]
173 pub const fn duration_for(self, preference: MotionPreference) -> Duration {
174 match preference {
175 MotionPreference::Full => self.duration(),
176 MotionPreference::Reduced => Duration::ZERO,
177 }
178 }
179
180 /// The CSS custom property name, without the leading dashes.
181 #[must_use]
182 pub const fn token(self) -> &'static str {
183 match self {
184 Self::Fade => "motion-fade",
185 Self::Quick => "motion-quick",
186 }
187 }
188
189 /// The CSS value, as a `ms` time.
190 #[must_use]
191 pub fn css(self) -> String {
192 format!("{}ms", self.ms())
193 }
194
195 /// Every motion, in the order they are emitted.
196 #[must_use]
197 pub const fn all() -> &'static [Self] {
198 &[Self::Fade, Self::Quick]
199 }
200}
201
202/// How often a repeating mark repeats.
203#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
204pub enum Cadence {
205 /// How long the activity mark holds each of its two states.
206 ///
207 /// The mark says something is happening on a wait with no countable size
208 /// (`makeover-layout`'s `Awaiting`). **A half-period, not a cycle**: lit
209 /// for 500ms, dark for 500ms, so a full [`cycle`](Self::cycle) is a second.
210 ///
211 /// For waits with nothing to watch. Where the activity is observable, the
212 /// mark follows it instead (an upload blinks per chunk delivered).
213 Activity,
214}
215
216impl Cadence {
217 /// The duration in whole milliseconds.
218 #[must_use]
219 pub const fn ms(self) -> u32 {
220 match self {
221 Self::Activity => 500,
222 }
223 }
224
225 /// The duration as a [`Duration`].
226 #[must_use]
227 pub const fn duration(self) -> Duration {
228 Duration::from_millis(self.ms() as u64)
229 }
230
231 /// One full on-and-off cycle, twice [`duration`](Self::duration).
232 #[must_use]
233 pub const fn cycle(self) -> Duration {
234 Duration::from_millis(self.ms() as u64 * 2)
235 }
236
237 /// The CSS custom property name, without the leading dashes.
238 #[must_use]
239 pub const fn token(self) -> &'static str {
240 match self {
241 Self::Activity => "cadence-activity",
242 }
243 }
244
245 /// The CSS value, as a `ms` time.
246 #[must_use]
247 pub fn css(self) -> String {
248 format!("{}ms", self.ms())
249 }
250
251 /// Every cadence, in the order they are emitted.
252 #[must_use]
253 pub const fn all() -> &'static [Self] {
254 &[Self::Activity]
255 }
256}
257
258/// Whether the reader has asked for less motion, as their platform reported it.
259#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
260pub enum MotionPreference {
261 /// No preference stated, so things move.
262 #[default]
263 Full,
264 /// The reader asked for less movement.
265 Reduced,
266}
267
268/// Whether a notice goes away on its own.
269///
270/// The answer `makeover_layout::Notice::transient` gives, carried without the
271/// `Notice` itself: this crate does not depend on `makeover-layout`.
272#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
273pub enum Persistence {
274 /// It dismisses itself, after [`Moment::Dismiss`].
275 Transient,
276 /// It stays until its cause is fixed, and has no lifetime.
277 Standing,
278}
279
280/// How fast the activity mark blinks, given whether the reader has asked for
281/// less motion.
282///
283/// The caller supplies the preference its platform reported.
284///
285/// ```
286/// # use makeover_timing::{Cadence, MotionPreference, activity_blink};
287/// assert_eq!(
288/// activity_blink(MotionPreference::Full),
289/// Some(Cadence::Activity.duration())
290/// );
291/// assert_eq!(activity_blink(MotionPreference::Reduced), None);
292/// ```
293///
294/// `None` means the mark does not blink: it is drawn, lit and still for as
295/// long as the wait lasts. It does not mean "the caller decides" or "draw
296/// nothing".
297///
298/// A web surface does not call this: the `prefers-reduced-motion` block in
299/// [`reduced_motion_css`] does the same in the cascade.
300#[must_use]
301pub const fn activity_blink(preference: MotionPreference) -> Option<Duration> {
302 match preference {
303 MotionPreference::Full => Some(Cadence::Activity.duration()),
304 MotionPreference::Reduced => None,
305 }
306}
307
308/// Which state the activity mark is in, `elapsed` into a wait, and how long it
309/// holds that state.
310///
311/// Lit for the first half-period of every cycle of [`Cadence::Activity`], dark
312/// for the second. Lit and still, with no next flip, whenever
313/// [`activity_blink`] gives no cadence or a zero one. The delay is what a
314/// renderer that schedules its own repaints waits before drawing the flip.
315///
316/// ```
317/// # use makeover_timing::{MotionPreference, blink};
318/// # use std::time::Duration;
319/// let ms = Duration::from_millis;
320/// assert_eq!(blink(ms(0), MotionPreference::Full), (true, Some(ms(500))));
321/// assert_eq!(blink(ms(600), MotionPreference::Full), (false, Some(ms(400))));
322/// assert_eq!(blink(ms(1100), MotionPreference::Full), (true, Some(ms(400))));
323/// assert_eq!(blink(ms(600), MotionPreference::Reduced), (true, None));
324/// ```
325#[must_use]
326pub fn blink(elapsed: Duration, motion: MotionPreference) -> (bool, Option<Duration>) {
327 let Some(half) = activity_blink(motion).map(|half| half.as_nanos()) else {
328 return (true, None);
329 };
330 let Some(into) = elapsed.as_nanos().checked_rem(half * 2) else {
331 return (true, None);
332 };
333 let lit = into < half;
334 let until = if lit { half } else { half * 2 } - into;
335 (
336 lit,
337 Some(Duration::from_nanos(
338 u64::try_from(until).unwrap_or(u64::MAX),
339 )),
340 )
341}
342
343/// How long a notice lives, given whether it goes away on its own.
344///
345/// `makeover-layout` documents `Notice::Toast` as "transient, stacked,
346/// dismisses itself" and says nothing about when. This is when:
347///
348/// ```
349/// # use makeover_timing::{Moment, Persistence, notice_lifetime};
350/// assert_eq!(
351/// notice_lifetime(Persistence::Transient),
352/// Some(Moment::Dismiss.duration())
353/// );
354/// assert_eq!(notice_lifetime(Persistence::Standing), None);
355/// ```
356///
357/// `None` means the notice has no lifetime: it stays until its cause is
358/// fixed. It does not mean "the caller decides".
359#[must_use]
360pub const fn notice_lifetime(persistence: Persistence) -> Option<Duration> {
361 match persistence {
362 Persistence::Transient => Some(Moment::Dismiss.duration()),
363 Persistence::Standing => None,
364 }
365}
366
367/// Emit the time axis as CSS declarations, no selector.
368///
369/// [`Moment::Debounce`] is emitted with the rest although no stylesheet uses
370/// it, so a script can read the value off the computed style.
371#[must_use]
372pub fn timing_css_declarations() -> String {
373 let mut out = String::new();
374 out.push_str(" /* Time. Named for what is being waited on; the number\n");
375 out.push_str(" follows. One duration per moment on every renderer. */\n");
376 write_declarations(&mut out, Moment::all(), Moment::token, Moment::css);
377 out.push_str("\n /* Motion: how long a change takes, not how long a state\n");
378 out.push_str(" lasts. A separate question, so a separate axis. */\n");
379 write_declarations(&mut out, Motion::all(), Motion::token, Motion::css);
380
381 out.push_str("\n /* Cadence: how often a repeating mark repeats. A\n");
382 out.push_str(" half-period, so a full cycle is twice it. */\n");
383 write_declarations(&mut out, Cadence::all(), Cadence::token, Cadence::css);
384 out
385}
386
387/// The motion-off block, for a reader who has asked for less of it.
388///
389/// Every [`Motion`] and [`Cadence`] token resolves to `0ms`. A zero-length
390/// transition jumps to its end state, so a notice that would fade is removed
391/// when its lifetime ends.
392///
393/// **The blink needs one thing from the rule that consumes it**, and it does
394/// not fall out of the zero on its own. A zero-length animation with no
395/// `animation-fill-mode` leaves the element in its *base* style, not at its
396/// last keyframe. So write the rule with the mark lit in the base style and the
397/// keyframes doing the dimming, never the other way round. Then reduced motion
398/// stills a lit mark, and the inverted spelling would blank it.
399///
400/// [`Moment`] tokens are not in the block.
401#[must_use]
402pub fn reduced_motion_css() -> String {
403 let mut out = String::new();
404 out.push_str("@media (prefers-reduced-motion: reduce) {\n");
405 out.push_str(" :root {\n");
406 out.push_str(" /* Motion off. The state a stroke was heading for,\n");
407 out.push_str(" reached at once; nothing is removed. */\n");
408 for motion in Motion::all() {
409 let _ = writeln!(out, " --{}: 0ms;", motion.token());
410 }
411 for cadence in Cadence::all() {
412 let _ = writeln!(out, " --{}: 0ms;", cadence.token());
413 }
414 out.push_str(" }\n}\n");
415 out
416}
417
418/// Emit the whole time axis: a `:root { ... }` block, then the motion-off block
419/// ([`reduced_motion_css`]).
420///
421/// None of it varies at runtime, so a web consumer bakes it in at build time.
422#[must_use]
423pub fn timing_css_vars() -> String {
424 format!(
425 ":root {{\n{}}}\n\n{}",
426 timing_css_declarations(),
427 reduced_motion_css()
428 )
429}
430
431/// The time axis as a stylesheet, inside the family's cascade layer.
432///
433/// The whole-file entry point a build script calls. The layer is
434/// `makeover_geometry::CSS_LAYER`.
435#[must_use]
436pub fn timing_css() -> String {
437 in_css_layer(&timing_css_vars())
438}
439
440#[cfg(test)]
441mod tests {
442 use super::*;
443 use makeover_geometry::CSS_LAYER;
444
445 /// Token, ms, CSS and `Duration` of every rung on every axis.
446 fn rungs() -> Vec<(&'static str, u32, String, Duration)> {
447 let mut out: Vec<_> = Moment::all()
448 .iter()
449 .map(|r| (r.token(), r.ms(), r.css(), r.duration()))
450 .collect();
451 out.extend(
452 Motion::all()
453 .iter()
454 .map(|r| (r.token(), r.ms(), r.css(), r.duration())),
455 );
456 out.extend(
457 Cadence::all()
458 .iter()
459 .map(|r| (r.token(), r.ms(), r.css(), r.duration())),
460 );
461 out
462 }
463
464 #[test]
465 fn every_intent_resolves_to_one_number_in_three_spellings() {
466 // ms, Duration and CSS are three renderings of one value, so a rung
467 // cannot drift between the browser and egui.
468 for (token, ms, css, duration) in rungs() {
469 assert_eq!(duration.as_millis() as u32, ms, "{token}");
470 assert_eq!(css, format!("{ms}ms"), "{token}");
471 }
472 }
473
474 #[test]
475 fn the_measured_values_are_the_ones_the_tree_had() {
476 // Changing one of these is a design decision about every consumer at
477 // once, which is the point of the crate; a test failure is the argument
478 // happening out loud.
479 assert_eq!(Moment::Revert.ms(), 1500);
480 assert_eq!(Moment::Clear.ms(), 2000);
481 assert_eq!(Moment::Dismiss.ms(), 3000);
482 assert_eq!(Moment::Debounce.ms(), 150);
483 assert_eq!(Motion::Fade.ms(), 300);
484 // The one value that is not a count. Pinned all the same: it is the
485 // number three renderers agree on, which is the whole reason it is
486 // here rather than in each of them.
487 assert_eq!(Cadence::Activity.ms(), 500);
488 }
489
490 #[test]
491 fn a_notice_leaves_after_its_lifetime_and_its_fade() {
492 // The two numbers the toast class needs, and the reason they are on
493 // different axes: a renderer that removes the node at Dismiss drops
494 // the animation, and one that waits Dismiss + Fade is correct.
495 assert_eq!(
496 notice_lifetime(Persistence::Transient),
497 Some(Duration::from_secs(3))
498 );
499 assert!(Motion::Fade.duration() < Moment::Dismiss.duration());
500 }
501
502 #[test]
503 fn a_notice_that_is_not_transient_has_no_lifetime() {
504 // Not "the caller decides": a banner is dismissed by fixing the cause.
505 assert_eq!(notice_lifetime(Persistence::Standing), None);
506 }
507
508 #[test]
509 fn debounce_is_the_shortest_wait_and_a_notice_the_longest() {
510 // The ordering is the sanity check on the set: input settling is the
511 // one wait a user is inside rather than watching, so it is the only
512 // sub-second rung, and nothing may quietly grow past a notice.
513 assert!(
514 Moment::all()
515 .iter()
516 .all(|i| i.ms() >= Moment::Debounce.ms())
517 );
518 assert!(Moment::all().iter().all(|i| i.ms() <= Moment::Dismiss.ms()));
519 }
520
521 #[test]
522 fn no_intent_is_long_enough_to_be_a_poll_interval() {
523 // A ceiling with an argument behind it: every rung here is a wait a
524 // reader sits through, and past a few seconds that stops being true.
525 // A backoff or a health check answers to what it talks to, not here.
526 assert!(Moment::all().iter().all(|i| i.ms() <= 5_000));
527 }
528
529 #[test]
530 fn the_layer_is_emitted_inside_the_family_layer() {
531 let css = timing_css();
532 assert!(css.starts_with(&format!("@layer {CSS_LAYER} {{\n")));
533 assert!(css.contains(" :root {"));
534 assert!(css.trim_end().ends_with('}'));
535 }
536
537 #[test]
538 fn every_token_reaches_the_stylesheet_exactly_once() {
539 let css = timing_css();
540 for (token, _, value, _) in rungs() {
541 let decl = format!("--{token}: {value}");
542 assert_eq!(css.matches(&decl).count(), 1, "{token}");
543 }
544 }
545
546 #[test]
547 fn tokens_are_prefixed_by_their_axis() {
548 // `--timing-*` for a state's length, `--motion-*` for a change's. A
549 // reader scanning the sheet can tell which question a var answers.
550 assert!(
551 Moment::all()
552 .iter()
553 .all(|i| i.token().starts_with("timing-"))
554 );
555 assert!(
556 Motion::all()
557 .iter()
558 .all(|m| m.token().starts_with("motion-"))
559 );
560 assert!(
561 Cadence::all()
562 .iter()
563 .all(|c| c.token().starts_with("cadence-"))
564 );
565 }
566
567 #[test]
568 fn the_blink_is_a_half_period_and_the_cycle_is_twice_it() {
569 // The one arithmetic a renderer must not be doing itself. A mark lit
570 // for 500ms and dark for 500ms is a one-second cycle, and a renderer
571 // scheduling the cycle where it meant the half draws at half speed.
572 assert_eq!(Cadence::Activity.cycle(), Cadence::Activity.duration() * 2);
573 assert_eq!(Cadence::Activity.cycle(), Duration::from_secs(1));
574 }
575
576 #[test]
577 fn the_blink_is_slower_than_a_stroke_and_faster_than_a_notice() {
578 // Where it sits between the existing axes is the sanity check on the
579 // number. Faster than Fade and it is a strobe; slower than the shortest
580 // thing a reader sits through and it reads as stalled rather than busy.
581 assert!(Cadence::Activity.duration() > Motion::Fade.duration());
582 assert!(Cadence::Activity.duration() < Moment::Revert.duration());
583 }
584
585 #[test]
586 fn reduced_motion_stills_the_mark_rather_than_removing_it() {
587 // None means lit and static, not absent. The distinction is the whole
588 // of what reduced motion asks for: less movement, not less information.
589 assert_eq!(
590 activity_blink(MotionPreference::Full),
591 Some(Cadence::Activity.duration())
592 );
593 assert_eq!(activity_blink(MotionPreference::Reduced), None);
594 }
595
596 #[test]
597 fn the_mark_is_lit_in_the_first_half_of_each_cycle_and_still_when_reduced() {
598 let half = Cadence::Activity.duration();
599 let full = MotionPreference::Full;
600 assert_eq!(blink(Duration::ZERO, full), (true, Some(half)));
601 assert!(blink(half.saturating_sub(Duration::from_nanos(1)), full).0);
602 assert_eq!(blink(half, full), (false, Some(half)));
603 assert_eq!(blink(Cadence::Activity.cycle(), full), (true, Some(half)));
604 assert!(
605 blink(Duration::MAX, full)
606 .1
607 .is_some_and(|until| until <= half)
608 );
609 for ms in [0, 250, 499, 500, 999, 12_345] {
610 assert_eq!(
611 blink(Duration::from_millis(ms), MotionPreference::Reduced),
612 (true, None)
613 );
614 }
615 }
616
617 #[test]
618 fn a_quick_answer_is_shorter_than_a_fade_and_stills_under_reduced_motion() {
619 assert!(Motion::Quick.duration() < Motion::Fade.duration());
620 assert!(reduced_motion_css().contains("--motion-quick: 0ms;"));
621 }
622
623 #[test]
624 fn every_motion_stills_natively_where_the_stylesheet_zeroes_it() {
625 // The native seam and the cascade agree: what the reduced-motion
626 // block sets to 0ms, duration_for answers as zero.
627 let css = reduced_motion_css();
628 for &motion in Motion::all() {
629 assert_eq!(
630 motion.duration_for(MotionPreference::Full),
631 motion.duration()
632 );
633 assert_eq!(
634 motion.duration_for(MotionPreference::Reduced),
635 Duration::ZERO
636 );
637 assert!(
638 css.contains(&format!("--{}: 0ms;", motion.token())),
639 "{motion:?}"
640 );
641 }
642 }
643
644 #[test]
645 fn reduced_motion_leaves_the_waits_alone() {
646 // Only what animates is zeroed. A debounce is a wait, and a reader
647 // asking for less motion has not asked their input to settle sooner.
648 let css = reduced_motion_css();
649 for moment in Moment::all() {
650 assert!(!css.contains(moment.token()), "{}", moment.token());
651 }
652 for motion in Motion::all() {
653 assert!(css.contains(&format!("--{}: 0ms;", motion.token())));
654 }
655 for cadence in Cadence::all() {
656 assert!(css.contains(&format!("--{}: 0ms;", cadence.token())));
657 }
658 }
659
660 #[test]
661 fn the_motion_off_block_ships_with_the_values_it_overrides() {
662 // A consumer taking the vars and not the block animates at every rung
663 // for a reader who asked it not to, and does it silently. So the
664 // whole-file entry point carries both, inside the family layer.
665 let css = timing_css();
666 assert!(css.contains("@media (prefers-reduced-motion: reduce)"));
667 let vars = css
668 .find(&format!(
669 "--{}: {}",
670 Cadence::Activity.token(),
671 Cadence::Activity.css()
672 ))
673 .expect("the value");
674 let off = css.find("prefers-reduced-motion").expect("the block");
675 assert!(
676 vars < off,
677 "the override has to come after what it overrides"
678 );
679 }
680
681 #[test]
682 fn no_two_rungs_share_a_name_or_a_value() {
683 // A duplicate name silently overwrites in the cascade; a duplicate
684 // value is two names for one thing, which is a distinction nobody can
685 // choose between.
686 let tokens: Vec<&str> = rungs().into_iter().map(|r| r.0).collect();
687 let mut sorted = tokens.clone();
688 sorted.sort_unstable();
689 sorted.dedup();
690 assert_eq!(sorted.len(), tokens.len(), "{tokens:?}");
691
692 let mut values: Vec<u32> = Moment::all().iter().map(|i| i.ms()).collect();
693 values.sort_unstable();
694 values.dedup();
695 assert_eq!(values.len(), Moment::all().len(), "two intents, one number");
696 }
697}