Skip to main content

fc_ui/capabilities/effects/
animated_switch.rs

1use gpui::*;
2use std::time::Duration;
3
4use crate::capabilities::motion::animations::easings;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub enum AnimatedSwitchTransition {
8    #[default]
9    Fade,
10    SlideLeft,
11    SlideRight,
12    SlideUp,
13    SlideDown,
14}
15
16#[derive(IntoElement)]
17pub struct AnimatedSwitch {
18    id: ElementId,
19    active: usize,
20    children: Vec<(usize, AnyElement)>,
21    previous: Option<(usize, AnyElement)>,
22    transition: AnimatedSwitchTransition,
23    duration: Duration,
24}
25
26impl AnimatedSwitch {
27    pub fn new(id: impl Into<ElementId>) -> Self {
28        Self {
29            id: id.into(),
30            active: 0,
31            children: Vec::new(),
32            previous: None,
33            transition: AnimatedSwitchTransition::default(),
34            duration: Duration::from_millis(300),
35        }
36    }
37
38    pub fn active(mut self, key: usize) -> Self {
39        self.active = key;
40        self
41    }
42
43    pub fn transition(mut self, transition: AnimatedSwitchTransition) -> Self {
44        self.transition = transition;
45        self
46    }
47
48    pub fn duration(mut self, duration: Duration) -> Self {
49        self.duration = duration;
50        self
51    }
52
53    pub fn child(mut self, key: usize, content: impl IntoElement) -> Self {
54        self.children.push((key, content.into_any_element()));
55        self
56    }
57
58    pub fn previous(mut self, key: usize, content: impl IntoElement) -> Self {
59        self.previous = Some((key, content.into_any_element()));
60        self
61    }
62}
63
64impl RenderOnce for AnimatedSwitch {
65    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
66        let active_child = self
67            .children
68            .into_iter()
69            .find(|(key, _)| *key == self.active);
70
71        let has_previous = self.previous.is_some();
72        let transition = self.transition;
73        let duration = self.duration;
74        let id = self.id;
75
76        let mut container = div().relative().size_full().overflow_hidden();
77
78        if let Some((prev_key, prev_content)) = self.previous {
79            let exit_id = ElementId::Name(format!("{}-exit-{}", id, prev_key).into());
80
81            container = container.child(
82                div()
83                    .absolute()
84                    .inset_0()
85                    .child(prev_content)
86                    .with_animation(
87                        exit_id,
88                        Animation::new(duration).with_easing(easings::ease_in_cubic),
89                        move |el, delta| apply_exit_transform(el, delta, transition),
90                    ),
91            );
92        }
93
94        if let Some((active_key, active_content)) = active_child {
95            if has_previous {
96                let enter_id = ElementId::Name(format!("{}-enter-{}", id, active_key).into());
97
98                container =
99                    container.child(div().size_full().child(active_content).with_animation(
100                        enter_id,
101                        Animation::new(duration).with_easing(easings::ease_out_cubic),
102                        move |el, delta| apply_enter_transform(el, delta, transition),
103                    ));
104            } else {
105                container = container.child(div().size_full().child(active_content));
106            }
107        }
108
109        container
110    }
111}
112
113fn apply_exit_transform(el: Div, delta: f32, transition: AnimatedSwitchTransition) -> Div {
114    let slide_distance = 100.0;
115    match transition {
116        AnimatedSwitchTransition::Fade => el.opacity(1.0 - delta),
117        AnimatedSwitchTransition::SlideLeft => {
118            el.opacity(1.0 - delta).left(px(-slide_distance * delta))
119        }
120        AnimatedSwitchTransition::SlideRight => {
121            el.opacity(1.0 - delta).left(px(slide_distance * delta))
122        }
123        AnimatedSwitchTransition::SlideUp => {
124            el.opacity(1.0 - delta).top(px(-slide_distance * delta))
125        }
126        AnimatedSwitchTransition::SlideDown => {
127            el.opacity(1.0 - delta).top(px(slide_distance * delta))
128        }
129    }
130}
131
132fn apply_enter_transform(el: Div, delta: f32, transition: AnimatedSwitchTransition) -> Div {
133    let slide_distance = 100.0;
134    let inverse = 1.0 - delta;
135    match transition {
136        AnimatedSwitchTransition::Fade => el.opacity(delta),
137        AnimatedSwitchTransition::SlideLeft => el.opacity(delta).left(px(slide_distance * inverse)),
138        AnimatedSwitchTransition::SlideRight => {
139            el.opacity(delta).left(px(-slide_distance * inverse))
140        }
141        AnimatedSwitchTransition::SlideUp => el.opacity(delta).top(px(slide_distance * inverse)),
142        AnimatedSwitchTransition::SlideDown => el.opacity(delta).top(px(-slide_distance * inverse)),
143    }
144}