Alert
Tells people about a change in status without interrupting them, tinted for its kind of news, drawn by a style: a card, a banner, a call-out or one of your own.
Default
ui.add(
Alert::new("Your workspace is ready for the team. 12 members have access.")
.title("Workspace settings saved"),
);
Variants
if state.info_visible {
let info = Alert::info("Maintenance starts Friday at 22:00 UTC.")
.title("Scheduled maintenance")
.closable();
if info.show(ui).closed {
state.info_visible = false;
}
} else if ui
.add(Button::new("Show the info alert again").small())
.clicked()
{
state.info_visible = true;
}
ui.add(
Alert::success(
"The transfer is queued and usually settles within one business day.",
)
.title("Transfer submitted"),
);
ui.add(Alert::warning(
"Two teammates still use recovery codes generated more than a year ago. \
Ask them to generate a fresh set in Security settings.",
));
ui.add(
Alert::error("Please verify your billing information and try again.")
.title("Unable to process your payment."),
);
Banner
let banner = &alert_styles::Banner;
if state.banner_visible {
let alert = Alert::new("Reporting is read-only while the nightly ledger closes.")
.style(banner)
.closable();
if alert.show(ui).closed {
state.banner_visible = false;
}
} else if ui
.add(Button::new("Show the banner again").small())
.clicked()
{
state.banner_visible = true;
}
ui.add(
Alert::info("A new desktop update will install after you restart.").style(banner),
);
ui.add(Alert::success("All 1,284 records finished importing.").style(banner));
ui.add(
Alert::warning("Your API key expires in 6 days. Rotate it before August 19.")
.style(banner),
);
ui.add(
Alert::error("Live updates are disconnected. Changes may be delayed.")
.style(banner),
);
Actions
Alert::warning("The quarterly planning review overlaps with the APAC operations call by 30 minutes.")
.title("Two events overlap")
.show_with_actions(ui, |ui| {
ui.add(Button::primary("Move the review").small());
ui.add(Button::new("Keep both").small());
});
Sizes
for (name, size) in [
("Extra small", Size::XSmall),
("Small", Size::Small),
("Medium", Size::Medium),
("Large", Size::Large),
] {
ui.add(
Alert::info("Maintenance starts Friday at 22:00 UTC.")
.title(name)
.size(size),
);
}
Styles
let message = "Maintenance starts Friday at 22:00 UTC.";
ui.add(
Alert::info(message)
.title("Standard")
.style(&alert_styles::Standard),
);
ui.add(
Alert::info(message)
.title("Banner")
.style(&alert_styles::Banner),
);
ui.add(
Alert::info(message)
.title("Callout")
.style(&alert_styles::Callout),
);
Callout
// TonePush's card above a list while a tone waits for a slot.
if state.sending {
let callout = Alert::new(
"Free slots are lit; a used one says what it replaces. Esc cancels.",
)
.title("Choose a slot for Glass Lead.")
.icon(super::empty::glyph(ui.ctx(), "download"))
.style(&alert_styles::Callout);
callout.show_with_actions(ui, |ui| {
if ui.add(Button::ghost("Cancel").small()).clicked() {
state.sending = false;
}
});
} else if ui.add(Button::new("Send Glass Lead").small()).clicked() {
state.sending = true;
}
ui.add(
Alert::new("The pedal restarts when the update finishes.")
.title("Keep it plugged in")
.tint(Tint::Warning)
.style(&alert_styles::Callout),
);
A style for a whole area
// Every alert inside takes the style and size, unless it picks
// its own.
ui.control_size(Size::Small, |ui| {
ui.alert_style(alert_styles::Banner, |ui| {
ui.add(Alert::success("Synced 12 seconds ago."));
ui.add(Alert::warning("Two devices are offline."));
ui.add(
Alert::info("Backups run at 02:00.")
.title("Nightly backups")
.style(&alert_styles::Standard),
);
});
});
Your own style
ui.alert_style(Rule, |ui| {
ui.add(Alert::new("Comments are on for everyone in the workspace."));
ui.add(
Alert::success("The transfer settled this morning.").title("Transfer complete"),
);
Alert::error("The card was declined.")
.title("Unable to renew")
.closable()
.show(ui);
});
/// A quiet strip with a rule in the tint's colour down its start.
struct Rule;
impl AlertStyle for Rule {
fn layout(&self, ui: &egui::Ui, tint: Option<Tint>, size: Size) -> AlertLayout {
let theme = tokens(ui.ctx());
AlertLayout {
padding: egui::vec2(16.0, 10.0),
..AlertLayout::standard(ui, tint, size)
}
.colors(theme.foreground, theme.muted_foreground)
}
fn paint(&self, ui: &egui::Ui, alert: &AlertConfiguration<'_>) {
let theme = tokens(ui.ctx());
let color = alert
.tint
.map_or(theme.border, |tint| tint.fill(&theme).background);
let rule = egui::Rect::from_min_size(alert.rect.min, egui::vec2(3.0, alert.rect.height()));
alert.paint_behind(egui::Shape::rect_filled(rule, 1.5, color));
alert.paint_icon(ui, color);
alert.paint_close(&theme, theme.muted, theme.foreground);
}
}
The story above is gallery/src/stories/alert.rs, running in your browser. Every snippet on this page is cut from it.