Skip to main content

uzor_core/widgets/overlay/
state.rs

1//! Overlay state adapter - Contract/Connector for overlay interaction state
2
3use std::collections::HashMap;
4use std::time::Instant;
5
6/// State adapter for overlay interaction
7pub trait OverlayState {
8    fn is_visible(&self, overlay_id: &str) -> bool;
9    fn position(&self, overlay_id: &str) -> (f64, f64);
10    fn opacity(&self, overlay_id: &str) -> f64;
11    fn hover_start_time(&self, overlay_id: &str) -> Option<Instant>;
12    fn set_visible(&mut self, overlay_id: &str, visible: bool);
13    fn set_position(&mut self, overlay_id: &str, pos: (f64, f64));
14    fn set_opacity(&mut self, overlay_id: &str, opacity: f64);
15    fn set_hover_start_time(&mut self, overlay_id: &str, time: Option<Instant>);
16}
17
18/// Simple implementation of OverlayState for prototyping
19#[derive(Clone, Debug, Default)]
20pub struct SimpleOverlayState {
21    pub visible: HashMap<String, bool>,
22    pub position: HashMap<String, (f64, f64)>,
23    pub opacity: HashMap<String, f64>,
24    pub hover_start: HashMap<String, Option<Instant>>,
25}
26
27impl SimpleOverlayState {
28    pub fn new() -> Self {
29        Self {
30            visible: HashMap::new(),
31            position: HashMap::new(),
32            opacity: HashMap::new(),
33            hover_start: HashMap::new(),
34        }
35    }
36}
37
38impl OverlayState for SimpleOverlayState {
39    fn is_visible(&self, overlay_id: &str) -> bool {
40        *self.visible.get(overlay_id).unwrap_or(&false)
41    }
42
43    fn position(&self, overlay_id: &str) -> (f64, f64) {
44        *self.position.get(overlay_id).unwrap_or(&(0.0, 0.0))
45    }
46
47    fn opacity(&self, overlay_id: &str) -> f64 {
48        *self.opacity.get(overlay_id).unwrap_or(&0.0)
49    }
50
51    fn hover_start_time(&self, overlay_id: &str) -> Option<Instant> {
52        self.hover_start.get(overlay_id).copied().flatten()
53    }
54
55    fn set_visible(&mut self, overlay_id: &str, visible: bool) {
56        self.visible.insert(overlay_id.to_string(), visible);
57    }
58
59    fn set_position(&mut self, overlay_id: &str, pos: (f64, f64)) {
60        self.position.insert(overlay_id.to_string(), pos);
61    }
62
63    fn set_opacity(&mut self, overlay_id: &str, opacity: f64) {
64        self.opacity.insert(overlay_id.to_string(), opacity);
65    }
66
67    fn set_hover_start_time(&mut self, overlay_id: &str, time: Option<Instant>) {
68        self.hover_start.insert(overlay_id.to_string(), time);
69    }
70}