75 lines
1.8 KiB
Rust
75 lines
1.8 KiB
Rust
use crate::prelude::*;
|
|
|
|
#[derive(Resource)]
|
|
pub struct Notifications {
|
|
items: Vec<(Notification, NotificationLevel)>,
|
|
}
|
|
|
|
impl Default for Notifications {
|
|
fn default() -> Self {
|
|
Self { items: Vec::new() }
|
|
}
|
|
}
|
|
|
|
impl Notifications {
|
|
fn new(
|
|
&mut self,
|
|
level: NotificationLevel,
|
|
title: Option<impl Into<String>>,
|
|
message: impl Into<String>,
|
|
) {
|
|
self.items.push((
|
|
Notification {
|
|
title: title.map(|s| s.into()),
|
|
message: message.into(),
|
|
},
|
|
level,
|
|
));
|
|
}
|
|
|
|
pub fn drain(&mut self) -> std::vec::Drain<'_, (Notification, NotificationLevel)> {
|
|
self.items.drain(..)
|
|
}
|
|
|
|
pub fn info(&mut self, title: Option<impl Into<String>>, message: impl Into<String>) {
|
|
self.new(NotificationLevel::Info, title, message);
|
|
}
|
|
|
|
pub fn warn(&mut self, title: Option<impl Into<String>>, message: impl Into<String>) {
|
|
self.new(NotificationLevel::Warning, title, message);
|
|
}
|
|
|
|
pub fn error(&mut self, title: Option<impl Into<String>>, message: impl Into<String>) {
|
|
self.new(NotificationLevel::Error, title, message);
|
|
}
|
|
}
|
|
|
|
#[derive(Component)]
|
|
pub struct NotificationContainer;
|
|
|
|
#[derive(Component)]
|
|
pub struct NotificationLifetime(pub Timer);
|
|
|
|
#[derive(Component)]
|
|
pub struct Notification {
|
|
pub title: Option<String>,
|
|
pub message: String,
|
|
}
|
|
|
|
#[derive(Component)]
|
|
pub enum NotificationLevel {
|
|
Info,
|
|
Warning,
|
|
Error,
|
|
}
|
|
|
|
impl NotificationLevel {
|
|
pub fn bg_color(&self) -> Color {
|
|
match self {
|
|
NotificationLevel::Info => Color::srgba(0.0, 0.0, 0.0, 0.7),
|
|
NotificationLevel::Warning => Color::srgba(1.0, 1.0, 0.0, 0.7),
|
|
NotificationLevel::Error => Color::srgba(1.0, 0.0, 0.0, 0.7),
|
|
}
|
|
}
|
|
}
|