bevy_material_ui 0.2.7

Material Design 3 UI components for Bevy game engine
Documentation
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
602
603
604
605
606
607
608
609
610
611
//! Material Design 3 Menu component
//!
//! Menus display a list of choices on a temporary surface.
//! This module leverages native `BoxShadow` for elevation shadows.
//!
//! Reference: <https://m3.material.io/components/menus/overview>

use bevy::prelude::*;
use bevy::ui::BoxShadow;

use std::collections::HashMap;

use crate::{
    elevation::Elevation,
    ripple::RippleHost,
    telemetry::{InsertTestIdIfExists, TelemetryConfig, TestId},
    theme::MaterialTheme,
    tokens::{CornerRadius, Spacing},
};

/// Plugin for the menu component
pub struct MenuPlugin;

impl Plugin for MenuPlugin {
    fn build(&self, app: &mut App) {
        if !app.is_plugin_added::<crate::MaterialUiCorePlugin>() {
            app.add_plugins(crate::MaterialUiCorePlugin);
        }
        app.add_message::<MenuOpenEvent>()
            .add_message::<MenuCloseEvent>()
            .add_message::<MenuItemSelectEvent>()
            .add_systems(
                Update,
                (
                    menu_visibility_system,
                    menu_shadow_system,
                    menu_item_interaction_system,
                    menu_item_style_system,
                    menu_telemetry_system,
                ),
            );
    }
}

fn sanitize_test_id_component(raw: &str) -> String {
    let mut out = String::with_capacity(raw.len());
    for ch in raw.chars() {
        let c = ch.to_ascii_lowercase();
        if c.is_ascii_alphanumeric() {
            out.push(c);
        } else if (c.is_ascii_whitespace() || c == '-') && !out.ends_with('_') {
            out.push('_');
        }
    }

    while out.ends_with('_') {
        out.pop();
    }

    if out.is_empty() {
        "item".to_string()
    } else {
        out
    }
}

fn menu_telemetry_system(
    mut commands: Commands,
    telemetry: Option<Res<TelemetryConfig>>,
    menus: Query<(&TestId, &Children), With<MaterialMenu>>,
    children_query: Query<&Children>,
    menu_items: Query<&MaterialMenuItem>,
    menu_dividers: Query<(), With<MenuDivider>>,
) {
    let Some(telemetry) = telemetry else {
        return;
    };
    if !telemetry.enabled {
        return;
    }

    for (test_id, children) in menus.iter() {
        let base = test_id.id();
        let mut item_counts: HashMap<String, u32> = HashMap::new();
        let mut divider_index: u32 = 0;

        let mut stack: Vec<Entity> = children.iter().collect();
        while let Some(entity) = stack.pop() {
            if let Ok(item) = menu_items.get(entity) {
                let slug = sanitize_test_id_component(item.label.as_str());
                let count = item_counts.entry(slug.clone()).or_insert(0);
                *count += 1;
                let unique = if *count == 1 {
                    slug
                } else {
                    format!("{slug}_{}", *count)
                };

                commands.queue(InsertTestIdIfExists {
                    entity,
                    id: format!("{base}/item/{unique}"),
                });
            }

            if menu_dividers.get(entity).is_ok() {
                divider_index += 1;
                commands.queue(InsertTestIdIfExists {
                    entity,
                    id: format!("{base}/divider/{divider_index}"),
                });
            }

            if let Ok(children) = children_query.get(entity) {
                stack.extend(children.iter());
            }
        }
    }
}

/// Material menu component
#[derive(Component)]
pub struct MaterialMenu {
    /// Whether the menu is currently open
    pub open: bool,
    /// Anchor corner for positioning
    pub anchor: MenuAnchor,
    /// Whether clicking outside closes the menu
    pub close_on_click_outside: bool,
}

