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
//! Material Design 3 Badge component
//!
//! Badges show notifications, counts, or status information on navigation items and icons.
//!
//! Reference: <https://m3.material.io/components/badges/overview>

use bevy::prelude::*;

use crate::theme::MaterialTheme;

/// Plugin for the badge component
pub struct BadgePlugin;

impl Plugin for BadgePlugin {
    fn build(&self, app: &mut App) {
        if !app.is_plugin_added::<crate::MaterialUiCorePlugin>() {
            app.add_plugins(crate::MaterialUiCorePlugin);
        }
        app.add_systems(Update, (badge_style_system, badge_theme_refresh_system));
    }
}

// ============================================================================
// Types
// ============================================================================

/// Badge size variants
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum BadgeSize {
    /// Small dot badge (no content)
    Small,
    /// Large badge (with content)
    #[default]
    Large,
}

// ============================================================================
// Components
// ============================================================================

/// Material badge component
#[derive(Component)]
pub struct MaterialBadge {
    /// Badge size
    pub size: BadgeSize,
    /// Content (number or text) - None for small dot badge
    pub content: Option<String>,
    /// Maximum number to display (shows "99+" if exceeded)
    pub max: u32,
    /// Whether the badge is visible
    pub visible: bool,
}

impl MaterialBadge {
    /// Create a small dot badge
    pub fn dot() -> Self {
        Self {
            size: BadgeSize::Small,
            content: None,
            max: 999,
            visible: true,
        }
    }

    /// Create a large badge with a number
    pub fn count(count: u32) -> Self {
        Self {
            size: BadgeSize::Large,
            content: Some(Self::format_count(count, 999)),
            max: 999,
            visible: true,
        }
    }

    /// Create a large badge with text
    pub fn text(text: impl Into<String>) -> Self {
        Self {
            size: BadgeSize::Large,
            content: Some(text.into()),
            max: 999,
            visible: true,
        }
    }

    /// Set the maximum count before showing "+"
    pub fn with_max(mut self, max: u32) -> Self {
        self.max = max;
        if let Some(ref content) = self.content {
            if let Ok(count) = content.trim_end_matches('+').parse::<u32>() {
                self.content = Some(Self::format_count(count, max));
            }
        }
        self
    }

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

    /// Update the count
    pub fn set_count(&mut self, count: u32) {
        self.size = BadgeSize::Large;
        self.content = Some(Self::format_count(count, self.max));
    }

    /// Update the text
    pub fn set_text(&mut self, text: impl Into<String>) {
        self.size = BadgeSize::Large;
        self.content = Some(text.into());
    }

    /// Convert to dot badge
    pub fn set_dot(&mut self) {
        self.size = BadgeSize::Small;
        self.content = None;
    }

    /// Get the display text
    pub fn display_text(&self) -> Option<&str> {
        self.content.as_deref()
    }

    /// Format a count with max limit
    fn format_count(count: u32, max: u32) -> String {
        if count > max {
            format!("{}+", max)
        } else {
            count.to_string()
        }
    }

    /// Get the badge color
    pub fn background_color(&self, theme: &MaterialTheme) -> Color {
        theme.error
    }

    /// Get the content color
    pub fn content_color(&self, theme: &MaterialTheme) -> Color {
        theme.on_error
    }

    /// Get the badge width
    pub fn width(&self) -> f32 {
        match self.size {
            BadgeSize::Small => BADGE_SIZE_SMALL,
            BadgeSize::Large => {
                // Minimum width is BADGE_SIZE_LARGE, grows with content
                if let Some(ref content) = self.content {
                    let char_count = content.len();
                    if char_count <= 1 {
                        BADGE_SIZE_LARGE
                    } else {
                        BADGE_SIZE_LARGE + (char_count as f32 - 1.0) * 6.0
                    }
                } else {
                    BADGE_SIZE_LARGE
                }
            }
        }
    }

    /// Get the badge height
    pub fn height(&self) -> f32 {
        match self.size {
            BadgeSize::Small => BADGE_SIZE_SMALL,
            BadgeSize::Large => BADGE_SIZE_LARGE,
        }
    }
}

impl Default for MaterialBadge {
    fn default() -> Self {
        Self::dot()
    }
}

/// Marker for badge content text
#[derive(Component)]
pub struct BadgeContent;

// ============================================================================
// Dimensions
// ============================================================================

/// Small badge (dot) size
pub const BADGE_SIZE_SMALL: f32 = 6.0;
/// Large badge minimum size
pub const BADGE_SIZE_LARGE: f32 = 16.0;
/// Badge horizontal padding
pub const BADGE_PADDING: f32 = 4.0;
/// Badge offset from parent edge
pub const BADGE_OFFSET: f32 = -4.0;

