ori_core/layout/
axis.rs

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
/// An axis is a direction in which a layout is applied.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Axis {
    /// The horizontal axis.
    Horizontal,
    /// The vertical axis.
    Vertical,
}

impl Axis {
    /// Get the major component of a pair.
    pub fn major(&self, size: impl Into<(f32, f32)>) -> f32 {
        let (x, y) = size.into();
        match self {
            Axis::Horizontal => x,
            Axis::Vertical => y,
        }
    }

    /// Get the minor component of a pair.
    pub fn minor(&self, size: impl Into<(f32, f32)>) -> f32 {
        let (x, y) = size.into();
        match self {
            Axis::Horizontal => y,
            Axis::Vertical => x,
        }
    }

    /// Unpack a pair into it's (major, minor) components.
    pub fn unpack(&self, size: impl Into<(f32, f32)>) -> (f32, f32) {
        let (x, y) = size.into();
        match self {
            Axis::Horizontal => (x, y),
            Axis::Vertical => (y, x),
        }
    }

    /// Pack a major and minor component into a pair.
    pub fn pack<T: From<(f32, f32)>>(&self, major: f32, minor: f32) -> T {
        match self {
            Axis::Horizontal => T::from((major, minor)),
            Axis::Vertical => T::from((minor, major)),
        }
    }
}