gpui_material_components/fab/
standard.rs1use gpui::{
2 AnyElement, App, ElementId, Empty, IntoElement, ParentElement, Pixels, RenderOnce,
3 SharedString, Styled, Window, anchored, deferred, div, point, px,
4};
5use gpui_material_foundation::{ThemeScope, current_material_theme, logical_anchor};
6use gpui_material_theme::Direction;
7
8use crate::action::icon_slot;
9
10use super::{
11 fab_builders,
12 types::{FabAppearance, FabContent, FabSize},
13};
14
15#[derive(IntoElement)]
18pub struct Fab {
19 content: FabContent,
20 icon: AnyElement,
21 appearance: FabAppearance,
22}
23
24impl Fab {
25 pub fn new(
27 id: impl Into<ElementId>,
28 name: impl Into<SharedString>,
29 icon: impl IntoElement,
30 ) -> Self {
31 Self {
32 content: FabContent::new(id, name),
33 icon: icon.into_any_element(),
34 appearance: FabAppearance::default(),
35 }
36 }
37
38 fab_builders!();
39}
40
41impl RenderOnce for Fab {
42 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
43 let style = self.content.style();
44 let (_, icon_size, _, _, _, _) = self.content.size.metrics();
45 let icon_size = if self.content.size == FabSize::Large {
46 36.
47 } else {
48 icon_size
49 };
50 let theme = current_material_theme(window, cx);
51 let direction = theme.layout.direction;
52 let extent = theme.layout.density.interactive_size(style.height);
53 let motion = self.appearance.motion(&self.content.id, extent, window, cx);
54 let action = self
55 .content
56 .action(icon_slot(self.icon, icon_size * motion.scale));
57 if self.appearance.visible.is_none() {
58 return self.appearance.control(action, 1., true, window, cx);
59 }
60 let interactive = self.appearance.interactive(&action, motion, window, cx);
61 if motion.alpha == 0. {
62 return Empty.into_any_element();
63 }
64 let extent = px(extent);
65 let mut footprint = div().relative().size(extent).min_w(extent).min_h(extent);
66 if motion.scale > 0. {
67 let pivot = self.appearance.animation.alignment.pivot(direction);
68 let distance = extent * (1. - motion.scale);
69 let control = self
70 .appearance
71 .control(action, motion.scale, interactive, window, cx);
72 footprint = footprint.child(
73 div()
74 .absolute()
75 .left(distance * pivot.x)
76 .top(distance * pivot.y)
77 .opacity(motion.alpha)
78 .child(control),
79 );
80 }
81 footprint.into_any_element()
82 }
83}
84
85#[derive(IntoElement)]
88pub struct FloatingAction {
89 action: AnyElement,
90 margin: Pixels,
91}
92
93impl FloatingAction {
94 pub fn new(action: impl IntoElement) -> Self {
96 Self {
97 action: action.into_any_element(),
98 margin: px(16.),
99 }
100 }
101
102 pub fn margin(mut self, margin: Pixels) -> Self {
104 self.margin = margin.max(px(0.));
105 self
106 }
107}
108
109impl RenderOnce for FloatingAction {
110 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
111 let viewport = window.viewport_size();
112 let direction = current_material_theme(window, cx).layout.direction;
113 let margin = self.margin.min(viewport.width.min(viewport.height) / 4.);
114 let x = if direction == Direction::Rtl {
115 margin
116 } else {
117 viewport.width - margin
118 };
119 deferred(
120 anchored()
121 .anchor(logical_anchor(gpui::Anchor::BottomRight, direction))
122 .position(point(x, viewport.height - margin))
123 .snap_to_window_with_margin(margin)
124 .child(ThemeScope::inherit(
125 div()
126 .max_w((viewport.width - margin * 2.).max(px(0.)))
127 .child(self.action),
128 window,
129 cx,
130 )),
131 )
132 }
133}