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