egui-material3 0.0.9

Material Design 3 components for egui with comprehensive theming support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
use crate::get_global_color;
use crate::icon::MaterialIcon;
use crate::material_symbol::material_symbol_text;
use egui::{self, Color32, Pos2, Rect, Response, Sense, Ui, Vec2, Widget};

/// Material Design FAB (Floating Action Button) variants
#[derive(Clone, Copy, PartialEq)]
pub enum FabVariant {
    /// Surface FAB - uses surface colors for neutral actions
    Surface,
    /// Primary FAB - uses primary colors for main actions (most common)
    Primary,
    /// Secondary FAB - uses secondary colors for secondary actions
    Secondary,
    /// Tertiary FAB - uses tertiary colors for alternative actions
    Tertiary,
    /// Branded FAB - uses custom brand colors
    Branded,
}

/// Material Design FAB sizes following Material Design 3 specifications
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum FabSize {
    /// Small FAB - 40x40dp, used in compact layouts
    Small,
    /// Regular FAB - 56x56dp, the standard size
    Regular,
    /// Large FAB - 96x96dp, used for prominent actions
    Large,
    /// Extended FAB - Variable width with text, at least 80dp wide
    Extended,
}

/// Material Design Floating Action Button (FAB) component
///
/// FABs help users take primary actions within an app. They appear in front of all screen content,
/// typically as a circular button with an icon in the center.
///
/// ## Usage Examples
/// ```rust
/// # egui::__run_test_ui(|ui| {
/// // Primary FAB with add icon
/// ui.add(MaterialFab::primary()
///     .icon("add")
///     .action(|| println!("Add clicked")));
///
/// // Extended FAB with text
/// ui.add(MaterialFab::primary()
///     .size(FabSize::Extended)
///     .icon("edit")
///     .text("Compose")
///     .action(|| println!("Compose clicked")));
///
/// // Large FAB for prominent action
/// ui.add(MaterialFab::primary()
///     .size(FabSize::Large)
///     .icon("camera")
///     .action(|| println!("Camera clicked")));
/// # });
/// ```
///
/// ## Material Design Spec
/// - Elevation: 6dp (raised above content)
/// - Corner radius: 50% (fully rounded)
/// - Sizes: Small (40dp), Regular (56dp), Large (96dp), Extended (≥80dp)
/// - Icon size: 24dp for regular, 32dp for large
/// - Placement: 16dp from screen edge, above navigation bars
pub struct MaterialFab<'a> {
    /// Color variant of the FAB
    variant: FabVariant,
    /// Size of the FAB
    size: FabSize,
    /// Material Design icon name
    icon: Option<String>,
    /// Text content (for extended FABs)
    text: Option<String>,
    /// Custom SVG icon data
    svg_icon: Option<SvgIcon>,
    /// Raw SVG data string
    svg_data: Option<String>,
    /// Whether the FAB is interactive
    enabled: bool,
    /// Action callback when FAB is pressed
    action: Option<Box<dyn Fn() + 'a>>,
}

/// SVG icon data for custom FAB icons
#[derive(Clone)]
pub struct SvgIcon {
    /// Vector of SVG paths that make up the icon
    pub paths: Vec<SvgPath>,
    /// Viewbox dimensions of the SVG
    pub viewbox_size: Vec2,
}

/// Individual SVG path with styling
#[derive(Clone)]
pub struct SvgPath {
    /// SVG path data string
    pub path: String,
    /// Fill color for the path
    pub fill: Color32,
}

impl<'a> MaterialFab<'a> {
    /// Create a new FAB with the specified variant
    ///
    /// ## Parameters
    /// - `variant`: The color variant to use for the FAB
    pub fn new(variant: FabVariant) -> Self {
        Self {
            variant,
            size: FabSize::Regular,
            icon: None,
            text: None,
            svg_icon: None,
            svg_data: None,
            enabled: true,
            action: None,
        }
    }

    /// Create a surface FAB
    pub fn surface() -> Self {
        Self::new(FabVariant::Surface)
    }

    /// Create a primary FAB
    pub fn primary() -> Self {
        Self::new(FabVariant::Primary)
    }

    /// Create a secondary FAB
    pub fn secondary() -> Self {
        Self::new(FabVariant::Secondary)
    }

    /// Create a tertiary FAB
    pub fn tertiary() -> Self {
        Self::new(FabVariant::Tertiary)
    }

    /// Create a branded FAB
    pub fn branded() -> Self {
        Self::new(FabVariant::Branded)
    }

    /// Set the size of the FAB
    pub fn size(mut self, size: FabSize) -> Self {
        self.size = size;
        self
    }

