Skip to main content

uzor_core/widgets/toast/
state.rs

1//! Toast state adapter - Contract/Connector for toast notification state
2
3use std::collections::HashMap;
4
5/// State adapter for toast notifications
6pub trait ToastState {
7    fn is_visible(&self, toast_id: &str) -> bool;
8    fn opacity(&self, toast_id: &str) -> f64;
9    fn remaining_duration_ms(&self, toast_id: &str) -> u32;
10    fn vertical_offset(&self, toast_id: &str) -> f64;
11    fn is_hovered(&self, toast_id: &str) -> bool;
12    fn set_visible(&mut self, toast_id: &str, visible: bool);
13    fn set_opacity(&mut self, toast_id: &str, opacity: f64);
14    fn set_remaining_duration_ms(&mut self, toast_id: &str, ms: u32);
15    fn set_vertical_offset(&mut self, toast_id: &str, offset: f64);
16    fn set_hovered(&mut self, toast_id: &str, hovered: bool);
17}
18
19/// Simple implementation of ToastState for prototyping
20#[derive(Clone, Debug, Default)]
21pub struct SimpleToastState {
22    pub visibility: HashMap<String, bool>,
23    pub opacity: HashMap<String, f64>,
24    pub remaining_duration_ms: HashMap<String, u32>,
25    pub vertical_offset: HashMap<String, f64>,
26    pub hovered: HashMap<String, bool>,
27}
28
29impl SimpleToastState {
30    pub fn new() -> Self {
31        Self {
32            visibility: HashMap::new(),
33            opacity: HashMap::new(),
34            remaining_duration_ms: HashMap::new(),
35            vertical_offset: HashMap::new(),
36            hovered: HashMap::new(),
37        }
38    }
39
40    pub fn remove_toast(&mut self, toast_id: &str) {
41        self.visibility.remove(toast_id);
42        self.opacity.remove(toast_id);
43        self.remaining_duration_ms.remove(toast_id);
44        self.vertical_offset.remove(toast_id);
45        self.hovered.remove(toast_id);
46    }
47}
48
49impl ToastState for SimpleToastState {
50    fn is_visible(&self, toast_id: &str) -> bool {
51        self.visibility.get(toast_id).copied().unwrap_or(false)
52    }
53
54    fn opacity(&self, toast_id: &str) -> f64 {
55        self.opacity.get(toast_id).copied().unwrap_or(0.0)
56    }
57
58    fn remaining_duration_ms(&self, toast_id: &str) -> u32 {
59        self.remaining_duration_ms.get(toast_id).copied().unwrap_or(0)
60    }
61
62    fn vertical_offset(&self, toast_id: &str) -> f64 {
63        self.vertical_offset.get(toast_id).copied().unwrap_or(0.0)
64    }
65
66    fn is_hovered(&self, toast_id: &str) -> bool {
67        self.hovered.get(toast_id).copied().unwrap_or(false)
68    }
69
70    fn set_visible(&mut self, toast_id: &str, visible: bool) {
71        self.visibility.insert(toast_id.to_string(), visible);
72    }
73
74    fn set_opacity(&mut self, toast_id: &str, opacity: f64) {
75        self.opacity.insert(toast_id.to_string(), opacity);
76    }
77
78    fn set_remaining_duration_ms(&mut self, toast_id: &str, ms: u32) {
79        self.remaining_duration_ms.insert(toast_id.to_string(), ms);
80    }
81
82    fn set_vertical_offset(&mut self, toast_id: &str, offset: f64) {
83        self.vertical_offset.insert(toast_id.to_string(), offset);
84    }
85
86    fn set_hovered(&mut self, toast_id: &str, hovered: bool) {
87        self.hovered.insert(toast_id.to_string(), hovered);
88    }
89}