ui/
notifications.rs

1//! A thin wrapper around the `egui-notify` crate to integrate it into Bevy's ECS.
2//!
3//! An additional benefit of this wrapper is we can switch to a different implementation later.
4
5use bevy::prelude::Resource;
6use egui::{Context, WidgetText};
7use egui_notify::{Toast, Toasts};
8
9/// Provides access to the notification system in the UI.
10#[derive(Resource, Default)]
11pub struct Notifications {
12    /// The underlyling handle to the `egui-notify` crate.
13    toasts: Toasts,
14}
15
16impl Notifications {
17    /// Renders the notifications in the UI.
18    pub fn ui(&mut self, ctx: &Context) {
19        self.toasts.show(ctx);
20    }
21
22    /// Renders an *info* level notification.
23    pub fn info(&mut self, text: impl Into<WidgetText>) {
24        let toast = Toast::info(text);
25
26        self.toasts.add(toast);
27    }
28
29    /// Renders an *warn* level notification.
30    pub fn warn(&mut self, text: impl Into<WidgetText>) {
31        let toast = Toast::warning(text);
32
33        self.toasts.add(toast);
34    }
35
36    /// Renders an *error* level notification.
37    pub fn error(&mut self, text: impl Into<WidgetText>) {
38        let toast = Toast::error(text);
39
40        self.toasts.add(toast);
41    }
42}