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
//! Material Design 3 Search Bar and Search View
//!
//! The Search Bar represents a floating search field with affordances for search and navigation.
//! Reference: <https://m3.material.io/components/search/overview>

use bevy::prelude::*;

use crate::{
    i18n::{MaterialI18n, MaterialLanguage, MaterialLanguageOverride},
    icons::{IconStyle, MaterialIcon},
    ripple::RippleHost,
    theme::MaterialTheme,
    tokens::{CornerRadius, Spacing},
};

/// Plugin for search components
pub struct SearchPlugin;

impl Plugin for SearchPlugin {
    fn build(&self, app: &mut App) {
        if !app.is_plugin_added::<crate::MaterialUiCorePlugin>() {
            app.add_plugins(crate::MaterialUiCorePlugin);
        }
        app.add_message::<SearchBarClickEvent>()
            .add_message::<SearchQueryEvent>()
            .add_systems(
                Update,
                (
                    search_bar_interaction_system,
                    search_bar_localization_system,
                    search_bar_display_text_system,
                )
                    .chain(),
            );
    }
}

// ============================================================================
// Events
// ============================================================================

/// Event fired when the search bar is clicked
#[derive(Event, bevy::prelude::Message, Clone)]
pub struct SearchBarClickEvent {
    pub search_bar: Entity,
}

/// Event fired when search query changes
#[derive(Event, bevy::prelude::Message, Clone)]
pub struct SearchQueryEvent {
    pub search_bar: Entity,
    pub query: String,
}

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

/// Search Bar component
#[derive(Component)]
pub struct MaterialSearchBar {
    /// Hint text shown when empty
    pub hint: String,
    /// Current search text
    pub text: String,
    /// Navigation icon (usually menu or back)
    pub navigation_icon: Option<MaterialIcon>,
    /// Whether to show the trailing action icon
    pub show_action: bool,
}

impl MaterialSearchBar {
    pub fn new(hint: impl Into<String>) -> Self {
        Self {
            hint: hint.into(),
            text: String::new(),
            navigation_icon: None,
            show_action: true,
        }
    }

    pub fn with_navigation(mut self, icon: MaterialIcon) -> Self {
        self.navigation_icon = Some(icon);
        self
    }

    pub fn with_text(mut self, text: impl Into<String>) -> Self {
        self.text = text.into();
        self
    }
}

/// Marker for search bar navigation button
#[derive(Component)]
pub struct SearchBarNavigation;

/// Marker for search bar action button
#[derive(Component)]
pub struct SearchBarAction;

/// Marker for search bar text container
#[derive(Component)]
pub struct SearchBarTextContainer;

/// Optional localization keys for a search bar.
#[derive(Component, Debug, Default, Clone, PartialEq, Eq)]
pub struct SearchBarLocalization {
    pub hint_key: Option<String>,
    pub text_key: Option<String>,
}

/// Tracks the last i18n revision/language used to resolve a search bar.
#[derive(Component, Debug, Default, Clone, PartialEq, Eq)]
struct SearchBarLocalizationState {
    last_revision: u64,
    last_language: String,
}

/// Marker for the search bar's displayed text node.
#[derive(Component)]
pub struct SearchBarDisplayText;

/// Links a displayed text node to its owning search bar.
#[derive(Component)]
pub struct SearchBarDisplayTextFor(pub Entity);

// ============================================================================
// Constants
// ============================================================================

pub const SEARCH_BAR_HEIGHT: f32 = 56.0;

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

pub struct SearchBarBuilder {
    search_bar: MaterialSearchBar,
    localization: SearchBarLocalization,
}

impl SearchBarBuilder {
    pub fn new(hint: impl Into<String>) -> Self {
        Self {
            search_bar: MaterialSearchBar::new(hint),
            localization: SearchBarLocalization::default(),
        }
    }

