Button
Triggers an action. A button says what it does with a role and a tint; a style, built in or your own, decides how it looks.
Roles
ui.add(Button::new("Normal"));
ui.add(Button::primary("Primary"));
ui.add(Button::destructive("Destructive"));
ui.add(Button::new("Cancel").role(ButtonRole::Cancel));
Tints
ui.add(Button::new("Secondary").tint(Tint::Secondary));
ui.add(Button::new("Warning").tint(Tint::Warning));
ui.add(Button::new("Success").tint(Tint::Success));
ui.add(Button::new("Info").tint(Tint::Info));
Styles
let styles: [(&str, &dyn ButtonStyle); 5] = [
("Standard", &button_styles::Standard),
("Outline", &button_styles::Outline),
("Ghost", &button_styles::Ghost),
("Link", &button_styles::Link),
("Plain", &button_styles::Plain),
];
for (name, style) in styles {
ui.add(Button::new(name).style(style));
ui.add(Button::primary("Primary").style(style));
ui.add(Button::destructive("Destructive").style(style));
ui.end_row();
}
A style for a whole area
// Every button inside takes the style and size, unless it picks
// its own.
ui.control_size(Size::Small, |ui| {
ui.button_style(button_styles::Ghost, |ui| {
ui.add(Button::new("Cut"));
ui.add(Button::new("Copy"));
ui.add(Button::new("Paste"));
ui.add(Button::primary("Share").style(&button_styles::Standard));
});
});
Your own style
ui.button_style(Pill, |ui| {
ui.add(Button::new("Follow"));
ui.add(Button::destructive("Unfollow"));
ui.add(Button::new("Shuffle").tint(Tint::Success).small());
ui.add(Button::new("Offline").disabled(true));
if ui
.add(Button::new("Favourite").selected(state.bold))
.clicked()
{
state.bold = !state.bold;
}
});
Sizes
ui.add(Button::primary("Extra small").xsmall());
ui.add(Button::primary("Small").small());
ui.add(Button::primary("Medium"));
ui.add(Button::primary("Large").large());
Rounded
ui.add(Button::new("None").rounded(Rounded::None));
ui.add(Button::new("Small").rounded(Rounded::Small));
ui.add(Button::new("Medium").rounded(Rounded::Medium));
ui.add(Button::new("Large").rounded(Rounded::Large));
ui.add(Button::new("Full").rounded(Rounded::Full));
States
if ui
.add(Button::primary(format!("Clicked {} times", state.clicks)))
.clicked()
{
state.clicks += 1;
}
if ui.add(Button::new("Bold").selected(state.bold)).clicked() {
state.bold = !state.bold;
}
ui.add(Button::primary("Disabled").disabled(true));
ui.add(Button::new("Disabled").disabled(true));
if ui.add(Button::new("Save").loading(state.loading)).clicked() {
state.loading = true;
}
if state.loading && ui.add(Button::ghost("Stop").small()).clicked() {
state.loading = false;
}
Selected
// Held down: each tint's fill, darker in light and lighter in dark.
ui.add(Button::new("Normal").selected(true));
ui.add(Button::primary("Primary").selected(true));
ui.add(Button::destructive("Destructive").selected(true));
for (name, tint) in [
("Secondary", Tint::Secondary),
("Warning", Tint::Warning),
("Success", Tint::Success),
("Info", Tint::Info),
] {
ui.add(Button::new(name).tint(tint).selected(true));
}
One button’s own size and colours
// A chevron for a button that opens something.
ui.add(Button::new("Export").chevron());
// Round icon buttons outside the look's four sizes.
for side in [32.0, 44.0, 56.0] {
ui.add(
Button::icon_only(glyph(ui.ctx(), "star"))
.tooltip("Favourite")
.height(side)
.icon_size((side / 2.5).round())
.rounded(Rounded::Full),
);
}
// Colours of its own, at rest and hovered, in any look.
let green = egui::Color32::from_rgb(30, 215, 96);
ui.add(
Button::icon_only(glyph(ui.ctx(), "plus"))
.tooltip("Add")
.height(44.0)
.rounded(Rounded::Full)
.colors(ButtonColors {
background: green,
foreground: egui::Color32::BLACK,
hover_background: green.gamma_multiply(0.85),
hover_foreground: egui::Color32::BLACK,
}),
);
Compact and wide
ui.add(Button::new("Compact").compact());
ui.add(Button::primary("At least 160 wide").min_width(160.0));
ui.add(
Button::destructive("Small, outlined destructive")
.style(&button_styles::Outline)
.size(Size::Small),
);
Pill and bare icon
ui.add(Button::primary("Play").style(&button_styles::Pill));
ui.add(Button::new("Follow").style(&button_styles::Pill));
// An icon with no frame, in colours of its own.
let theme = tokens(ui.ctx());
let bare = button_styles::FramelessIcon {
color: theme.muted_foreground,
hover: theme.foreground,
};
ui.add(
Button::icon_only(glyph(ui.ctx(), "plus"))
.tooltip("Add")
.icon_size(18.0)
.style(&bare),
);
The story above is gallery/src/stories/button.rs, running in your browser. Every snippet on this page is cut from it.