1//! Axis enums representing CSS Grid axis
2use crate::geometry::{Rect, Size};
3use core::ops::Add;
4
5/// The simple absolute horizontal and vertical axis
6#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7pub enum AbsoluteAxis {
8 /// The horizontal axis
9 Horizontal,
10 /// The vertical axis
11 Vertical,
12}
13
14impl AbsoluteAxis {
15 /// Returns the other variant of the enum
16 #[inline]
17 pub const fn other_axis(&self) -> Self {
18 match *self {
19 AbsoluteAxis::Horizontal => AbsoluteAxis::Vertical,
20 AbsoluteAxis::Vertical => AbsoluteAxis::Horizontal,
21 }
22 }
23}
24
25impl<T> Size<T> {
26 #[inline(always)]
27 /// Get either the width or height depending on the AbsoluteAxis passed in
28 pub fn get_abs(self, axis: AbsoluteAxis) -> T {
29 match axis {
30 AbsoluteAxis::Horizontal => self.width,
31 AbsoluteAxis::Vertical => self.height,
32 }
33 }
34}
35
36impl<T: Add> Rect<T> {
37 #[inline(always)]
38 /// Get either the width or height depending on the AbsoluteAxis passed in
39 pub fn grid_axis_sum(self, axis: AbsoluteAxis) -> <T as Add>::Output {
40 match axis {
41 AbsoluteAxis::Horizontal => self.left + self.right,
42 AbsoluteAxis::Vertical => self.top + self.bottom,
43 }
44 }
45}
46
47/// The CSS abstract axis
48/// https://www.w3.org/TR/css-writing-modes-3/#abstract-axes
49#[derive(Copy, Clone, Debug, PartialEq, Eq)]
50pub enum AbstractAxis {
51 /// The axis in the inline dimension, i.e. the horizontal axis in horizontal writing modes and the vertical axis in vertical writing modes.
52 Inline,
53 /// The axis in the block dimension, i.e. the vertical axis in horizontal writing modes and the horizontal axis in vertical writing modes.
54 Block,
55}
56
57impl AbstractAxis {
58 /// Returns the other variant of the enum
59 pub fn other(&self) -> AbstractAxis {
60 match *self {
61 AbstractAxis::Inline => AbstractAxis::Block,
62 AbstractAxis::Block => AbstractAxis::Inline,
63 }
64 }
65}
66
67/// Container that holds an item in each absolute axis without specifying
68/// what kind of item it is.
69#[derive(Clone, Copy, Debug, PartialEq, Eq)]
70pub(crate) struct InBothAbsAxis<T> {
71 /// The item in the horizontal axis
72 pub horizontal: T,
73 /// The item in the vertical axis
74 pub vertical: T,
75}
76
77impl<T: Copy> InBothAbsAxis<T> {
78 /// Get the contained item based on the AbsoluteAxis passed
79 pub fn get(&self, axis: AbsoluteAxis) -> T {
80 match axis {
81 AbsoluteAxis::Horizontal => self.horizontal,
82 AbsoluteAxis::Vertical => self.vertical,
83 }
84 }
85}