alchemy_styles/stretch/
geometry.rs

1//! This module is included while awaiting an upstream merge in stretch proper.
2//! You should not rely on it, and consider it an implementation detail.
3
4use core::ops::Add;
5
6use crate::stretch::number::Number;
7use crate::stretch::style;
8
9#[derive(Debug, Copy, Clone, PartialEq)]
10pub struct Rect<T> {
11    pub start: T,
12    pub end: T,
13    pub top: T,
14    pub bottom: T,
15}
16
17impl<T> Rect<T> {
18    pub(crate) fn map<R, F>(self, f: F) -> Rect<R>
19    where
20        F: Fn(T) -> R,
21    {
22        Rect { start: f(self.start), end: f(self.end), top: f(self.top), bottom: f(self.bottom) }
23    }
24}
25
26impl<T> Rect<T>
27where
28    T: Add<Output = T> + Copy + Clone,
29{
30    pub(crate) fn horizontal(&self) -> T {
31        self.start + self.end
32    }
33
34    pub(crate) fn vertical(&self) -> T {
35        self.top + self.bottom
36    }
37
38    pub(crate) fn main(&self, direction: style::FlexDirection) -> T {
39        match direction {
40            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.start + self.end,
41            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.top + self.bottom,
42        }
43    }
44
45    pub(crate) fn cross(&self, direction: style::FlexDirection) -> T {
46        match direction {
47            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.top + self.bottom,
48            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.start + self.end,
49        }
50    }
51}
52
53impl<T> Rect<T>
54where
55    T: Copy + Clone,
56{
57    pub(crate) fn main_start(&self, direction: style::FlexDirection) -> T {
58        match direction {
59            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.start,
60            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.top,
61        }
62    }
63
64    pub(crate) fn main_end(&self, direction: style::FlexDirection) -> T {
65        match direction {
66            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.end,
67            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.bottom,
68        }
69    }
70
71    pub(crate) fn cross_start(&self, direction: style::FlexDirection) -> T {
72        match direction {
73            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.top,
74            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.start,
75        }
76    }
77
78    pub(crate) fn cross_end(&self, direction: style::FlexDirection) -> T {
79        match direction {
80            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.bottom,
81            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.end,
82        }
83    }
84}
85
86#[derive(Debug, Copy, Clone, PartialEq)]
87pub struct Size<T> {
88    pub width: T,
89    pub height: T,
90}
91
92impl Size<()> {
93    pub fn undefined() -> Size<Number> {
94        Size { width: Number::Undefined, height: Number::Undefined }
95    }
96}
97
98impl<T> Size<T> {
99    pub(crate) fn map<R, F>(self, f: F) -> Size<R>
100    where
101        F: Fn(T) -> R,
102    {
103        Size { width: f(self.width), height: f(self.height) }
104    }
105
106    pub(crate) fn set_main(&mut self, direction: style::FlexDirection, value: T) {
107        match direction {
108            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.width = value,
109            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.height = value,
110        }
111    }
112
113    pub(crate) fn set_cross(&mut self, direction: style::FlexDirection, value: T) {
114        match direction {
115            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.height = value,
116            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.width = value,
117        }
118    }
119
120    pub(crate) fn main(self, direction: style::FlexDirection) -> T {
121        match direction {
122            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.width,
123            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.height,
124        }
125    }
126
127    pub(crate) fn cross(self, direction: style::FlexDirection) -> T {
128        match direction {
129            style::FlexDirection::Row | style::FlexDirection::RowReverse => self.height,
130            style::FlexDirection::Column | style::FlexDirection::ColumnReverse => self.width,
131        }
132    }
133}
134
135#[derive(Debug, Copy, Clone, PartialEq)]
136pub struct Point<T> {
137    pub x: T,
138    pub y: T,
139}