Toggle
Turns something on or off. A toggle says what it turns on; a style, a switch, a checkbox, a button or your own, decides how it looks.
Default
ui.vertical(|ui| {
ui.set_max_width(400.0);
let settings = [
(
"Product updates",
"New features and release notes.",
&mut state.updates,
),
(
"Security alerts",
"Important activity on your account.",
&mut state.alerts,
),
];
for (index, (title, detail, on)) in settings.into_iter().enumerate() {
if index > 0 {
ui.separator();
}
ui.horizontal(|ui| {
ui.vertical(|ui| {
ui.label(title);
ui.label(egui::RichText::new(detail).color(theme.muted_foreground));
});
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.add(Toggle::without_label(on)).on_hover_text(title);
});
});
}
});
Long labels
ui.vertical(|ui| {
ui.set_max_width(320.0);
ui.add(Toggle::new(
&mut state.transcribe,
"Automatically transcribe downloaded episodes",
));
ui.add(Toggle::new(&mut true, "Automatically download new episodes").disabled(true));
});
Disabled
ui.add(Toggle::without_label(&mut false).disabled(true));
ui.add(Toggle::new(&mut true, "Airplane mode").disabled(true));
Tint
ui.add(Toggle::new(&mut state.success, "Success").tint(Tint::Success));
ui.add(Toggle::new(&mut state.danger, "Destructive").tint(Tint::Danger));
ui.add(
Toggle::new(&mut true, "Disabled")
.tint(Tint::Success)
.disabled(true),
);
Sizes and label side
let [small, medium, large] = &mut state.switch_sizes;
ui.add(Toggle::new(small, "Small").size(Size::Small));
ui.add(Toggle::new(medium, "Medium"));
ui.add(
Toggle::new(large, "Large, label first")
.size(Size::Large)
.label_first(),
);
Checkbox
ui.add(Toggle::new(&mut state.product_updates, "Product updates").style(checkbox));
ui.add(Toggle::new(&mut state.remember, "Remember this device").style(checkbox));
Checkbox without label
ui.add(Toggle::without_label(&mut state.unlabelled).style(checkbox))
.on_hover_text("Select row");
Mixed
ui.vertical(|ui| {
let all = state.files.iter().all(|checked| *checked);
let some = state.files.iter().any(|checked| *checked);
let mut group = all;
if ui
.add(
Toggle::new(&mut group, "All files")
.mixed(some && !all)
.style(checkbox),
)
.changed()
{
state.files = [group; 3];
}
for (checked, name) in
state
.files
.iter_mut()
.zip(["README.md", "Cargo.toml", "main.rs"])
{
ui.horizontal(|ui| {
ui.add_space(24.0);
ui.add(Toggle::new(checked, name).style(checkbox));
});
}
});
Disabled checkboxes
ui.add(
Toggle::new(&mut true, "Checked")
.style(checkbox)
.disabled(true),
);
ui.add_space(16.0);
ui.add(
Toggle::new(&mut false, "Unchecked")
.style(checkbox)
.disabled(true),
);
Checkbox labels
ui.vertical(|ui| {
ui.set_max_width(320.0);
ui.spacing_mut().item_spacing.y = 20.0;
ui.add(
Toggle::new(&mut state.automatic, "Automatic updates")
.description("Download updates when the application is idle.")
.style(checkbox),
);
ui.add(
Toggle::new(
&mut state.notify,
"Notify me when a new device signs in to my account",
)
.style(checkbox),
);
});
Checkbox sizes
let sizes = [Size::XSmall, Size::Small, Size::Medium, Size::Large];
let labels = ["Extra small", "Small", "Medium", "Large"];
for ((checked, size), label) in state.checkbox_sizes.iter_mut().zip(sizes).zip(labels) {
ui.add(Toggle::new(checked, label).style(checkbox).size(size));
}
Button
ui.add(Toggle::new(&mut state.preview, "Preview").style(button));
ui.add(Toggle::new(&mut state.favourite, "★").style(button))
.on_hover_text("Favourite");
ui.add(
Toggle::new(&mut true, "Disabled")
.style(button)
.disabled(true),
);
Button and outline button
ui.vertical(|ui| {
for (name, style, row) in [
("Ghost", button as &dyn ToggleStyle, &mut state.ghost),
("Outline", outline, &mut state.outline),
] {
if name == "Outline" {
ui.add_space(8.0);
}
ui.label(name);
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing = egui::vec2(8.0, 8.0);
for (on, label) in row.iter_mut().zip(["B", "I", "U"]) {
ui.add(Toggle::new(on, label).style(style));
}
});
}
});
Joined
// Independent toggles joined into one bar are a button group that
// lets several be selected.
ui.add(
ButtonGroup::new()
.style(&button_styles::Outline)
.button(Button::new("Bold"))
.button(Button::new("Italic"))
.button(Button::new("Code"))
.selection(&mut state.formatting)
.multiple(true),
);
Button sizes
let sizes = [Size::XSmall, Size::Small, Size::Medium, Size::Large];
let labels = ["Extra small", "Small", "Medium", "Large"];
for ((on, size), label) in state.button_sizes.iter_mut().zip(sizes).zip(labels) {
ui.add(Toggle::new(on, label).style(outline).size(size));
}
Styles
let styles: [(&str, &dyn ToggleStyle); 3] = [
("Switch", &toggle_styles::Switch),
("Checkbox", &toggle_styles::Checkbox),
("Button", &toggle_styles::OutlineButton),
];
for ((name, style), on) in styles.into_iter().zip(&mut state.styles) {
ui.add(Toggle::new(on, name).style(style));
}
Environment
// Every toggle inside takes the style and size set around it.
ui.toggle_style(toggle_styles::Checkbox, |ui| {
ui.control_size(Size::Small, |ui| {
ui.add(Toggle::new(&mut state.compact, "Compact rows"));
ui.add(Toggle::new(&mut state.sound, "Play a sound"));
});
});
Your own style
ui.toggle_style(Dot, |ui| {
let [first, second, third] = &mut state.dots;
ui.add(Toggle::new(first, "Wi-Fi"));
ui.add(Toggle::new(second, "Bluetooth").tint(Tint::Info));
ui.add(Toggle::new(third, "Offline").disabled(true));
});
The story above is gallery/src/stories/toggle.rs, running in your browser. Every snippet on this page is cut from it.