tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 583HTML 275Markdown 8TOML 41586 Definiciones

⎇ c2db86dfc600067c6f4d70c8ea398cbcc5d42dbd src/axis.rs Rust · 86 líneas · 2.6 KB Historia Quién tocó qué crudo

Fix building with grid feature disabled 6c62bd8db · Nico Burns ·

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    #[cfg(feature = "grid")]
79    /// Get the contained item based on the AbsoluteAxis passed
80    pub fn get(&self, axis: AbsoluteAxis) -> T {
81        match axis {
82            AbsoluteAxis::Horizontal => self.horizontal,
83            AbsoluteAxis::Vertical => self.vertical,
84        }
85    }
86}