impl MaterialMenu {
    /// Create a new menu
    pub fn new() -> Self {
        Self {
            open: false,
            anchor: MenuAnchor::default(),
            close_on_click_outside: true,
        }
    }

    /// Set anchor position
    pub fn anchor(mut self, anchor: MenuAnchor) -> Self {
        self.anchor = anchor;
        self
    }

    /// Start open
    pub fn open(mut self) -> Self {
        self.open = true;
        self
    }

    /// Keep open when clicking outside
    pub fn no_close_on_outside(mut self) -> Self {
        self.close_on_click_outside = false;
        self
    }

    /// Get the surface color
    pub fn surface_color(&self, theme: &MaterialTheme) -> Color {
        theme.surface_container
    }

    /// Get the elevation
    pub fn elevation(&self) -> Elevation {
        Elevation::Level2
    }
}

impl Default for MaterialMenu {
    fn default() -> Self {
        Self::new()
    }
}

/// Menu anchor position
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum MenuAnchor {
    /// Top-left corner
    TopLeft,
    /// Top-right corner
    TopRight,
    /// Bottom-left corner (default dropdown position)
    #[default]
    BottomLeft,
    /// Bottom-right corner
    BottomRight,
}

/// Material menu item
#[derive(Component)]
pub struct MaterialMenuItem {
    /// Item label text
    pub label: String,
    /// Leading icon
    pub leading_icon: Option<String>,
    /// Trailing icon
    pub trailing_icon: Option<String>,
    /// Trailing text (e.g., keyboard shortcut)
    pub trailing_text: Option<String>,
    /// Whether this item opens a submenu
    pub has_submenu: bool,
    /// Whether the item is disabled
    pub disabled: bool,
    /// Whether the item is selected/checked
    pub selected: bool,
    /// Interaction states
    pub pressed: bool,
    pub hovered: bool,
}

impl MaterialMenuItem {
    /// Create a new menu item
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            leading_icon: None,
            trailing_icon: None,
            trailing_text: None,
            has_submenu: false,
            disabled: false,
            selected: false,
            pressed: false,
            hovered: false,
        }
    }

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

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

    /// Set trailing text (keyboard shortcut)
    pub fn shortcut(mut self, text: impl Into<String>) -> Self {
        self.trailing_text = Some(text.into());
        self
    }

    /// Mark as having a submenu
    pub fn submenu(mut self) -> Self {
        self.has_submenu = true;
        self.trailing_icon = Some("chevron_right".into());
        self
    }

    /// Set disabled state
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Set selected state
    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self
    }

    /// Get the text color
    pub fn text_color(&self, theme: &MaterialTheme) -> Color {
        if self.disabled {
            theme.on_surface.with_alpha(0.38)
        } else {
            theme.on_surface
        }
    }

    /// Get the icon color
    pub fn icon_color(&self, theme: &MaterialTheme) -> Color {
        if self.disabled {
            theme.on_surface.with_alpha(0.38)
        } else {
            theme.on_surface_variant
        }
    }

    /// Get the background color
    pub fn background_color(&self, theme: &MaterialTheme) -> Color {
        if self.selected {
            theme.secondary_container
        } else {
            Color::NONE
        }
    }
}

/// Event to open a menu
#[derive(Event, bevy::prelude::Message)]
pub struct MenuOpenEvent {
    pub entity: Entity,
}

/// Event when menu is closed
#[derive(Event, bevy::prelude::Message)]
pub struct MenuCloseEvent {
    pub entity: Entity,
}

/// Event when menu item is selected
#[derive(Event, bevy::prelude::Message)]
pub struct MenuItemSelectEvent {
    pub menu_entity: Entity,
    pub item_entity: Entity,
}

/// Menu dimensions
pub const MENU_MIN_WIDTH: f32 = 112.0;
pub const MENU_MAX_WIDTH: f32 = 280.0;
pub const MENU_ITEM_HEIGHT: f32 = 48.0;

