fc_ui/capabilities/navigation/
floating_action_button.rs1use gpui::{prelude::FluentBuilder as _, *};
4use std::rc::Rc;
5use std::time::Duration;
6
7use crate::capabilities::foundation::theme::use_theme;
8use crate::capabilities::motion::animations::{durations, easings};
9
10type FABActionHandler = Rc<dyn Fn(&mut Window, &mut App)>;
11
12struct FABAction {
13 id: SharedString,
14 icon: SharedString,
15 handler: FABActionHandler,
16}
17
18pub struct FABState {
19 is_expanded: bool,
20 animation_version: usize,
21}
22
23impl Default for FABState {
24 fn default() -> Self {
25 Self::new()
26 }
27}
28
29impl FABState {
30 pub fn new() -> Self {
31 Self {
32 is_expanded: false,
33 animation_version: 0,
34 }
35 }
36
37 pub fn is_expanded(&self) -> bool {
38 self.is_expanded
39 }
40
41 pub fn toggle(&mut self, cx: &mut Context<Self>) {
42 self.is_expanded = !self.is_expanded;
43 self.animation_version = self.animation_version.wrapping_add(1);
44 cx.notify();
45 }
46
47 pub fn expand(&mut self, cx: &mut Context<Self>) {
48 if !self.is_expanded {
49 self.toggle(cx);
50 }
51 }
52
53 pub fn collapse(&mut self, cx: &mut Context<Self>) {
54 if self.is_expanded {
55 self.toggle(cx);
56 }
57 }
58}
59
60#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
61pub enum FABSize {
62 Sm,
63 #[default]
64 Md,
65 Lg,
66}
67
68impl FABSize {
69 fn main_size(&self) -> Pixels {
70 match self {
71 Self::Sm => px(48.0),
72 Self::Md => px(56.0),
73 Self::Lg => px(64.0),
74 }
75 }
76
77 fn action_size(&self) -> Pixels {
78 match self {
79 Self::Sm => px(36.0),
80 Self::Md => px(44.0),
81 Self::Lg => px(52.0),
82 }
83 }
84
85 fn icon_size(&self) -> Pixels {
86 match self {
87 Self::Sm => px(18.0),
88 Self::Md => px(22.0),
89 Self::Lg => px(26.0),
90 }
91 }
92
93 fn action_icon_size(&self) -> Pixels {
94 match self {
95 Self::Sm => px(14.0),
96 Self::Md => px(18.0),
97 Self::Lg => px(22.0),
98 }
99 }
100}
101
102#[derive(IntoElement)]
103pub struct FloatingActionButton {
104 id: ElementId,
105 state: Entity<FABState>,
106 icon: SharedString,
107 actions: Vec<FABAction>,
108 fab_size: FABSize,
109 stagger: Duration,
110 style: StyleRefinement,
111}
112
113impl FloatingActionButton {
114 pub fn new(id: impl Into<ElementId>, state: Entity<FABState>) -> Self {
115 Self {
116 id: id.into(),
117 state,
118 icon: "+".into(),
119 actions: Vec::new(),
120 fab_size: FABSize::default(),
121 stagger: Duration::from_millis(50),
122 style: StyleRefinement::default(),
123 }
124 }
125
126 pub fn icon(mut self, icon: impl Into<SharedString>) -> Self {
127 self.icon = icon.into();
128 self
129 }
130
131 pub fn action<F>(
132 mut self,
133 id: impl Into<SharedString>,
134 icon: impl Into<SharedString>,
135 handler: F,
136 ) -> Self
137 where
138 F: Fn(&mut Window, &mut App) + 'static,
139 {
140 self.actions.push(FABAction {
141 id: id.into(),
142 icon: icon.into(),
143 handler: Rc::new(handler),
144 });
145 self
146 }
147
148 pub fn size(mut self, size: FABSize) -> Self {
149 self.fab_size = size;
150 self
151 }
152
153 pub fn stagger(mut self, stagger: Duration) -> Self {
154 self.stagger = stagger;
155 self
156 }
157}
158
159impl Styled for FloatingActionButton {
160 fn style(&mut self) -> &mut StyleRefinement {
161 &mut self.style
162 }
163}
164
165impl RenderOnce for FloatingActionButton {
166 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
167 let theme = use_theme(cx);
168 let user_style = self.style;
169 let state = self.state.read(cx);
170 let is_expanded = state.is_expanded;
171 let animation_version = state.animation_version;
172 let main_size = self.fab_size.main_size();
173 let action_size = self.fab_size.action_size();
174 let icon_size = self.fab_size.icon_size();
175 let action_icon_size = self.fab_size.action_icon_size();
176 let stagger = self.stagger;
177 let state_for_toggle = self.state.clone();
178
179 div()
180 .id(self.id)
181 .flex()
182 .flex_col_reverse()
183 .items_center()
184 .gap(px(12.0))
185 .child(
186 div()
187 .id("fab-main")
188 .flex()
189 .items_center()
190 .justify_center()
191 .size(main_size)
192 .rounded_full()
193 .bg(theme.tokens.primary)
194 .text_color(theme.tokens.primary_foreground)
195 .text_size(icon_size)
196 .font_weight(FontWeight::BOLD)
197 .cursor_pointer()
198 .shadow(smallvec::smallvec![BoxShadow {
199 color: hsla(0.0, 0.0, 0.0, 0.2),
200 offset: point(px(0.0), px(4.0)),
201 blur_radius: px(12.0),
202 spread_radius: px(0.0),
203 inset: false,
204 }])
205 .hover(|s| s.opacity(0.9))
206 .active(|s| s.opacity(0.8))
207 .on_mouse_down(MouseButton::Left, move |_, _, cx| {
208 state_for_toggle.update(cx, |s, cx| s.toggle(cx));
209 })
210 .child(self.icon.clone())
211 .with_animation(
212 ElementId::Name(format!("fab-rotate-{}", animation_version).into()),
213 Animation::new(durations::FAST).with_easing(easings::ease_out_cubic),
214 move |el, delta| {
215 if is_expanded {
216 el.opacity(0.7 + 0.3 * delta)
217 } else {
218 el.opacity(1.0)
219 }
220 },
221 ),
222 )
223 .when(is_expanded, |this| {
224 let action_count = self.actions.len();
225 this.children(
226 self.actions
227 .into_iter()
228 .enumerate()
229 .map(move |(idx, action)| {
230 let handler = action.handler.clone();
231 let delay = Duration::from_millis(
232 stagger.as_millis() as u64 * (action_count - 1 - idx) as u64,
233 );
234
235 div()
236 .id(ElementId::Name(format!("fab-action-{}", action.id).into()))
237 .flex()
238 .items_center()
239 .justify_center()
240 .size(action_size)
241 .rounded_full()
242 .bg(theme.tokens.secondary)
243 .text_color(theme.tokens.secondary_foreground)
244 .text_size(action_icon_size)
245 .cursor_pointer()
246 .shadow(smallvec::smallvec![BoxShadow {
247 color: hsla(0.0, 0.0, 0.0, 0.15),
248 offset: point(px(0.0), px(2.0)),
249 blur_radius: px(8.0),
250 spread_radius: px(0.0),
251 inset: false,
252 }])
253 .hover(|s| s.opacity(0.9))
254 .active(|s| s.opacity(0.8))
255 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
256 (handler)(window, cx);
257 })
258 .child(action.icon.clone())
259 .with_animation(
260 ElementId::Name(
261 format!("fab-action-anim-{}-{}", idx, animation_version)
262 .into(),
263 ),
264 Animation::new(durations::FAST)
265 .with_easing(easings::ease_out_cubic)
266 .with_delay(delay),
267 move |el, delta| el.opacity(delta).mt(px(-8.0 * (1.0 - delta))),
268 )
269 }),
270 )
271 })
272 .map(|this| {
273 let mut el = this;
274 el.style().refine(&user_style);
275 el
276 })
277 }
278}