egui_charts/ui_kit/notifications/
toasts.rs1use super::toast::Toast;
7
8pub(crate) fn current_time_seconds() -> f64 {
14 web_time::SystemTime::now()
15 .duration_since(web_time::UNIX_EPOCH)
16 .map(|d| d.as_secs_f64())
17 .unwrap_or(0.0)
18}
19
20#[derive(Default)]
22pub struct Toasts {
23 toasts: Vec<Toast>,
24 pub(super) expanded: bool,
26}
27
28impl Toasts {
29 pub fn new() -> Self {
31 Self::default()
32 }
33
34 pub fn add(&mut self, mut toast: Toast) {
36 toast.created_at = current_time_seconds();
37 self.toasts.push(toast);
38 }
39
40 pub fn info(&mut self, message: impl Into<String>) {
42 self.add(Toast::info(message));
43 }
44
45 pub fn success(&mut self, message: impl Into<String>) {
47 self.add(Toast::success(message));
48 }
49
50 pub fn warning(&mut self, message: impl Into<String>) {
52 self.add(Toast::warning(message));
53 }
54
55 pub fn error(&mut self, message: impl Into<String>) {
57 self.add(Toast::error(message));
58 }
59
60 pub fn remove(&mut self, id: u64) {
62 self.toasts.retain(|t| t.id != id);
63 }
64
65 pub fn clear(&mut self) {
67 self.toasts.clear();
68 self.expanded = false;
69 }
70
71 pub fn toasts(&self) -> &[Toast] {
73 &self.toasts
74 }
75
76 pub fn toasts_mut(&mut self) -> &mut Vec<Toast> {
78 &mut self.toasts
79 }
80
81 pub fn cleanup_expired(&mut self) {
83 let current_time = current_time_seconds();
84 self.toasts.retain(|t| !t.is_expired(current_time));
85 }
86
87 pub fn len(&self) -> usize {
89 self.toasts.len()
90 }
91
92 pub fn is_empty(&self) -> bool {
94 self.toasts.is_empty()
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn cleanup_keeps_permanent_and_fresh_toasts() {
104 let mut toasts = Toasts::new();
105 toasts.add(Toast::info("permanent").permanent());
106 toasts.add(Toast::info("fresh").duration(60.0));
107 toasts.cleanup_expired();
108 assert_eq!(toasts.len(), 2);
109 }
110}