/// System to handle menu visibility
fn menu_visibility_system(mut menus: Query<(&MaterialMenu, &mut Node), Changed<MaterialMenu>>) {
    for (menu, mut node) in menus.iter_mut() {
        node.display = if menu.open {
            Display::Flex
        } else {
            Display::None
        };
    }
}

/// System to update menu shadows using native BoxShadow
fn menu_shadow_system(mut menus: Query<(&MaterialMenu, &mut BoxShadow), Changed<MaterialMenu>>) {
    for (menu, mut shadow) in menus.iter_mut() {
        // Only show shadow when menu is open
        if menu.open {
            *shadow = menu.elevation().to_box_shadow();
        } else {
            *shadow = BoxShadow::default();
        }
    }
}

/// System to handle menu item interactions
fn menu_item_interaction_system(
    mut interaction_query: Query<
        (Entity, &Interaction, &mut MaterialMenuItem, &ChildOf),
        (Changed<Interaction>, With<MaterialMenuItem>),
    >,
    menus: Query<Entity, With<MaterialMenu>>,
    mut select_events: MessageWriter<MenuItemSelectEvent>,
) {
    for (entity, interaction, mut item, parent) in interaction_query.iter_mut() {
        if item.disabled {
            continue;
        }

        match *interaction {
            Interaction::Pressed => {
                item.pressed = true;
                item.hovered = false;

                if !item.has_submenu {
                    // Find the menu ancestor
                    if let Ok(menu_entity) = menus.get(parent.parent()) {
                        select_events.write(MenuItemSelectEvent {
                            menu_entity,
                            item_entity: entity,
                        });
                    }
                }
            }
            Interaction::Hovered => {
                item.pressed = false;
                item.hovered = true;
            }
            Interaction::None => {
                item.pressed = false;
                item.hovered = false;
            }
        }
    }
}

/// System to update menu item styles
fn menu_item_style_system(
    theme: Option<Res<MaterialTheme>>,
    mut items: Query<(&MaterialMenuItem, &mut BackgroundColor), Changed<MaterialMenuItem>>,
) {
    let Some(theme) = theme else { return };

    for (item, mut bg_color) in items.iter_mut() {
        *bg_color = BackgroundColor(item.background_color(&theme));
    }
}

/// Builder for menus
pub struct MenuBuilder {
    menu: MaterialMenu,
}

impl MenuBuilder {
    /// Create a new menu builder
    pub fn new() -> Self {
        Self {
            menu: MaterialMenu::new(),
        }
    }

    /// Set anchor position
    pub fn anchor(mut self, anchor: MenuAnchor) -> Self {
        self.menu.anchor = anchor;
        self
    }

    /// Start open
    pub fn open(mut self) -> Self {
        self.menu.open = true;
        self
    }

    /// Keep open when clicking outside
    pub fn no_close_on_outside(mut self) -> Self {
        self.menu.close_on_click_outside = false;
        self
    }

    /// Build the menu bundle with native BoxShadow
    pub fn build(self, theme: &MaterialTheme) -> impl Bundle {
        let bg_color = self.menu.surface_color(theme);

        (
            self.menu,
            Node {
                display: Display::None, // Hidden by default
                position_type: PositionType::Absolute,
                min_width: Val::Px(MENU_MIN_WIDTH),
                max_width: Val::Px(MENU_MAX_WIDTH),
                flex_direction: FlexDirection::Column,
                padding: UiRect::vertical(Val::Px(Spacing::SMALL)),
                border_radius: BorderRadius::all(Val::Px(CornerRadius::EXTRA_SMALL)),
                ..default()
            },
            BackgroundColor(bg_color),
            // Native Bevy 0.17 shadow support (starts hidden since menu is closed)
            BoxShadow::default(),
        )
    }
}

impl Default for MenuBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Builder for menu items
pub struct MenuItemBuilder {
    item: MaterialMenuItem,
}

