1use crate::*;
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum FabElevation {
6 #[default]
7 Standard,
8 Lowered,
9 BottomAppBar,
10 FloatingToolbar,
12}
13
14#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
16pub enum FabColor {
17 #[default]
18 Primary,
19 Secondary,
20 Tertiary,
21 Surface,
22}
23
24#[derive(Clone, Copy, Debug)]
25pub struct FabOptions {
26 pub size: f32,
27 pub extended: bool,
28 pub enabled: bool,
29 pub rtl: bool,
30 pub elevation: FabElevation,
31 pub color: FabColor,
32 pub corner_radius: Option<f32>,
34}
35impl Default for FabOptions {
36 fn default() -> Self {
37 Self {
38 size: 56.0,
39 extended: false,
40 enabled: true,
41 rtl: false,
42 elevation: FabElevation::Standard,
43 color: FabColor::Primary,
44 corner_radius: None,
45 }
46 }
47}
48
49impl Frame<'_> {
50 pub fn fab(
51 &mut self,
52 id: u64,
53 label: &str,
54 glyph: &str,
55 size: f32,
56 extended: bool,
57 enabled: bool,
58 ) -> Response {
59 self.fab_impl(
60 id,
61 label,
62 glyph,
63 None,
64 FabOptions {
65 size,
66 extended,
67 enabled,
68 ..FabOptions::default()
69 },
70 )
71 }
72 pub fn fab_with_icon(
74 &mut self,
75 id: u64,
76 label: &str,
77 icon: AppBarIcon,
78 size: f32,
79 extended: bool,
80 enabled: bool,
81 ) -> Response {
82 self.fab_with_icon_direction(id, label, icon, size, extended, enabled, false)
83 }
84 #[allow(clippy::too_many_arguments)]
86 pub fn fab_with_icon_direction(
87 &mut self,
88 id: u64,
89 label: &str,
90 icon: AppBarIcon,
91 size: f32,
92 extended: bool,
93 enabled: bool,
94 rtl: bool,
95 ) -> Response {
96 self.fab_with_options(
97 id,
98 label,
99 icon,
100 FabOptions {
101 size,
102 extended,
103 enabled,
104 rtl,
105 ..FabOptions::default()
106 },
107 )
108 }
109 pub fn fab_with_options(
110 &mut self,
111 id: u64,
112 label: &str,
113 icon: AppBarIcon,
114 options: FabOptions,
115 ) -> Response {
116 self.fab_impl(id, label, "", Some(icon), options)
117 }
118 #[allow(clippy::too_many_arguments)]
119 fn fab_impl(
120 &mut self,
121 id: u64,
122 label: &str,
123 glyph: &str,
124 icon: Option<AppBarIcon>,
125 options: FabOptions,
126 ) -> Response {
127 let FabOptions {
128 size,
129 extended,
130 enabled,
131 rtl,
132 elevation,
133 color,
134 corner_radius,
135 } = options;
136 let icon_size = if size >= 96.0 { 36.0 } else if size >= 80.0 { 28.0 } else { 24.0 };
137 let (leading,gap,trailing,label_style)=if size>=96.0 {(28.0,16.0,28.0,TextStyle{size:24.0,weight:400,..TextStyle::default()})}else if size>=80.0 {(26.0,12.0,26.0,TextStyle{size:22.0,weight:400,..TextStyle::default()})}else{(16.0,12.0,20.0,TextStyle::LABEL)};
138 let target=f32::from(extended);
139 let expansion=if icon.is_some(){self.animate_geometry(id,97,target,target).clamp(0.0,1.0)}else{target};
140 let label_opacity=if icon.is_some(){self.ui.motion.effect(id,98,target,target,crate::motion::Spring::FAST_EFFECTS).clamp(0.0,1.0)}else{target};
141 let mut b = self.allocate(size);
142 let collapsed=b.width.min(size);
143 if icon.is_some() {
144 let expanded=b.width.min((leading + icon_size + gap + self.text_width(label, label_style) + trailing).max(size));
145 b.width=collapsed+(expanded-collapsed)*expansion;
146 } else if !extended {b.width=collapsed;}
147 let default_radius = if size <= 40.0 {12.0} else if size <= 56.0 {16.0} else if size < 96.0 {20.0} else {28.0};
148 let radius=corner_radius.filter(|radius|radius.is_finite()).unwrap_or(default_radius).clamp(0.0,b.width.min(b.height).max(0.0)/2.0);
149 let r = self.interact(id, b, enabled);
150 let t = self.ui.theme;
151 let hover_elevation = self
154 .effect(
155 id,
156 96,
157 f32::from(
158 r.enabled && r.hovered && !r.pressed && !(r.focused && self.ui.keyboard_focus),
159 ),
160 0.0,
161 )
162 .clamp(0.0, 1.0);
163 if r.enabled && elevation == FabElevation::FloatingToolbar {
164 self.elevation_dp(b,radius,3.0+3.0*hover_elevation);
165 } else if r.enabled && elevation != FabElevation::BottomAppBar {
166 let shadows = if elevation == FabElevation::Lowered {
167 [
168 (
169 2.0 + hover_elevation,
170 1.0 + 2.0 * hover_elevation,
171 -1.0 - hover_elevation,
172 0.2,
173 ),
174 (
175 1.0 + 2.0 * hover_elevation,
176 1.0 + 3.0 * hover_elevation,
177 0.0,
178 0.14,
179 ),
180 (1.0, 3.0 + 5.0 * hover_elevation, 0.0, 0.12),
181 ]
182 } else {
183 [
184 (
185 3.0 + 2.0 * hover_elevation,
186 5.0,
187 -1.0 - 2.0 * hover_elevation,
188 0.2,
189 ),
190 (6.0 + 2.0 * hover_elevation, 10.0, hover_elevation, 0.14),
191 (
192 1.0 + 2.0 * hover_elevation,
193 18.0 - 4.0 * hover_elevation,
194 2.0 * hover_elevation,
195 0.12,
196 ),
197 ]
198 };
199 for (y, blur, spread, alpha) in shadows {
200 self.shadow(
201 b,
202 radius,
203 [0.0, y],
204 blur,
205 spread,
206 Color(0, 0, 0, 255).opacity(alpha),
207 );
208 }
209 }
210 let (container, on_container) = match color {
211 FabColor::Primary => (t.primary_container, t.on_primary_container),
212 FabColor::Secondary => (t.secondary_container, t.on_secondary_container),
213 FabColor::Tertiary => (t.tertiary_container, t.on_tertiary_container),
214 FabColor::Surface => (t.surface_high, t.primary),
215 };
216 self.rect(
217 b,
218 radius,
219 if r.enabled {
220 container
221 } else {
222 t.text.opacity(0.12)
223 },
224 );
225 self.state_layer(r, radius, on_container);
226 let foreground = if r.enabled {
227 on_container
228 } else {
229 t.text.opacity(0.38)
230 };
231 if let Some(icon) = icon {
232 self.commands
233 .push(DrawCommand::PushClip(b.intersect(self.clip)));
234 let drawn_icon_size=icon_size.min(b.width).min(b.height).max(0.0);
235 let icon_bounds = Rect {
236 x: {let center=b.x+(b.width-drawn_icon_size)/2.0;let inset=leading.min(((b.width-drawn_icon_size)/2.0).max(0.0));let leading=if rtl{b.x+b.width-inset-drawn_icon_size}else{b.x+inset};center+(leading-center)*expansion},
237 y: b.y + (b.height - drawn_icon_size) / 2.0,
238 width: drawn_icon_size,
239 height: drawn_icon_size,
240 };
241 self.commands.push(DrawCommand::Shape {
242 bounds: icon_bounds,
243 index: icon.shape_index(rtl),
244 color: foreground,
245 rotation: 0.0,
246 });
247 if label_opacity > 0.0 {
248 self.with_opacity(label_opacity, |this| this.styled_text(
249 Rect {
250 x: if rtl {
251 b.x + trailing
252 } else {
253 b.x + leading + icon_size + gap
254 },
255 width: (b.width - leading - gap - trailing - icon_size).max(0.0),
256 ..b
257 },
258 label,
259 foreground,
260 TextStyle {
261 align: if rtl { 2 } else { 0 },
262 padding: 0.0,
263 ..label_style
264 },
265 ));
266 }
267 self.commands.push(DrawCommand::PopClip);
268 } else {
269 self.styled_text(
270 b,
271 if extended { label } else { glyph },
272 if r.enabled {
273 t.on_primary_container
274 } else {
275 t.text.opacity(0.38)
276 },
277 if extended {
278 TextStyle::LABEL
279 } else {
280 TextStyle {
281 size: 13.333_333,
282 align: 1,
283 ..TextStyle::default()
284 }
285 },
286 );
287 }
288 self.focus_ring(r);
289 self.semantic(r, Role::Button, label, false, None, None, None);
290 r
291 }
292
293 pub fn fab_at(
295 &mut self,
296 id: u64,
297 bounds: Rect,
298 icon: MaterialIcon,
299 label: &str,
300 options: FabOptions,
301 ) -> Response {
302 let t = self.ui.theme;
303 let radius = options
304 .corner_radius
305 .filter(|r| r.is_finite())
306 .unwrap_or_else(|| {
307 let sz = bounds.width.min(bounds.height);
308 if sz <= 40.0 { 12.0 } else if sz <= 56.0 { 16.0 } else if sz < 96.0 { 20.0 } else { 28.0 }
309 })
310 .clamp(0.0, bounds.width.min(bounds.height).max(0.0) * 0.5);
311 let r = self.interact(id, bounds, options.enabled);
312 if r.enabled && options.elevation != FabElevation::BottomAppBar {
313 let level = match options.elevation {
314 FabElevation::Lowered => 1,
315 FabElevation::FloatingToolbar => 2,
316 _ => 6,
317 };
318 self.elevation(bounds, radius, level);
319 }
320 let (container, on_container) = match options.color {
321 FabColor::Primary => (t.primary_container, t.on_primary_container),
322 FabColor::Secondary => (t.secondary_container, t.on_secondary_container),
323 FabColor::Tertiary => (t.tertiary_container, t.on_tertiary_container),
324 FabColor::Surface => (t.surface_high, t.primary),
325 };
326 let fg = if r.enabled { on_container } else { t.text.opacity(0.38) };
327 self.rect(bounds, radius, if r.enabled { container } else { t.text.opacity(0.12) });
328 self.state_layer(r, radius, on_container);
329 let icon_sz = if bounds.height >= 96.0 { 36.0 } else if bounds.height >= 80.0 { 28.0 } else { 24.0 };
330 if options.extended && !label.is_empty() {
331 let lead = 16.0;
332 let icon_rect = Rect {
333 x: if options.rtl { bounds.x + bounds.width - lead - icon_sz } else { bounds.x + lead },
334 y: bounds.y + (bounds.height - icon_sz) * 0.5,
335 width: icon_sz,
336 height: icon_sz,
337 };
338 self.paint_material_icon(icon_rect, icon, fg);
339 let text_rect = Rect {
340 x: if options.rtl { bounds.x + 16.0 } else { bounds.x + lead + icon_sz + 12.0 },
341 width: (bounds.width - lead - icon_sz - 28.0).max(0.0),
342 ..bounds
343 };
344 self.styled_text(text_rect, label, fg, TextStyle { align: if options.rtl { 2 } else { 0 }, padding: 0.0, ..TextStyle::LABEL });
345 } else {
346 let icon_rect = Rect {
347 x: bounds.x + (bounds.width - icon_sz) * 0.5,
348 y: bounds.y + (bounds.height - icon_sz) * 0.5,
349 width: icon_sz,
350 height: icon_sz,
351 };
352 self.paint_material_icon(icon_rect, icon, fg);
353 }
354 self.focus_ring(r);
355 self.semantic(r, Role::Button, label, false, None, None, None);
356 r
357 }
358
359 pub fn fab_at_animated(
362 &mut self,
363 id: u64,
364 anchor_bounds: Rect,
365 icon: MaterialIcon,
366 label: &str,
367 options: FabOptions,
368 ) -> Response {
369 let size = options.size.max(40.0).min(anchor_bounds.height.max(40.0));
370 let icon_sz = if size >= 96.0 { 36.0 } else if size >= 80.0 { 28.0 } else { 24.0 };
371 let target = f32::from(options.extended && !label.is_empty());
372 let expansion = self.animate_geometry(id, 97, target, target).clamp(0.0, 1.0);
373 let label_alpha = self
374 .ui
375 .motion
376 .effect(id, 98, target, target, crate::motion::Spring::FAST_EFFECTS)
377 .clamp(0.0, 1.0);
378 let expanded_w = (16.0 + icon_sz + 12.0 + self.text_width(label, TextStyle::LABEL) + 20.0)
379 .max(size)
380 .min(anchor_bounds.width.max(size));
381 let width = size + (expanded_w - size) * expansion;
382 let bounds = Rect {
383 x: if options.rtl {
384 anchor_bounds.x
385 } else {
386 anchor_bounds.x + (anchor_bounds.width - width).max(0.0)
387 },
388 y: anchor_bounds.y + (anchor_bounds.height - size).max(0.0) * 0.5,
389 width,
390 height: size,
391 };
392 let t = self.ui.theme;
393 let default_r = if size <= 40.0 { 12.0 } else if size <= 56.0 { 16.0 } else if size < 96.0 { 20.0 } else { 28.0 };
394 let radius = options
395 .corner_radius
396 .filter(|r| r.is_finite())
397 .unwrap_or(default_r)
398 .clamp(0.0, bounds.width.min(bounds.height).max(0.0) * 0.5);
399 let r = self.interact(id, bounds, options.enabled);
400 if r.enabled && options.elevation != FabElevation::BottomAppBar {
401 let level = match options.elevation {
402 FabElevation::Lowered => 1,
403 FabElevation::FloatingToolbar => 2,
404 _ => 6,
405 };
406 self.elevation(bounds, radius, level);
407 }
408 let (container, on_container) = match options.color {
409 FabColor::Primary => (t.primary_container, t.on_primary_container),
410 FabColor::Secondary => (t.secondary_container, t.on_secondary_container),
411 FabColor::Tertiary => (t.tertiary_container, t.on_tertiary_container),
412 FabColor::Surface => (t.surface_high, t.primary),
413 };
414 let fg = if r.enabled { on_container } else { t.text.opacity(0.38) };
415 self.rect(bounds, radius, if r.enabled { container } else { t.text.opacity(0.12) });
416 self.state_layer(r, radius, on_container);
417 self.within(bounds, |f| {
418 let center_x = bounds.x + (bounds.width - icon_sz) * 0.5;
419 let inset_x = if options.rtl { bounds.x + bounds.width - 16.0 - icon_sz } else { bounds.x + 16.0 };
420 let icon_rect = Rect {
421 x: center_x + (inset_x - center_x) * expansion,
422 y: bounds.y + (bounds.height - icon_sz) * 0.5,
423 width: icon_sz,
424 height: icon_sz,
425 };
426 f.paint_material_icon(icon_rect, icon, fg);
427 if label_alpha > 0.01 && !label.is_empty() {
428 let text_rect = Rect {
429 x: if options.rtl { bounds.x + 20.0 } else { bounds.x + 16.0 + icon_sz + 12.0 },
430 width: (bounds.width - 16.0 - icon_sz - 32.0).max(0.0),
431 ..bounds
432 };
433 f.with_opacity(label_alpha, |f| {
434 f.styled_text(
435 text_rect,
436 label,
437 fg,
438 TextStyle { align: if options.rtl { 2 } else { 0 }, padding: 0.0, ..TextStyle::LABEL },
439 );
440 });
441 }
442 });
443 self.focus_ring(r);
444 self.semantic(r, Role::Button, label, false, None, None, None);
445 r
446 }
447}
448
449#[cfg(test)]
450#[path = "fab.spec.rs"]
451mod tests;