pub type Rgba = [f32; 4];
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Length {
#[default]
Auto,
Px(f32),
Percent(f32),
}
impl Length {
pub fn resolve(self, base: Option<f32>) -> Option<f32> {
match self {
Length::Auto => None,
Length::Px(v) => Some(v),
Length::Percent(p) => base.map(|b| b * p / 100.0),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Edges {
pub top: f32,
pub right: f32,
pub bottom: f32,
pub left: f32,
}
impl Edges {
pub const ZERO: Edges = Edges { top: 0.0, right: 0.0, bottom: 0.0, left: 0.0 };
pub fn all(v: f32) -> Self {
Self { top: v, right: v, bottom: v, left: v }
}
pub fn symmetric(vertical: f32, horizontal: f32) -> Self {
Self { top: vertical, right: horizontal, bottom: vertical, left: horizontal }
}
pub fn new(top: f32, right: f32, bottom: f32, left: f32) -> Self {
Self { top, right, bottom, left }
}
pub fn horizontal(&self) -> f32 {
self.left + self.right
}
pub fn vertical(&self) -> f32 {
self.top + self.bottom
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Display {
#[default]
Block,
Flex,
None,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FlexDirection {
#[default]
Row,
Column,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AlignItems {
#[default]
Stretch,
FlexStart,
Center,
FlexEnd,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum JustifyContent {
#[default]
FlexStart,
Center,
FlexEnd,
SpaceBetween,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Position {
#[default]
Static,
Relative,
Absolute,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BoxSizing {
#[default]
ContentBox,
BorderBox,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FontWeight {
#[default]
Normal,
Bold,
Weight(u16),
}
#[derive(Debug, Clone, PartialEq)]
pub struct Style {
pub display: Display,
pub position: Position,
pub box_sizing: BoxSizing,
pub flex_direction: FlexDirection,
pub flex_grow: f32,
pub flex_shrink: f32,
pub flex_basis: Length,
pub align_items: AlignItems,
pub align_self: Option<AlignItems>,
pub justify_content: JustifyContent,
pub gap: f32,
pub width: Length,
pub height: Length,
pub min_width: Length,
pub min_height: Length,
pub max_width: Length,
pub max_height: Length,
pub margin: Edges,
pub padding: Edges,
pub border: Edges,
pub border_color: Rgba,
pub background: Option<Rgba>,
pub color: Rgba,
pub font_family: String,
pub font_size: f32,
pub font_weight: FontWeight,
pub line_height: Option<f32>,
pub left: Length,
pub top: Length,
pub right: Length,
pub bottom: Length,
pub clip: bool,
pub opacity: f32,
pub border_radius: Length,
pub line_through: bool,
pub background_gradient: Vec<(f32, Rgba)>,
pub blur: f32,
pub scroll: bool,
pub scroll_top: f32,
pub background_clip_content: bool,
pub subpixel: bool,
pub wrap: bool,
pub italic: bool,
pub ellipsis: bool,
pub vertical: bool,
}
impl Default for Style {
fn default() -> Self {
Self {
display: Display::Block,
position: Position::Static,
box_sizing: BoxSizing::ContentBox,
flex_direction: FlexDirection::Row,
flex_grow: 0.0,
flex_shrink: 1.0,
flex_basis: Length::Auto,
align_items: AlignItems::Stretch,
align_self: None,
justify_content: JustifyContent::FlexStart,
gap: 0.0,
width: Length::Auto,
height: Length::Auto,
min_width: Length::Auto,
min_height: Length::Auto,
max_width: Length::Auto,
max_height: Length::Auto,
margin: Edges::ZERO,
padding: Edges::ZERO,
border: Edges::ZERO,
border_color: [0.0, 0.0, 0.0, 1.0],
background: None,
color: [1.0, 1.0, 1.0, 1.0],
font_family: "sans-serif".into(),
font_size: 16.0,
font_weight: FontWeight::Normal,
line_height: None,
left: Length::Auto,
top: Length::Auto,
right: Length::Auto,
bottom: Length::Auto,
clip: false,
opacity: 1.0,
border_radius: Length::Auto,
line_through: false,
background_gradient: Vec::new(),
blur: 0.0,
scroll: false,
scroll_top: 0.0,
background_clip_content: false,
subpixel: false,
wrap: false,
italic: false,
ellipsis: false,
vertical: false,
}
}
}
impl Style {
pub fn flex(direction: FlexDirection) -> Self {
Self { display: Display::Flex, flex_direction: direction, ..Self::default() }
}
}