impl MenuItemBuilder {
    /// Create a new menu item builder
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            item: MaterialMenuItem::new(label),
        }
    }

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

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

    /// Set keyboard shortcut
    pub fn shortcut(mut self, text: impl Into<String>) -> Self {
        self.item.trailing_text = Some(text.into());
        self
    }

    /// Mark as submenu trigger
    pub fn submenu(mut self) -> Self {
        self.item.has_submenu = true;
        self.item.trailing_icon = Some("chevron_right".into());
        self
    }

    /// Set disabled
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.item.disabled = disabled;
        self
    }

    /// Set selected
    pub fn selected(mut self, selected: bool) -> Self {
        self.item.selected = selected;
        self
    }

    /// Build the menu item bundle
    pub fn build(self, theme: &MaterialTheme) -> impl Bundle {
        let bg_color = self.item.background_color(theme);

        (
            self.item,
            Button,
            RippleHost::new(),
            Node {
                width: Val::Percent(100.0),
                height: Val::Px(MENU_ITEM_HEIGHT),
                padding: UiRect::horizontal(Val::Px(Spacing::MEDIUM)),
                align_items: AlignItems::Center,
                column_gap: Val::Px(Spacing::MEDIUM),
                ..default()
            },
            BackgroundColor(bg_color),
        )
    }
}

/// Marker for menu divider
#[derive(Component)]
pub struct MenuDivider;

/// Create a menu divider
pub fn create_menu_divider(theme: &MaterialTheme) -> impl Bundle {
    (
        MenuDivider,
        Node {
            width: Val::Percent(100.0),
            height: Val::Px(1.0),
            margin: UiRect::vertical(Val::Px(Spacing::SMALL)),
            ..default()
        },
        BackgroundColor(theme.outline_variant),
    )
}

// ============================================================================
// Spawn Traits for ChildSpawnerCommands
// ============================================================================

/// Extension trait to spawn Material menus as children
pub trait SpawnMenuChild {
    /// Spawn a menu container
    fn spawn_menu(
        &mut self,
        theme: &MaterialTheme,
        with_items: impl FnOnce(&mut ChildSpawnerCommands),
    );

    /// Spawn a menu item
    fn spawn_menu_item(&mut self, theme: &MaterialTheme, label: impl Into<String>);

    /// Spawn a menu item with full builder control
    fn spawn_menu_item_with(&mut self, theme: &MaterialTheme, builder: MenuItemBuilder);

    /// Spawn a menu divider
    fn spawn_menu_divider(&mut self, theme: &MaterialTheme);
}

impl SpawnMenuChild for ChildSpawnerCommands<'_> {
    fn spawn_menu(
        &mut self,
        theme: &MaterialTheme,
        with_items: impl FnOnce(&mut ChildSpawnerCommands),
    ) {
        self.spawn(MenuBuilder::new().build(theme))
            .with_children(with_items);
    }

    fn spawn_menu_item(&mut self, theme: &MaterialTheme, label: impl Into<String>) {
        let label_str = label.into();
        let label_color = theme.on_surface;

        self.spawn(MenuItemBuilder::new(&label_str).build(theme))
            .with_children(|item| {
                item.spawn((
                    Text::new(&label_str),
                    TextFont {
                        font_size: 14.0,
                        ..default()
                    },
                    TextColor(label_color),
                ));
            });
    }

    fn spawn_menu_item_with(&mut self, theme: &MaterialTheme, builder: MenuItemBuilder) {
        let label_str = builder.item.label.clone();
        let label_color = theme.on_surface;

        self.spawn(builder.build(theme)).with_children(|item| {
            item.spawn((
                Text::new(&label_str),
                TextFont {
                    font_size: 14.0,
                    ..default()
                },
                TextColor(label_color),
            ));
        });
    }

    fn spawn_menu_divider(&mut self, theme: &MaterialTheme) {
        self.spawn(create_menu_divider(theme));
    }
}