    /// Set the hint from an i18n key.
    pub fn hint_key(mut self, key: impl Into<String>) -> Self {
        self.search_bar.hint = String::new();
        self.localization.hint_key = Some(key.into());
        self
    }

    /// Set the initial text from an i18n key.
    pub fn text_key(mut self, key: impl Into<String>) -> Self {
        self.search_bar.text = String::new();
        self.localization.text_key = Some(key.into());
        self
    }

    pub fn with_navigation(mut self, icon: MaterialIcon) -> Self {
        self.search_bar.navigation_icon = Some(icon);
        self
    }

    pub fn with_text(mut self, text: impl Into<String>) -> Self {
        self.search_bar.text = text.into();
        self
    }

    pub fn build(self, theme: &MaterialTheme) -> impl Bundle {
        (
            self.search_bar,
            self.localization,
            SearchBarLocalizationState::default(),
            Node {
                width: Val::Percent(100.0),
                height: Val::Px(SEARCH_BAR_HEIGHT),
                padding: UiRect::horizontal(Val::Px(Spacing::EXTRA_SMALL)),
                flex_direction: FlexDirection::Row,
                align_items: AlignItems::Center,
                column_gap: Val::Px(Spacing::SMALL),
                border_radius: BorderRadius::all(Val::Px(CornerRadius::FULL)),
                ..default()
            },
            BackgroundColor(theme.surface_container_high),
            RippleHost::new(),
            Button,
            Interaction::None,
        )
    }
}

// ============================================================================
// Spawn Trait
// ============================================================================

pub trait SpawnSearchBarChild {
    fn spawn_search_bar(&mut self, theme: &MaterialTheme, hint: impl Into<String>);

    fn spawn_search_bar_with(&mut self, theme: &MaterialTheme, builder: SearchBarBuilder);
}

impl SpawnSearchBarChild for ChildSpawnerCommands<'_> {
    fn spawn_search_bar(&mut self, theme: &MaterialTheme, hint: impl Into<String>) {
        self.spawn_search_bar_with(theme, SearchBarBuilder::new(hint));
    }

    fn spawn_search_bar_with(&mut self, theme: &MaterialTheme, builder: SearchBarBuilder) {
        let hint = builder.search_bar.hint.clone();
        let text = builder.search_bar.text.clone();
        let nav_icon = builder.search_bar.navigation_icon;
        let has_text = !text.is_empty();

        let mut cmds = self.spawn(builder.build(theme));
        let bar_entity = cmds.id();

        cmds.with_children(|bar| {
            // Navigation icon
            if let Some(icon) = nav_icon {
                bar.spawn((
                    SearchBarNavigation,
                    Button,
                    RippleHost::new(),
                    Node {
                        width: Val::Px(48.0),
                        height: Val::Px(48.0),
                        justify_content: JustifyContent::Center,
                        align_items: AlignItems::Center,
                        border_radius: BorderRadius::all(Val::Px(CornerRadius::FULL)),
                        ..default()
                    },
                    BackgroundColor(Color::NONE),
                ))
                .with_children(|btn| {
                    btn.spawn((
                        icon,
                        IconStyle::outlined()
                            .with_color(theme.on_surface)
                            .with_size(24.0),
                    ));
                });
            }

            // Text container
            bar.spawn((
                SearchBarTextContainer,
                Node {
                    flex_grow: 1.0,
                    flex_direction: FlexDirection::Row,
                    align_items: AlignItems::Center,
                    ..default()
                },
            ))
            .with_children(|container| {
                let initial = if has_text { &text } else { &hint };
                let color = if has_text {
                    theme.on_surface
                } else {
                    theme.on_surface_variant
                };

                container.spawn((
                    SearchBarDisplayText,
                    SearchBarDisplayTextFor(bar_entity),
                    Text::new(initial),
                    TextFont {
                        font_size: 16.0,
                        ..default()
                    },
                    TextColor(color),
                ));
            });

            // Trailing action (search icon)
            bar.spawn((
                SearchBarAction,
                Button,
                RippleHost::new(),
                Node {
                    width: Val::Px(48.0),
                    height: Val::Px(48.0),
                    justify_content: JustifyContent::Center,
                    align_items: AlignItems::Center,
                    border_radius: BorderRadius::all(Val::Px(CornerRadius::FULL)),
                    ..default()
                },
                BackgroundColor(Color::NONE),
            ))
            .with_children(|btn| {
                if let Some(icon) = MaterialIcon::from_name("search") {
                    btn.spawn((
                        icon,
                        IconStyle::outlined()
                            .with_color(theme.on_surface_variant)
                            .with_size(24.0),
                    ));
                }
            });
        });
    }
}

