#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub enum Dimension {
Pixels(f32),
Percent(f32),
#[default]
Auto,
}
impl Dimension {
pub fn is_auto(&self) -> bool {
matches!(self, Self::Auto)
}
}
pub fn px(value: f32) -> Dimension {
Dimension::Pixels(value)
}
pub fn pct(value: f32) -> Dimension {
Dimension::Percent(value.clamp(0.0, 1.0))
}
pub fn auto() -> Dimension {
Dimension::Auto
}
impl From<f32> for Dimension {
fn from(value: f32) -> Self {
Dimension::Pixels(value)
}
}
impl std::fmt::Display for Dimension {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Pixels(v) => write!(f, "{v}px"),
Self::Percent(v) => write!(f, "{}%", v * 100.0),
Self::Auto => write!(f, "auto"),
}
}
}