1use gpui::{
2 AnyElement, App, ElementId, Entity, FocusHandle, IntoElement, Point, SharedString, Window,
3 point,
4};
5use gpui_material_foundation::{InteractionSource, OverlayMotion, animate, current_material_theme};
6use gpui_material_theme::{
7 ColorRole, CornerRole, Direction, Elevation, MotionKind, MotionSpec, MotionSpeed, TypeRole,
8};
9
10use crate::action::{
11 Action, ActionControl, ActionLayout, ActionStyle, Activate, DisabledStyle, InvokeAction,
12};
13use crate::control_style::ButtonStyle;
14
15#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
17pub enum FabSize {
18 #[default]
20 Small,
21 Medium,
23 Large,
25}
26
27impl FabSize {
28 pub(super) fn metrics(self) -> (f32, f32, CornerRole, f32, f32, TypeRole) {
29 match self {
31 Self::Small => (56., 24., CornerRole::Large, 16., 8., TypeRole::TitleMedium),
32 Self::Medium => (
33 80.,
34 28.,
35 CornerRole::LargeIncreased,
36 26.,
37 12.,
38 TypeRole::TitleLarge,
39 ),
40 Self::Large => (
41 96.,
42 32.,
43 CornerRole::ExtraLarge,
44 28.,
45 16.,
46 TypeRole::HeadlineSmall,
47 ),
48 }
49 }
50}
51
52#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
54pub enum FabColor {
55 #[default]
57 Primary,
58 Secondary,
60 Tertiary,
62 PrimaryStrong,
64 SecondaryStrong,
66 TertiaryStrong,
68}
69
70#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
72pub enum FabAlignment {
73 TopStart,
75 TopCenter,
77 TopEnd,
79 CenterStart,
81 #[default]
83 Center,
84 CenterEnd,
86 BottomStart,
88 BottomCenter,
90 BottomEnd,
92}
93
94impl FabAlignment {
95 pub(super) fn pivot(self, direction: Direction) -> Point<f32> {
96 use FabAlignment::*;
97 let x = match self {
98 TopStart | CenterStart | BottomStart => 0.,
99 TopCenter | Center | BottomCenter => 0.5,
100 TopEnd | CenterEnd | BottomEnd => 1.,
101 };
102 let y = match self {
103 TopStart | TopCenter | TopEnd => 0.,
104 CenterStart | Center | CenterEnd => 0.5,
105 BottomStart | BottomCenter | BottomEnd => 1.,
106 };
107 point(
108 if direction == Direction::Rtl {
109 1. - x
110 } else {
111 x
112 },
113 y,
114 )
115 }
116}
117
118#[derive(Clone, Copy, Debug, PartialEq)]
120pub struct FabVisibilityAnimation {
121 pub alignment: FabAlignment,
123 pub target_scale: f32,
126 pub scale_motion: Option<MotionSpec>,
128 pub alpha_motion: Option<MotionSpec>,
130}
131
132impl Default for FabVisibilityAnimation {
133 fn default() -> Self {
134 Self {
135 alignment: FabAlignment::Center,
136 target_scale: 0.2,
137 scale_motion: None,
138 alpha_motion: None,
139 }
140 }
141}
142
143impl FabVisibilityAnimation {
144 #[doc(hidden)]
146 pub fn frame(
147 self,
148 id: &ElementId,
149 visible: bool,
150 extent: f32,
151 window: &mut Window,
152 cx: &mut App,
153 ) -> (f32, f32, Point<f32>) {
154 let sample = self.sample(id, visible, extent, window, cx);
155 let direction = current_material_theme(window, cx).layout.direction;
156 (sample.scale, sample.alpha, self.alignment.pivot(direction))
157 }
158
159 pub(super) fn sample(
160 self,
161 id: &ElementId,
162 visible: bool,
163 extent: f32,
164 window: &mut Window,
165 cx: &mut App,
166 ) -> OverlayMotion {
167 let scheme = current_material_theme(window, cx).motion;
168 let reduced = cx.reduce_motion();
169 let mut progress = |channel, kind, spec: Option<MotionSpec>| {
170 let spec = if reduced {
171 MotionSpec::Instant
172 } else {
173 spec.unwrap_or_else(|| scheme.spec(kind, MotionSpeed::Fast, false))
174 };
175 animate((id.clone(), channel), f32::from(visible), spec, window, cx)
176 };
177 let spatial = progress(
178 "fab-visibility-scale",
179 MotionKind::Spatial,
180 self.scale_motion,
181 );
182 let alpha = progress(
183 "fab-visibility-alpha",
184 MotionKind::Effects,
185 self.alpha_motion,
186 );
187 let target = if self.target_scale >= 0. && (self.target_scale * extent).is_finite() {
188 self.target_scale
189 } else {
190 Self::default().target_scale
191 };
192 let scale = ((1. - spatial) * target + spatial).max(0.);
193 OverlayMotion {
194 scale: if (scale * extent).is_finite() {
195 scale
196 } else {
197 1.
198 },
199 alpha: alpha.clamp(0., 1.),
200 }
201 }
202}
203
204pub(super) struct FabContent {
205 pub(super) id: ElementId,
206 pub(super) name: SharedString,
207 pub(super) size: FabSize,
208 pub(super) color: FabColor,
209 pub(super) disabled: bool,
210 pub(super) focus: Option<FocusHandle>,
211 pub(super) on_activate: Option<Activate>,
212}
213
214impl FabContent {
215 pub(super) fn new(id: impl Into<ElementId>, name: impl Into<SharedString>) -> Self {
216 Self {
217 id: id.into(),
218 name: name.into(),
219 size: FabSize::default(),
220 color: FabColor::default(),
221 disabled: false,
222 focus: None,
223 on_activate: None,
224 }
225 }
226
227 pub(super) fn style(&self) -> ActionStyle {
228 let (height, _, corner, _, _, _) = self.size.metrics();
229 let (background, foreground) = match self.color {
230 FabColor::Primary => (ColorRole::PrimaryContainer, ColorRole::OnPrimaryContainer),
231 FabColor::Secondary => (
232 ColorRole::SecondaryContainer,
233 ColorRole::OnSecondaryContainer,
234 ),
235 FabColor::Tertiary => (ColorRole::TertiaryContainer, ColorRole::OnTertiaryContainer),
236 FabColor::PrimaryStrong => (ColorRole::Primary, ColorRole::OnPrimary),
237 FabColor::SecondaryStrong => (ColorRole::Secondary, ColorRole::OnSecondary),
238 FabColor::TertiaryStrong => (ColorRole::Tertiary, ColorRole::OnTertiary),
239 };
240 ActionStyle {
241 background: Some(background),
242 background_color: None,
243 foreground,
244 disabled: DisabledStyle {
247 foreground: ColorRole::OnSurface,
248 background: Some((ColorRole::OnSurface, 0.12)),
249 outline: (ColorRole::OnSurface, 0.12),
250 },
251 height,
252 min_width: height,
253 corner,
254 pressed_corner: corner,
255 selected_corner: None,
256 corner_motion: (MotionKind::Spatial, MotionSpeed::Fast),
257 outline: 0.,
258 outline_role: ColorRole::OutlineVariant,
259 elevation: Elevation::Level3,
260 hover_elevation: Elevation::Level4,
261 }
262 }
263
264 pub(super) fn action(self, content: impl IntoElement) -> ActionControl {
265 ActionControl {
266 style: self.style(),
267 id: self.id,
268 label: self.name,
269 focus: self.focus,
270 disabled: self.disabled,
271 action: Action::Invoke(self.on_activate.map(InvokeAction::Full)),
272 content: content.into_any_element(),
273 }
274 }
275}
276
277#[derive(Default)]
278pub(super) struct FabAppearance {
279 pub(super) style: ButtonStyle,
280 pub(super) interaction_source: Option<Entity<InteractionSource>>,
281 pub(super) visible: Option<bool>,
282 pub(super) animation: FabVisibilityAnimation,
283}
284
285impl FabAppearance {
286 pub(super) fn motion(
287 &self,
288 id: &ElementId,
289 extent: f32,
290 window: &mut Window,
291 cx: &mut App,
292 ) -> OverlayMotion {
293 self.visible.map_or(
294 OverlayMotion {
295 scale: 1.,
296 alpha: 1.,
297 },
298 |visible| self.animation.sample(id, visible, extent, window, cx),
299 )
300 }
301
302 pub(super) fn interactive(
303 &self,
304 action: &ActionControl,
305 motion: OverlayMotion,
306 window: &mut Window,
307 cx: &mut App,
308 ) -> bool {
309 let interactive = self.visible != Some(false) && motion.alpha > 0. && motion.scale > 0.;
310 if !interactive {
311 action.clear_interaction(window, cx);
312 }
313 interactive
314 }
315
316 pub(super) fn control(
317 &self,
318 action: ActionControl,
319 scale: f32,
320 interactive: bool,
321 window: &mut Window,
322 cx: &mut App,
323 ) -> AnyElement {
324 action.render_layout(
325 scale,
326 ActionLayout {
327 tab_stop: interactive,
328 decorative: !interactive,
329 appearance: self.style,
330 ..Default::default()
331 },
332 self.interaction_source.as_ref().filter(|_| interactive),
333 window,
334 cx,
335 )
336 }
337}