nitron 0.1.1

Create native UIs in Rust
Documentation
/// A spacial unit in one dimension.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Dim {
    /// The default in most cases
    Auto,
    /// Raw display pixels
    Px(f32),
    /// Backend specific sizing unit
    Pt(f32),
    /// Percent
    Pct(f32),
    /// Percent of the viewport height
    ///
    /// Vh(1) = Px(108) on a 1080 height viewport
    Vh(f32),
    /// Percent of the viewport width
    ///
    /// Vw(1) = Px(192) on a 1920 width viewport
    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),
        }
    }
}

/// How far to push out a boundary.
#[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()
    }
}

// For the Lord gives wisdom; from his mouth come knowledge and understanding. - Proverbs 2:6