tuix_core/state/style/
rule.rs

1use std::fmt::write;
2
3use crate::entity::Entity;
4
5use super::Property;
6
7use crate::state::storage::dense_storage::DenseStorage;
8
9use crate::style::*;
10
11#[derive(Clone, Debug, PartialEq)]
12pub struct StyleRule {
13    pub selectors: Vec<Selector>,
14    pub properties: Vec<Property>,
15}
16
17// impl std::fmt::Display for StyleRule {
18//     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19//         for selector in self.selectors.iter() {
20//             write!(f, "{}", selector)?;
21//         }
22
23//         write!(f, " {{\n")?;
24
25//         for property in self.properties.iter() {
26//             write!(f, "    {}\n", property)?;
27//         }
28
29//         write!(f, "}}\n\n")?;
30
31//         Ok(())
32//     }
33// }
34
35impl StyleRule {
36    pub fn new() -> Self {
37        StyleRule {
38            selectors: Vec::new(),
39            properties: Vec::new(),
40        }
41    }
42
43    pub fn selector(mut self, selector: Selector) -> Self {
44        self.selectors.push(selector);
45
46        self
47    }
48
49    pub fn parent_selector(mut self, mut selector: Selector) -> Self {
50        selector.relation = Relation::Parent;
51        self.selectors.push(selector);
52
53        self
54    }
55
56    pub fn property(mut self, property: Property) -> Self {
57        self.properties.push(property);
58
59        self
60    }
61
62    pub fn specificity(&self) -> Specificity {
63        let mut specificity = Specificity([0, 0, 0]);
64        for selector in self.selectors.iter() {
65            specificity += selector.specificity();
66        }
67
68        return specificity;
69    }
70
71    // Property Setters
72
73    pub fn set_display(mut self, value: Display) -> Self {
74        self.properties.push(Property::Display(value));
75
76        self
77    }
78
79    pub fn set_visibility(mut self, value: Visibility) -> Self {
80        self.properties.push(Property::Visibility(value));
81
82        self
83    }
84
85    pub fn set_overflow(mut self, value: Overflow) -> Self {
86        self.properties.push(Property::Overflow(value));
87
88        self
89    }
90
91    // Background
92    pub fn set_background_color(mut self, value: Color) -> Self {
93        self.properties.push(Property::BackgroundColor(value));
94
95        self
96    }
97
98    pub fn set_background_gradient(mut self, value: LinearGradient) -> Self {
99        self.properties.push(Property::BackgroundGradient(value));
100
101        self
102    }
103
104    // Outer Shadow
105    pub fn set_outer_shadow_h_offset(mut self, value: Units) -> Self {
106        let mut box_shadow = BoxShadow::default();
107        box_shadow.horizontal_offset = value;
108
109        self.properties.push(Property::OuterShadow(box_shadow));
110
111        self
112    }
113
114    pub fn set_outer_shadow_v_offset(mut self, value: Units) -> Self {
115        let mut box_shadow = BoxShadow::default();
116        box_shadow.vertical_offset = value;
117
118        self.properties.push(Property::OuterShadow(box_shadow));
119
120        self
121    }
122
123    pub fn set_outer_shadow_color(mut self, value: Color) -> Self {
124        let mut box_shadow = BoxShadow::default();
125        box_shadow.color = value;
126
127        self.properties.push(Property::OuterShadow(box_shadow));
128
129        self
130    }
131
132    pub fn set_outer_shadow_blur(mut self, value: Units) -> Self {
133        let mut box_shadow = BoxShadow::default();
134        box_shadow.blur_radius = value;
135
136        self.properties.push(Property::OuterShadow(box_shadow));
137
138        self
139    }
140
141    // Inner Shadow
142    pub fn set_inner_shadow_h_offset(mut self, value: Units) -> Self {
143        let mut box_shadow = BoxShadow::default();
144        box_shadow.horizontal_offset = value;
145
146        self.properties.push(Property::InnerShadow(box_shadow));
147
148        self
149    }
150
151    pub fn set_inner_shadow_v_offset(mut self, value: Units) -> Self {
152        let mut box_shadow = BoxShadow::default();
153        box_shadow.vertical_offset = value;
154
155        self.properties.push(Property::InnerShadow(box_shadow));
156
157        self
158    }
159
160    pub fn set_inner_shadow_color(mut self, value: Color) -> Self {
161        let mut box_shadow = BoxShadow::default();
162        box_shadow.color = value;
163
164        self.properties.push(Property::InnerShadow(box_shadow));
165
166        self
167    }
168
169    pub fn set_inner_shadow_blur(mut self, value: Units) -> Self {
170        let mut box_shadow = BoxShadow::default();
171        box_shadow.blur_radius = value;
172
173        self.properties.push(Property::InnerShadow(box_shadow));
174
175        self
176    }
177
178    // Positioning
179
180    pub fn set_space(mut self, value: Units) -> Self {
181        self.properties.push(Property::Space(value));
182
183        self
184    }
185
186    pub fn set_left(mut self, value: Units) -> Self {
187        self.properties.push(Property::Left(value));
188
189        self
190    }
191
192    pub fn set_right(mut self, value: Units) -> Self {
193        self.properties.push(Property::Right(value));
194
195        self
196    }
197
198    pub fn set_top(mut self, value: Units) -> Self {
199        self.properties.push(Property::Top(value));
200        self
201    }
202
203    pub fn set_bottom(mut self, value: Units) -> Self {
204        self.properties.push(Property::Bottom(value));
205        self
206    }
207
208    // Alignment and Justification
209
210    // pub fn set_justification(mut self, val: Justification) -> Self {
211    //     self.state.style.justification.set(self.entity, val);
212    //     self
213    // }
214
215    // pub fn set_alignment(mut self, val: Alignment) -> Self {
216    //     self.state.style.alignment.set(self.entity, val);
217    //     self
218    // }
219
220    // Size
221
222    pub fn set_width(mut self, value: Units) -> Self {
223        self.properties.push(Property::Width(value));
224
225        self
226    }
227
228    pub fn set_height(mut self, value: Units) -> Self {
229        self.properties.push(Property::Height(value));
230
231        self
232    }
233
234    // Size Constraints
235
236    pub fn set_min_width(mut self, value: Units) -> Self {
237        self.properties.push(Property::MinHeight(value));
238
239        self
240    }
241
242    pub fn set_max_width(mut self, value: Units) -> Self {
243        self.properties.push(Property::MaxWidth(value));
244
245        self
246    }
247
248    pub fn set_min_height(mut self, value: Units) -> Self {
249        self.properties.push(Property::MinHeight(value));
250
251        self
252    }
253
254    pub fn set_max_height(mut self, value: Units) -> Self {
255        self.properties.push(Property::MaxHeight(value));
256
257        self
258    }
259
260    // Child Spacing
261    pub fn set_child_space(mut self, value: Units) -> Self {
262        self.properties.push(Property::ChildSpace(value));
263
264        self
265    }
266
267    pub fn set_child_left(mut self, value: Units) -> Self {
268        self.properties.push(Property::ChildLeft(value));
269
270        self
271    }
272
273    pub fn set_child_right(mut self, value: Units) -> Self {
274        self.properties.push(Property::ChildRight(value));
275
276        self
277    }
278
279    pub fn set_child_top(mut self, value: Units) -> Self {
280        self.properties.push(Property::ChildTop(value));
281
282        self
283    }
284
285    pub fn set_child_bottom(mut self, value: Units) -> Self {
286        self.properties.push(Property::ChildBottom(value));
287
288        self
289    }
290
291    // Border
292
293    pub fn set_border_color(mut self, value: Color) -> Self {
294        self.properties.push(Property::BorderColor(value));
295
296        self
297    }
298
299    pub fn set_border_width(mut self, value: Units) -> Self {
300        self.properties.push(Property::BorderWidth(value));
301
302        self
303    }
304
305    pub fn set_border_radius(mut self, value: Units) -> Self {
306        self.properties.push(Property::BorderTopLeftRadius(value));
307
308        self
309    }
310
311    pub fn set_border_radius_top_left(mut self, value: Units) -> Self {
312        self.properties.push(Property::BorderTopLeftRadius(value));
313
314        self
315    }
316
317    pub fn set_border_radius_top_right(mut self, value: Units) -> Self {
318        self.properties.push(Property::BorderTopRightRadius(value));
319
320        self
321    }
322
323    pub fn set_border_radius_bottom_left(mut self, value: Units) -> Self {
324        self.properties
325            .push(Property::BorderBottomLeftRadius(value));
326
327        self
328    }
329
330    pub fn set_border_radius_bottom_right(mut self, value: Units) -> Self {
331        self.properties
332            .push(Property::BorderBottomRightRadius(value));
333
334        self
335    }
336
337    pub fn set_color(mut self, value: Color) -> Self {
338        self.properties.push(Property::FontColor(value));
339
340        self
341    }
342
343    pub fn set_font_size(mut self, value: f32) -> Self {
344        self.properties.push(Property::FontSize(value));
345
346        self
347    }
348}