Skip to main content

uzor_core/widgets/button/
state.rs

1//! Button state adapter - Contract/Connector for button interaction state
2//!
3//! **ButtonState is a CONTRACT/CONNECTOR trait** that connects:
4//! - Factory rendering functions (`factory/render.rs`)
5//! - External state management systems (app state, Redux, ECS, etc.)
6
7/// State adapter for button interaction
8///
9/// This trait defines the contract for tracking button interaction state.
10/// External projects implement this trait to integrate with their state management systems.
11pub trait ButtonState {
12    // =========================================================================
13    // Read State (Immutable)
14    // =========================================================================
15
16    /// Check if button is currently hovered
17    fn is_hovered(&self, button_id: &str) -> bool;
18
19    /// Check if button is currently pressed
20    fn is_pressed(&self, button_id: &str) -> bool;
21
22    /// Check if button is currently focused
23    fn is_focused(&self, button_id: &str) -> bool;
24
25    // =========================================================================
26    // Write State (Mutable)
27    // =========================================================================
28
29    /// Set button hover state
30    fn set_hovered(&mut self, button_id: Option<&str>);
31
32    /// Set button pressed state
33    fn set_pressed(&mut self, button_id: Option<&str>);
34
35    /// Set button focus state
36    fn set_focused(&mut self, button_id: Option<&str>);
37}
38
39// =============================================================================
40// Default State Implementation
41// =============================================================================
42
43/// Simple implementation of ButtonState for prototyping
44#[derive(Clone, Debug, Default)]
45pub struct SimpleButtonState {
46    /// Currently hovered button ID
47    pub hovered: Option<String>,
48
49    /// Currently pressed button ID
50    pub pressed: Option<String>,
51
52    /// Currently focused button ID
53    pub focused: Option<String>,
54}
55
56impl SimpleButtonState {
57    /// Create new button state
58    pub fn new() -> Self {
59        Self {
60            hovered: None,
61            pressed: None,
62            focused: None,
63        }
64    }
65}
66
67impl ButtonState for SimpleButtonState {
68    fn is_hovered(&self, button_id: &str) -> bool {
69        self.hovered.as_deref() == Some(button_id)
70    }
71
72    fn is_pressed(&self, button_id: &str) -> bool {
73        self.pressed.as_deref() == Some(button_id)
74    }
75
76    fn is_focused(&self, button_id: &str) -> bool {
77        self.focused.as_deref() == Some(button_id)
78    }
79
80    fn set_hovered(&mut self, button_id: Option<&str>) {
81        self.hovered = button_id.map(|s| s.to_string());
82    }
83
84    fn set_pressed(&mut self, button_id: Option<&str>) {
85        self.pressed = button_id.map(|s| s.to_string());
86    }
87
88    fn set_focused(&mut self, button_id: Option<&str>) {
89        self.focused = button_id.map(|s| s.to_string());
90    }
91}