Notification
Toasts stacked in a corner: info, success, warning and error, with a title, a message and an action. They hide after five seconds, pause while the pointer is over them, and spread out on hover.
Default
if ui.add(Button::new("Show Notification")).clicked() {
Notifications::push(ui.ctx(), "This is a notification.");
}
Types
if ui.add(Button::new("Info")).clicked() {
Notifications::push(
ui.ctx(),
Notification::info("This is an info notification."),
);
}
if ui.add(Button::new("Success")).clicked() {
Notifications::push(
ui.ctx(),
Notification::success("Your changes have been saved."),
);
}
if ui.add(Button::new("Warning")).clicked() {
Notifications::push(
ui.ctx(),
Notification::warning("Your session expires soon."),
);
}
if ui.add(Button::new("Error")).clicked() {
// Errors stay until closed.
Notifications::push(ui.ctx(), Notification::error("The upload failed."));
}
Title and description
for (label, kind, title, message) in [
(
"Info",
NotificationType::Info,
"All changes saved",
"Your workspace is up to date.",
),
(
"Success",
NotificationType::Success,
"Payment received",
"Thanks, your invoice is paid.",
),
(
"Warning",
NotificationType::Warning,
"Connection unstable",
"Changes may take longer to sync.",
),
(
"Error",
NotificationType::Error,
"Request failed",
"Check your connection and try again.",
),
] {
if ui.add(Button::new(label)).clicked() {
Notifications::push(
ui.ctx(),
Notification::new()
.with_type(kind)
.title(title)
.message(message),
);
}
}
Unique
if ui.add(Button::new("Unique Notification")).clicked() {
// Pushing the same id again replaces it instead of stacking.
Notifications::push(
ui.ctx(),
Notification::info("This is a unique notification.")
.id(NotificationId::new("unique")),
);
}
Action
if ui.add(Button::new("Notification with Action")).clicked() {
Notifications::push(
ui.ctx(),
Notification::new()
.id(retry)
.title("Uh oh! Something went wrong.")
.message("There was a problem with your request.")
.action("Retry"),
);
}
Lifecycle
if ui.add(Button::new("Click vs Close")).clicked() {
Notifications::push(
ui.ctx(),
Notification::info("Click the body to report a click; the cross closes it.")
.id(lifecycle)
.title("Click vs close")
.autohide(false)
.clickable(true),
);
}
Placement per notification
for placement in [
NotificationPlacement::TopLeft,
NotificationPlacement::TopCenter,
NotificationPlacement::TopRight,
NotificationPlacement::BottomLeft,
NotificationPlacement::BottomCenter,
NotificationPlacement::BottomRight,
] {
if ui.add(Button::new(format!("{placement:?}"))).clicked() {
Notifications::push(
ui.ctx(),
Notification::info(format!("This notification is at {placement:?}."))
.placement(placement),
);
}
}
// Once per frame, after the rest of the UI.
let response = Notifications::show(&ctx);
if response.action_clicked(retry) {
state.retries += 1;
}
The story above is gallery/src/stories/notification.rs, running in your browser. Every snippet on this page is cut from it.