summaryrefslogtreecommitdiff
path: root/src/geom/align.rs
blob: 8f02a4eacc00a94912e12d724fe0ae0012c55fc4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use super::*;

/// The alignment of a child in a container.
pub type ChildAlign = Gen<Align>;

/// Where to align something along a directed axis.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum Align {
    /// Align at the start of the axis.
    Start,
    /// Align in the middle of the axis.
    Center,
    /// Align at the end of the axis.
    End,
}

impl Align {
    /// Returns the position of this alignment in the given range.
    pub fn resolve(self, range: Range<Length>) -> Length {
        match self {
            Self::Start => range.start,
            Self::Center => (range.start + range.end) / 2.0,
            Self::End => range.end,
        }
    }

    /// The inverse alignment.
    pub fn inv(self) -> Self {
        match self {
            Self::Start => Self::End,
            Self::Center => Self::Center,
            Self::End => Self::Start,
        }
    }
}

impl Default for Align {
    fn default() -> Self {
        Self::Start
    }
}

impl Display for Align {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.pad(match self {
            Self::Start => "start",
            Self::Center => "center",
            Self::End => "end",
        })
    }
}