Skip to main content

use_layout/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4/// A layout direction.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
6pub enum Direction {
7    Horizontal,
8    Vertical,
9}
10
11impl Direction {
12    pub fn axis(self) -> Axis {
13        match self {
14            Self::Horizontal => Axis::Inline,
15            Self::Vertical => Axis::Block,
16        }
17    }
18}
19
20/// A logical layout axis.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
22pub enum Axis {
23    Inline,
24    Block,
25}
26
27impl Axis {
28    pub fn direction(self) -> Direction {
29        match self {
30            Self::Inline => Direction::Horizontal,
31            Self::Block => Direction::Vertical,
32        }
33    }
34}
35
36/// Cross-axis alignment.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
38pub enum Alignment {
39    Start,
40    Center,
41    End,
42    Stretch,
43}
44
45impl Alignment {
46    pub fn is_stretched(self) -> bool {
47        matches!(self, Self::Stretch)
48    }
49}
50
51/// Main-axis distribution.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
53pub enum Justification {
54    Start,
55    Center,
56    End,
57    SpaceBetween,
58    SpaceAround,
59    SpaceEvenly,
60}
61
62/// Placement relative to an anchor or container.
63#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
64pub enum Placement {
65    Top,
66    Right,
67    Bottom,
68    Left,
69    Center,
70}
71
72impl Placement {
73    pub fn is_edge(self) -> bool {
74        !matches!(self, Self::Center)
75    }
76}
77
78/// Flow behavior for ordered UI content.
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
80pub enum Flow {
81    Row,
82    Column,
83    Wrap,
84    Grid,
85    Overlay,
86}
87
88impl Flow {
89    pub fn direction(self) -> Option<Direction> {
90        match self {
91            Self::Row => Some(Direction::Horizontal),
92            Self::Column => Some(Direction::Vertical),
93            Self::Wrap | Self::Grid | Self::Overlay => None,
94        }
95    }
96}
97
98#[cfg(test)]
99mod tests {
100    use super::{Alignment, Axis, Direction, Flow, Placement};
101
102    #[test]
103    fn maps_direction_and_axis() {
104        assert_eq!(Direction::Horizontal.axis(), Axis::Inline);
105        assert_eq!(Axis::Block.direction(), Direction::Vertical);
106    }
107
108    #[test]
109    fn exposes_layout_helpers() {
110        assert!(Alignment::Stretch.is_stretched());
111        assert!(!Alignment::Center.is_stretched());
112        assert!(Placement::Top.is_edge());
113        assert!(!Placement::Center.is_edge());
114        assert_eq!(Flow::Column.direction(), Some(Direction::Vertical));
115        assert_eq!(Flow::Grid.direction(), None);
116    }
117}