GroupBox
Groups related controls under a muted title, on a surface its style draws: plain, filled, outlined or your own.
Default
GroupBox::new().title("Email notifications").show(ui, |ui| {
let labels = ["All activity", "Product updates", "Account activity"];
for (checked, label) in state.email.iter_mut().zip(labels) {
ui.checkbox(checked, label);
}
ui.add(Button::primary("Save preferences"));
});
Filled
GroupBox::new()
.fill()
.title("Contributions & activity")
.footer("Private contributions never reveal repository names.")
.show(ui, |ui| {
setting(
ui,
"Make profile private and hide activity",
&mut state.private_profile,
);
setting(
ui,
"Include private contributions on my profile",
&mut state.private_contributions,
);
ui.add(Button::primary("Save"));
});
Outlined
GroupBox::new()
.outline()
.title("Appearance")
.show(ui, |ui| {
ui.vertical(|ui| {
ui.radio_value(&mut state.theme, 0, "Light");
ui.radio_value(&mut state.theme, 1, "Dark");
ui.radio_value(&mut state.theme, 2, "System");
});
});
Without title
GroupBox::new().outline().show(ui, |ui| {
setting(
ui,
"Make profile private and hide activity",
&mut state.compact_private,
);
});
Styles
let styles: [(&str, &dyn GroupBoxStyle); 3] = [
("Plain", &group_box_styles::Plain),
("Fill", &group_box_styles::Fill),
("Outline", &group_box_styles::Outline),
];
for (name, style) in styles {
ui.vertical(|ui| {
ui.set_width(160.0);
GroupBox::new().title(name).style(style).show(ui, |ui| {
ui.label("Sync over Wi-Fi only");
});
});
}
A style for a whole area
// Every group box inside takes the style, unless it picks its
// own; a Settings view's groups do too.
ui.group_box_style(group_box_styles::Fill, |ui| {
GroupBox::new().title("Privacy").show(ui, |ui| {
ui.label("Hide my activity from the team");
});
ui.add_space(12.0);
GroupBox::new()
.title("Danger zone")
.outline()
.show(ui, |ui| {
ui.add(Button::destructive("Delete account"));
});
});
Your own style
// TonePush's fact cards.
for (caption, strong, quiet, folder) in [
(
"Your backup",
"Backup 14 March.tpb",
"1,024 presets, 3.2 MB",
true,
),
("Firmware", "Version 4.2.0", "Released 2 weeks ago", false),
] {
ui.vertical(|ui| {
ui.set_width(220.0);
GroupBox::new().title(caption).style(&Card).show(ui, |ui| {
ui.spacing_mut().item_spacing.y = 4.0;
ui.strong(strong);
ui.weak(quiet);
if folder {
ui.add(Button::ghost("Show in folder").small());
}
});
});
}
/// A card on the page's background with a hairline and round corners.
struct Card;
impl GroupBoxStyle for Card {
fn paint(&self, ui: &egui::Ui, group: &GroupBoxConfiguration<'_>) {
let theme = tokens(ui.ctx());
group.paint_behind(egui::epaint::RectShape::new(
group.rect,
12.0,
theme.background,
egui::Stroke::new(1.0, theme.border),
egui::StrokeKind::Inside,
));
}
fn padding(&self) -> f32 {
15.0
}
fn title_gap(&self) -> f32 {
8.0
}
}
The story above is gallery/src/stories/group_box.rs, running in your browser. Every snippet on this page is cut from it.