summaryrefslogtreecommitdiff
path: root/src/geom/align.rs
blob: 599600848ab02de4ebfe6665e9a4542c09341cb7 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use super::*;

/// Where to align something along an axis.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
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,
    /// Align at the left side of the axis.
    Left,
    /// Align at the right side of the axis.
    Right,
    /// Align at the top side of the axis.
    Top,
    /// Align at the bottom side of the axis.
    Bottom,
}

impl Align {
    /// The axis this alignment belongs to if it is specific.
    pub fn axis(self) -> Option<SpecAxis> {
        match self {
            Self::Start => None,
            Self::Center => None,
            Self::End => None,
            Self::Left => Some(SpecAxis::Horizontal),
            Self::Right => Some(SpecAxis::Horizontal),
            Self::Top => Some(SpecAxis::Vertical),
            Self::Bottom => Some(SpecAxis::Vertical),
        }
    }

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

    /// Returns the position of this alignment in the given range.
    pub fn resolve(self, dir: Dir, range: Range<Length>) -> Length {
        #[cfg(debug_assertions)]
        if let Some(axis) = self.axis() {
            debug_assert_eq!(axis, dir.axis())
        }

        match self {
            Self::Start => {
                if dir.is_positive() {
                    range.start
                } else {
                    range.end
                }
            }
            Self::Center => (range.start + range.end) / 2.0,
            Self::End => {
                if dir.is_positive() {
                    range.end
                } else {
                    range.start
                }
            }
            Self::Left | Self::Top => range.start,
            Self::Right | Self::Bottom => range.end,
        }
    }
}

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

impl Debug for Align {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.pad(match self {
            Self::Start => "start",
            Self::Center => "center",
            Self::End => "end",
            Self::Left => "left",
            Self::Right => "right",
            Self::Top => "top",
            Self::Bottom => "bottom",
        })
    }
}