Expand description
This crate provides a convenient interface for showing toast notifications with the egui library.
For a complete example, see https://github.com/urholaukkarinen/egui-toast/tree/main/demo.
§Usage
To get started, create a Toasts instance in your rendering code and specify the anchor position and
direction for the notifications. Toast notifications will show up starting from the specified
anchor position and stack up in the specified direction.
use egui::Align2;
let mut toasts = Toasts::new()
.anchor(Align2::LEFT_TOP, (10.0, 10.0))
.direction(egui::Direction::TopDown);
toasts.add(Toast {
text: "Hello, World".into(),
kind: ToastKind::Info,
options: ToastOptions::default()
.duration_in_seconds(3.0)
.show_progress(true)
.show_icon(true),
..Default::default()
});
// Show all toasts
toasts.show(ctx);§Layer Ordering
By default, toasts are shown in the egui::Order::Foreground layer. To ensure toasts
appear above modals or other foreground elements, you can use egui::Order::Tooltip:
let mut toasts = Toasts::new()
.anchor(Align2::RIGHT_TOP, (-10.0, 10.0))
.order(Order::Tooltip); // Ensures toasts appear above modalsLook of the notifications can be fully customized by specifying a custom rendering function for a specific toast kind
with Toasts::custom_contents. ToastKind::Custom can be used if the default kinds are not sufficient.
const MY_CUSTOM_TOAST: u32 = 0;
fn custom_toast_contents(ui: &mut egui::Ui, toast: &mut Toast) -> egui::Response {
egui::Frame::window(ui.style()).show(ui, |ui| {
ui.label(toast.text.clone());
}).response
}
let mut toasts = Toasts::new()
.custom_contents(MY_CUSTOM_TOAST, custom_toast_contents);
// Add a custom toast that never expires
toasts.add(Toast {
text: "Hello, World".into(),
kind: ToastKind::Custom(MY_CUSTOM_TOAST),
options: ToastOptions::default(),
..Default::default()
});