    /// Set the icon of the FAB
    pub fn icon(mut self, icon: impl Into<String>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Set the text of the FAB (for extended FABs)
    pub fn text(mut self, text: impl Into<String>) -> Self {
        self.text = Some(text.into());
        self.size = FabSize::Extended;
        self
    }

    /// Enable or disable the FAB
    pub fn enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    /// Set the lowered state of the FAB (elevation effect)
    pub fn lowered(self, _lowered: bool) -> Self {
        // Placeholder for lowered state (elevation effect)
        // In a real implementation, this would affect the visual elevation
        self
    }

    /// Set a custom SVG icon for the FAB
    pub fn svg_icon(mut self, svg_icon: SvgIcon) -> Self {
        self.svg_icon = Some(svg_icon);
        self
    }

    /// Set raw SVG data for the FAB icon
    pub fn svg_data(mut self, svg_data: impl Into<String>) -> Self {
        self.svg_data = Some(svg_data.into());
        self
    }

    /// Set the action to perform when the FAB is clicked
    pub fn on_click<F>(mut self, f: F) -> Self
    where
        F: Fn() + 'a,
    {
        self.action = Some(Box::new(f));
        self
    }
}

impl<'a> Widget for MaterialFab<'a> {
    fn ui(self, ui: &mut Ui) -> Response {
        let size = match self.size {
            FabSize::Small => Vec2::splat(40.0),
            FabSize::Regular => Vec2::splat(56.0),
            FabSize::Large => Vec2::splat(96.0),
            FabSize::Extended => {
                let left_margin = 16.0;
                let right_margin = 24.0;
                let icon_width = if self.icon.is_some() || self.svg_icon.is_some() || self.svg_data.is_some() {
                    24.0 + 12.0
                } else {
                    0.0
                };

                let text_width = if let Some(ref text) = self.text {
                    let font_id = egui::FontId::proportional(14.0);
                    ui.painter().layout_no_wrap(text.clone(), font_id, Color32::WHITE)
                        .size()
                        .x
                } else {
                    0.0
                };

                let total_width = left_margin + icon_width + text_width + right_margin;
                Vec2::new(total_width.max(80.0), 56.0) // Minimum width of 80px
            }
        };

        let (rect, response) = ui.allocate_exact_size(size, Sense::click());

        // Extract all needed data before partial move
        let action = self.action;
        let enabled = self.enabled;
        let variant = self.variant;
        let size_enum = self.size;
        let icon = self.icon;
        let text = self.text;
        let svg_icon = self.svg_icon;
        let svg_data = self.svg_data;

        let clicked = response.clicked() && enabled;

        if clicked {
            if let Some(action) = action {
                action();
            }
        }

        // Material Design colors
        let primary_color = get_global_color("primary");
        let secondary_color = get_global_color("secondary");
        let tertiary_color = get_global_color("tertiary");
        let surface = get_global_color("surface");
        let on_primary = get_global_color("onPrimary");
        let on_surface = get_global_color("onSurface");

        let (bg_color, icon_color) = if !enabled {
            (
                get_global_color("surfaceContainer"),
                get_global_color("outline"),
            )
        } else {
            match variant {
                FabVariant::Surface => {
                    if response.is_pointer_button_down_on() {
                        (get_global_color("surfaceContainerHighest"), on_surface)
                    } else if response.hovered() {
                        (get_global_color("surfaceContainerHigh"), on_surface)
                    } else {
                        (surface, on_surface)
                    }
                }
                FabVariant::Primary => {
                    if response.hovered() || response.is_pointer_button_down_on() {
                        let lighten_amount = if response.is_pointer_button_down_on() { 40 } else { 20 };
                        (
                            Color32::from_rgba_premultiplied(
                                primary_color.r().saturating_add(lighten_amount),
                                primary_color.g().saturating_add(lighten_amount),
                                primary_color.b().saturating_add(lighten_amount),
                                255,
                            ),
                            on_primary,
                        )
                    } else {
                        (primary_color, on_primary)
                    }
                }
                FabVariant::Secondary => {
                    if response.hovered() || response.is_pointer_button_down_on() {
                        let lighten_amount = if response.is_pointer_button_down_on() { 40 } else { 20 };
                        (
                            Color32::from_rgba_premultiplied(
                                secondary_color.r().saturating_add(lighten_amount),
                                secondary_color.g().saturating_add(lighten_amount),
                                secondary_color.b().saturating_add(lighten_amount),
                                255,
                            ),
                            on_primary,
                        )
                    } else {
                        (secondary_color, on_primary)
                    }
                }
                FabVariant::Tertiary => {
                    if response.hovered() || response.is_pointer_button_down_on() {
                        let lighten_amount = if response.is_pointer_button_down_on() { 40 } else { 20 };
                        (
                            Color32::from_rgba_premultiplied(
                                tertiary_color.r().saturating_add(lighten_amount),
                                tertiary_color.g().saturating_add(lighten_amount),
                                tertiary_color.b().saturating_add(lighten_amount),
                                255,
                            ),
                            on_primary,
                        )
                    } else {
                        (tertiary_color, on_primary)
                    }
                }
                FabVariant::Branded => {
                    // Google brand colors
                    let google_brand = Color32::from_rgb(66, 133, 244);
                    if response.hovered() || response.is_pointer_button_down_on() {
                        let lighten_amount = if response.is_pointer_button_down_on() { 40 } else { 20 };
                        (
                            Color32::from_rgba_premultiplied(
                                google_brand.r().saturating_add(lighten_amount),
                                google_brand.g().saturating_add(lighten_amount),
                                google_brand.b().saturating_add(lighten_amount),
                                255,
                            ),
                            on_primary,
                        )
                    } else {
                        (google_brand, on_primary)
                    }
                }
            }
        };

        // Calculate corner radius for FAB
        let corner_radius = match size_enum {
            FabSize::Small => 12.0,
            FabSize::Large => 16.0,
            _ => 14.0,
        };

        // Draw FAB background with less rounded corners
        ui.painter().rect_filled(rect, corner_radius, bg_color);

        // Draw content
        match size_enum {
            FabSize::Extended => {
                // Draw icon and text with proper spacing
                let left_margin = 16.0;
                let _right_margin = 24.0;
                let icon_text_gap = 12.0;
                let mut content_x = rect.min.x + left_margin;

                if let Some(ref svg_str) = svg_data {
                    // Render SVG data
                    if let Ok(texture) = render_svg_to_texture(ui.ctx(), svg_str, 24) {
                        let icon_rect = Rect::from_center_size(
                            Pos2::new(content_x + 12.0, rect.center().y),
                            Vec2::splat(24.0),
                        );
                        ui.painter().image(
                            texture.id(),
                            icon_rect,
                            Rect::from_min_max(Pos2::ZERO, Pos2::new(1.0, 1.0)),
                            Color32::WHITE,
                        );
                    }
                    content_x += 24.0 + icon_text_gap;
                } else if let Some(ref icon_name) = icon {
                    let icon_rect = Rect::from_min_size(
                        Pos2::new(content_x, rect.center().y - 12.0),
                        Vec2::splat(24.0),
                    );

                    // Draw material icon
                    let icon_char = material_symbol_text(icon_name);
                    let icon = MaterialIcon::new(icon_char).size(24.0).color(icon_color);
                    ui.scope_builder(egui::UiBuilder::new().max_rect(icon_rect), |ui| {
                        ui.add(icon);
                    });

                    content_x += 24.0 + icon_text_gap;
                } else if let Some(ref _svg_icon) = svg_icon {
                    // Render simplified Google logo for branded FAB
                    draw_google_logo(ui, Pos2::new(content_x + 12.0, rect.center().y), 24.0);
                    content_x += 24.0 + icon_text_gap;
                }

                if let Some(ref text) = text {
                    let text_pos = Pos2::new(content_x, rect.center().y);
                    ui.painter().text(
                        text_pos,
                        egui::Align2::LEFT_CENTER,
                        text,
                        egui::FontId::proportional(14.0),
                        icon_color,
                    );
                }
            }
            _ => {
                // Draw centered icon
                if let Some(ref svg_str) = svg_data {
                    let icon_size = match size_enum {
                        FabSize::Small => 18,
                        FabSize::Large => 36,
                        _ => 24,
                    };

                    // Render SVG data
                    if let Ok(texture) = render_svg_to_texture(ui.ctx(), svg_str, icon_size) {
                        let icon_rect = Rect::from_center_size(rect.center(), Vec2::splat(icon_size as f32));
                        ui.painter().image(
                            texture.id(),
                            icon_rect,
                            Rect::from_min_max(Pos2::ZERO, Pos2::new(1.0, 1.0)),
                            Color32::WHITE,
                        );
                    }
                } else if let Some(ref _svg_icon) = svg_icon {
                    let icon_size = match size_enum {
                        FabSize::Small => 18.0,
                        FabSize::Large => 36.0,
                        _ => 24.0,
                    };

                    // Render simplified Google logo for branded FAB
                    draw_google_logo(ui, rect.center(), icon_size);
                } else if let Some(ref icon_name) = icon {
                    let icon_size = match size_enum {
                        FabSize::Small => 18.0,
                        FabSize::Large => 36.0,
                        _ => 24.0,
                    };

                    let icon_rect = Rect::from_center_size(rect.center(), Vec2::splat(icon_size));
                    let icon_char = material_symbol_text(icon_name);
                    let icon = MaterialIcon::new(icon_char)
                        .size(icon_size)
                        .color(icon_color);
                    ui.scope_builder(egui::UiBuilder::new().max_rect(icon_rect), |ui| {
                        ui.add(icon);
                    });
                } else {
                    // Default add icon
                    let icon_size = match size_enum {
                        FabSize::Small => 18.0,
                        FabSize::Large => 36.0,
                        _ => 24.0,
                    };

                    let icon_rect = Rect::from_center_size(rect.center(), Vec2::splat(icon_size));
                    let icon_char = material_symbol_text("add");
                    let icon = MaterialIcon::new(icon_char).size(icon_size).color(icon_color);
                    ui.scope_builder(egui::UiBuilder::new().max_rect(icon_rect), |ui| {
                        ui.add(icon);
                    });
                }
            }
        }

        response
    }
}

// Helper function to draw Google logo
fn draw_google_logo(ui: &mut Ui, center: Pos2, size: f32) {
    let half_size = size / 2.0;
    let quarter_size = size / 4.0;

    // Google 4-color logo - simplified version
    // Green (top right)
    ui.painter().rect_filled(
        Rect::from_min_size(
            Pos2::new(center.x, center.y - half_size),
            Vec2::new(half_size, quarter_size),
        ),
        0.0,
        Color32::from_rgb(52, 168, 83), // Green #34A853
    );

    // Blue (right)
    ui.painter().rect_filled(
        Rect::from_min_size(
            Pos2::new(center.x, center.y - quarter_size),
            Vec2::new(half_size, half_size),
        ),
        0.0,
        Color32::from_rgb(66, 133, 244), // Blue #4285F4
    );

    // Yellow (bottom left)
    ui.painter().rect_filled(
        Rect::from_min_size(
            Pos2::new(center.x - half_size, center.y + quarter_size),
            Vec2::new(half_size, quarter_size),
        ),
        0.0,
        Color32::from_rgb(251, 188, 5), // Yellow #FBBC05
    );

    // Red (left)
    ui.painter().rect_filled(
        Rect::from_min_size(
            Pos2::new(center.x - half_size, center.y - half_size),
            Vec2::new(quarter_size, size),
        ),
        0.0,
        Color32::from_rgb(234, 67, 53), // Red #EA4335
    );
}

// Helper function to render SVG data to texture
fn render_svg_to_texture(
    ctx: &egui::Context,
    svg_data: &str,
    size: u32,
) -> Result<egui::TextureHandle, String> {
    use resvg::{usvg, tiny_skia};

    let tree = usvg::Tree::from_str(svg_data, &usvg::Options::default())
        .map_err(|e| e.to_string())?;
    let mut pixmap =
        tiny_skia::Pixmap::new(size, size).ok_or_else(|| "pixmap alloc failed".to_string())?;

    let ts = tree.size();
    let scale = (size as f32 / ts.width()).min(size as f32 / ts.height());
    resvg::render(
        &tree,
        tiny_skia::Transform::from_scale(scale, scale),
        &mut pixmap.as_mut(),
    );

    let color_image = egui::ColorImage::from_rgba_unmultiplied(
        [size as usize, size as usize],
        pixmap.data(),
    );

    // Create a unique key for this texture
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};
    let mut hasher = DefaultHasher::new();
    svg_data.hash(&mut hasher);
    size.hash(&mut hasher);
    let key = format!("fab_svg_{:x}", hasher.finish());

