1use serde::{Deserialize, Serialize};
2
3use crate::boxstyle::{Radius, Shadow};
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
6#[serde(default, rename_all = "camelCase")]
7pub struct Size {
8 pub width: f32,
9 pub height: f32,
10}
11
12impl Size {
13 pub const fn new(width: f32, height: f32) -> Self {
17 Self { width, height }
18 }
19
20 pub fn width(self, value: f32) -> Self {
21 Self {
22 width: value,
23 ..self
24 }
25 }
26
27 pub fn height(self, value: f32) -> Self {
28 Self {
29 width: self.width,
30 height: value,
31 }
32 }
33}
34
35#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
36#[serde(default, rename_all = "camelCase")]
37pub struct Rect {
38 pub x: f32,
39 pub y: f32,
40 pub width: f32,
41 pub height: f32,
42}
43
44impl Rect {
45 pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
46 Self {
47 x,
48 y,
49 width,
50 height,
51 }
52 }
53
54 pub fn right(self) -> f32 {
55 self.x + self.width
56 }
57
58 pub fn bottom(self) -> f32 {
59 self.y + self.height
60 }
61
62 pub fn contains(self, x: f32, y: f32) -> bool {
63 x >= self.x && x < self.right() && y >= self.y && y < self.bottom()
64 }
65}
66
67#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
68#[serde(default, rename_all = "camelCase")]
69pub struct Insets {
70 pub left: f32,
71 pub top: f32,
72 pub right: f32,
73 pub bottom: f32,
74}
75
76impl Insets {
77 pub const fn all(value: f32) -> Self {
78 Self {
79 left: value,
80 top: value,
81 right: value,
82 bottom: value,
83 }
84 }
85
86 pub const fn symmetric(horizontal: f32, vertical: f32) -> Self {
87 Self {
88 left: horizontal,
89 top: vertical,
90 right: horizontal,
91 bottom: vertical,
92 }
93 }
94}
95
96#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
97#[serde(default, rename_all = "camelCase")]
98pub struct Color {
99 pub r: f32,
100 pub g: f32,
101 pub b: f32,
102 pub a: f32,
103}
104
105impl Color {
106 pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
107 Self { r, g, b, a }
108 }
109
110 pub fn hex(value: u32) -> Self {
111 Self::rgba(
112 ((value >> 16) & 0xff) as f32 / 255.0,
113 ((value >> 8) & 0xff) as f32 / 255.0,
114 (value & 0xff) as f32 / 255.0,
115 1.0,
116 )
117 }
118
119 pub const TRANSPARENT: Self = Self {
120 r: 0.0,
121 g: 0.0,
122 b: 0.0,
123 a: 0.0,
124 };
125}
126
127impl Default for Color {
129 fn default() -> Self {
130 Self::rgba(0.0, 0.0, 0.0, 1.0)
131 }
132}
133
134#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
135#[serde(rename_all = "camelCase")]
136pub enum Axis {
137 #[default]
138 Column,
139 Row,
140}
141
142#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
143#[serde(rename_all = "camelCase")]
144pub enum Align {
145 #[default]
146 Stretch,
147 Start,
148 Center,
149 End,
150}
151
152#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
153#[serde(rename_all = "camelCase")]
154pub enum Justify {
155 #[default]
156 Start,
157 Center,
158 End,
159 SpaceBetween,
160}
161
162#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
163#[serde(default, rename_all = "camelCase")]
164pub struct Style {
165 pub size: Option<Size>,
166 pub min_size: Option<Size>,
167 pub max_size: Option<Size>,
168 pub padding: Insets,
169 pub gap: f32,
170 pub flex_grow: f32,
171 pub flex_shrink: f32,
172 pub align: Align,
173 pub justify: Justify,
174 pub background: Option<Color>,
175 pub clip: bool,
176 pub fill: bool,
179 pub row_height: f32,
180 pub line_height: Option<f32>,
183 pub font_size: f32,
185 pub color: Option<Color>,
186 pub weight: f32,
187 pub radius: Radius,
190 pub border_width: f32,
192 pub border_color: Option<Color>,
193 pub shadow: Option<Shadow>,
195}
196
197impl Default for Style {
198 fn default() -> Self {
199 Self {
200 size: None,
201 min_size: None,
202 max_size: None,
203 padding: Insets::default(),
204 gap: 0.0,
205 flex_grow: 0.0,
206 flex_shrink: 1.0,
207 align: Align::Stretch,
208 justify: Justify::Start,
209 background: None,
210 clip: false,
211 fill: false,
212 row_height: 24.0,
213 line_height: None,
214 font_size: 14.0,
215 color: None,
216 weight: 400.0,
217 radius: Radius::default(),
218 border_width: 0.0,
219 border_color: None,
220 shadow: None,
221 }
222 }
223}
224
225impl Style {
226 pub fn size(mut self, size: Size) -> Self {
227 self.size = Some(size);
228 self
229 }
230
231 pub fn width(mut self, value: f32) -> Self {
232 let size = self.size.unwrap_or_default();
233 self.size = Some(size.width(value));
234 self
235 }
236
237 pub fn height(mut self, value: f32) -> Self {
238 let size = self.size.unwrap_or_default();
239 self.size = Some(size.height(value));
240 self
241 }
242
243 pub fn min_size(mut self, size: Size) -> Self {
244 self.min_size = Some(size);
245 self
246 }
247
248 pub fn max_size(mut self, size: Size) -> Self {
249 self.max_size = Some(size);
250 self
251 }
252
253 pub fn padding(mut self, value: Insets) -> Self {
254 self.padding = value;
255 self
256 }
257
258 pub fn gap(mut self, value: f32) -> Self {
259 self.gap = value;
260 self
261 }
262
263 pub fn flex_grow(mut self, value: f32) -> Self {
264 self.flex_grow = value.max(0.0);
265 self
266 }
267
268 pub fn align(mut self, value: Align) -> Self {
269 self.align = value;
270 self
271 }
272
273 pub fn justify(mut self, value: Justify) -> Self {
274 self.justify = value;
275 self
276 }
277
278 pub fn background(mut self, value: Color) -> Self {
279 self.background = Some(value);
280 self
281 }
282
283 pub fn clip(mut self, value: bool) -> Self {
284 self.clip = value;
285 self
286 }
287
288 pub fn fill(mut self, value: bool) -> Self {
289 self.fill = value;
290 self
291 }
292
293 pub fn row_height(mut self, value: f32) -> Self {
294 self.row_height = value.max(1.0);
295 self
296 }
297
298 pub fn line_height(mut self, value: f32) -> Self {
300 self.line_height = Some(value.max(1.0));
301 self
302 }
303
304 pub fn font_size(mut self, value: f32) -> Self {
305 self.font_size = value.max(1.0);
306 self
307 }
308
309 pub fn resolved_line_height(&self, font_line_height: f32) -> f32 {
311 self.line_height
312 .unwrap_or_else(|| font_line_height.max(1.0))
313 }
314
315 pub fn color(mut self, value: Color) -> Self {
316 self.color = Some(value);
317 self
318 }
319
320 pub fn weight(mut self, value: f32) -> Self {
321 self.weight = value;
322 self
323 }
324
325 pub fn radius(mut self, value: f32) -> Self {
328 self.radius = Radius {
329 sm: value,
330 md: value,
331 lg: value,
332 };
333 self
334 }
335
336 pub fn radius_sized(mut self, radius: Radius) -> Self {
339 self.radius = radius;
340 self
341 }
342
343 pub fn border(mut self, width: f32, color: Color) -> Self {
346 self.border_width = width.max(0.0);
347 self.border_color = Some(color);
348 self
349 }
350
351 pub fn shadow(mut self, shadow: Shadow) -> Self {
352 self.shadow = Some(shadow);
353 self
354 }
355
356 pub fn corner_radius(&self, width: f32, height: f32) -> f32 {
359 self.radius
360 .standard()
361 .max(0.0)
362 .min((width.min(height)) / 2.0)
363 }
364
365 pub fn border_px(&self) -> f32 {
367 self.border_width.max(0.0)
368 }
369
370 pub fn shadow_reach(&self) -> f32 {
373 self.shadow.map(|shadow| shadow.reach()).unwrap_or(0.0)
374 }
375
376 pub fn main_size(&self, axis: Axis) -> Option<f32> {
378 self.size.and_then(|size| {
379 let value = match axis {
380 Axis::Column => size.height,
381 Axis::Row => size.width,
382 };
383 (value > 0.0).then_some(value)
384 })
385 }
386
387 pub fn cross_size(&self, axis: Axis) -> Option<f32> {
389 self.size.and_then(|size| {
390 let value = match axis {
391 Axis::Column => size.width,
392 Axis::Row => size.height,
393 };
394 (value > 0.0).then_some(value)
395 })
396 }
397}
398
399#[cfg(test)]
400mod tests {
401 use super::*;
402
403 #[test]
404 fn a_box_defaults_to_a_flat_rectangle() {
405 let style = Style::default();
406 assert_eq!(style.radius, Radius::default());
407 assert_eq!(style.border_width, 0.0);
408 assert!(style.border_color.is_none());
409 assert!(style.shadow.is_none());
410 assert_eq!(style.corner_radius(100.0, 40.0), 0.0);
411 assert_eq!(style.border_px(), 0.0);
412 assert_eq!(style.shadow_reach(), 0.0);
413 }
414
415 #[test]
416 fn a_border_sets_its_width_and_colour_together() {
417 let style = Style::default().border(1.0, Color::rgba(0.2, 0.2, 0.2, 1.0));
418 assert_eq!(style.border_px(), 1.0);
419 assert_eq!(style.border_color, Some(Color::rgba(0.2, 0.2, 0.2, 1.0)));
420 }
421
422 #[test]
423 fn a_negative_border_is_clamped_to_nothing() {
424 let style = Style::default().border(-3.0, Color::rgba(1.0, 1.0, 1.0, 1.0));
425 assert_eq!(style.border_px(), 0.0);
426 }
427
428 #[test]
429 fn a_radius_rounds_every_corner_the_same_way() {
430 let style = Style::default().radius(6.0);
431 assert_eq!(style.corner_radius(100.0, 40.0), 6.0);
432 }
433
434 #[test]
435 fn a_radius_wider_than_the_box_is_halved_rather_than_inverted() {
436 let style = Style::default().radius(40.0);
439 assert_eq!(style.corner_radius(20.0, 20.0), 10.0);
440 }
441
442 #[test]
443 fn separate_radius_sizes_are_kept() {
444 let style = Style::default().radius_sized(Radius {
445 sm: 2.0,
446 md: 6.0,
447 lg: 10.0,
448 });
449 assert_eq!(
450 style.corner_radius(100.0, 40.0),
451 6.0,
452 "a box uses md by default"
453 );
454 assert_eq!(style.radius.lg, 10.0);
455 }
456
457 #[test]
458 fn a_shadow_reports_how_much_room_it_needs() {
459 let style = Style::default().shadow(Shadow {
460 offset_y: 2.0,
461 blur: 8.0,
462 color: Color::rgba(0.0, 0.0, 0.0, 0.4),
463 ..Shadow::default()
464 });
465 assert_eq!(style.shadow_reach(), 10.0);
466 }
467
468 #[test]
469 fn a_box_decodes_a_border_and_a_radius_from_json() {
470 let style: Style = serde_json::from_str(
471 r#"{"radius":6,"borderWidth":1,"borderColor":{"r":1,"g":0,"b":0,"a":1}}"#,
472 )
473 .expect("decodes");
474 assert_eq!(style.corner_radius(100.0, 40.0), 6.0);
475 assert_eq!(style.border_px(), 1.0);
476 assert_eq!(style.border_color.map(|c| c.r), Some(1.0));
477 }
478
479 #[test]
480 fn a_box_omitting_the_new_keys_still_decodes() {
481 let style: Style = serde_json::from_str(r#"{"fontSize":13}"#).expect("decodes");
482 assert_eq!(style.font_size, 13.0);
483 assert_eq!(style.radius, Radius::default());
484 }
485}