// ============================================================================
// Builder
// ============================================================================

/// Builder for creating badges
pub struct BadgeBuilder {
    badge: MaterialBadge,
}

impl BadgeBuilder {
    /// Create a dot badge
    pub fn dot() -> Self {
        Self {
            badge: MaterialBadge::dot(),
        }
    }

    /// Create a count badge
    pub fn count(count: u32) -> Self {
        Self {
            badge: MaterialBadge::count(count),
        }
    }

    /// Create a text badge
    pub fn text(text: impl Into<String>) -> Self {
        Self {
            badge: MaterialBadge::text(text),
        }
    }

    /// Set maximum count
    pub fn max(mut self, max: u32) -> Self {
        self.badge = self.badge.with_max(max);
        self
    }

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

        (
            self.badge,
            Node {
                position_type: PositionType::Absolute,
                top: Val::Px(BADGE_OFFSET),
                right: Val::Px(BADGE_OFFSET),
                width: Val::Px(width),
                height: Val::Px(height),
                min_width: Val::Px(width),
                min_height: Val::Px(height),
                justify_content: JustifyContent::Center,
                align_items: AlignItems::Center,
                padding: UiRect::axes(Val::Px(BADGE_PADDING), Val::Px(0.0)),
                border_radius: BorderRadius::all(Val::Px(height / 2.0)),
                ..default()
            },
            BackgroundColor(bg_color),
        )
    }
}

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

/// Extension trait to spawn Material badges as children
pub trait SpawnBadgeChild {
    /// Spawn a small badge (dot indicator)
    fn spawn_small_badge(&mut self, theme: &MaterialTheme);

    /// Spawn a large badge with count
    fn spawn_badge_count(&mut self, theme: &MaterialTheme, count: u32);

    /// Spawn a badge with text
    fn spawn_badge_text(&mut self, theme: &MaterialTheme, text: impl Into<String>);

    /// Spawn a badge with full builder control
    fn spawn_badge_with(&mut self, theme: &MaterialTheme, builder: BadgeBuilder);
}

