use crate::geometry::Size;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Length {
Px(f32),
Em(f32),
Rem(f32),
Percent(f32),
Vw(f32),
Vh(f32),
Vmin(f32),
Vmax(f32),
Fr(f32),
Token(Spacing),
#[default]
Auto,
MaxContent,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Spacing {
None,
Xs,
Sm,
Md,
Lg,
Xl,
Indent,
}
impl From<Spacing> for Length {
fn from(token: Spacing) -> Length {
Length::Token(token)
}
}
pub trait LengthExt {
fn px(self) -> Length;
fn em(self) -> Length;
fn rem(self) -> Length;
fn pct(self) -> Length;
fn vw(self) -> Length;
fn vh(self) -> Length;
fn vmin(self) -> Length;
fn vmax(self) -> Length;
fn fr(self) -> Length;
}
macro_rules! length_ext {
($($t:ty),*) => {$(
impl LengthExt for $t {
fn px(self) -> Length { Length::Px(self as f32) }
fn em(self) -> Length { Length::Em(self as f32) }
fn rem(self) -> Length { Length::Rem(self as f32) }
fn pct(self) -> Length { Length::Percent(self as f32) }
fn vw(self) -> Length { Length::Vw(self as f32) }
fn vh(self) -> Length { Length::Vh(self as f32) }
fn vmin(self) -> Length { Length::Vmin(self as f32) }
fn vmax(self) -> Length { Length::Vmax(self as f32) }
fn fr(self) -> Length { Length::Fr(self as f32) }
}
)*};
}
length_ext!(i32, u32, f32, f64);
#[derive(Clone, Copy, Debug)]
pub struct ResolveContext<'a> {
pub font_size: f32,
pub root_font_size: f32,
pub viewport: Size,
pub spacing: &'a SpacingScale,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SpacingScale {
pub xs: f32,
pub sm: f32,
pub md: f32,
pub lg: f32,
pub xl: f32,
pub indent: f32,
}
impl SpacingScale {
pub fn get(&self, token: Spacing) -> f32 {
match token {
Spacing::None => 0.0,
Spacing::Xs => self.xs,
Spacing::Sm => self.sm,
Spacing::Md => self.md,
Spacing::Lg => self.lg,
Spacing::Xl => self.xl,
Spacing::Indent => self.indent,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Resolved {
Length(f32),
Percent(f32),
Fr(f32),
Auto,
}
impl Length {
pub fn resolve(&self, cx: &ResolveContext<'_>) -> Resolved {
let vw = cx.viewport.width / 100.0;
let vh = cx.viewport.height / 100.0;
match *self {
Length::Px(v) => Resolved::Length(v),
Length::Em(v) => Resolved::Length(v * cx.font_size),
Length::Rem(v) => Resolved::Length(v * cx.root_font_size),
Length::Percent(v) => Resolved::Percent(v / 100.0),
Length::Vw(v) => Resolved::Length(v * vw),
Length::Vh(v) => Resolved::Length(v * vh),
Length::Vmin(v) => Resolved::Length(v * vw.min(vh)),
Length::Vmax(v) => Resolved::Length(v * vw.max(vh)),
Length::Fr(v) => Resolved::Fr(v),
Length::Token(t) => Resolved::Length(cx.spacing.get(t)),
Length::Auto | Length::MaxContent => Resolved::Auto,
}
}
}
impl From<i32> for Length {
fn from(v: i32) -> Length {
Length::Px(v as f32)
}
}
impl From<f32> for Length {
fn from(v: f32) -> Length {
Length::Px(v)
}
}
macro_rules! into_length_value {
($($t:ty => $e:expr),*) => {$(
impl mitsuami_reactive::IntoValue<Length> for $t {
fn into_value(self) -> mitsuami_reactive::Value<Length> {
let f: fn($t) -> Length = $e;
mitsuami_reactive::Value::Static(f(self))
}
}
)*};
}
into_length_value!(
Length => |l| l,
Spacing => Length::Token,
i32 => |v| Length::Px(v as f32),
f32 => Length::Px
);