Skip to main content

bhoo/systems/material/
transitions.rs

1use crate::*;
2
3/// Edge from which `Frame::slide` enters and exits.
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum SlideDirection {
6    #[default]
7    Up,
8    Down,
9    Left,
10    Right,
11}
12
13/// Direction/style for dual-screen `Frame::route_transition`.
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum RouteTransitionKind {
16    Push,
17    Pop,
18    ModalUp,
19    ModalDown,
20}
21
22/// Caller-owned animation state for `Frame::route_transition`.
23#[derive(Clone, Copy, Debug)]
24pub struct RouteTransitionState {
25    pub kind: RouteTransitionKind,
26    pub start: f64,
27    pub duration: f64,
28}
29
30impl RouteTransitionState {
31    pub fn start(kind: RouteTransitionKind, now: f64) -> Self {
32        let duration = match kind {
33            RouteTransitionKind::Push | RouteTransitionKind::ModalUp => 0.32,
34            RouteTransitionKind::Pop | RouteTransitionKind::ModalDown => 0.24,
35        };
36        Self { kind, start: now, duration }
37    }
38
39    pub fn progress(&self, now: f64, reduced_motion: bool) -> f32 {
40        if reduced_motion || self.duration <= 0.0 {
41            return 1.0;
42        }
43        ((now - self.start) / self.duration).clamp(0.0, 1.0) as f32
44    }
45}
46
47/// Direction of a page's entry relative to its final position.
48#[derive(Clone, Copy, Debug, Default)]
49pub enum PageDirection {
50    #[default]
51    None,
52    Left,
53    Right,
54    Top,
55    Bottom,
56}
57
58/// Caller-owned playback clock for a rotating shape.
59#[derive(Default)]
60pub struct ShapeRotationState {
61    elapsed: f64,
62    last: Option<f64>,
63    generation: u64,
64}
65impl ShapeRotationState {
66    pub fn pause(&mut self) {
67        self.last = None;
68    }
69}
70
71impl Frame<'_> {
72    /// A caller-selected page with Beer CSS's 400ms fade and directional entry.
73    pub fn page(
74        &mut self,
75        id: u64,
76        active: bool,
77        bounds: Rect,
78        direction: PageDirection,
79        build: impl FnOnce(&mut Self, Rect),
80    ) -> f32 {
81        use crate::timing::{Easing, TweenSpec};
82        let spec = TweenSpec::new(0.4, Easing::Cubic([0.25, 0.1, 0.25, 1.0]));
83        let amount = self.ui.motion.css_tween(
84            id,
85            86,
86            f32::from(active),
87            0.0,
88            spec,
89            TweenSpec::new(0.0, Easing::Linear),
90        );
91        if active && amount > 0.001 {
92            let distance = 64.0 * (1.0 - amount);
93            let offset = match direction {
94                PageDirection::None => [0.0, 0.0],
95                PageDirection::Left => [-distance, 0.0],
96                PageDirection::Right => [distance, 0.0],
97                PageDirection::Top => [0.0, -distance],
98                PageDirection::Bottom => [0.0, distance],
99            };
100            let posed = Rect {
101                x: bounds.x + offset[0],
102                y: bounds.y + offset[1],
103                ..bounds
104            };
105            self.within(posed, |f| f.with_opacity(amount, |f| build(f, posed)));
106        }
107        amount
108    }
109    /// Grow a bounded subtree using MUI's height-based timing and scale profile.
110    pub fn grow(
111        &mut self,
112        id: u64,
113        open: bool,
114        bounds: Rect,
115        build: impl FnOnce(&mut Self),
116    ) -> (f32, f32) {
117        use crate::timing::{Easing, TweenSpec};
118        let factor = bounds.height.max(0.0) / 36.0;
119        let duration = if factor == 0.0 {
120            0.0
121        } else {
122            ((4.0 + 15.0 * factor.powf(0.25) + factor / 5.0) * 10.0)
123                .round()
124                .min(3000.0) as f64
125                / 1000.0
126        };
127        let curve = Easing::Cubic([0.4, 0.0, 0.2, 1.0]);
128        let target = f32::from(open);
129        let opacity = self.ui.motion.css_tween(
130            id,
131            84,
132            target,
133            target,
134            TweenSpec::new(duration, curve),
135            TweenSpec::new(duration, curve),
136        );
137        let enter = TweenSpec::new(duration * 0.666, curve);
138        let exit = TweenSpec {
139            delay: duration * 0.333,
140            duration: duration * 0.666,
141            easing: curve,
142        };
143        let progress = self
144            .ui
145            .motion
146            .css_tween(id, 85, target, target, enter, exit);
147        let scale = 0.75 + 0.25 * progress;
148        let vertical_ratio = (0.5625 + 0.4375 * progress) / scale;
149        if opacity > 0.001 {
150            let origin = [
151                bounds.x + bounds.width / 2.0,
152                bounds.y + bounds.height / 2.0,
153            ];
154            self.within(bounds, |f| {
155                f.with_enabled(open, |f| {
156                    let input = f.input;
157                    if let Some(pointer) = &mut f.input.pointer {
158                        pointer[1] = origin[1] + (pointer[1] - origin[1]) / vertical_ratio;
159                    }
160                    let nodes = f.ui.semantics.len();
161                    f.commands.push(DrawCommand::PushScaleY {
162                        origin: origin[1],
163                        scale: vertical_ratio,
164                    });
165                    f.with_scale(origin, scale, |f| f.with_opacity(opacity, build));
166                    f.commands.push(DrawCommand::PopScaleY);
167                    for node in &mut f.ui.semantics[nodes..] {
168                        node.bounds.y = origin[1] + (node.bounds.y - origin[1]) * vertical_ratio;
169                        node.bounds.height *= vertical_ratio;
170                    }
171                    f.input = input;
172                })
173            });
174        }
175        (opacity, scale)
176    }
177    /// Slide a bounded subtree up from the lower viewport edge.
178    pub fn slide_up(
179        &mut self,
180        id: u64,
181        open: bool,
182        bounds: Rect,
183        build: impl FnOnce(&mut Self, Rect),
184    ) -> f32 {
185        self.slide(id, open, bounds, SlideDirection::Up, build)
186    }
187    /// Slide a bounded subtree in from any edge (`Up`, `Down`, `Left`, `Right`).
188    pub fn slide(
189        &mut self,
190        id: u64,
191        open: bool,
192        bounds: Rect,
193        direction: SlideDirection,
194        build: impl FnOnce(&mut Self, Rect),
195    ) -> f32 {
196        use crate::timing::{Easing, TweenSpec};
197        let target = f32::from(open);
198        let amount = self.ui.motion.css_tween(
199            id,
200            83,
201            target,
202            target,
203            TweenSpec::new(0.225, Easing::Cubic([0.0, 0.0, 0.2, 1.0])),
204            TweenSpec::new(0.195, Easing::Cubic([0.4, 0.0, 0.6, 1.0])),
205        );
206        if amount > 0.001 {
207            let viewport = self.viewport();
208            let inv = 1.0 - amount;
209            let posed = match direction {
210                SlideDirection::Up => Rect {
211                    y: bounds.y + (viewport.y + viewport.height - bounds.y) * inv,
212                    ..bounds
213                },
214                SlideDirection::Down => Rect {
215                    y: bounds.y + (viewport.y - bounds.height - bounds.y) * inv,
216                    ..bounds
217                },
218                SlideDirection::Left => Rect {
219                    x: bounds.x + (viewport.x + viewport.width - bounds.x) * inv,
220                    ..bounds
221                },
222                SlideDirection::Right => Rect {
223                    x: bounds.x + (viewport.x - bounds.width - bounds.x) * inv,
224                    ..bounds
225                },
226            };
227            self.within(posed, |f| f.with_enabled(open, |f| build(f, posed)));
228        }
229        amount
230    }
231    /// Collapse a subtree vertically using MUI's automatic height duration.
232    pub fn collapse(
233        &mut self,
234        id: u64,
235        open: bool,
236        height: f32,
237        build: impl FnOnce(&mut Self),
238    ) -> f32 {
239        use crate::timing::{Easing, TweenSpec};
240        let height = height.max(0.0);
241        let factor = height / 36.0;
242        let duration = if height == 0.0 {
243            0.0
244        } else {
245            ((4.0 + 15.0 * factor.powf(0.25) + factor / 5.0) * 10.0)
246                .round()
247                .min(3000.0) as f64
248                / 1000.0
249        };
250        let spec = TweenSpec::new(duration, Easing::Cubic([0.4, 0.0, 0.2, 1.0]));
251        let target = f32::from(open);
252        let amount = self.ui.motion.css_tween(id, 82, target, target, spec, spec);
253        let visible = self.reserve(height * amount);
254        if amount > 0.001 {
255            self.within(visible, |f| f.with_enabled(open, |f| build(f)));
256        }
257        amount
258    }
259    /// Scale a bounded subtree using MUI's default Zoom timings.
260    pub fn zoom(
261        &mut self,
262        id: u64,
263        open: bool,
264        bounds: Rect,
265        build: impl FnOnce(&mut Self),
266    ) -> f32 {
267        use crate::timing::{Easing, TweenSpec};
268        let easing = Easing::Cubic([0.4, 0.0, 0.2, 1.0]);
269        let target = f32::from(open);
270        let amount = self.ui.motion.css_tween(
271            id,
272            81,
273            target,
274            target,
275            TweenSpec::new(0.225, easing),
276            TweenSpec::new(0.195, easing),
277        );
278        if amount > 0.001 {
279            let origin = [
280                bounds.x + bounds.width / 2.0,
281                bounds.y + bounds.height / 2.0,
282            ];
283            self.within(bounds, |f| {
284                f.with_enabled(open, |f| f.with_scale(origin, amount, build))
285            });
286        }
287        amount
288    }
289    /// Fade a bounded subtree using MUI's default enter and exit timings.
290    pub fn fade(
291        &mut self,
292        id: u64,
293        open: bool,
294        bounds: Rect,
295        build: impl FnOnce(&mut Self),
296    ) -> f32 {
297        use crate::timing::{Easing, TweenSpec};
298        let easing = Easing::Cubic([0.4, 0.0, 0.2, 1.0]);
299        let target = f32::from(open);
300        let amount = self.ui.motion.css_tween(
301            id,
302            80,
303            target,
304            target,
305            TweenSpec::new(0.225, easing),
306            TweenSpec::new(0.195, easing),
307        );
308        if amount > 0.001 {
309            self.within(bounds, |f| {
310                f.with_enabled(open, |f| f.with_opacity(amount, build))
311            });
312        }
313        amount
314    }
315    /// Scale a subtree's geometry, text, clipping, semantics and pointer coordinates together.
316    pub fn with_scale(&mut self, origin: [f32; 2], scale: f32, build: impl FnOnce(&mut Self)) {
317        let scale = scale.clamp(0.001, 4.0);
318        let start = self.commands.len();
319        let nodes = self.ui.semantics.len();
320        let input = self.input;
321        if let Some(p) = self.input.pointer {
322            self.input.pointer = Some([
323                origin[0] + (p[0] - origin[0]) / scale,
324                origin[1] + (p[1] - origin[1]) / scale,
325            ]);
326        }
327        build(self);
328        self.input = input;
329        let point = |p: [f32; 2]| {
330            [
331                origin[0] + (p[0] - origin[0]) * scale,
332                origin[1] + (p[1] - origin[1]) * scale,
333            ]
334        };
335        let rect = |b: &mut Rect| {
336            let p = point([b.x, b.y]);
337            b.x = p[0];
338            b.y = p[1];
339            b.width *= scale;
340            b.height *= scale;
341        };
342        for command in &mut self.commands[start..] {
343            match command {
344                DrawCommand::Ellipse { bounds,.. } => rect(bounds),
345                DrawCommand::Outline { bounds,radius,width,.. } => { rect(bounds); *radius*=scale; *width*=scale; },
346                DrawCommand::RoundedRect { bounds, radius, .. } => {
347                    rect(bounds);
348                    *radius *= scale;
349                }
350                DrawCommand::Text { bounds, style, .. } => {
351                    rect(bounds);
352                    style.size *= scale;
353                    style.padding *= scale;
354                    style.letter_spacing *= scale;
355                }
356                DrawCommand::Image { bounds, radius, .. } => {
357                    rect(bounds);
358                    *radius *= scale;
359                }
360                DrawCommand::Shadow {
361                    bounds,
362                    radius,
363                    offset,
364                    blur,
365                    spread,
366                    ..
367                } => {
368                    rect(bounds);
369                    *radius *= scale;
370                    offset[0] *= scale;
371                    offset[1] *= scale;
372                    *blur *= scale;
373                    *spread *= scale;
374                }
375                DrawCommand::Ripple {
376                    bounds,
377                    corner_radius,
378                    center,
379                    radius,
380                    ..
381                } => {
382                    rect(bounds);
383                    *corner_radius *= scale;
384                    *center = point(*center);
385                    *radius *= scale;
386                }
387                DrawCommand::Arc { bounds, stroke, .. } => {
388                    rect(bounds);
389                    *stroke *= scale;
390                }
391                DrawCommand::Line {
392                    from, to, width, ..
393                } => {
394                    *from = point(*from);
395                    *to = point(*to);
396                    *width *= scale;
397                }
398                DrawCommand::Polyline { points, width, .. } => {
399                    for p in points {
400                        *p = point(*p);
401                    }
402                    *width *= scale;
403                }
404                DrawCommand::Loading { bounds, .. }
405                | DrawCommand::Shape { bounds, .. }
406                | DrawCommand::PushClip(bounds) => rect(bounds),
407                _ => {}
408            }
409        }
410        for node in &mut self.ui.semantics[nodes..] {
411            rect(&mut node.bounds);
412        }
413    }
414    pub fn with_opacity(&mut self, alpha: f32, build: impl FnOnce(&mut Self)) {
415        let alpha = alpha.clamp(0.0, 1.0);
416        let start = self.commands.len();
417        self.with_enabled(alpha > 0.001, build);
418        for command in &mut self.commands[start..] {
419            match command {
420                DrawCommand::RoundedRect { color, .. }
421                | DrawCommand::Ellipse { color, .. }
422                | DrawCommand::Outline { color, .. }
423                | DrawCommand::Text { color, .. }
424                | DrawCommand::Shadow { color, .. }
425                | DrawCommand::Ripple { color, .. }
426                | DrawCommand::Loading { color, .. }
427                | DrawCommand::Shape { color, .. }
428                | DrawCommand::Image { color, .. }
429                | DrawCommand::Arc { color, .. }
430                | DrawCommand::Line { color, .. }
431                | DrawCommand::Polyline { color, .. } => *color = color.opacity(alpha),
432                _ => {}
433            }
434        }
435    }
436    /// Animate Beer CSS's two wavy shapes. Other shapes remain static.
437    pub fn animated_wavy_shape(
438        &mut self,
439        bounds: Rect,
440        index: usize,
441        color: Color,
442        state: &mut ShapeRotationState,
443    ) -> bool {
444        let Some(shape) = crate::beercss_shapes::SHAPES.get(index) else {
445            return false;
446        };
447        let circle = shape.name == "wavy-circle";
448        if !circle && shape.name != "wavy" {
449            return self.paint_shape(bounds, index, color);
450        }
451        let visible = bounds.intersect(self.clip);
452        if visible.width <= 0.0 || visible.height <= 0.0 {
453            state.pause();
454            return false;
455        }
456        if state.generation != self.ui.pause_generation {
457            state.pause();
458            state.generation = self.ui.pause_generation;
459        }
460        let period = if circle { 8.0 } else { 1.0 };
461        let phase = if self.reduced_motion() {
462            state.pause();
463            0.0
464        } else {
465            if let Some(last) = state.last {
466                state.elapsed += (self.ui.time - last).max(0.0);
467            }
468            state.last = Some(self.ui.time);
469            self.ui.continuous = true;
470            (state.elapsed.rem_euclid(period) / period) as f32
471        };
472        if circle {
473            self.commands.push(DrawCommand::Shape {
474                bounds,
475                index: index as u8,
476                color,
477                rotation: -phase * std::f32::consts::TAU,
478            });
479        } else {
480            // The source SVG translates 312.5 units in a 1220-unit viewport.
481            let shift = phase * bounds.width.min(bounds.height) * (312.5 / 1220.0);
482            self.within(bounds, |f| {
483                for x in [bounds.x + shift - bounds.width, bounds.x + shift] {
484                    f.paint_shape(Rect { x, ..bounds }, index, color);
485                }
486            });
487        }
488        true
489    }
490    /// Paint a continuously rotating shape; duration is seconds per revolution.
491    pub fn rotating_shape(
492        &mut self,
493        bounds: Rect,
494        index: usize,
495        color: Color,
496        seconds: f64,
497        state: &mut ShapeRotationState,
498    ) -> bool {
499        if index >= crate::beercss_shapes::SHAPES.len() || !seconds.is_finite() || seconds <= 0.0 {
500            return false;
501        }
502        let visible = bounds.intersect(self.clip);
503        if visible.width <= 0.0 || visible.height <= 0.0 {
504            state.pause();
505            return false;
506        }
507        if state.generation != self.ui.pause_generation {
508            state.pause();
509            state.generation = self.ui.pause_generation;
510        }
511        let rotation = if self.reduced_motion() {
512            state.pause();
513            0.0
514        } else {
515            if let Some(last) = state.last {
516                state.elapsed += (self.ui.time - last).max(0.0);
517            }
518            state.last = Some(self.ui.time);
519            self.ui.continuous = true;
520            (state.elapsed.rem_euclid(seconds) / seconds * std::f64::consts::TAU) as f32
521        };
522        self.commands.push(DrawCommand::Shape {
523            bounds,
524            index: index as u8,
525            color,
526            rotation,
527        });
528        true
529    }
530    /// Reversible measured reveal: full child layout, clipped presentation and no exit input.
531    pub fn reveal(&mut self, id: u64, open: bool, height: f32, build: impl FnOnce(&mut Self)) {
532        let amount = self.spatial(id, 21, f32::from(open), 0.0);
533        let alpha = self.animate(id, 22, f32::from(open), 0.0);
534        if amount <= 0.001 {
535            return;
536        }
537        let b = self.allocate(height * amount);
538        if amount > 0.001 {
539            self.within(b, |f| {
540                f.with_enabled(open, |f| {
541                    f.with_opacity(alpha, |f| f.within(Rect { height, ..b }, build))
542                })
543            });
544        }
545    }
546    pub fn transition(
547        &mut self,
548        id: u64,
549        property: u8,
550        target: f32,
551        initial: f32,
552        duration: f64,
553        easing: crate::timing::Easing,
554    ) -> f32 {
555        let spec = crate::timing::TweenSpec::new(duration, easing);
556        self.ui
557            .motion
558            .tween(id, property, target, initial, spec, spec)
559    }
560    /// CSS transition sampling, including shortened reversals.
561    pub fn transition_css(
562        &mut self,
563        id: u64,
564        property: u8,
565        target: f32,
566        initial: f32,
567        duration: f64,
568        easing: crate::timing::Easing,
569    ) -> f32 {
570        let spec = crate::timing::TweenSpec::new(duration, easing);
571        self.ui
572            .motion
573            .css_tween(id, property, target, initial, spec, spec)
574    }
575
576    /// Dual-screen Android / Material route transition (`Push`/`Pop` Shared-Axis X parallax with
577    /// scrim & page-edge shadow, or `ModalUp`/`ModalDown` vertical slide+fade).
578    /// Returns `true` while the transition is still animating.
579    pub fn route_transition(
580        &mut self,
581        bounds: Rect,
582        state: RouteTransitionState,
583        outgoing: impl FnOnce(&mut Self),
584        incoming: impl FnOnce(&mut Self),
585    ) -> bool {
586        let raw = state.progress(self.time(), self.reduced_motion());
587        let active = raw < 1.0;
588        if active {
589            self.continuous(bounds);
590        }
591        let ease = match state.kind {
592            RouteTransitionKind::Push | RouteTransitionKind::ModalUp => {
593                crate::timing::Easing::EmphasizedDecelerate.sample(raw)
594            }
595            RouteTransitionKind::Pop | RouteTransitionKind::ModalDown => {
596                crate::timing::Easing::EmphasizedAccelerate.sample(raw)
597            }
598        };
599        let origin = [bounds.x, bounds.y];
600        let paint_edge_shadow = |f: &mut Self, edge_x: f32, strength: f32| {
601            if strength <= 0.01 {
602                return;
603            }
604            for (w, a) in [(12.0_f32, 0.04_f32), (8.0, 0.07), (4.0, 0.11), (1.5, 0.16)] {
605                let alpha = (a * strength * 255.0).round().clamp(0.0, 255.0) as u8;
606                if alpha > 0 {
607                    f.paint_rect(
608                        Rect { x: edge_x - w, y: bounds.y, width: w, height: bounds.height },
609                        0.0,
610                        Color(0, 0, 0, alpha),
611                    );
612                }
613            }
614        };
615        match state.kind {
616            RouteTransitionKind::Push => {
617                let out_dx = -bounds.width * 0.28 * ease;
618                let in_dx = bounds.width * (1.0 - ease);
619                self.within(bounds, |f| {
620                    f.without_input(true, |f| {
621                        f.push_transform(origin, [out_dx, 0.0], [1.0, 1.0], 0.0);
622                        outgoing(f);
623                        f.pop_transform();
624                    });
625                    f.paint_rect(bounds, 0.0, Color(0, 0, 0, ((0.22 * ease) * 255.0) as u8));
626                    paint_edge_shadow(f, bounds.x + in_dx, 1.0 - ease);
627                    f.push_transform(origin, [in_dx, 0.0], [1.0, 1.0], 0.0);
628                    incoming(f);
629                    f.pop_transform();
630                });
631            }
632            RouteTransitionKind::Pop => {
633                let in_dx = -bounds.width * 0.28 * (1.0 - ease);
634                let out_dx = bounds.width * ease;
635                self.within(bounds, |f| {
636                    f.push_transform(origin, [in_dx, 0.0], [1.0, 1.0], 0.0);
637                    incoming(f);
638                    f.pop_transform();
639                    f.paint_rect(bounds, 0.0, Color(0, 0, 0, ((0.22 * (1.0 - ease)) * 255.0) as u8));
640                    paint_edge_shadow(f, bounds.x + out_dx, 1.0 - ease);
641                    f.without_input(true, |f| {
642                        f.push_transform(origin, [out_dx, 0.0], [1.0, 1.0], 0.0);
643                        outgoing(f);
644                        f.pop_transform();
645                    });
646                });
647            }
648            RouteTransitionKind::ModalUp => {
649                let in_dy = bounds.height * 0.12 * (1.0 - ease);
650                self.within(bounds, |f| {
651                    f.without_input(true, outgoing);
652                    f.push_transform(origin, [0.0, in_dy], [1.0, 1.0], 0.0);
653                    f.push_layer(bounds, ease);
654                    incoming(f);
655                    f.pop_layer();
656                    f.pop_transform();
657                });
658            }
659            RouteTransitionKind::ModalDown => {
660                let out_dy = bounds.height * 0.12 * ease;
661                self.within(bounds, |f| {
662                    incoming(f);
663                    f.without_input(true, |f| {
664                        f.push_transform(origin, [0.0, out_dy], [1.0, 1.0], 0.0);
665                        f.push_layer(bounds, 1.0 - ease);
666                        outgoing(f);
667                        f.pop_layer();
668                        f.pop_transform();
669                    });
670                });
671            }
672        }
673        active
674    }
675
676    /// Predictive edge-back folding chevron indicator (`|` -> `<` / `>`) floating at edge `y`.
677    pub fn edge_back_indicator(
678        &mut self,
679        bounds: Rect,
680        from_right_edge: bool,
681        y: f32,
682        progress: f32,
683        committed: bool,
684    ) {
685        let p = progress.clamp(0.0, 1.0);
686        if p <= 0.01 {
687            return;
688        }
689        let t = self.theme();
690        let dir = if from_right_edge { -1.0 } else { 1.0 };
691        let inset = 14.0 + 18.0 * p;
692        let cx = if from_right_edge { bounds.x + bounds.width - inset } else { bounds.x + inset };
693        let cy = y.clamp(bounds.y + 40.0, bounds.y + bounds.height - 40.0);
694        let pill = Rect { x: cx - 18.0, y: cy - 22.0, width: 36.0, height: 44.0 };
695        self.elevation(pill, 18.0, 2);
696        self.paint_rect(
697            pill,
698            18.0,
699            if committed { t.primary_container } else { t.surface_high }.opacity(p),
700        );
701        let stroke = if committed { 2.4 } else { 1.8 };
702        let fg = if committed { t.on_primary_container } else { t.on_surface_variant }.opacity(p);
703        let fold = 5.0 * p;
704        self.paint_line([cx - dir * fold * 0.5, cy - 7.0], [cx + dir * fold * 0.5, cy], stroke, fg);
705        self.paint_line([cx + dir * fold * 0.5, cy], [cx - dir * fold * 0.5, cy + 7.0], stroke, fg);
706    }
707
708    /// Material circular reveal transition expanding from `origin` across `bounds`.
709    /// Calls `build(frame, reveal_radius)` inside `bounds` once `progress > 0.005`.
710    pub fn circular_reveal(
711        &mut self,
712        bounds: Rect,
713        origin: [f32; 2],
714        progress: f32,
715        backdrop: Color,
716        build: impl FnOnce(&mut Self, f32),
717    ) {
718        let p = progress.clamp(0.0, 1.0);
719        if p <= 0.005 {
720            return;
721        }
722        let max_r = bounds.width.hypot(bounds.height) * 1.04;
723        let reveal_r = 16.0 + (max_r - 16.0) * p;
724        self.within(bounds, |f| {
725            if p >= 0.995 {
726                f.paint_rect(bounds, 0.0, backdrop);
727            } else {
728                let disc = Rect {
729                    x: origin[0] - reveal_r,
730                    y: origin[1] - reveal_r,
731                    width: reveal_r * 2.0,
732                    height: reveal_r * 2.0,
733                };
734                f.paint_rect(disc, reveal_r, backdrop);
735            }
736            build(f, reveal_r);
737        });
738    }
739
740    /// Generic Material container-transform morph interpolating rectangle bounds, corner radius,
741    /// surface fill color, and cross-fading outgoing & incoming content.
742    #[allow(clippy::too_many_arguments)]
743    pub fn container_transform(
744        &mut self,
745        from: Rect,
746        to: Rect,
747        from_radius: f32,
748        to_radius: f32,
749        from_bg: Color,
750        to_bg: Color,
751        progress: f32,
752        from_content: impl FnOnce(&mut Self, Rect),
753        to_content: impl FnOnce(&mut Self, Rect),
754    ) {
755        let p = progress.clamp(0.0, 1.0);
756        let lerp = |a: f32, b: f32| a + (b - a) * p;
757        let current = Rect {
758            x: lerp(from.x, to.x),
759            y: lerp(from.y, to.y),
760            width: lerp(from.width, to.width).max(0.0),
761            height: lerp(from.height, to.height).max(0.0),
762        };
763        let radius = lerp(from_radius, to_radius).max(0.0);
764        let bg = from_bg.mix(to_bg, p);
765        self.paint_rect(current, radius, bg);
766        let out_alpha = (1.0 - p * 3.0).clamp(0.0, 1.0);
767        let in_alpha = ((p - 0.25) / 0.75).clamp(0.0, 1.0);
768        self.within(current, |f| {
769            if out_alpha > 0.01 {
770                f.with_opacity(out_alpha, |f| from_content(f, current));
771            }
772            if in_alpha > 0.01 {
773                f.with_opacity(in_alpha, |f| to_content(f, current));
774            }
775        });
776    }
777}
778
779#[cfg(test)]
780#[path = "transitions.spec.rs"]
781mod tests;