maycoon_theme/
style.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use dashmap::DashMap;
use peniko::{Brush, Color, Gradient};

/// Styling map for defining widget appearance.
#[derive(Clone, Debug)]
pub struct Style {
    map: DashMap<String, StyleVal>,
}

impl Style {
    /// Create a new empty style.
    pub fn new() -> Self {
        Self {
            map: DashMap::with_capacity(16),
        }
    }

    /// Create a style from an array of strings and style values.
    pub fn from_values(values: impl IntoIterator<Item = (String, StyleVal)>) -> Self {
        Self {
            map: DashMap::from_iter(values),
        }
    }

    /// Removes the style value from the map with the give name.
    pub fn remove(&mut self, name: impl ToString) {
        self.map.remove(&name.to_string());
    }

    /// Insert a style value with the given name into the style map.
    pub fn with_value(self, name: impl ToString, value: StyleVal) -> Self {
        self.map.insert(name.to_string(), value);
        self
    }

    /// Set a style value by name.
    pub fn set(&mut self, name: impl ToString, value: StyleVal) {
        self.map.insert(name.to_string(), value);
    }

    /// Set a color style value by name.
    pub fn set_color(&mut self, name: impl ToString, color: Color) {
        self.map.insert(name.to_string(), StyleVal::Color(color));
    }

    /// Set a gradient style value by name.
    pub fn set_gradient(&mut self, name: impl ToString, gradient: Gradient) {
        self.map
            .insert(name.to_string(), StyleVal::Gradient(gradient));
    }

    /// Set a bool style value by name.
    pub fn set_bool(&mut self, name: impl ToString, value: bool) {
        self.map.insert(name.to_string(), StyleVal::Bool(value));
    }

    /// Set a brush style value by name.
    pub fn set_brush(&mut self, name: impl ToString, brush: Brush) {
        self.map.insert(name.to_string(), StyleVal::Brush(brush));
    }

    /// Set a float style value by name.
    pub fn set_float(&mut self, name: impl ToString, value: f32) {
        self.map.insert(name.to_string(), StyleVal::Float(value));
    }

    /// Set an int style value by name.
    pub fn set_int(&mut self, name: impl ToString, value: i32) {
        self.map.insert(name.to_string(), StyleVal::Int(value));
    }

    /// Set an unsized int style value by name.
    pub fn set_uint(&mut self, name: impl ToString, value: u32) {
        self.map.insert(name.to_string(), StyleVal::UInt(value));
    }

    /// Get a style value by name. Returns [None] if the value name does not exist.
    pub fn get(&self, name: impl ToString) -> Option<StyleVal> {
        self.map.get(&name.to_string()).map(|val| val.clone())
    }

    /// Get a color style value by name. Returns [None] if the value name does not exist.
    pub fn get_color(&self, name: impl ToString) -> Option<Color> {
        if let Some(val) = self.map.get(&name.to_string()) {
            match val.value() {
                StyleVal::Color(color) => Some(*color),
                _ => None,
            }
        } else {
            None
        }
    }

    /// Get a gradient style value by name. Returns [None] if the value name does not exist.
    pub fn get_gradient(&self, name: impl ToString) -> Option<Gradient> {
        if let Some(val) = self.map.get(&name.to_string()) {
            match val.value() {
                StyleVal::Gradient(gradient) => Some(gradient.clone()),
                _ => None,
            }
        } else {
            None
        }
    }

    /// Get a brush style value by name. Returns [None] if the value name does not exist.
    pub fn get_brush(&self, name: impl ToString) -> Option<Brush> {
        if let Some(val) = self.map.get(&name.to_string()) {
            match val.value() {
                StyleVal::Brush(brush) => Some(brush.clone()),
                _ => None,
            }
        } else {
            None
        }
    }

    /// Get a float style value by name. Returns [None] if the value name does not exist.
    pub fn get_float(&self, name: impl ToString) -> Option<f32> {
        if let Some(val) = self.map.get(&name.to_string()) {
            match val.value() {
                StyleVal::Float(float) => Some(*float),
                _ => None,
            }
        } else {
            None
        }
    }

