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
//! Material button group / toggle group (segmented buttons)
//!
//! This is an ECS-native analog to the reference MDC `MaterialButtonToggleGroup`.
//! The group is responsible for:
//! - Orientation (horizontal / vertical)
//! - Optional single-selection behavior
//! - Optional selection-required behavior
//! - Applying connected corner radii to child buttons

use bevy::prelude::*;

use crate::button::MaterialButton;
use crate::telemetry::{InsertTestIdIfExists, TelemetryConfig, TestId};
use crate::tokens::CornerRadius;

/// Plugin for button groups.
pub struct ButtonGroupPlugin;

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

        app.add_systems(
            Update,
            (
                button_group_layout_system,
                button_group_toggle_system,
                button_group_corner_radius_system,
                button_group_telemetry_system,
            ),
        );
    }
}

fn button_group_telemetry_system(
    mut commands: Commands,
    telemetry: Option<Res<TelemetryConfig>>,
    groups: Query<(&TestId, &Children), With<MaterialButtonGroup>>,
    buttons: Query<(), With<MaterialButton>>,
) {
    let Some(telemetry) = telemetry else {
        return;
    };
    if !telemetry.enabled {
        return;
    }

    for (group_id, children) in groups.iter() {
        let group_id = group_id.id();
        let mut index = 0usize;

        for child in children.iter() {
            if buttons.get(child).is_err() {
                continue;
            }

            commands.queue(InsertTestIdIfExists {
                entity: child,
                id: format!("{group_id}/button/{index}"),
            });
            index += 1;
        }
    }
}

/// Orientation for a button group.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ButtonGroupOrientation {
    /// Lay out buttons left-to-right.
    #[default]
    Horizontal,
    /// Lay out buttons top-to-bottom.
    Vertical,
}

/// Material button group (segmented buttons).
///
/// Intended to be used as a container entity whose direct children are `MaterialButton` entities.
#[derive(Component, Debug, Clone)]
pub struct MaterialButtonGroup {
    pub orientation: ButtonGroupOrientation,

    /// If true, only one button in the group can be checked at a time.
    pub single_selection: bool,

    /// If true, prevents the group from having zero checked buttons.
    pub selection_required: bool,

    /// Gap between buttons in pixels.
    ///
    /// Note: a typical segmented control uses `0.0` spacing.
    pub spacing: f32,
}

impl Default for MaterialButtonGroup {
    fn default() -> Self {
        Self {
            orientation: ButtonGroupOrientation::Horizontal,
            single_selection: false,
            selection_required: false,
            spacing: 0.0,
        }
    }
}

impl MaterialButtonGroup {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn orientation(mut self, orientation: ButtonGroupOrientation) -> Self {
        self.orientation = orientation;
        self
    }

    pub fn vertical(mut self) -> Self {
        self.orientation = ButtonGroupOrientation::Vertical;
        self
    }

    pub fn horizontal(mut self) -> Self {
        self.orientation = ButtonGroupOrientation::Horizontal;
        self
    }

    pub fn single_selection(mut self, enabled: bool) -> Self {
        self.single_selection = enabled;
        self
    }

    pub fn selection_required(mut self, required: bool) -> Self {
        self.selection_required = required;
        self
    }

    pub fn spacing(mut self, px: f32) -> Self {
        self.spacing = px;
        self
    }
}

/// Builder for creating button groups.
pub struct ButtonGroupBuilder {
    group: MaterialButtonGroup,
}

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

impl ButtonGroupBuilder {
    pub fn new() -> Self {
        Self {
            group: MaterialButtonGroup::default(),
        }
    }

    pub fn horizontal(mut self) -> Self {
        self.group.orientation = ButtonGroupOrientation::Horizontal;
        self
    }

    pub fn vertical(mut self) -> Self {
        self.group.orientation = ButtonGroupOrientation::Vertical;
        self
    }

    pub fn single_selection(mut self, enabled: bool) -> Self {
        self.group.single_selection = enabled;
        self
    }

    pub fn selection_required(mut self, required: bool) -> Self {
        self.group.selection_required = required;
        self
    }

    pub fn spacing(mut self, px: f32) -> Self {
        self.group.spacing = px;
        self
    }

    pub fn build(self) -> MaterialButtonGroup {
        self.group
    }
}

fn button_group_layout_system(
    mut groups: Query<
        (&MaterialButtonGroup, &mut Node),
        Or<(Added<MaterialButtonGroup>, Changed<MaterialButtonGroup>)>,
    >,
) {
    for (group, mut node) in groups.iter_mut() {
        node.flex_direction = match group.orientation {
            ButtonGroupOrientation::Horizontal => FlexDirection::Row,
            ButtonGroupOrientation::Vertical => FlexDirection::Column,
        };

        // Apply spacing on the major axis.
        match group.orientation {
            ButtonGroupOrientation::Horizontal => {
                node.column_gap = Val::Px(group.spacing);
                node.row_gap = Val::Px(0.0);
            }
            ButtonGroupOrientation::Vertical => {
                node.row_gap = Val::Px(group.spacing);
                node.column_gap = Val::Px(0.0);
            }
        }
    }
}