    Ok(ctx.load_texture(key, color_image, egui::TextureOptions::LINEAR))
}

pub fn fab_surface() -> MaterialFab<'static> {
    MaterialFab::surface()
}

pub fn fab_primary() -> MaterialFab<'static> {
    MaterialFab::primary()
}

pub fn fab_secondary() -> MaterialFab<'static> {
    MaterialFab::secondary()
}

pub fn fab_tertiary() -> MaterialFab<'static> {
    MaterialFab::tertiary()
}

pub fn fab_branded() -> MaterialFab<'static> {
    MaterialFab::branded()
}

/// Create Google branded icon (4-color logo)
pub fn google_branded_icon() -> SvgIcon {
    SvgIcon {
        paths: vec![
            SvgPath {
                path: "M16 16v14h4V20z".to_string(),
                fill: Color32::from_rgb(52, 168, 83), // Green #34A853
            },
            SvgPath {
                path: "M30 16H20l-4 4h14z".to_string(),
                fill: Color32::from_rgb(66, 133, 244), // Blue #4285F4
            },
            SvgPath {
                path: "M6 16v4h10l4-4z".to_string(),
                fill: Color32::from_rgb(251, 188, 5), // Yellow #FBBC05
            },
            SvgPath {
                path: "M20 16V6h-4v14z".to_string(),
                fill: Color32::from_rgb(234, 67, 53), // Red #EA4335
            },
        ],
        viewbox_size: Vec2::new(36.0, 36.0),
    }
}