Skip to main content

fc_ui/capabilities/effects/
animated_text.rs

1use crate::capabilities::motion::animations::easings;
2use gpui::{prelude::FluentBuilder as _, *};
3use std::time::Duration;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum TextAnimation {
7    FadeUp,
8    FadeDown,
9    SlideUp,
10    SlideDown,
11    Scale,
12    Wave,
13}
14
15#[derive(IntoElement)]
16pub struct AnimatedText {
17    id: ElementId,
18    text: SharedString,
19    animation: TextAnimation,
20    stagger: Duration,
21    duration: Duration,
22    text_size: Option<Pixels>,
23    font_weight: Option<FontWeight>,
24    text_color: Option<Hsla>,
25    style: StyleRefinement,
26}
27
28impl AnimatedText {
29    pub fn new(id: impl Into<ElementId>, text: impl Into<SharedString>) -> Self {
30        Self {
31            id: id.into(),
32            text: text.into(),
33            animation: TextAnimation::FadeUp,
34            stagger: Duration::from_millis(30),
35            duration: Duration::from_millis(400),
36            text_size: None,
37            font_weight: None,
38            text_color: None,
39            style: StyleRefinement::default(),
40        }
41    }
42
43    pub fn animation(mut self, animation: TextAnimation) -> Self {
44        self.animation = animation;
45        self
46    }
47
48    pub fn stagger(mut self, stagger: Duration) -> Self {
49        self.stagger = stagger;
50        self
51    }
52
53    pub fn duration(mut self, duration: Duration) -> Self {
54        self.duration = duration;
55        self
56    }
57
58    pub fn text_size(mut self, size: Pixels) -> Self {
59        self.text_size = Some(size);
60        self
61    }
62
63    pub fn font_weight(mut self, weight: FontWeight) -> Self {
64        self.font_weight = Some(weight);
65        self
66    }
67
68    pub fn text_color(mut self, color: Hsla) -> Self {
69        self.text_color = Some(color);
70        self
71    }
72}
73
74impl Styled for AnimatedText {
75    fn style(&mut self) -> &mut StyleRefinement {
76        &mut self.style
77    }
78}
79
80impl RenderOnce for AnimatedText {
81    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
82        let chars: Vec<char> = self.text.chars().collect();
83        let animation_type = self.animation;
84        let stagger = self.stagger;
85        let duration = self.duration;
86        let user_style = self.style;
87
88        let mut row = div().flex().flex_row().flex_wrap().map(|mut el| {
89            el.style().refine(&user_style);
90            el
91        });
92
93        if let Some(size) = self.text_size {
94            row = row.text_size(size);
95        }
96        if let Some(weight) = self.font_weight {
97            row = row.font_weight(weight);
98        }
99        if let Some(color) = self.text_color {
100            row = row.text_color(color);
101        }
102
103        for (i, ch) in chars.into_iter().enumerate() {
104            let s: SharedString = ch.to_string().into();
105            let char_id: ElementId = ElementId::Name(format!("{}-char-{}", self.id, i).into());
106            let delay = Duration::from_millis(stagger.as_millis() as u64 * i as u64);
107
108            let char_el = div().id(char_id.clone()).child(s);
109
110            let anim_el = char_el.with_animation(
111                char_id,
112                Animation::new(duration)
113                    .with_easing(easings::ease_out_cubic)
114                    .with_delay(delay),
115                move |el, char_t| match animation_type {
116                    TextAnimation::FadeUp => {
117                        let offset = (1.0 - char_t) * 12.0;
118                        el.opacity(char_t).mt(px(offset))
119                    }
120                    TextAnimation::FadeDown => {
121                        let offset = (1.0 - char_t) * -12.0;
122                        el.opacity(char_t).mt(px(offset))
123                    }
124                    TextAnimation::SlideUp => {
125                        let offset = (1.0 - char_t) * 20.0;
126                        el.mt(px(offset))
127                    }
128                    TextAnimation::SlideDown => {
129                        let offset = (1.0 - char_t) * -20.0;
130                        el.mt(px(offset))
131                    }
132                    TextAnimation::Scale => {
133                        let size_px = 8.0 + char_t * 8.0;
134                        el.opacity(char_t).text_size(px(size_px))
135                    }
136                    TextAnimation::Wave => {
137                        let wave_offset = (char_t * std::f32::consts::PI * 2.0 + i as f32 * 0.5)
138                            .sin()
139                            * 6.0
140                            * (1.0 - char_t);
141                        el.mt(px(wave_offset))
142                    }
143                },
144            );
145
146            row = row.child(anim_el);
147        }
148
149        row
150    }
151}