fn resolve_language_tag_for_entity(
    mut entity: Entity,
    child_of: &Query<&ChildOf>,
    overrides: &Query<&MaterialLanguageOverride>,
    global: &MaterialLanguage,
) -> String {
    if let Ok(ov) = overrides.get(entity) {
        return ov.tag.clone();
    }

    while let Ok(parent) = child_of.get(entity) {
        entity = parent.parent();
        if let Ok(ov) = overrides.get(entity) {
            return ov.tag.clone();
        }
    }

    global.tag.clone()
}

fn search_bar_localization_system(
    i18n: Option<Res<MaterialI18n>>,
    language: Option<Res<MaterialLanguage>>,
    child_of: Query<&ChildOf>,
    overrides: Query<&MaterialLanguageOverride>,
    mut bars: Query<(
        Entity,
        &SearchBarLocalization,
        &mut MaterialSearchBar,
        &mut SearchBarLocalizationState,
    )>,
) {
    let (Some(i18n), Some(language)) = (i18n, language) else {
        return;
    };

    let global_revision = i18n.revision();

    for (entity, loc, mut bar, mut state) in bars.iter_mut() {
        if loc.hint_key.is_none() && loc.text_key.is_none() {
            continue;
        }

        let resolved_language =
            resolve_language_tag_for_entity(entity, &child_of, &overrides, &language);
        let needs_update =
            state.last_revision != global_revision || state.last_language != resolved_language;
        if !needs_update {
            continue;
        }

        if let Some(key) = loc.hint_key.as_deref() {
            if let Some(v) = i18n.translate(&resolved_language, key) {
                let next = v.to_string();
                if bar.hint != next {
                    bar.hint = next;
                }
            }
        }

        if let Some(key) = loc.text_key.as_deref() {
            if let Some(v) = i18n.translate(&resolved_language, key) {
                let next = v.to_string();
                if bar.text != next {
                    bar.text = next;
                }
            }
        }

        state.last_revision = global_revision;
        state.last_language = resolved_language;
    }
}

fn search_bar_display_text_system(
    theme: Option<Res<MaterialTheme>>,
    bars: Query<&MaterialSearchBar>,
    mut display_texts: Query<(&SearchBarDisplayTextFor, &mut Text, &mut TextColor)>,
) {
    let Some(theme) = theme else {
        return;
    };

    for (owner, mut text, mut color) in display_texts.iter_mut() {
        let Ok(bar) = bars.get(owner.0) else {
            continue;
        };

        let has_text = !bar.text.is_empty();
        let content = if has_text {
            bar.text.as_str()
        } else {
            bar.hint.as_str()
        };

        *text = Text::new(content);
        color.0 = if has_text {
            theme.on_surface
        } else {
            theme.on_surface_variant
        };
    }
}

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

fn search_bar_interaction_system(
    search_bars: Query<(&Interaction, Entity), (Changed<Interaction>, With<MaterialSearchBar>)>,
    mut click_events: MessageWriter<SearchBarClickEvent>,
) {
    for (interaction, entity) in search_bars.iter() {
        if *interaction == Interaction::Pressed {
            click_events.write(SearchBarClickEvent { search_bar: entity });
        }
    }
}