use kova_native_core::Edges;
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub enum Length {
#[default]
Auto,
Px(f32),
Relative(f32),
}
pub const fn px(v: f32) -> Length {
Length::Px(v)
}
pub fn pct(v: f32) -> Length {
Length::Relative(v / 100.0)
}
pub const fn relative(fraction: f32) -> Length {
Length::Relative(fraction)
}
pub const fn auto() -> Length {
Length::Auto
}
impl From<f32> for Length {
fn from(v: f32) -> Self {
Length::Px(v)
}
}
impl From<i32> for Length {
fn from(v: i32) -> Self {
Length::Px(v as f32)
}
}
impl Length {
pub fn is_auto(&self) -> bool {
matches!(self, Length::Auto)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Display {
#[default]
Flex,
Grid,
Block,
None,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum FlexDirection {
#[default]
Row,
Column,
RowReverse,
ColumnReverse,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum FlexWrap {
#[default]
NoWrap,
Wrap,
WrapReverse,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Align {
Start,
End,
Center,
Stretch,
Baseline,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Justify {
Start,
End,
Center,
Stretch,
SpaceBetween,
SpaceAround,
SpaceEvenly,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Position {
#[default]
Relative,
Absolute,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Overflow {
#[default]
Visible,
Hidden,
Scroll,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Track {
Px(f32),
Relative(f32),
Fr(f32),
Auto,
MinContent,
MaxContent,
MinMax(f32, f32),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum GridPlacement {
#[default]
Auto,
Span(u16),
Line { start: i16, span: u16 },
}
#[derive(Clone, Debug, PartialEq, Default)]
pub struct GridStyle {
pub template_columns: Vec<Track>,
pub template_rows: Vec<Track>,
pub auto_rows: Vec<Track>,
pub column: GridPlacement,
pub row: GridPlacement,
}
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct Axes<T> {
pub width: T,
pub height: T,
}
impl<T: Copy> Axes<T> {
pub const fn both(v: T) -> Self {
Axes {
width: v,
height: v,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct LayoutStyle {
pub display: Display,
pub position: Position,
pub direction: FlexDirection,
pub wrap: FlexWrap,
pub align_items: Option<Align>,
pub align_self: Option<Align>,
pub align_content: Option<Justify>,
pub justify_content: Option<Justify>,
pub size: Axes<Length>,
pub min_size: Axes<Length>,
pub max_size: Axes<Length>,
pub aspect_ratio: Option<f32>,
pub padding: Edges<Length>,
pub margin: Edges<Length>,
pub border: Edges<f32>,
pub inset: Edges<Length>,
pub gap: Axes<Length>,
pub flex_grow: f32,
pub flex_shrink: f32,
pub flex_basis: Length,
pub overflow_x: Overflow,
pub overflow_y: Overflow,
pub grid: Option<Box<GridStyle>>,
}
impl Default for LayoutStyle {
fn default() -> Self {
LayoutStyle {
display: Display::Flex,
position: Position::Relative,
direction: FlexDirection::Row,
wrap: FlexWrap::NoWrap,
align_items: None,
align_self: None,
align_content: None,
justify_content: None,
size: Axes::both(Length::Auto),
min_size: Axes::both(Length::Auto),
max_size: Axes::both(Length::Auto),
aspect_ratio: None,
padding: Edges::all(Length::Px(0.0)),
margin: Edges::all(Length::Px(0.0)),
border: Edges::all(0.0),
inset: Edges::all(Length::Auto),
gap: Axes::both(Length::Px(0.0)),
flex_grow: 0.0,
flex_shrink: 1.0,
flex_basis: Length::Auto,
overflow_x: Overflow::Visible,
overflow_y: Overflow::Visible,
grid: None,
}
}
}
impl LayoutStyle {
pub fn clips(&self) -> bool {
self.overflow_x != Overflow::Visible || self.overflow_y != Overflow::Visible
}
pub fn scrolls(&self) -> bool {
self.overflow_x == Overflow::Scroll || self.overflow_y == Overflow::Scroll
}
pub fn grid_mut(&mut self) -> &mut GridStyle {
self.grid.get_or_insert_with(Default::default)
}
}