use serde::{Deserialize, Serialize};
use crate::boxstyle::{Radius, Shadow};
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Size {
pub width: f32,
pub height: f32,
}
impl Size {
pub const fn new(width: f32, height: f32) -> Self {
Self { width, height }
}
pub fn width(self, value: f32) -> Self {
Self {
width: value,
..self
}
}
pub fn height(self, value: f32) -> Self {
Self {
width: self.width,
height: value,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Rect {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl Rect {
pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Self {
x,
y,
width,
height,
}
}
pub fn right(self) -> f32 {
self.x + self.width
}
pub fn bottom(self) -> f32 {
self.y + self.height
}
pub fn contains(self, x: f32, y: f32) -> bool {
x >= self.x && x < self.right() && y >= self.y && y < self.bottom()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Insets {
pub left: f32,
pub top: f32,
pub right: f32,
pub bottom: f32,
}
impl Insets {
pub const fn all(value: f32) -> Self {
Self {
left: value,
top: value,
right: value,
bottom: value,
}
}
pub const fn symmetric(horizontal: f32, vertical: f32) -> Self {
Self {
left: horizontal,
top: vertical,
right: horizontal,
bottom: vertical,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl Color {
pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
pub fn hex(value: u32) -> Self {
Self::rgba(
((value >> 16) & 0xff) as f32 / 255.0,
((value >> 8) & 0xff) as f32 / 255.0,
(value & 0xff) as f32 / 255.0,
1.0,
)
}
pub const TRANSPARENT: Self = Self {
r: 0.0,
g: 0.0,
b: 0.0,
a: 0.0,
};
}
impl Default for Color {
fn default() -> Self {
Self::rgba(0.0, 0.0, 0.0, 1.0)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Axis {
#[default]
Column,
Row,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Align {
#[default]
Stretch,
Start,
Center,
End,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Justify {
#[default]
Start,
Center,
End,
SpaceBetween,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Style {
pub size: Option<Size>,
pub min_size: Option<Size>,
pub max_size: Option<Size>,
pub padding: Insets,
pub gap: f32,
pub flex_grow: f32,
pub flex_shrink: f32,
pub align: Align,
pub justify: Justify,
pub background: Option<Color>,
pub clip: bool,
pub fill: bool,
pub row_height: f32,
pub line_height: Option<f32>,
pub font_size: f32,
pub color: Option<Color>,
pub weight: f32,
pub radius: Radius,
pub border_width: f32,
pub border_color: Option<Color>,
pub shadow: Option<Shadow>,
}
impl Default for Style {
fn default() -> Self {
Self {
size: None,
min_size: None,
max_size: None,
padding: Insets::default(),
gap: 0.0,
flex_grow: 0.0,
flex_shrink: 1.0,
align: Align::Stretch,
justify: Justify::Start,
background: None,
clip: false,
fill: false,
row_height: 24.0,
line_height: None,
font_size: 14.0,
color: None,
weight: 400.0,
radius: Radius::default(),
border_width: 0.0,
border_color: None,
shadow: None,
}
}
}
impl Style {
pub fn size(mut self, size: Size) -> Self {
self.size = Some(size);
self
}
pub fn width(mut self, value: f32) -> Self {
let size = self.size.unwrap_or_default();
self.size = Some(size.width(value));
self
}
pub fn height(mut self, value: f32) -> Self {
let size = self.size.unwrap_or_default();
self.size = Some(size.height(value));
self
}
pub fn min_size(mut self, size: Size) -> Self {
self.min_size = Some(size);
self
}
pub fn max_size(mut self, size: Size) -> Self {
self.max_size = Some(size);
self
}
pub fn padding(mut self, value: Insets) -> Self {
self.padding = value;
self
}
pub fn gap(mut self, value: f32) -> Self {
self.gap = value;
self
}
pub fn flex_grow(mut self, value: f32) -> Self {
self.flex_grow = value.max(0.0);
self
}
pub fn align(mut self, value: Align) -> Self {
self.align = value;
self
}
pub fn justify(mut self, value: Justify) -> Self {
self.justify = value;
self
}
pub fn background(mut self, value: Color) -> Self {
self.background = Some(value);
self
}
pub fn clip(mut self, value: bool) -> Self {
self.clip = value;
self
}
pub fn fill(mut self, value: bool) -> Self {
self.fill = value;
self
}
pub fn row_height(mut self, value: f32) -> Self {
self.row_height = value.max(1.0);
self
}
pub fn line_height(mut self, value: f32) -> Self {
self.line_height = Some(value.max(1.0));
self
}
pub fn font_size(mut self, value: f32) -> Self {
self.font_size = value.max(1.0);
self
}
pub fn resolved_line_height(&self, font_line_height: f32) -> f32 {
self.line_height
.unwrap_or_else(|| font_line_height.max(1.0))
}
pub fn color(mut self, value: Color) -> Self {
self.color = Some(value);
self
}
pub fn weight(mut self, value: f32) -> Self {
self.weight = value;
self
}
pub fn radius(mut self, value: f32) -> Self {
self.radius = Radius {
sm: value,
md: value,
lg: value,
};
self
}
pub fn radius_sized(mut self, radius: Radius) -> Self {
self.radius = radius;
self
}
pub fn border(mut self, width: f32, color: Color) -> Self {
self.border_width = width.max(0.0);
self.border_color = Some(color);
self
}
pub fn shadow(mut self, shadow: Shadow) -> Self {
self.shadow = Some(shadow);
self
}
pub fn corner_radius(&self, width: f32, height: f32) -> f32 {
self.radius
.standard()
.max(0.0)
.min((width.min(height)) / 2.0)
}
pub fn border_px(&self) -> f32 {
self.border_width.max(0.0)
}
pub fn shadow_reach(&self) -> f32 {
self.shadow.map(|shadow| shadow.reach()).unwrap_or(0.0)
}
pub fn main_size(&self, axis: Axis) -> Option<f32> {
self.size.and_then(|size| {
let value = match axis {
Axis::Column => size.height,
Axis::Row => size.width,
};
(value > 0.0).then_some(value)
})
}
pub fn cross_size(&self, axis: Axis) -> Option<f32> {
self.size.and_then(|size| {
let value = match axis {
Axis::Column => size.width,
Axis::Row => size.height,
};
(value > 0.0).then_some(value)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_box_defaults_to_a_flat_rectangle() {
let style = Style::default();
assert_eq!(style.radius, Radius::default());
assert_eq!(style.border_width, 0.0);
assert!(style.border_color.is_none());
assert!(style.shadow.is_none());
assert_eq!(style.corner_radius(100.0, 40.0), 0.0);
assert_eq!(style.border_px(), 0.0);
assert_eq!(style.shadow_reach(), 0.0);
}
#[test]
fn a_border_sets_its_width_and_colour_together() {
let style = Style::default().border(1.0, Color::rgba(0.2, 0.2, 0.2, 1.0));
assert_eq!(style.border_px(), 1.0);
assert_eq!(style.border_color, Some(Color::rgba(0.2, 0.2, 0.2, 1.0)));
}
#[test]
fn a_negative_border_is_clamped_to_nothing() {
let style = Style::default().border(-3.0, Color::rgba(1.0, 1.0, 1.0, 1.0));
assert_eq!(style.border_px(), 0.0);
}
#[test]
fn a_radius_rounds_every_corner_the_same_way() {
let style = Style::default().radius(6.0);
assert_eq!(style.corner_radius(100.0, 40.0), 6.0);
}
#[test]
fn a_radius_wider_than_the_box_is_halved_rather_than_inverted() {
let style = Style::default().radius(40.0);
assert_eq!(style.corner_radius(20.0, 20.0), 10.0);
}
#[test]
fn separate_radius_sizes_are_kept() {
let style = Style::default().radius_sized(Radius {
sm: 2.0,
md: 6.0,
lg: 10.0,
});
assert_eq!(
style.corner_radius(100.0, 40.0),
6.0,
"a box uses md by default"
);
assert_eq!(style.radius.lg, 10.0);
}
#[test]
fn a_shadow_reports_how_much_room_it_needs() {
let style = Style::default().shadow(Shadow {
offset_y: 2.0,
blur: 8.0,
color: Color::rgba(0.0, 0.0, 0.0, 0.4),
..Shadow::default()
});
assert_eq!(style.shadow_reach(), 10.0);
}
#[test]
fn a_box_decodes_a_border_and_a_radius_from_json() {
let style: Style = serde_json::from_str(
r#"{"radius":6,"borderWidth":1,"borderColor":{"r":1,"g":0,"b":0,"a":1}}"#,
)
.expect("decodes");
assert_eq!(style.corner_radius(100.0, 40.0), 6.0);
assert_eq!(style.border_px(), 1.0);
assert_eq!(style.border_color.map(|c| c.r), Some(1.0));
}
#[test]
fn a_box_omitting_the_new_keys_still_decodes() {
let style: Style = serde_json::from_str(r#"{"fontSize":13}"#).expect("decodes");
assert_eq!(style.font_size, 13.0);
assert_eq!(style.radius, Radius::default());
}
}