Skip to main content

fc_ui/capabilities/effects/
layout_transition.rs

1//! Layout transition - container that applies staggered entry animations to children.
2
3use gpui::{prelude::FluentBuilder as _, *};
4use std::time::Duration;
5
6use crate::capabilities::motion::animations::{durations, easings};
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
9pub enum LayoutAnimation {
10    #[default]
11    FadeUp,
12    FadeDown,
13    SlideLeft,
14    SlideRight,
15    Scale,
16}
17
18#[derive(IntoElement)]
19pub struct LayoutTransition {
20    id: ElementId,
21    children: Vec<AnyElement>,
22    duration: Duration,
23    stagger: Duration,
24    animation: LayoutAnimation,
25    version: usize,
26    style: StyleRefinement,
27}
28
29impl LayoutTransition {
30    pub fn new(id: impl Into<ElementId>) -> Self {
31        Self {
32            id: id.into(),
33            children: Vec::new(),
34            duration: durations::NORMAL,
35            stagger: Duration::from_millis(50),
36            animation: LayoutAnimation::default(),
37            version: 0,
38            style: StyleRefinement::default(),
39        }
40    }
41
42    pub fn duration(mut self, duration: Duration) -> Self {
43        self.duration = duration;
44        self
45    }
46
47    pub fn stagger(mut self, stagger: Duration) -> Self {
48        self.stagger = stagger;
49        self
50    }
51
52    pub fn animation(mut self, animation: LayoutAnimation) -> Self {
53        self.animation = animation;
54        self
55    }
56
57    pub fn version(mut self, version: usize) -> Self {
58        self.version = version;
59        self
60    }
61}
62
63impl Styled for LayoutTransition {
64    fn style(&mut self) -> &mut StyleRefinement {
65        &mut self.style
66    }
67}
68
69impl ParentElement for LayoutTransition {
70    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
71        self.children.extend(elements);
72    }
73}
74
75impl RenderOnce for LayoutTransition {
76    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
77        let user_style = self.style;
78        let duration = self.duration;
79        let stagger = self.stagger;
80        let animation = self.animation;
81        let version = self.version;
82        let id = self.id;
83
84        div()
85            .id(id.clone())
86            .children(
87                self.children
88                    .into_iter()
89                    .enumerate()
90                    .map(move |(idx, child)| {
91                        let delay = Duration::from_millis(stagger.as_millis() as u64 * idx as u64);
92
93                        div()
94                            .id(ElementId::Name(
95                                format!("{}-lt-child-{}-{}", id, idx, version).into(),
96                            ))
97                            .child(child)
98                            .with_animation(
99                                ElementId::Name(
100                                    format!("{}-lt-anim-{}-{}", id, idx, version).into(),
101                                ),
102                                Animation::new(duration)
103                                    .with_easing(easings::ease_out_cubic)
104                                    .with_delay(delay),
105                                move |el, delta| match animation {
106                                    LayoutAnimation::FadeUp => {
107                                        el.opacity(delta).mt(px(-12.0 * (1.0 - delta)))
108                                    }
109                                    LayoutAnimation::FadeDown => {
110                                        el.opacity(delta).mt(px(12.0 * (1.0 - delta)))
111                                    }
112                                    LayoutAnimation::SlideLeft => {
113                                        el.opacity(delta).ml(px(20.0 * (1.0 - delta)))
114                                    }
115                                    LayoutAnimation::SlideRight => {
116                                        el.opacity(delta).ml(px(-20.0 * (1.0 - delta)))
117                                    }
118                                    LayoutAnimation::Scale => {
119                                        let scale_val = 0.8 + 0.2 * delta;
120                                        el.opacity(delta)
121                                            .w(relative(scale_val))
122                                            .h(relative(scale_val))
123                                    }
124                                },
125                            )
126                    }),
127            )
128            .map(|this| {
129                let mut el = this;
130                el.style().refine(&user_style);
131                el
132            })
133    }
134}