fn button_group_toggle_system(
    groups: Query<&MaterialButtonGroup>,
    mut buttons: ParamSet<(
        Query<
            (Entity, &Interaction, &ChildOf),
            (Changed<Interaction>, With<Button>, With<MaterialButton>),
        >,
        Query<(Entity, &ChildOf, &MaterialButton), (With<Button>, With<MaterialButton>)>,
        Query<(Entity, &ChildOf, &mut MaterialButton), (With<Button>, With<MaterialButton>)>,
    )>,
) {
    // Collect plans first (no mutation), then apply in one pass.
    #[derive(Clone, Copy)]
    struct TogglePlan {
        group: Entity,
        clicked: Entity,
        clicked_set_checked: Option<bool>,
        uncheck_others: bool,
    }

    let mut plans: Vec<TogglePlan> = Vec::new();

    // First, collect pressed interactions without borrowing multiple ParamSet views at once.
    let pressed: Vec<(Entity, Entity)> = {
        let changed = buttons.p0();
        changed
            .iter()
            .filter_map(|(entity, interaction, parent)| {
                (*interaction == Interaction::Pressed).then_some((entity, parent.parent()))
            })
            .collect()
    };

    if pressed.is_empty() {
        return;
    }

    {
        let read_buttons = buttons.p1();
        for (clicked_entity, group_entity) in pressed.iter().copied() {
            let Ok(group) = groups.get(group_entity) else {
                continue;
            };

            let Ok((_, _, clicked_button)) = read_buttons.get(clicked_entity) else {
                continue;
            };

            if clicked_button.disabled || !clicked_button.checkable {
                continue;
            }

            // Count checked buttons in this group.
            let mut checked_count = 0usize;
            for (_, p, b) in read_buttons.iter() {
                if p.parent() == group_entity && b.checkable && b.checked {
                    checked_count += 1;
                }
            }

            let clicked_is_checked = clicked_button.checked;

            if group.single_selection {
                if clicked_is_checked {
                    // Clicking an already-checked button toggles it off only if selection isn't required.
                    if !group.selection_required {
                        plans.push(TogglePlan {
                            group: group_entity,
                            clicked: clicked_entity,
                            clicked_set_checked: Some(false),
                            uncheck_others: false,
                        });
                    }
                } else {
                    // Select clicked and unselect everyone else.
                    plans.push(TogglePlan {
                        group: group_entity,
                        clicked: clicked_entity,
                        clicked_set_checked: Some(true),
                        uncheck_others: true,
                    });
                }
            } else {
                // Multi-select.
                if clicked_is_checked {
                    // Prevent unselecting the last checked item when selection_required.
                    if !(group.selection_required && checked_count <= 1) {
                        plans.push(TogglePlan {
                            group: group_entity,
                            clicked: clicked_entity,
                            clicked_set_checked: Some(false),
                            uncheck_others: false,
                        });
                    }
                } else {
                    plans.push(TogglePlan {
                        group: group_entity,
                        clicked: clicked_entity,
                        clicked_set_checked: Some(true),
                        uncheck_others: false,
                    });
                }
            }
        }
    }

    if plans.is_empty() {
        return;
    }

    // Apply plans.
    let mut write_buttons = buttons.p2();
    for plan in plans {
        if plan.uncheck_others {
            for (entity, parent, mut button) in write_buttons.iter_mut() {
                if parent.parent() != plan.group {
                    continue;
                }
                if entity != plan.clicked && button.checkable {
                    button.checked = false;
                }
            }
        }

        if let Some(set_checked) = plan.clicked_set_checked {
            if let Ok((_, _, mut button)) = write_buttons.get_mut(plan.clicked) {
                button.checked = set_checked;
            }
        }
    }
}

fn button_group_corner_radius_system(
    groups: Query<
        (&MaterialButtonGroup, &Children),
        Or<(
            Added<MaterialButtonGroup>,
            Changed<MaterialButtonGroup>,
            Changed<Children>,
        )>,
    >,
    buttons: Query<&MaterialButton>,
    mut nodes: Query<&mut Node>,
) {
    for (group, children) in groups.iter() {
        let mut button_children: Vec<(Entity, f32)> = Vec::new();
        for child in children.iter() {
            if let Ok(button) = buttons.get(child) {
                if button.checkable {
                    let radius = button.corner_radius.unwrap_or(CornerRadius::FULL);
                    button_children.push((child, radius));
                }
            }
        }

        let count = button_children.len();
        if count == 0 {
            continue;
        }

        for (index, (entity, radius)) in button_children.iter().enumerate() {
            let border_radius = segment_border_radius(group.orientation, index, count, *radius);
            if let Ok(mut node) = nodes.get_mut(*entity) {
                node.border_radius = border_radius;
            }
        }
    }
}

fn segment_border_radius(
    orientation: ButtonGroupOrientation,
    index: usize,
    count: usize,
    radius_px: f32,
) -> BorderRadius {
    let r = Val::Px(radius_px);
    let z = Val::Px(0.0);

    if count <= 1 {
        return BorderRadius::all(r);
    }

    match orientation {
        ButtonGroupOrientation::Horizontal => {
            if index == 0 {
                BorderRadius {
                    top_left: r,
                    bottom_left: r,
                    top_right: z,
                    bottom_right: z,
                }
            } else if index + 1 == count {
                BorderRadius {
                    top_left: z,
                    bottom_left: z,
                    top_right: r,
                    bottom_right: r,
                }
            } else {
                BorderRadius::all(z)
            }
        }
        ButtonGroupOrientation::Vertical => {
            if index == 0 {
                BorderRadius {
                    top_left: r,
                    top_right: r,
                    bottom_left: z,
                    bottom_right: z,
                }
            } else if index + 1 == count {
                BorderRadius {
                    top_left: z,
                    top_right: z,
                    bottom_left: r,
                    bottom_right: r,
                }
            } else {
                BorderRadius::all(z)
            }
        }
    }
}