use-layout 0.1.0

Platform-neutral layout vocabulary primitives for RustUse UI
Documentation
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

/// A layout direction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum Direction {
    Horizontal,
    Vertical,
}

impl Direction {
    pub fn axis(self) -> Axis {
        match self {
            Self::Horizontal => Axis::Inline,
            Self::Vertical => Axis::Block,
        }
    }
}

/// A logical layout axis.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum Axis {
    Inline,
    Block,
}

impl Axis {
    pub fn direction(self) -> Direction {
        match self {
            Self::Inline => Direction::Horizontal,
            Self::Block => Direction::Vertical,
        }
    }
}

/// Cross-axis alignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum Alignment {
    Start,
    Center,
    End,
    Stretch,
}

impl Alignment {
    pub fn is_stretched(self) -> bool {
        matches!(self, Self::Stretch)
    }
}

/// Main-axis distribution.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum Justification {
    Start,
    Center,
    End,
    SpaceBetween,
    SpaceAround,
    SpaceEvenly,
}

/// Placement relative to an anchor or container.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum Placement {
    Top,
    Right,
    Bottom,
    Left,
    Center,
}

impl Placement {
    pub fn is_edge(self) -> bool {
        !matches!(self, Self::Center)
    }
}

/// Flow behavior for ordered UI content.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum Flow {
    Row,
    Column,
    Wrap,
    Grid,
    Overlay,
}

impl Flow {
    pub fn direction(self) -> Option<Direction> {
        match self {
            Self::Row => Some(Direction::Horizontal),
            Self::Column => Some(Direction::Vertical),
            Self::Wrap | Self::Grid | Self::Overlay => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{Alignment, Axis, Direction, Flow, Placement};

    #[test]
    fn maps_direction_and_axis() {
        assert_eq!(Direction::Horizontal.axis(), Axis::Inline);
        assert_eq!(Axis::Block.direction(), Direction::Vertical);
    }

    #[test]
    fn exposes_layout_helpers() {
        assert!(Alignment::Stretch.is_stretched());
        assert!(!Alignment::Center.is_stretched());
        assert!(Placement::Top.is_edge());
        assert!(!Placement::Center.is_edge());
        assert_eq!(Flow::Column.direction(), Some(Direction::Vertical));
        assert_eq!(Flow::Grid.direction(), None);
    }
}