fc_ui/capabilities/effects/
expandable_card.rs1use gpui::{prelude::FluentBuilder as _, *};
4use std::time::Duration;
5
6use crate::capabilities::foundation::theme::use_theme;
7use crate::capabilities::motion::animations::{durations, easings};
8
9pub struct ExpandableCardState {
10 is_expanded: bool,
11 is_animating: bool,
12 is_expanding: bool,
13 animation_version: usize,
14 duration: Duration,
15}
16
17impl ExpandableCardState {
18 pub fn new() -> Self {
19 Self {
20 is_expanded: false,
21 is_animating: false,
22 is_expanding: false,
23 animation_version: 0,
24 duration: durations::NORMAL,
25 }
26 }
27
28 pub fn is_expanded(&self) -> bool {
29 self.is_expanded
30 }
31
32 pub fn set_duration(&mut self, duration: Duration) {
33 self.duration = duration;
34 }
35
36 pub fn toggle(&mut self, cx: &mut Context<Self>) {
37 if self.is_expanded {
38 self.collapse(cx);
39 } else {
40 self.expand(cx);
41 }
42 }
43
44 pub fn expand(&mut self, cx: &mut Context<Self>) {
45 if !self.is_expanded && !self.is_animating {
46 self.is_expanded = true;
47 self.is_expanding = true;
48 self.is_animating = true;
49 self.animation_version = self.animation_version.wrapping_add(1);
50 cx.notify();
51
52 let duration = self.duration;
53 cx.spawn(async move |this, cx| {
54 cx.background_executor().timer(duration).await;
55 _ = this.update(cx, |state, cx| {
56 state.is_animating = false;
57 state.is_expanding = false;
58 cx.notify();
59 });
60 })
61 .detach();
62 }
63 }
64
65 pub fn collapse(&mut self, cx: &mut Context<Self>) {
66 if self.is_expanded && !self.is_animating {
67 self.is_expanding = false;
68 self.is_animating = true;
69 self.animation_version = self.animation_version.wrapping_add(1);
70 cx.notify();
71
72 let duration = self.duration;
73 cx.spawn(async move |this, cx| {
74 cx.background_executor().timer(duration).await;
75 _ = this.update(cx, |state, cx| {
76 state.is_expanded = false;
77 state.is_animating = false;
78 cx.notify();
79 });
80 })
81 .detach();
82 }
83 }
84}
85
86impl Default for ExpandableCardState {
87 fn default() -> Self {
88 Self::new()
89 }
90}
91
92#[derive(IntoElement)]
93pub struct ExpandableCard {
94 id: ElementId,
95 state: Entity<ExpandableCardState>,
96 collapsed_content: Option<AnyElement>,
97 expanded_content: Option<AnyElement>,
98 duration: Duration,
99 style: StyleRefinement,
100}
101
102impl ExpandableCard {
103 pub fn new(id: impl Into<ElementId>, state: Entity<ExpandableCardState>) -> Self {
104 Self {
105 id: id.into(),
106 state,
107 collapsed_content: None,
108 expanded_content: None,
109 duration: durations::NORMAL,
110 style: StyleRefinement::default(),
111 }
112 }
113
114 pub fn collapsed(mut self, content: impl IntoElement) -> Self {
115 self.collapsed_content = Some(content.into_any_element());
116 self
117 }
118
119 pub fn expanded(mut self, content: impl IntoElement) -> Self {
120 self.expanded_content = Some(content.into_any_element());
121 self
122 }
123
124 pub fn duration(mut self, duration: Duration) -> Self {
125 self.duration = duration;
126 self
127 }
128}
129
130impl Styled for ExpandableCard {
131 fn style(&mut self) -> &mut StyleRefinement {
132 &mut self.style
133 }
134}
135
136impl RenderOnce for ExpandableCard {
137 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
138 let theme = use_theme(cx);
139 let user_style = self.style;
140 self.state.update(cx, |s, _| s.set_duration(self.duration));
141 let state = self.state.read(cx);
142 let is_expanded = state.is_expanded;
143 let is_animating = state.is_animating;
144 let is_expanding = state.is_expanding;
145 let animation_version = state.animation_version;
146 let duration = self.duration;
147 let id = self.id.clone();
148 let state_for_click = self.state.clone();
149
150 let shadow = BoxShadow {
151 color: hsla(0.0, 0.0, 0.0, 0.08),
152 offset: point(px(0.0), px(1.0)),
153 blur_radius: px(3.0),
154 spread_radius: px(0.0),
155 inset: false,
156 };
157
158 div()
159 .id(id.clone())
160 .bg(theme.tokens.card)
161 .border_1()
162 .border_color(theme.tokens.border)
163 .rounded(theme.tokens.radius_lg)
164 .shadow(smallvec::smallvec![shadow])
165 .overflow_hidden()
166 .cursor_pointer()
167 .on_mouse_down(MouseButton::Left, move |_, _, cx| {
168 state_for_click.update(cx, |s, cx| s.toggle(cx));
169 })
170 .when(!is_expanded && !is_animating, |this| {
171 this.when_some(self.collapsed_content, |this, content| {
172 this.child(div().px(px(24.0)).py(px(16.0)).child(content))
173 })
174 })
175 .when(is_expanded || is_animating, |this| {
176 this.when_some(self.expanded_content, |this, content| {
177 this.child(
178 div()
179 .id("expandable-content")
180 .px(px(24.0))
181 .py(px(16.0))
182 .overflow_hidden()
183 .child(content)
184 .with_animation(
185 ElementId::Name(
186 format!("{}-expand-{}", id, animation_version).into(),
187 ),
188 Animation::new(duration).with_easing(easings::ease_out_cubic),
189 move |el, delta| {
190 if is_expanding {
191 el.opacity(delta)
192 } else if is_animating && !is_expanding {
193 el.opacity(1.0 - delta)
194 } else {
195 el.opacity(1.0)
196 }
197 },
198 ),
199 )
200 })
201 })
202 .map(|this| {
203 let mut el = this;
204 el.style().refine(&user_style);
205 el
206 })
207 }
208}