torin/values/
alignment.rs

1#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2#[derive(PartialEq, Eq, Clone, Debug, Default)]
3pub enum Alignment {
4    /// Align children to the start of the axis. This is the default.
5    #[default]
6    Start,
7    /// Align children to the center of the axis.
8    Center,
9    /// Align children to the end of the axis.
10    End,
11    /// Distribute children with equal space between them, no space at the edges.
12    SpaceBetween,
13    /// Distribute children with equal space between and around them, including the edges.
14    SpaceEvenly,
15    /// Distribute children with equal space around them, half-size space at the edges.
16    SpaceAround,
17}
18
19impl Alignment {
20    /// Use a [`Start`](Alignment::Start) alignment.
21    pub fn start() -> Alignment {
22        Alignment::Start
23    }
24
25    /// Use a [`Center`](Alignment::Center) alignment.
26    pub fn center() -> Alignment {
27        Alignment::Center
28    }
29
30    /// Use an [`End`](Alignment::End) alignment.
31    pub fn end() -> Alignment {
32        Alignment::End
33    }
34
35    /// Use a [`SpaceBetween`](Alignment::SpaceBetween) alignment.
36    pub fn space_between() -> Alignment {
37        Alignment::SpaceBetween
38    }
39
40    /// Use a [`SpaceEvenly`](Alignment::SpaceEvenly) alignment.
41    pub fn space_evenly() -> Alignment {
42        Alignment::SpaceEvenly
43    }
44
45    /// Use a [`SpaceAround`](Alignment::SpaceAround) alignment.
46    pub fn space_around() -> Alignment {
47        Alignment::SpaceAround
48    }
49
50    pub const fn is_not_start(&self) -> bool {
51        !matches!(self, Self::Start)
52    }
53
54    pub const fn is_spaced(&self) -> bool {
55        matches!(
56            self,
57            Self::SpaceBetween | Self::SpaceAround | Self::SpaceEvenly
58        )
59    }
60
61    pub fn pretty(&self) -> String {
62        match self {
63            Self::Start => "start".to_string(),
64            Self::Center => "center".to_string(),
65            Self::End => "end".to_string(),
66            Self::SpaceBetween => "space-between".to_string(),
67            Self::SpaceEvenly => "space-evenly".to_string(),
68            Self::SpaceAround => "space-around".to_string(),
69        }
70    }
71}