summaryrefslogtreecommitdiff
path: root/src/geom/spec.rs
blob: c61f9526ccc4fdc0452bb2acceddb6328ffed241 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use super::*;

/// A container with a horizontal and vertical component.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct Spec<T> {
    /// The horizontal component.
    pub horizontal: T,
    /// The vertical component.
    pub vertical: T,
}

impl<T> Spec<T> {
    /// Create a new instance from the two components.
    pub fn new(horizontal: T, vertical: T) -> Self {
        Self { horizontal, vertical }
    }

    /// Create a new instance with two equal components.
    pub fn uniform(value: T) -> Self
    where
        T: Clone,
    {
        Self {
            horizontal: value.clone(),
            vertical: value,
        }
    }
}

impl Spec<Length> {
    /// The zero value.
    pub const ZERO: Self = Self {
        horizontal: Length::ZERO,
        vertical: Length::ZERO,
    };

    /// Convert to a point.
    pub fn to_point(self) -> Point {
        Point::new(self.horizontal, self.vertical)
    }

    /// Convert to a size.
    pub fn to_size(self) -> Size {
        Size::new(self.horizontal, self.vertical)
    }
}

impl<T> Get<SpecAxis> for Spec<T> {
    type Component = T;

    fn get(self, axis: SpecAxis) -> T {
        match axis {
            SpecAxis::Horizontal => self.horizontal,
            SpecAxis::Vertical => self.vertical,
        }
    }

    fn get_mut(&mut self, axis: SpecAxis) -> &mut T {
        match axis {
            SpecAxis::Horizontal => &mut self.horizontal,
            SpecAxis::Vertical => &mut self.vertical,
        }
    }
}

impl<T> Switch for Spec<T> {
    type Other = Gen<T>;

    fn switch(self, dirs: LayoutDirs) -> Self::Other {
        match dirs.main.axis() {
            SpecAxis::Horizontal => Gen::new(self.horizontal, self.vertical),
            SpecAxis::Vertical => Gen::new(self.vertical, self.horizontal),
        }
    }
}

/// The two specific layouting axes.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SpecAxis {
    /// The vertical layouting axis.
    Vertical,
    /// The horizontal layouting axis.
    Horizontal,
}

impl SpecAxis {
    /// The other axis.
    pub fn other(self) -> Self {
        match self {
            Self::Horizontal => Self::Vertical,
            Self::Vertical => Self::Horizontal,
        }
    }
}

impl Switch for SpecAxis {
    type Other = GenAxis;

    fn switch(self, dirs: LayoutDirs) -> Self::Other {
        if self == dirs.main.axis() {
            GenAxis::Main
        } else {
            debug_assert_eq!(self, dirs.cross.axis());
            GenAxis::Cross
        }
    }
}

impl Display for SpecAxis {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.pad(match self {
            Self::Vertical => "vertical",
            Self::Horizontal => "horizontal",
        })
    }
}