    /// Get an int style value by name. Returns [None] if the value name does not exist.
    pub fn get_int(&self, name: impl ToString) -> Option<i32> {
        if let Some(val) = self.map.get(&name.to_string()) {
            match val.value() {
                StyleVal::Int(int) => Some(*int),
                _ => None,
            }
        } else {
            None
        }
    }

    /// Get an unsized int style value by name. Returns [None] if the value name does not exist.
    pub fn get_uint(&self, name: impl ToString) -> Option<u32> {
        if let Some(val) = self.map.get(&name.to_string()) {
            match val.value() {
                StyleVal::UInt(uint) => Some(*uint),
                _ => None,
            }
        } else {
            None
        }
    }

    /// Get a bool style value by name. Returns [None] if the value name does not exist.
    pub fn get_bool(&self, name: impl ToString) -> Option<bool> {
        if let Some(val) = self.map.get(&name.to_string()) {
            match val.value() {
                StyleVal::Bool(bool) => Some(*bool),
                _ => None,
            }
        } else {
            None
        }
    }
}

impl Default for Style {
    fn default() -> Self {
        Self::new()
    }
}

/// Default widget styles.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct DefaultStyles {
    text: DefaultTextStyles,
    container: DefaultContainerStyles,
    interactive: DefaultInteractiveStyles,
}

impl DefaultStyles {
    /// Create new default styles with given styles.
    pub fn new(
        text: DefaultTextStyles,
        container: DefaultContainerStyles,
        interactive: DefaultInteractiveStyles,
    ) -> Self {
        Self {
            text,
            container,
            interactive,
        }
    }

    /// Get the default styles for text widgets.
    pub fn text(&self) -> &DefaultTextStyles {
        &self.text
    }

    /// Get the default styles for container widgets.
    pub fn container(&self) -> &DefaultContainerStyles {
        &self.container
    }

    /// Get the default styles for interactive widgets.
    pub fn interactive(&self) -> &DefaultInteractiveStyles {
        &self.interactive
    }
}

/// The default text widget styles.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct DefaultTextStyles {
    foreground: Color,
    background: Color,
}

impl DefaultTextStyles {
    /// Create new default text styles with given colors.
    pub fn new(foreground: Color, background: Color) -> Self {
        Self {
            foreground,
            background,
        }
    }

    /// Get the default foreground color.
    pub fn foreground(&self) -> Color {
        self.foreground
    }

    /// Get the default background color.
    pub fn background(&self) -> Color {
        self.background
    }
}

/// The default container widget styles.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct DefaultContainerStyles {
    foreground: Color,
    background: Color,
}

impl DefaultContainerStyles {
    /// Create new default container styles with given colors.
    pub fn new(foreground: Color, background: Color) -> Self {
        Self {
            foreground,
            background,
        }
    }

    /// Get the default foreground color.
    pub fn foreground(&self) -> Color {
        self.foreground
    }

    /// Get the default background color.
    pub fn background(&self) -> Color {
        self.background
    }
}

/// The default interactive widget styles.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)]
pub struct DefaultInteractiveStyles {
    active: Color,
    inactive: Color,
    hover: Color,
    disabled: Color,
}

impl DefaultInteractiveStyles {
    /// Create new default interactive styles with given colors.
    pub fn new(active: Color, inactive: Color, hover: Color, disabled: Color) -> Self {
        Self {
            active,
            inactive,
            hover,
            disabled,
        }
    }

    /// Get the default active widget color.
    pub fn active(&self) -> Color {
        self.active
    }

    /// Get the default inactive widget color.
    pub fn inactive(&self) -> Color {
        self.inactive
    }

    /// Get the default on-hover widget color.
    pub fn hover(&self) -> Color {
        self.hover
    }

    /// Get the default disabled widget color.
    pub fn disabled(&self) -> Color {
        self.disabled
    }
}

/// A style value.
#[derive(Clone, Debug)]
pub enum StyleVal {
    /// A color style value.
    Color(Color),
    /// A gradient style value.
    Gradient(Gradient),
    /// A brush style value.
    Brush(Brush),
    /// A float style value.
    Float(f32),
    /// An int style value.
    Int(i32),
    /// An unsized int style value.
    UInt(u32),
    /// A bool style value.
    Bool(bool),
}