Skip to main content

repose_ui/
anim_ext.rs

1use std::cell::RefCell;
2use std::collections::hash_map::DefaultHasher;
3use std::hash::{Hash, Hasher};
4
5use crate::{Box, ViewExt, ZStack};
6use repose_core::*;
7
8use crate::anim::animate_f32_from;
9use crate::anim::animate_vec2_from;
10
11/// Vertical/horizontal alignment for expand/shrink transitions (0 = start, 1 = end).
12pub type ExpandFrom = f32;
13
14/// Describes how content enters the screen.
15#[derive(Clone, Debug)]
16pub enum EnterTransition {
17    /// Fade from alpha 0 to 1.
18    FadeIn,
19    /// Slide from the given offset (dx, dy in [`Dp`]) to position (0,0).
20    SlideIn { offset_x: Dp, offset_y: Dp },
21    /// Scale from initial to 1.0 combined with fade from 0 to 1.
22    ScaleIn { initial: f32 },
23    /// Animate layout height 0 -> full and clip (Compose `expandVertically`).
24    /// Participates in layout: siblings reflow as the height animates.
25    ExpandVertically {
26        /// Clip content to the animated bounds (Compose default: true).
27        clip: bool,
28        /// 0.0 = top, 0.5 = center, 1.0 = bottom.
29        expand_from: ExpandFrom,
30    },
31    /// Animate layout width 0 -> full and clip (Compose `expandHorizontally`).
32    ExpandHorizontally {
33        clip: bool,
34        /// 0.0 = start/left, 1.0 = end/right.
35        expand_from: ExpandFrom,
36    },
37    /// Expand both axes (Compose `expandIn`).
38    ExpandIn { clip: bool },
39    /// Multiple enter transitions applied together (Compose `+`).
40    Composite(Vec<EnterTransition>),
41}
42
43impl Default for EnterTransition {
44    fn default() -> Self {
45        Self::fade_in().and(Self::expand_vertically())
46    }
47}
48
49impl EnterTransition {
50    pub fn fade_in() -> Self {
51        Self::FadeIn
52    }
53    pub fn expand_vertically() -> Self {
54        Self::ExpandVertically {
55            clip: true,
56            expand_from: 0.0,
57        }
58    }
59    pub fn expand_horizontally() -> Self {
60        Self::ExpandHorizontally {
61            clip: true,
62            expand_from: 0.0,
63        }
64    }
65    pub fn expand_in() -> Self {
66        Self::ExpandIn { clip: true }
67    }
68    pub fn slide_in(offset_x: Dp, offset_y: Dp) -> Self {
69        Self::SlideIn { offset_x, offset_y }
70    }
71    pub fn scale_in(initial: f32) -> Self {
72        Self::ScaleIn { initial }
73    }
74    /// Compose-style `this + other`.
75    pub fn and(self, other: Self) -> Self {
76        match (self, other) {
77            (Self::Composite(mut a), Self::Composite(b)) => {
78                a.extend(b);
79                Self::Composite(a)
80            }
81            (Self::Composite(mut a), b) => {
82                a.push(b);
83                Self::Composite(a)
84            }
85            (a, Self::Composite(mut b)) => {
86                let mut v = vec![a];
87                v.append(&mut b);
88                Self::Composite(v)
89            }
90            (a, b) => Self::Composite(vec![a, b]),
91        }
92    }
93}
94
95/// Describes how content exits the screen.
96#[derive(Clone, Debug)]
97pub enum ExitTransition {
98    /// Fade from alpha 1 to 0.
99    FadeOut,
100    /// Slide from position (0,0) to the given offset (dx, dy in [`Dp`]).
101    SlideOut { offset_x: Dp, offset_y: Dp },
102    /// Scale from 1.0 to target combined with fade from 1 to 0.
103    ScaleOut { target: f32 },
104    /// Animate layout height full -> 0 and clip (Compose `shrinkVertically`).
105    ShrinkVertically {
106        clip: bool,
107        /// 0.0 = toward top, 1.0 = toward bottom.
108        shrink_towards: ExpandFrom,
109    },
110    /// Animate layout width full -> 0 and clip (Compose `shrinkHorizontally`).
111    ShrinkHorizontally {
112        clip: bool,
113        shrink_towards: ExpandFrom,
114    },
115    /// Shrink both axes (Compose `shrinkOut`).
116    ShrinkOut { clip: bool },
117    /// Multiple exit transitions applied together (Compose `+`).
118    Composite(Vec<ExitTransition>),
119}
120
121impl Default for ExitTransition {
122    fn default() -> Self {
123        Self::fade_out().and(Self::shrink_vertically())
124    }
125}
126
127impl ExitTransition {
128    pub fn fade_out() -> Self {
129        Self::FadeOut
130    }
131    pub fn shrink_vertically() -> Self {
132        Self::ShrinkVertically {
133            clip: true,
134            shrink_towards: 0.0,
135        }
136    }
137    pub fn shrink_horizontally() -> Self {
138        Self::ShrinkHorizontally {
139            clip: true,
140            shrink_towards: 0.0,
141        }
142    }
143    pub fn shrink_out() -> Self {
144        Self::ShrinkOut { clip: true }
145    }
146    pub fn slide_out(offset_x: Dp, offset_y: Dp) -> Self {
147        Self::SlideOut { offset_x, offset_y }
148    }
149    pub fn scale_out(target: f32) -> Self {
150        Self::ScaleOut { target }
151    }
152    /// Compose-style `this + other`.
153    pub fn and(self, other: Self) -> Self {
154        match (self, other) {
155            (Self::Composite(mut a), Self::Composite(b)) => {
156                a.extend(b);
157                Self::Composite(a)
158            }
159            (Self::Composite(mut a), b) => {
160                a.push(b);
161                Self::Composite(a)
162            }
163            (a, Self::Composite(mut b)) => {
164                let mut v = vec![a];
165                v.append(&mut b);
166                Self::Composite(v)
167            }
168            (a, b) => Self::Composite(vec![a, b]),
169        }
170    }
171}
172
173#[derive(Clone)]
174pub struct CrossfadeConfig {
175    pub key: String,
176    pub spec: AnimationSpec,
177}
178
179impl Default for CrossfadeConfig {
180    fn default() -> Self {
181        Self {
182            key: "crossfade".into(),
183            spec: AnimationSpec::default(),
184        }
185    }
186}
187
188/// Crossfades between two pieces of content when `target` changes.
189///
190/// When the target state changes, the old content fades out while the new
191/// content fades in, with no other transforms applied.
192pub fn Crossfade<T, F>(target: T, config: CrossfadeConfig, content: F) -> View
193where
194    T: PartialEq + Clone + 'static,
195    F: Fn(T) -> View + 'static,
196{
197    let key = config.key;
198    let spec = config.spec;
199
200    let prev = remember_with_key(format!("cf_prev:{key}"), || RefCell::new(target.clone()));
201    let old_content =
202        remember_with_key(format!("cf_old_view:{key}"), || RefCell::new(None::<View>));
203    let version = remember_with_key(format!("cf_version:{key}"), || RefCell::new(0u64));
204
205    let is_new = *prev.borrow() != target;
206    if is_new {
207        let _old_ver = *version.borrow();
208        let mut prev_view = content(prev.borrow().clone());
209        prev_view.scope_key = None;
210        prev_view.modifier.repaint_boundary = true;
211        old_content.borrow_mut().replace(prev_view);
212        prev.borrow_mut().clone_from(&target);
213        *version.borrow_mut() += 1;
214    }
215
216    let v = *version.borrow();
217    let mut new_view = content(target.clone());
218    new_view.scope_key = None;
219    new_view.modifier.repaint_boundary = true;
220
221    // Exit: versioned key ensures fresh animation state per transition.
222    let old_view = {
223        let mut oc = old_content.borrow_mut();
224        if let Some(ref ov) = *oc {
225            let exit_alpha = animate_f32_from(format!("cf_exit:{key}:v{v}"), 1.0, 0.0, spec);
226            if exit_alpha > 0.005 {
227                let mut exit_box = Box(Modifier::new()
228                    .fill_max_size()
229                    .alpha(exit_alpha)
230                    .hit_passthrough())
231                .child(ov.clone());
232                exit_box.modifier.key = Some(transition_child_key(&key, v, "cf_exit"));
233                Some(exit_box)
234            } else {
235                *oc = None;
236                None
237            }
238        } else {
239            None
240        }
241    };
242
243    // Enter: versioned key ensures fade-in starts at 0 on each transition.
244    let enter_alpha = animate_f32_from(format!("cf_enter:{key}:v{v}"), 0.0, 1.0, spec);
245    let mut enter_box = Box(Modifier::new().fill_max_size().alpha(enter_alpha)).child(new_view);
246    enter_box.modifier.key = Some(transition_child_key(&key, v, "cf_enter"));
247
248    match old_view {
249        Some(ov) => ZStack(Modifier::new().fill_max_size()).child((ov, enter_box)),
250        None => enter_box,
251    }
252}
253
254#[derive(Clone)]
255pub struct AnimatedContentConfig {
256    pub key: String,
257    pub spec: AnimationSpec,
258    pub enter: EnterTransition,
259    pub exit: ExitTransition,
260}
261
262impl Default for AnimatedContentConfig {
263    fn default() -> Self {
264        Self {
265            key: "anim_content".into(),
266            spec: AnimationSpec::default(),
267            enter: EnterTransition::FadeIn,
268            exit: ExitTransition::FadeOut,
269        }
270    }
271}
272
273/// Stable child key for tree reconciliation during animated transitions.
274fn transition_child_key(key: &str, version: u64, tag: &str) -> u64 {
275    let mut h = DefaultHasher::new();
276    key.hash(&mut h);
277    version.hash(&mut h);
278    tag.hash(&mut h);
279    h.finish()
280}
281
282/// Keep the child's fill constraints without expanding wrap-content children.
283fn flow_mod(content: &View) -> Modifier {
284    let m = &content.modifier;
285    let mut modifier = Modifier::new();
286    let explicit_size = m.size.is_some() || m.required_size.is_some();
287    if !explicit_size && m.width.is_none()
288        && let Some(frac) = m.fill_max_w.or(m.fill_max)
289    {
290        modifier = modifier.fill_max_width_frac(frac);
291    }
292    if !explicit_size && m.height.is_none()
293        && let Some(frac) = m.fill_max_h.or(m.fill_max)
294    {
295        modifier = modifier.fill_max_height_frac(frac);
296    }
297    modifier
298}
299
300/// Hit tests must pass through so a closing overlay can never steal clicks
301/// from content that appears beneath it.
302fn exit_mod_fill() -> Modifier {
303    Modifier::new().fill_max_size().hit_passthrough()
304}
305
306/// In-flow variant for exiting content in a Column/Row.
307fn exit_mod_flow(content: &View) -> Modifier {
308    flow_mod(content).hit_passthrough()
309}
310
311/// Which axis an expand/shrink transition animates.
312#[derive(Clone, Copy)]
313enum SizeAxis {
314    Vertical,
315    Horizontal,
316    Both,
317}
318
319/// Layout-true expand/shrink: the outer node reports an animated size (so
320/// siblings in the parent `Column` reflow smoothly) while the content keeps its
321/// natural measured size and is clipped to the animated bounds.
322///
323/// The natural size is measured via `on_size_changed` the first time the content
324/// is composed at full size; afterwards it is remembered (`measure_key`) and
325/// reused by both enter and exit animations.
326#[allow(clippy::too_many_arguments)]
327fn apply_size_fraction(
328    measure_key: &str,
329    anim_key: &str,
330    axis: SizeAxis,
331    from: f32,
332    to: f32,
333    clip: bool,
334    align: f32,
335    spec: AnimationSpec,
336    view: View,
337) -> View {
338    let full_w = remember_mutable_with_key(format!("{measure_key}:w"), || 0.0f32);
339    let full_h = remember_mutable_with_key(format!("{measure_key}:h"), || 0.0f32);
340
341    let have = match axis {
342        SizeAxis::Vertical => *full_h.get() > 0.5,
343        SizeAxis::Horizontal => *full_w.get() > 0.5,
344        SizeAxis::Both => *full_w.get() > 0.5 && *full_h.get() > 0.5,
345    };
346
347    let progress = if have {
348        animate_f32_from(anim_key, from, to, spec)
349    } else {
350        from
351    };
352
353    let capture = {
354        let fw = full_w.clone();
355        let fh = full_h.clone();
356        move |sz: Vec2| {
357            if sz.x > 0.5 {
358                fw.set_neq(sz.x);
359            }
360            if sz.y > 0.5 {
361                fh.set_neq(sz.y);
362            }
363        }
364    };
365
366    let settled = from < to && progress >= 0.999;
367
368    let full_w = *full_w.get();
369    let full_h = *full_h.get();
370
371    let shown_w = match axis {
372        SizeAxis::Vertical => full_w,
373        SizeAxis::Horizontal | SizeAxis::Both => full_w * progress,
374    };
375    let shown_h = match axis {
376        SizeAxis::Horizontal => full_h,
377        SizeAxis::Vertical | SizeAxis::Both => full_h * progress,
378    };
379
380    let mut outer = flow_mod(&view).flex_shrink(0.0);
381    if !have || settled {
382        // Not measured yet (first composition): lay out at natural size so
383        // `on_size_changed` can capture the true size, staying clipped. The
384        // companion fade keeps this frame invisible. When settled (enter done),
385        // release the fixed size and keep refreshing the measure while open.
386        outer = outer.on_size_changed(capture);
387    } else {
388        match axis {
389            SizeAxis::Vertical => {
390                outer = outer.height(Dp(shown_h.max(0.0)));
391            }
392            SizeAxis::Horizontal => {
393                outer = outer.width(Dp(shown_w.max(0.0)));
394            }
395            SizeAxis::Both => {
396                outer = outer.size(Dp(shown_w.max(0.0)), Dp(shown_h.max(0.0)));
397            }
398        }
399    }
400    if clip {
401        outer = outer.overflow(repose_core::Overflow::Clip);
402    }
403
404    // Inner keeps its full natural size and must not shrink into the animating
405    // window; the outer bounds clip hides the overflow. This is what makes the
406    // content get *clipped* (Compose `expandVertically`) instead of squashing.
407    let align = align.clamp(0.0, 1.0);
408    let ox = match axis {
409        SizeAxis::Vertical => 0.0,
410        SizeAxis::Horizontal | SizeAxis::Both => (shown_w - full_w) * align,
411    };
412    let oy = match axis {
413        SizeAxis::Horizontal => 0.0,
414        SizeAxis::Vertical | SizeAxis::Both => (shown_h - full_h) * align,
415    };
416    let mut inner = flow_mod(&view).flex_shrink(0.0);
417    if have && !settled {
418        match axis {
419            SizeAxis::Vertical => {
420                inner = inner.height(Dp(full_h));
421            }
422            SizeAxis::Horizontal => {
423                inner = inner.width(Dp(full_w));
424            }
425            SizeAxis::Both => {
426                inner = inner.size(Dp(full_w), Dp(full_h));
427            }
428        }
429    }
430    if ox.abs() > 0.01 || oy.abs() > 0.01 {
431        inner = inner.translate(Dp(ox).to_px().0, Dp(oy).to_px().0);
432    }
433
434    Box(outer).child(Box(inner).child(view))
435}
436
437/// Animates between different content based on the `target_state`, with
438/// configurable enter and exit transitions.
439///
440/// When the target state changes, the old content animates out using the
441/// `exit` transition while the new content animates in using the `enter`
442/// transition. During the transition both are stacked on top of each other.
443pub fn AnimatedContent<T, F>(target_state: T, content: F, config: AnimatedContentConfig) -> View
444where
445    T: PartialEq + Clone + 'static,
446    F: Fn(T) -> View + 'static,
447{
448    let key = config.key;
449    let spec = config.spec;
450    let enter = config.enter;
451    let exit = config.exit;
452
453    let prev = remember_with_key(format!("ac_prev:{key}"), || {
454        RefCell::new(target_state.clone())
455    });
456    let old_content =
457        remember_with_key(format!("ac_old_view:{key}"), || RefCell::new(None::<View>));
458    let version = remember_with_key(format!("ac_version:{key}"), || RefCell::new(0u64));
459
460    let is_new = *prev.borrow() != target_state;
461    if is_new {
462        let _old_ver = *version.borrow();
463        let mut prev_view = content(prev.borrow().clone());
464        prev_view.scope_key = None;
465        prev_view.modifier.repaint_boundary = true;
466        old_content.borrow_mut().replace(prev_view);
467        prev.borrow_mut().clone_from(&target_state);
468        *version.borrow_mut() += 1;
469    }
470
471    let v = *version.borrow();
472    let mut new_view = content(target_state.clone());
473    new_view.scope_key = None;
474    new_view.modifier.repaint_boundary = true;
475    let mut new_view = apply_enter(&key, v, &enter, &spec, new_view);
476    new_view.modifier.key = Some(transition_child_key(&key, v, "ac_enter"));
477
478    let old_view = {
479        let mut oc = old_content.borrow_mut();
480        if let Some(ref ov) = *oc {
481            // Check if exit is already done (read-only, no side effects).
482            if exit_animation_done(&key, v, &exit) {
483                *oc = None;
484                None
485            } else {
486                let mut exit_view = apply_exit(&key, v, &exit, &spec, ov.clone());
487                exit_view.modifier.key = Some(transition_child_key(&key, v, "ac_exit"));
488                Some(exit_view)
489            }
490        } else {
491            None
492        }
493    };
494
495    match old_view {
496        Some(ov) => ZStack(Modifier::new().fill_max_size()).child((ov, new_view)),
497        None => new_view,
498    }
499}
500
501fn apply_enter(
502    key: &str,
503    version: u64,
504    enter: &EnterTransition,
505    spec: &AnimationSpec,
506    view: View,
507) -> View {
508    match enter {
509        EnterTransition::FadeIn => {
510            let val = animate_f32_from(format!("{key}:v{version}:enter:fade"), 0.0, 1.0, *spec);
511            Box(Modifier::new().fill_max_size().alpha(val)).child(view)
512        }
513        EnterTransition::SlideIn { offset_x, offset_y } => {
514            let offset = animate_vec2_from(
515                format!("{key}:v{version}:enter:slide"),
516                Vec2 {
517                    x: offset_x.to_px().0,
518                    y: offset_y.to_px().0,
519                },
520                Vec2::default(),
521                *spec,
522            );
523            Box(Modifier::new().fill_max_size().translate_vec2(offset)).child(view)
524        }
525        EnterTransition::ScaleIn { initial } => {
526            let s = animate_f32_from(
527                format!("{key}:v{version}:enter:scale"),
528                *initial,
529                1.0,
530                *spec,
531            );
532            let a = animate_f32_from(format!("{key}:v{version}:enter:fade"), 0.0, 1.0, *spec);
533            Box(Modifier::new()
534                .fill_max_size()
535                .transform_origin(0.5, 0.5)
536                .scale(s)
537                .alpha(a))
538            .child(view)
539        }
540        EnterTransition::ExpandVertically { clip, expand_from } => apply_size_fraction(
541            &format!("{key}:meas"),
542            &format!("{key}:v{version}:enter:expand_v"),
543            SizeAxis::Vertical,
544            0.0,
545            1.0,
546            *clip,
547            *expand_from,
548            *spec,
549            view,
550        ),
551        EnterTransition::ExpandHorizontally { clip, expand_from } => apply_size_fraction(
552            &format!("{key}:meas"),
553            &format!("{key}:v{version}:enter:expand_h"),
554            SizeAxis::Horizontal,
555            0.0,
556            1.0,
557            *clip,
558            *expand_from,
559            *spec,
560            view,
561        ),
562        EnterTransition::ExpandIn { clip } => apply_size_fraction(
563            &format!("{key}:meas"),
564            &format!("{key}:v{version}:enter:expand_in"),
565            SizeAxis::Both,
566            0.0,
567            1.0,
568            *clip,
569            0.0,
570            *spec,
571            view,
572        ),
573        EnterTransition::Composite(transitions) => {
574            let mut v = view;
575            for t in transitions {
576                v = apply_enter_single(key, version, t, spec, v);
577            }
578            v
579        }
580    }
581}
582
583fn apply_enter_single(
584    key: &str,
585    version: u64,
586    enter: &EnterTransition,
587    spec: &AnimationSpec,
588    view: View,
589) -> View {
590    match enter {
591        EnterTransition::FadeIn => {
592            let val = animate_f32_from(format!("{key}:v{version}:enter:fade"), 0.0, 1.0, *spec);
593            Box(Modifier::new().fill_max_size().alpha(val)).child(view)
594        }
595        EnterTransition::SlideIn { offset_x, offset_y } => {
596            let offset = animate_vec2_from(
597                format!("{key}:v{version}:enter:slide"),
598                Vec2 {
599                    x: offset_x.to_px().0,
600                    y: offset_y.to_px().0,
601                },
602                Vec2::default(),
603                *spec,
604            );
605            Box(Modifier::new().fill_max_size().translate_vec2(offset)).child(view)
606        }
607        EnterTransition::ScaleIn { initial } => {
608            let s = animate_f32_from(
609                format!("{key}:v{version}:enter:scale"),
610                *initial,
611                1.0,
612                *spec,
613            );
614            Box(Modifier::new()
615                .fill_max_size()
616                .transform_origin(0.5, 0.5)
617                .scale(s))
618            .child(view)
619        }
620        EnterTransition::ExpandVertically { clip, expand_from } => apply_size_fraction(
621            &format!("{key}:meas"),
622            &format!("{key}:v{version}:enter:expand_v"),
623            SizeAxis::Vertical,
624            0.0,
625            1.0,
626            *clip,
627            *expand_from,
628            *spec,
629            view,
630        ),
631        EnterTransition::ExpandHorizontally { clip, expand_from } => apply_size_fraction(
632            &format!("{key}:meas"),
633            &format!("{key}:v{version}:enter:expand_h"),
634            SizeAxis::Horizontal,
635            0.0,
636            1.0,
637            *clip,
638            *expand_from,
639            *spec,
640            view,
641        ),
642        EnterTransition::ExpandIn { clip } => apply_size_fraction(
643            &format!("{key}:meas"),
644            &format!("{key}:v{version}:enter:expand_in"),
645            SizeAxis::Both,
646            0.0,
647            1.0,
648            *clip,
649            0.0,
650            *spec,
651            view,
652        ),
653        EnterTransition::Composite(inner) => {
654            let mut v = view;
655            for t in inner {
656                v = apply_enter_single(key, version, t, spec, v);
657            }
658            v
659        }
660    }
661}
662
663fn apply_exit(
664    key: &str,
665    version: u64,
666    exit: &ExitTransition,
667    spec: &AnimationSpec,
668    view: View,
669) -> View {
670    match exit {
671        ExitTransition::FadeOut => {
672            let val = animate_f32_from(format!("{key}:v{version}:exit:fade"), 1.0, 0.0, *spec);
673            Box(exit_mod_fill().alpha(val)).child(view)
674        }
675        ExitTransition::SlideOut { offset_x, offset_y } => {
676            let offset = animate_vec2_from(
677                format!("{key}:v{version}:exit:slide"),
678                Vec2::default(),
679                Vec2 {
680                    x: offset_x.to_px().0,
681                    y: offset_y.to_px().0,
682                },
683                *spec,
684            );
685            Box(exit_mod_fill().translate_vec2(offset)).child(view)
686        }
687        ExitTransition::ScaleOut { target } => {
688            let s = animate_f32_from(format!("{key}:v{version}:exit:scale"), 1.0, *target, *spec);
689            let a = animate_f32_from(format!("{key}:v{version}:exit:fade"), 1.0, 0.0, *spec);
690            Box(exit_mod_fill().transform_origin(0.5, 0.5).scale(s).alpha(a)).child(view)
691        }
692        ExitTransition::ShrinkVertically {
693            clip,
694            shrink_towards,
695        } => apply_size_fraction(
696            &format!("{key}:meas"),
697            &format!("{key}:v{version}:exit:expand_v"),
698            SizeAxis::Vertical,
699            1.0,
700            0.0,
701            *clip,
702            *shrink_towards,
703            *spec,
704            view,
705        ),
706        ExitTransition::ShrinkHorizontally {
707            clip,
708            shrink_towards,
709        } => apply_size_fraction(
710            &format!("{key}:meas"),
711            &format!("{key}:v{version}:exit:expand_h"),
712            SizeAxis::Horizontal,
713            1.0,
714            0.0,
715            *clip,
716            *shrink_towards,
717            *spec,
718            view,
719        ),
720        ExitTransition::ShrinkOut { clip } => apply_size_fraction(
721            &format!("{key}:meas"),
722            &format!("{key}:v{version}:exit:expand_in"),
723            SizeAxis::Both,
724            1.0,
725            0.0,
726            *clip,
727            0.0,
728            *spec,
729            view,
730        ),
731        ExitTransition::Composite(transitions) => {
732            let mut v = view;
733            for t in transitions {
734                v = apply_exit_single(key, version, t, spec, v);
735            }
736            v
737        }
738    }
739}
740
741fn apply_exit_single(
742    key: &str,
743    version: u64,
744    exit: &ExitTransition,
745    spec: &AnimationSpec,
746    view: View,
747) -> View {
748    match exit {
749        ExitTransition::FadeOut => {
750            let val = animate_f32_from(format!("{key}:v{version}:exit:fade"), 1.0, 0.0, *spec);
751            Box(exit_mod_fill().alpha(val)).child(view)
752        }
753        ExitTransition::SlideOut { offset_x, offset_y } => {
754            let offset = animate_vec2_from(
755                format!("{key}:v{version}:exit:slide"),
756                Vec2::default(),
757                Vec2 {
758                    x: offset_x.to_px().0,
759                    y: offset_y.to_px().0,
760                },
761                *spec,
762            );
763            Box(exit_mod_fill().translate_vec2(offset)).child(view)
764        }
765        ExitTransition::ScaleOut { target } => {
766            let s = animate_f32_from(format!("{key}:v{version}:exit:scale"), 1.0, *target, *spec);
767            Box(exit_mod_fill().transform_origin(0.5, 0.5).scale(s)).child(view)
768        }
769        ExitTransition::ShrinkVertically {
770            clip,
771            shrink_towards,
772        } => apply_size_fraction(
773            &format!("{key}:meas"),
774            &format!("{key}:v{version}:exit:expand_v"),
775            SizeAxis::Vertical,
776            1.0,
777            0.0,
778            *clip,
779            *shrink_towards,
780            *spec,
781            view,
782        ),
783        ExitTransition::ShrinkHorizontally {
784            clip,
785            shrink_towards,
786        } => apply_size_fraction(
787            &format!("{key}:meas"),
788            &format!("{key}:v{version}:exit:expand_h"),
789            SizeAxis::Horizontal,
790            1.0,
791            0.0,
792            *clip,
793            *shrink_towards,
794            *spec,
795            view,
796        ),
797        ExitTransition::ShrinkOut { clip } => apply_size_fraction(
798            &format!("{key}:meas"),
799            &format!("{key}:v{version}:exit:expand_in"),
800            SizeAxis::Both,
801            1.0,
802            0.0,
803            *clip,
804            0.0,
805            *spec,
806            view,
807        ),
808        ExitTransition::Composite(inner) => {
809            let mut v = view;
810            for t in inner {
811                v = apply_exit_single(key, version, t, spec, v);
812            }
813            v
814        }
815    }
816}
817
818/// In-flow enter transitions respect the wrapped view's layout constraints.
819fn apply_enter_inflow(
820    key: &str,
821    version: u64,
822    enter: &EnterTransition,
823    spec: &AnimationSpec,
824    view: View,
825) -> View {
826    match enter {
827        EnterTransition::FadeIn => {
828            let val = animate_f32_from(format!("{key}:v{version}:enter:fade"), 0.0, 1.0, *spec);
829            Box(flow_mod(&view).alpha(val)).child(view)
830        }
831        EnterTransition::SlideIn { offset_x, offset_y } => {
832            let offset = animate_vec2_from(
833                format!("{key}:v{version}:enter:slide"),
834                Vec2 {
835                    x: offset_x.to_px().0,
836                    y: offset_y.to_px().0,
837                },
838                Vec2::default(),
839                *spec,
840            );
841            Box(flow_mod(&view).translate_vec2(offset)).child(view)
842        }
843        EnterTransition::ScaleIn { initial } => {
844            let s = animate_f32_from(
845                format!("{key}:v{version}:enter:scale"),
846                *initial,
847                1.0,
848                *spec,
849            );
850            let a = animate_f32_from(format!("{key}:v{version}:enter:fade"), 0.0, 1.0, *spec);
851            Box(flow_mod(&view).transform_origin(0.5, 0.5).scale(s).alpha(a)).child(view)
852        }
853        EnterTransition::ExpandVertically { clip, expand_from } => apply_size_fraction(
854            &format!("{key}:meas"),
855            &format!("{key}:v{version}:enter:expand_v"),
856            SizeAxis::Vertical,
857            0.0,
858            1.0,
859            *clip,
860            *expand_from,
861            *spec,
862            view,
863        ),
864        EnterTransition::ExpandHorizontally { clip, expand_from } => apply_size_fraction(
865            &format!("{key}:meas"),
866            &format!("{key}:v{version}:enter:expand_h"),
867            SizeAxis::Horizontal,
868            0.0,
869            1.0,
870            *clip,
871            *expand_from,
872            *spec,
873            view,
874        ),
875        EnterTransition::ExpandIn { clip } => apply_size_fraction(
876            &format!("{key}:meas"),
877            &format!("{key}:v{version}:enter:expand_in"),
878            SizeAxis::Both,
879            0.0,
880            1.0,
881            *clip,
882            0.0,
883            *spec,
884            view,
885        ),
886        EnterTransition::Composite(transitions) => {
887            let mut v = view;
888            for t in transitions {
889                v = apply_enter_inflow_single(key, version, t, spec, v);
890            }
891            v
892        }
893    }
894}
895
896fn apply_enter_inflow_single(
897    key: &str,
898    version: u64,
899    enter: &EnterTransition,
900    spec: &AnimationSpec,
901    view: View,
902) -> View {
903    match enter {
904        EnterTransition::FadeIn => {
905            let val = animate_f32_from(format!("{key}:v{version}:enter:fade"), 0.0, 1.0, *spec);
906            Box(flow_mod(&view).alpha(val)).child(view)
907        }
908        EnterTransition::SlideIn { offset_x, offset_y } => {
909            let offset = animate_vec2_from(
910                format!("{key}:v{version}:enter:slide"),
911                Vec2 {
912                    x: offset_x.to_px().0,
913                    y: offset_y.to_px().0,
914                },
915                Vec2::default(),
916                *spec,
917            );
918            Box(flow_mod(&view).translate_vec2(offset)).child(view)
919        }
920        EnterTransition::ScaleIn { initial } => {
921            let s = animate_f32_from(
922                format!("{key}:v{version}:enter:scale"),
923                *initial,
924                1.0,
925                *spec,
926            );
927            Box(flow_mod(&view).transform_origin(0.5, 0.5).scale(s)).child(view)
928        }
929        EnterTransition::ExpandVertically { clip, expand_from } => apply_size_fraction(
930            &format!("{key}:meas"),
931            &format!("{key}:v{version}:enter:expand_v"),
932            SizeAxis::Vertical,
933            0.0,
934            1.0,
935            *clip,
936            *expand_from,
937            *spec,
938            view,
939        ),
940        EnterTransition::ExpandHorizontally { clip, expand_from } => apply_size_fraction(
941            &format!("{key}:meas"),
942            &format!("{key}:v{version}:enter:expand_h"),
943            SizeAxis::Horizontal,
944            0.0,
945            1.0,
946            *clip,
947            *expand_from,
948            *spec,
949            view,
950        ),
951        EnterTransition::ExpandIn { clip } => apply_size_fraction(
952            &format!("{key}:meas"),
953            &format!("{key}:v{version}:enter:expand_in"),
954            SizeAxis::Both,
955            0.0,
956            1.0,
957            *clip,
958            0.0,
959            *spec,
960            view,
961        ),
962        EnterTransition::Composite(inner) => {
963            let mut v = view;
964            for t in inner {
965                v = apply_enter_inflow_single(key, version, t, spec, v);
966            }
967            v
968        }
969    }
970}
971
972fn apply_exit_inflow(
973    key: &str,
974    version: u64,
975    exit: &ExitTransition,
976    spec: &AnimationSpec,
977    view: View,
978) -> View {
979    match exit {
980        ExitTransition::FadeOut => {
981            let val = animate_f32_from(format!("{key}:v{version}:exit:fade"), 1.0, 0.0, *spec);
982            Box(exit_mod_flow(&view).alpha(val)).child(view)
983        }
984        ExitTransition::SlideOut { offset_x, offset_y } => {
985            let offset = animate_vec2_from(
986                format!("{key}:v{version}:exit:slide"),
987                Vec2::default(),
988                Vec2 {
989                    x: offset_x.to_px().0,
990                    y: offset_y.to_px().0,
991                },
992                *spec,
993            );
994            Box(exit_mod_flow(&view).translate_vec2(offset)).child(view)
995        }
996        ExitTransition::ScaleOut { target } => {
997            let s = animate_f32_from(format!("{key}:v{version}:exit:scale"), 1.0, *target, *spec);
998            let a = animate_f32_from(format!("{key}:v{version}:exit:fade"), 1.0, 0.0, *spec);
999            Box(exit_mod_flow(&view)
1000                .transform_origin(0.5, 0.5)
1001                .scale(s)
1002                .alpha(a))
1003            .child(view)
1004        }
1005        ExitTransition::ShrinkVertically {
1006            clip,
1007            shrink_towards,
1008        } => apply_size_fraction(
1009            &format!("{key}:meas"),
1010            &format!("{key}:v{version}:exit:expand_v"),
1011            SizeAxis::Vertical,
1012            1.0,
1013            0.0,
1014            *clip,
1015            *shrink_towards,
1016            *spec,
1017            view,
1018        ),
1019        ExitTransition::ShrinkHorizontally {
1020            clip,
1021            shrink_towards,
1022        } => apply_size_fraction(
1023            &format!("{key}:meas"),
1024            &format!("{key}:v{version}:exit:expand_h"),
1025            SizeAxis::Horizontal,
1026            1.0,
1027            0.0,
1028            *clip,
1029            *shrink_towards,
1030            *spec,
1031            view,
1032        ),
1033        ExitTransition::ShrinkOut { clip } => apply_size_fraction(
1034            &format!("{key}:meas"),
1035            &format!("{key}:v{version}:exit:expand_in"),
1036            SizeAxis::Both,
1037            1.0,
1038            0.0,
1039            *clip,
1040            0.0,
1041            *spec,
1042            view,
1043        ),
1044        ExitTransition::Composite(transitions) => {
1045            let mut v = view;
1046            for t in transitions {
1047                v = apply_exit_inflow_single(key, version, t, spec, v);
1048            }
1049            v
1050        }
1051    }
1052}
1053
1054fn apply_exit_inflow_single(
1055    key: &str,
1056    version: u64,
1057    exit: &ExitTransition,
1058    spec: &AnimationSpec,
1059    view: View,
1060) -> View {
1061    match exit {
1062        ExitTransition::FadeOut => {
1063            let val = animate_f32_from(format!("{key}:v{version}:exit:fade"), 1.0, 0.0, *spec);
1064            Box(exit_mod_flow(&view).alpha(val)).child(view)
1065        }
1066        ExitTransition::SlideOut { offset_x, offset_y } => {
1067            let offset = animate_vec2_from(
1068                format!("{key}:v{version}:exit:slide"),
1069                Vec2::default(),
1070                Vec2 {
1071                    x: offset_x.to_px().0,
1072                    y: offset_y.to_px().0,
1073                },
1074                *spec,
1075            );
1076            Box(exit_mod_flow(&view).translate_vec2(offset)).child(view)
1077        }
1078        ExitTransition::ScaleOut { target } => {
1079            let s = animate_f32_from(format!("{key}:v{version}:exit:scale"), 1.0, *target, *spec);
1080            Box(exit_mod_flow(&view).transform_origin(0.5, 0.5).scale(s)).child(view)
1081        }
1082        ExitTransition::ShrinkVertically {
1083            clip,
1084            shrink_towards,
1085        } => apply_size_fraction(
1086            &format!("{key}:meas"),
1087            &format!("{key}:v{version}:exit:expand_v"),
1088            SizeAxis::Vertical,
1089            1.0,
1090            0.0,
1091            *clip,
1092            *shrink_towards,
1093            *spec,
1094            view,
1095        ),
1096        ExitTransition::ShrinkHorizontally {
1097            clip,
1098            shrink_towards,
1099        } => apply_size_fraction(
1100            &format!("{key}:meas"),
1101            &format!("{key}:v{version}:exit:expand_h"),
1102            SizeAxis::Horizontal,
1103            1.0,
1104            0.0,
1105            *clip,
1106            *shrink_towards,
1107            *spec,
1108            view,
1109        ),
1110        ExitTransition::ShrinkOut { clip } => apply_size_fraction(
1111            &format!("{key}:meas"),
1112            &format!("{key}:v{version}:exit:expand_in"),
1113            SizeAxis::Both,
1114            1.0,
1115            0.0,
1116            *clip,
1117            0.0,
1118            *spec,
1119            view,
1120        ),
1121        ExitTransition::Composite(inner) => {
1122            let mut v = view;
1123            for t in inner {
1124                v = apply_exit_inflow_single(key, version, t, spec, v);
1125            }
1126            v
1127        }
1128    }
1129}
1130
1131/// Read the current value of an `animate_f32` animation without advancing it.
1132/// Returns `None` if the animation is still running or the entry doesn't exist.
1133fn read_anim_value(key: &str, default: f32) -> Option<f32> {
1134    let anim = remember_state_with_key::<AnimatedValue<f32>>(format!("anim:f32:{key}"), || {
1135        AnimatedValue::new(default, AnimationSpec::default())
1136    });
1137    let a = anim.borrow();
1138    if a.is_animating() {
1139        None
1140    } else {
1141        Some(*a.get())
1142    }
1143}
1144
1145fn read_anim_vec2_value(key: &str, default: Vec2) -> Option<Vec2> {
1146    let anim = remember_state_with_key::<AnimatedValue<Vec2>>(format!("anim:vec2:{key}"), || {
1147        AnimatedValue::new(default, AnimationSpec::default())
1148    });
1149    let a = anim.borrow();
1150    if a.is_animating() {
1151        None
1152    } else {
1153        Some(*a.get())
1154    }
1155}
1156
1157/// Check whether an exit animation has completed (read-only, no advancement).
1158fn exit_animation_done(key: &str, version: u64, exit: &ExitTransition) -> bool {
1159    match exit {
1160        ExitTransition::FadeOut => read_anim_value(&format!("{key}:v{version}:exit:fade"), 1.0)
1161            .map(|v| v < 0.005)
1162            .unwrap_or(false),
1163        ExitTransition::SlideOut { offset_x, offset_y } => {
1164            let target = Vec2 {
1165                x: offset_x.to_px().0,
1166                y: offset_y.to_px().0,
1167            };
1168            read_anim_vec2_value(&format!("{key}:v{version}:exit:slide"), Vec2::default())
1169                .map(|v| (v.x - target.x).abs() < 0.5 && (v.y - target.y).abs() < 0.5)
1170                .unwrap_or(false)
1171        }
1172        ExitTransition::ScaleOut { target } => {
1173            let done_scale = read_anim_value(&format!("{key}:v{version}:exit:scale"), 1.0)
1174                .map(|v| (v - target).abs() < 0.005)
1175                .unwrap_or(false);
1176            let done_fade = read_anim_value(&format!("{key}:v{version}:exit:fade"), 1.0)
1177                .map(|v| v < 0.005)
1178                .unwrap_or(false);
1179            done_scale && done_fade
1180        }
1181        ExitTransition::ShrinkVertically { .. } => {
1182            read_anim_value(&format!("{key}:v{version}:exit:expand_v"), 1.0)
1183                .map(|v| v < 0.005)
1184                .unwrap_or(false)
1185        }
1186        ExitTransition::ShrinkHorizontally { .. } => {
1187            read_anim_value(&format!("{key}:v{version}:exit:expand_h"), 1.0)
1188                .map(|v| v < 0.005)
1189                .unwrap_or(false)
1190        }
1191        ExitTransition::ShrinkOut { .. } => {
1192            read_anim_value(&format!("{key}:v{version}:exit:expand_in"), 1.0)
1193                .map(|v| v < 0.005)
1194                .unwrap_or(false)
1195        }
1196        ExitTransition::Composite(ts) => ts.iter().all(|t| exit_animation_done(key, version, t)),
1197    }
1198}
1199
1200#[derive(Clone)]
1201pub struct AnimatedVisibilityConfig {
1202    pub key: String,
1203    pub spec: AnimationSpec,
1204    pub enter: EnterTransition,
1205    pub exit: ExitTransition,
1206}
1207
1208impl Default for AnimatedVisibilityConfig {
1209    fn default() -> Self {
1210        Self {
1211            key: "anim_vis".into(),
1212            spec: AnimationSpec::default(),
1213            enter: EnterTransition::default(),
1214            exit: ExitTransition::default(),
1215        }
1216    }
1217}
1218
1219impl AnimatedVisibilityConfig {
1220    pub fn with_key(key: impl Into<String>) -> Self {
1221        Self {
1222            key: key.into(),
1223            ..Default::default()
1224        }
1225    }
1226}
1227
1228/// Shows or hides content with animated enter/exit transitions.
1229///
1230/// When `visible` becomes `true`, the content enters using the specified `enter`
1231/// transition. When it becomes `false`, the content exits using the specified `exit`
1232/// transition.
1233pub fn AnimatedVisibility(visible: bool, content: View, config: AnimatedVisibilityConfig) -> View {
1234    let key = config.key;
1235    let spec = config.spec;
1236    let enter = config.enter;
1237    let exit = config.exit;
1238
1239    let old_content = remember_with_key(format!("av_old:{key}"), || RefCell::new(None::<View>));
1240    let version = remember_with_key(format!("av_ver:{key}"), || RefCell::new(0u64));
1241    let prev = remember_with_key(format!("av_prev:{key}"), || RefCell::new(visible));
1242
1243    // Detect transition
1244    if *prev.borrow() != visible {
1245        if !visible {
1246            // Going hidden: capture current content for exit animation
1247            let mut captured = content.clone();
1248            captured.scope_key = None;
1249            captured.modifier.repaint_boundary = true;
1250            old_content.borrow_mut().replace(captured);
1251        } else {
1252            // Re-opening: drop any pending exit snapshot so enter starts clean.
1253            *old_content.borrow_mut() = None;
1254        }
1255        *version.borrow_mut() += 1;
1256        prev.borrow_mut().clone_from(&visible);
1257    }
1258
1259    let v = *version.borrow();
1260
1261    // Handle exiting old content
1262    let exiting = {
1263        let mut oc = old_content.borrow_mut();
1264        if let Some(ref old) = *oc {
1265            if exit_animation_done(&key, v, &exit) {
1266                *oc = None;
1267                None
1268            } else {
1269                let mut exit_view = apply_exit_inflow(&key, v, &exit, &spec, old.clone());
1270                exit_view.modifier.key = Some(transition_child_key(&key, v, "av_exit"));
1271                Some(exit_view)
1272            }
1273        } else {
1274            None
1275        }
1276    };
1277
1278    if visible {
1279        let mut content = content;
1280        content.scope_key = None;
1281        content.modifier.repaint_boundary = true;
1282        let mut entering = if v > 0 {
1283            apply_enter_inflow(&key, v, &enter, &spec, content)
1284        } else {
1285            content
1286        };
1287        entering.modifier.key = Some(transition_child_key(&key, v, "av_enter"));
1288        entering
1289    } else {
1290        exiting.unwrap_or_else(|| Box(Modifier::new().height(Dp::ZERO)))
1291    }
1292}