impl SpawnBadgeChild for ChildSpawnerCommands<'_> {
    fn spawn_small_badge(&mut self, theme: &MaterialTheme) {
        // Small badge is just the dot indicator without content
        self.spawn(BadgeBuilder::count(0).build(theme));
    }

    fn spawn_badge_count(&mut self, theme: &MaterialTheme, count: u32) {
        self.spawn_badge_with(theme, BadgeBuilder::count(count));
    }

    fn spawn_badge_text(&mut self, theme: &MaterialTheme, text: impl Into<String>) {
        self.spawn_badge_with(theme, BadgeBuilder::text(text));
    }

    fn spawn_badge_with(&mut self, theme: &MaterialTheme, builder: BadgeBuilder) {
        let content = builder.badge.content.clone();
        let content_color = builder.badge.content_color(theme);

        self.spawn(builder.build(theme)).with_children(|badge| {
            // Content text (for large badges)
            if let Some(ref text) = content {
                badge.spawn((
                    BadgeContent,
                    Text::new(text),
                    TextFont {
                        font_size: 11.0,
                        ..default()
                    },
                    TextColor(content_color),
                ));
            }
        });
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Spawn a badge with content
pub fn spawn_badge(commands: &mut Commands, theme: &MaterialTheme, badge: MaterialBadge) -> Entity {
    let content = badge.content.clone();
    let content_color = badge.content_color(theme);
    let bg_color = badge.background_color(theme);
    let width = badge.width();
    let height = badge.height();
    let visible = badge.visible;

    let mut entity = commands.spawn((
        badge,
        Node {
            position_type: PositionType::Absolute,
            top: Val::Px(BADGE_OFFSET),
            right: Val::Px(BADGE_OFFSET),
            width: Val::Px(width),
            height: Val::Px(height),
            min_width: Val::Px(width),
            min_height: Val::Px(height),
            justify_content: JustifyContent::Center,
            align_items: AlignItems::Center,
            padding: UiRect::axes(Val::Px(BADGE_PADDING), Val::Px(0.0)),
            border_radius: BorderRadius::all(Val::Px(height / 2.0)),
            display: if visible {
                Display::Flex
            } else {
                Display::None
            },
            ..default()
        },
        BackgroundColor(bg_color),
    ));

    // Add text content for large badges
    if let Some(text) = content {
        entity.with_children(|parent| {
            parent.spawn((
                BadgeContent,
                Text::new(text),
                TextFont {
                    font_size: 11.0,
                    ..default()
                },
                TextColor(content_color),
            ));
        });
    }

    entity.id()
}

/// Spawn a badge attached to a parent (like an icon button)
pub fn spawn_badge_on(
    commands: &mut Commands,
    theme: &MaterialTheme,
    badge: MaterialBadge,
    parent: Entity,
) -> Entity {
    let entity = spawn_badge(commands, theme, badge);
    commands.entity(entity).insert(ChildOf(parent));
    entity
}

// ============================================================================
// Systems
// ============================================================================

/// System to update badge styles
fn badge_style_system(
    theme: Option<Res<MaterialTheme>>,
    mut badges: Query<
        (
            &MaterialBadge,
            &mut Node,
            &mut BackgroundColor,
        ),
        Changed<MaterialBadge>,
    >,
    mut badge_texts: Query<(&ChildOf, &mut Text, &mut TextColor), With<BadgeContent>>,
) {
    let Some(theme) = theme else { return };

    for (badge, mut node, mut bg_color) in badges.iter_mut() {
        let width = badge.width();
        let height = badge.height();

        node.width = Val::Px(width);
        node.height = Val::Px(height);
        node.min_width = Val::Px(width);
        node.min_height = Val::Px(height);
        node.display = if badge.visible {
            Display::Flex
        } else {
            Display::None
        };

        *bg_color = BackgroundColor(badge.background_color(&theme));
        node.border_radius = BorderRadius::all(Val::Px(height / 2.0));
    }

    // Update text content
    for (parent, mut text, mut color) in badge_texts.iter_mut() {
        if let Ok((badge, _, _)) = badges.get(parent.parent()) {
            if let Some(content) = &badge.content {
                **text = content.clone();
            }
            color.0 = badge.content_color(&theme);
        }
    }
}

/// Refresh badge visuals when the theme changes.
fn badge_theme_refresh_system(
    theme: Option<Res<MaterialTheme>>,
    mut badges: Query<(
        &MaterialBadge,
        &mut Node,
        &mut BackgroundColor,
    )>,
    mut badge_texts: Query<(&ChildOf, &mut Text, &mut TextColor), With<BadgeContent>>,
) {
    let Some(theme) = theme else { return };
    if !theme.is_changed() {
        return;
    }

    for (badge, mut node, mut bg_color) in badges.iter_mut() {
        let width = badge.width();
        let height = badge.height();

        node.width = Val::Px(width);
        node.height = Val::Px(height);
        node.min_width = Val::Px(width);
        node.min_height = Val::Px(height);
        node.display = if badge.visible {
            Display::Flex
        } else {
            Display::None
        };

        *bg_color = BackgroundColor(badge.background_color(&theme));
        node.border_radius = BorderRadius::all(Val::Px(height / 2.0));
    }

    for (parent, mut text, mut color) in badge_texts.iter_mut() {
        if let Ok(badge) = badges.get(parent.parent()).map(|(b, _, _)| b) {
            if let Some(content) = &badge.content {
                **text = content.clone();
            }
            color.0 = badge.content_color(&theme);
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_dot_badge() {
        let badge = MaterialBadge::dot();
        assert_eq!(badge.size, BadgeSize::Small);
        assert!(badge.content.is_none());
        assert_eq!(badge.width(), BADGE_SIZE_SMALL);
    }

    #[test]
    fn test_count_badge() {
        let badge = MaterialBadge::count(5);
        assert_eq!(badge.size, BadgeSize::Large);
        assert_eq!(badge.content, Some("5".to_string()));
    }

    #[test]
    fn test_count_badge_max() {
        let badge = MaterialBadge::count(150).with_max(99);
        assert_eq!(badge.content, Some("99+".to_string()));
    }

    #[test]
    fn test_text_badge() {
        let badge = MaterialBadge::text("NEW");
        assert_eq!(badge.content, Some("NEW".to_string()));
    }

    #[test]
    fn test_badge_width() {
        // Single digit
        let badge1 = MaterialBadge::count(5);
        assert_eq!(badge1.width(), BADGE_SIZE_LARGE);

        // Double digit
        let badge2 = MaterialBadge::count(25);
        assert!(badge2.width() > BADGE_SIZE_LARGE);

        // Triple digit
        let badge3 = MaterialBadge::count(100);
        assert!(badge3.width() > badge2.width());
    }

    #[test]
    fn test_badge_visibility() {
        let mut badge = MaterialBadge::dot();
        assert!(badge.visible);

        badge = badge.visible(false);
        assert!(!badge.visible);
    }

    #[test]
    fn test_badge_update() {
        let mut badge = MaterialBadge::dot();
        assert_eq!(badge.size, BadgeSize::Small);

        badge.set_count(10);
        assert_eq!(badge.size, BadgeSize::Large);
        assert_eq!(badge.content, Some("10".to_string()));

        badge.set_dot();
        assert_eq!(badge.size, BadgeSize::Small);
        assert!(badge.content.is_none());
    }
}