#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Dim {
Auto,
Px(f32),
Pt(f32),
Pct(f32),
Vh(f32),
Vw(f32),
}
impl Dim {
pub(crate) fn compute_taffy_dimension(
self,
pt_size: f32,
vp_width: f32,
vp_height: f32,
) -> taffy::Dimension {
match self {
Dim::Auto => taffy::Dimension::auto(),
Dim::Px(px) => taffy::Dimension::length(px),
Dim::Pt(pt) => taffy::Dimension::length(pt * pt_size),
Dim::Pct(pct) => taffy::Dimension::percent(pct * 0.01),
Dim::Vh(vh) => taffy::Dimension::length(vh * vp_height * 0.01),
Dim::Vw(vw) => taffy::Dimension::length(vw * vp_width * 0.01),
}
}
pub(crate) fn compute_taffy_length_percentage_auto(
self,
pt_size: f32,
vp_width: f32,
vp_height: f32,
) -> taffy::LengthPercentageAuto {
match self {
Dim::Auto => taffy::LengthPercentageAuto::auto(),
Dim::Px(px) => taffy::LengthPercentageAuto::length(px),
Dim::Pt(pt) => taffy::LengthPercentageAuto::length(pt * pt_size),
Dim::Pct(pct) => taffy::LengthPercentageAuto::percent(pct * 0.01),
Dim::Vh(vh) => taffy::LengthPercentageAuto::length(vh * vp_height * 0.01),
Dim::Vw(vw) => taffy::LengthPercentageAuto::length(vw * vp_width * 0.01),
}
}
pub(crate) fn compute_taffy_length_percentage(
self,
pt_size: f32,
vp_width: f32,
vp_height: f32,
) -> taffy::LengthPercentage {
match self {
Dim::Auto => taffy::LengthPercentage::length(0.0),
Dim::Px(px) => taffy::LengthPercentage::length(px),
Dim::Pt(pt) => taffy::LengthPercentage::length(pt * pt_size),
Dim::Pct(pct) => taffy::LengthPercentage::percent(pct * 0.01),
Dim::Vh(vh) => taffy::LengthPercentage::length(vh * vp_height * 0.01),
Dim::Vw(vw) => taffy::LengthPercentage::length(vw * vp_width * 0.01),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Margin {
pub left: Dim,
pub right: Dim,
pub top: Dim,
pub bottom: Dim,
}
impl Margin {
#[inline(always)]
pub const fn new() -> Self {
Margin {
left: Dim::Px(0.0),
right: Dim::Px(0.0),
top: Dim::Px(0.0),
bottom: Dim::Px(0.0),
}
}
#[inline(always)]
pub const fn l(mut self, l: Dim) -> Self {
self.left = l;
self
}
#[inline(always)]
pub const fn r(mut self, r: Dim) -> Self {
self.right = r;
self
}
#[inline(always)]
pub const fn t(mut self, t: Dim) -> Self {
self.top = t;
self
}
#[inline(always)]
pub const fn b(mut self, b: Dim) -> Self {
self.bottom = b;
self
}
#[inline(always)]
pub const fn x(mut self, x: Dim) -> Self {
self.left = x;
self.right = x;
self
}
#[inline(always)]
pub const fn y(mut self, y: Dim) -> Self {
self.top = y;
self.bottom = y;
self
}
}
impl Default for Margin {
fn default() -> Self {
Self::new()
}
}