orbtk_theming/
selector.rs

1use std::fmt;
2
3/// The selector is used to read a property value from the `Theme`.
4#[derive(Debug, Clone, Default, PartialEq)]
5pub struct Selector {
6    /// Represents the key of a style.
7    pub style: Option<String>,
8
9    /// Used to reference the state property list of the given style.
10    pub state: Option<String>,
11
12    /// Check if the selector is dirty.
13    dirty: bool,
14}
15
16impl Selector {
17    /// Creates a new selector with the given style key.
18    pub fn new(style: impl Into<String>) -> Self {
19        Selector {
20            style: Some(style.into()),
21            state: None,
22            dirty: true,
23        }
24    }
25
26    /// Set the current state of the selector.
27    pub fn set_state(&mut self, state: impl Into<String>) {
28        self.state = Some(state.into());
29        self.dirty = true;
30    }
31
32    /// Clears the current state and reset to default.
33    pub fn clear_state(&mut self) {
34        self.state = None;
35        self.dirty = true;
36    }
37
38    /// Gets the dirty flag.
39    pub fn dirty(&self) -> bool {
40        self.dirty
41    }
42
43    /// Sets the dirty flag.
44    pub fn set_dirty(&mut self, dirty: bool) {
45        self.dirty = dirty;
46    }
47
48    /// Check if the selector has the given state.
49    pub fn has_state(&self, state: &str) -> bool {
50        if let Some(st) = &self.state {
51            if st.as_str() == state {
52                return true;
53            }
54        }
55
56        false
57    }
58}
59
60impl fmt::Display for Selector {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        if let Some(style) = &self.style {
63            return write!(f, "Selector ( style: {} )", style);
64        }
65        write!(f, "Selector ( empty )")
66    }
67}
68
69impl From<&str> for Selector {
70    fn from(s: &str) -> Self {
71        Selector::new(s)
72    }
73}
74
75impl From<String> for Selector {
76    fn from(s: String) -> Self {
77        Selector::new(s)
78    }
79}