1use std::rc::Rc;
2
3use gpui::{
4 AnyElement, App, ElementId, FocusHandle, IntoElement, ParentElement, RenderOnce, SharedString,
5 Styled, Window, anchored, deferred, div, px,
6};
7use gpui_material_foundation::{animate, color, logical_anchor, window_material_theme};
8use gpui_material_theme::{
9 ColorRole, CornerRole, Direction, Elevation, MotionKind, MotionSpeed, TypeRole,
10 TypographyEmphasis,
11};
12
13use crate::action::{Action, ActionControl, ActionStyle, Activate, DisabledStyle, icon_slot};
14
15#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
17pub enum FabSize {
18 #[default]
19 Small,
20 Medium,
21 Large,
22}
23
24impl FabSize {
25 fn metrics(self) -> (f32, f32, CornerRole, f32, f32, TypeRole) {
26 match self {
28 Self::Small => (56., 24., CornerRole::Large, 16., 8., TypeRole::TitleMedium),
29 Self::Medium => (
30 80.,
31 28.,
32 CornerRole::LargeIncreased,
33 26.,
34 12.,
35 TypeRole::TitleLarge,
36 ),
37 Self::Large => (
38 96.,
39 32.,
40 CornerRole::ExtraLarge,
41 28.,
42 16.,
43 TypeRole::HeadlineSmall,
44 ),
45 }
46 }
47}
48
49#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
50pub enum FabColor {
51 #[default]
52 Primary,
53 Secondary,
54 Tertiary,
55 PrimaryStrong,
56 SecondaryStrong,
57 TertiaryStrong,
58}
59
60struct FabContent {
61 id: ElementId,
62 name: SharedString,
63 icon: AnyElement,
64 size: FabSize,
65 color: FabColor,
66 disabled: bool,
67 focus: Option<FocusHandle>,
68 on_activate: Option<Activate>,
69}
70
71impl FabContent {
72 fn new(
73 id: impl Into<ElementId>,
74 name: impl Into<SharedString>,
75 icon: impl IntoElement,
76 ) -> Self {
77 Self {
78 id: id.into(),
79 name: name.into(),
80 icon: icon.into_any_element(),
81 size: FabSize::default(),
82 color: FabColor::default(),
83 disabled: false,
84 focus: None,
85 on_activate: None,
86 }
87 }
88
89 fn style(&self) -> ActionStyle {
90 let (height, _, corner, _, _, _) = self.size.metrics();
91 let (background, foreground) = match self.color {
92 FabColor::Primary => (ColorRole::PrimaryContainer, ColorRole::OnPrimaryContainer),
93 FabColor::Secondary => (
94 ColorRole::SecondaryContainer,
95 ColorRole::OnSecondaryContainer,
96 ),
97 FabColor::Tertiary => (ColorRole::TertiaryContainer, ColorRole::OnTertiaryContainer),
98 FabColor::PrimaryStrong => (ColorRole::Primary, ColorRole::OnPrimary),
99 FabColor::SecondaryStrong => (ColorRole::Secondary, ColorRole::OnSecondary),
100 FabColor::TertiaryStrong => (ColorRole::Tertiary, ColorRole::OnTertiary),
101 };
102 ActionStyle {
103 background: Some(background),
104 background_color: None,
105 foreground,
106 disabled: DisabledStyle {
109 foreground: ColorRole::OnSurface,
110 background: Some((ColorRole::OnSurface, 0.12)),
111 outline: (ColorRole::OnSurface, 0.12),
112 },
113 height,
114 min_width: height,
115 corner,
116 pressed_corner: corner,
117 selected_corner: None,
118 corner_motion: (
119 gpui_material_theme::MotionKind::Spatial,
120 gpui_material_theme::MotionSpeed::Fast,
121 ),
122 outline: 0.,
123 elevation: Elevation::Level3,
124 hover_elevation: Elevation::Level4,
125 }
126 }
127}
128
129macro_rules! fab_builders {
130 () => {
131 pub fn size(mut self, size: FabSize) -> Self {
132 self.content.size = size;
133 self
134 }
135
136 pub fn color(mut self, color: FabColor) -> Self {
137 self.content.color = color;
138 self
139 }
140
141 pub fn disabled(mut self, disabled: bool) -> Self {
144 self.content.disabled = disabled;
145 self
146 }
147
148 pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
149 self.content.focus = Some(focus.clone());
150 self
151 }
152
153 pub fn on_activate(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
154 self.content.on_activate = Some(Rc::new(handler));
155 self
156 }
157 };
158}
159
160#[derive(IntoElement)]
163pub struct Fab {
164 content: FabContent,
165}
166
167impl Fab {
168 pub fn new(
169 id: impl Into<ElementId>,
170 name: impl Into<SharedString>,
171 icon: impl IntoElement,
172 ) -> Self {
173 Self {
174 content: FabContent::new(id, name, icon),
175 }
176 }
177
178 fab_builders!();
179}
180
181impl RenderOnce for Fab {
182 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
183 let style = self.content.style();
184 let (_, icon_size, _, _, _, _) = self.content.size.metrics();
185 let icon_size = if self.content.size == FabSize::Large {
186 36.
187 } else {
188 icon_size
189 };
190 ActionControl {
191 id: self.content.id,
192 label: self.content.name,
193 focus: self.content.focus,
194 disabled: self.content.disabled,
195 action: Action::Invoke(self.content.on_activate),
196 style,
197 content: icon_slot(self.content.icon, icon_size).into_any_element(),
198 }
199 .render(window, cx)
200 }
201}
202
203#[derive(IntoElement)]
206pub struct ExtendedFab {
207 content: FabContent,
208 expanded: bool,
209}
210
211impl ExtendedFab {
212 pub fn new(
213 id: impl Into<ElementId>,
214 label: impl Into<SharedString>,
215 icon: impl IntoElement,
216 ) -> Self {
217 Self {
218 content: FabContent::new(id, label, icon),
219 expanded: true,
220 }
221 }
222
223 fab_builders!();
224
225 pub fn expanded(mut self, expanded: bool) -> Self {
226 self.expanded = expanded;
227 self
228 }
229}
230
231impl RenderOnce for ExtendedFab {
232 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
233 let style = self.content.style();
234 let (_, icon_size, _, padding, gap, role) = self.content.size.metrics();
235 let theme = window_material_theme(window, cx);
236 let direction = theme.layout.direction;
237 let foreground = if self.content.disabled {
238 color(theme.colors.color(ColorRole::OnSurface)).opacity(0.38)
239 } else {
240 color(theme.colors.color(style.foreground))
241 };
242 let reduced = cx.reduce_motion();
243 let motion = theme
244 .motion
245 .spec(MotionKind::Spatial, MotionSpeed::Fast, reduced);
246 let effects = theme
247 .motion
248 .spec(MotionKind::Effects, MotionSpeed::Fast, reduced);
249 let progress = animate(
250 ElementId::from((self.content.id.clone(), "expansion")),
251 f32::from(self.expanded),
252 motion,
253 window,
254 cx,
255 )
256 .clamp(0., 1.);
257 let opacity = animate(
258 (self.content.id.clone(), "label-opacity"),
259 f32::from(self.expanded),
260 effects,
261 window,
262 cx,
263 )
264 .clamp(0., 1.);
265 let collapsed_padding = (style.height - icon_size) / 2.;
267 let start_padding = collapsed_padding + (padding - collapsed_padding) * progress;
268 let typography = role.style(TypographyEmphasis::Baseline);
269 let mut row = div()
270 .flex()
271 .items_center()
272 .min_w_0()
273 .max_w_full()
274 .px(px(start_padding))
275 .gap(px(gap * progress));
276 if direction == Direction::Rtl {
277 row = row.flex_row_reverse();
278 }
279 row = row.child(icon_slot(self.content.icon, icon_size));
280 if progress > 0. {
281 row = row.child(crate::text::single_line(
282 self.content.name.clone(),
283 typography,
284 1.,
285 foreground,
286 (None, progress, opacity),
287 window,
288 cx,
289 ));
290 }
291 ActionControl {
292 id: self.content.id,
293 label: self.content.name,
294 focus: self.content.focus,
295 disabled: self.content.disabled,
296 action: Action::Invoke(self.content.on_activate),
297 style,
298 content: row.into_any_element(),
299 }
300 .render(window, cx)
301 }
302}
303
304#[derive(IntoElement)]
307pub struct FloatingAction {
308 action: AnyElement,
309 margin: gpui::Pixels,
310}
311
312impl FloatingAction {
313 pub fn new(action: impl IntoElement) -> Self {
314 Self {
315 action: action.into_any_element(),
316 margin: px(16.),
317 }
318 }
319
320 pub fn margin(mut self, margin: gpui::Pixels) -> Self {
321 self.margin = margin.max(px(0.));
322 self
323 }
324}
325
326impl RenderOnce for FloatingAction {
327 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
328 let viewport = window.viewport_size();
329 let direction = window_material_theme(window, cx).layout.direction;
330 let margin = self.margin.min(viewport.width.min(viewport.height) / 4.);
331 let x = if direction == Direction::Rtl {
332 margin
333 } else {
334 viewport.width - margin
335 };
336 deferred(
337 anchored()
338 .anchor(logical_anchor(gpui::Anchor::BottomRight, direction))
339 .position(gpui::point(x, viewport.height - margin))
340 .snap_to_window_with_margin(margin)
341 .child(
342 div()
343 .max_w((viewport.width - margin * 2.).max(px(0.)))
344 .child(self.action),
345 ),
346 )
347 }
348}