Skip to main content

fc_ui/capabilities/effects/
animated_presence.rs

1use gpui::*;
2use std::time::Duration;
3
4use crate::capabilities::motion::animations::{durations, easings, lerp_pixels};
5
6pub struct AnimatedPresenceState {
7    is_visible: bool,
8    is_animating_out: bool,
9    version: usize,
10    exit_duration: Duration,
11}
12
13impl AnimatedPresenceState {
14    pub fn new() -> Self {
15        Self {
16            is_visible: false,
17            is_animating_out: false,
18            version: 0,
19            exit_duration: durations::NORMAL,
20        }
21    }
22
23    pub fn set_visible(&mut self, visible: bool, cx: &mut Context<Self>) {
24        if visible {
25            if self.is_visible && !self.is_animating_out {
26                return;
27            }
28            self.is_visible = true;
29            self.is_animating_out = false;
30            self.version += 1;
31            cx.notify();
32        } else {
33            if !self.is_visible || self.is_animating_out {
34                return;
35            }
36            self.is_animating_out = true;
37            self.version += 1;
38            cx.notify();
39
40            let version = self.version;
41            let duration = self.exit_duration;
42            cx.spawn(async move |this, cx| {
43                cx.background_executor().timer(duration).await;
44                _ = this.update(cx, |state, cx| {
45                    if state.version == version {
46                        state.is_visible = false;
47                        state.is_animating_out = false;
48                        cx.notify();
49                    }
50                });
51            })
52            .detach();
53        }
54    }
55
56    pub fn is_visible(&self) -> bool {
57        self.is_visible
58    }
59
60    pub fn is_animating_out(&self) -> bool {
61        self.is_animating_out
62    }
63}
64
65impl Default for AnimatedPresenceState {
66    fn default() -> Self {
67        Self::new()
68    }
69}
70
71#[derive(IntoElement)]
72pub struct AnimatedPresence {
73    base: Div,
74    id: ElementId,
75    state: Entity<AnimatedPresenceState>,
76    show: Option<bool>,
77    enter_duration: Duration,
78    exit_duration: Duration,
79}
80
81impl AnimatedPresence {
82    pub fn new(id: impl Into<ElementId>, state: Entity<AnimatedPresenceState>) -> Self {
83        Self {
84            base: div(),
85            id: id.into(),
86            state,
87            show: None,
88            enter_duration: durations::NORMAL,
89            exit_duration: durations::FAST,
90        }
91    }
92
93    pub fn show(mut self, visible: bool) -> Self {
94        self.show = Some(visible);
95        self
96    }
97
98    pub fn enter_duration(mut self, duration: Duration) -> Self {
99        self.enter_duration = duration;
100        self
101    }
102
103    pub fn exit_duration(mut self, duration: Duration) -> Self {
104        self.exit_duration = duration;
105        self
106    }
107}
108
109impl RenderOnce for AnimatedPresence {
110    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
111        if let Some(desired) = self.show {
112            let state = self.state.read(cx);
113            let needs_show = desired && !state.is_visible;
114            let needs_hide = !desired && state.is_visible && !state.is_animating_out;
115
116            if needs_show || needs_hide {
117                let exit_dur = self.exit_duration;
118                self.state.update(cx, |state, cx| {
119                    state.exit_duration = exit_dur;
120                    state.set_visible(desired, cx);
121                });
122            }
123        }
124
125        let state = self.state.read(cx);
126        let is_visible = state.is_visible;
127        let is_animating_out = state.is_animating_out;
128        let version = state.version;
129
130        if !is_visible && !is_animating_out {
131            return div().into_any_element();
132        }
133
134        let enter_dur = self.enter_duration;
135        let exit_dur = self.exit_duration;
136        let slide_offset = px(8.0);
137
138        if is_animating_out {
139            self.base
140                .with_animation(
141                    ElementId::Name(format!("{}-exit-{}", self.id, version).into()),
142                    Animation::new(exit_dur).with_easing(easings::ease_in_cubic),
143                    move |el, delta| {
144                        el.opacity(1.0 - delta)
145                            .mt(lerp_pixels(px(0.0), slide_offset, delta))
146                    },
147                )
148                .into_any_element()
149        } else {
150            self.base
151                .with_animation(
152                    ElementId::Name(format!("{}-enter-{}", self.id, version).into()),
153                    Animation::new(enter_dur).with_easing(easings::ease_out_cubic),
154                    move |el, delta| {
155                        el.opacity(delta)
156                            .mt(lerp_pixels(slide_offset, px(0.0), delta))
157                    },
158                )
159                .into_any_element()
160        }
161    }
162}
163
164impl Styled for AnimatedPresence {
165    fn style(&mut self) -> &mut StyleRefinement {
166        self.base.style()
167    }
168}
169
170impl InteractiveElement for AnimatedPresence {
171    fn interactivity(&mut self) -> &mut Interactivity {
172        self.base.interactivity()
173    }
174}
175
176impl StatefulInteractiveElement for AnimatedPresence {}
177
178impl ParentElement for AnimatedPresence {
179    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
180        self.base.extend(elements)
181    }
182}