gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 501HTML 254C 19JavaScript 81414 Definitions

⎇ e7aa3491b6c300d307038a189bdd145901c83283 src/geometry.rs Rust · 183 lines · 4.0 KB History Who touched what raw

Add `#[must_use]` to all static methods (#125) e7aa3491b · Andreas Weibye ·

1use core::ops::Add;
2
3use crate::number::Number;
4use crate::style::{Dimension, FlexDirection};
5
6#[derive(Debug, Copy, Clone, PartialEq)]
7#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8#[cfg_attr(feature = "serde", serde(default))]
9pub struct Rect<T> {
10    pub start: T,
11    pub end: T,
12    pub top: T,
13    pub bottom: T,
14}
15
16impl<T> Rect<T> {
17    pub(crate) fn map<R, F>(self, f: F) -> Rect<R>
18    where
19        F: Fn(T) -> R,
20    {
21        Rect { start: f(self.start), end: f(self.end), top: f(self.top), bottom: f(self.bottom) }
22    }
23    pub(crate) fn zip_size<R, F, U>(self, size: Size<U>, f: F) -> Rect<R>
24    where
25        F: Fn(T, U) -> R,
26        U: Copy,
27    {
28        Rect {
29            start: f(self.start, size.width),
30            end: f(self.end, size.width),
31            top: f(self.top, size.height),
32            bottom: f(self.bottom, size.height),
33        }
34    }
35}
36
37impl<T> Rect<T>
38where
39    T: Add<Output = T> + Copy + Clone,
40{
41    pub(crate) fn horizontal(&self) -> T {
42        self.start + self.end
43    }
44
45    pub(crate) fn vertical(&self) -> T {
46        self.top + self.bottom
47    }
48
49    pub(crate) fn main(&self, direction: FlexDirection) -> T {
50        if direction.is_row() {
51            self.horizontal()
52        } else {
53            self.vertical()
54        }
55    }
56
57    pub(crate) fn cross(&self, direction: FlexDirection) -> T {
58        if direction.is_row() {
59            self.vertical()
60        } else {
61            self.horizontal()
62        }
63    }
64}
65
66impl<T> Rect<T>
67where
68    T: Copy + Clone,
69{
70    pub(crate) fn main_start(&self, direction: FlexDirection) -> T {
71        if direction.is_row() {
72            self.start
73        } else {
74            self.top
75        }
76    }
77
78    pub(crate) fn main_end(&self, direction: FlexDirection) -> T {
79        if direction.is_row() {
80            self.end
81        } else {
82            self.bottom
83        }
84    }
85
86    pub(crate) fn cross_start(&self, direction: FlexDirection) -> T {
87        if direction.is_row() {
88            self.top
89        } else {
90            self.start
91        }
92    }
93
94    pub(crate) fn cross_end(&self, direction: FlexDirection) -> T {
95        if direction.is_row() {
96            self.bottom
97        } else {
98            self.end
99        }
100    }
101}
102
103#[derive(Debug, Copy, Clone, PartialEq)]
104#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
105#[cfg_attr(feature = "serde", serde(default))]
106pub struct Size<T> {
107    pub width: T,
108    pub height: T,
109}
110
111impl Size<()> {
112    #[must_use]
113    pub fn undefined() -> Size<Number> {
114        Size { width: Number::Undefined, height: Number::Undefined }
115    }
116}
117
118impl<T> Size<T> {
119    pub fn map<R, F>(self, f: F) -> Size<R>
120    where
121        F: Fn(T) -> R,
122    {
123        Size { width: f(self.width), height: f(self.height) }
124    }
125
126    pub(crate) fn set_main(&mut self, direction: FlexDirection, value: T) {
127        if direction.is_row() {
128            self.width = value
129        } else {
130            self.height = value
131        }
132    }
133
134    pub(crate) fn set_cross(&mut self, direction: FlexDirection, value: T) {
135        if direction.is_row() {
136            self.height = value
137        } else {
138            self.width = value
139        }
140    }
141
142    pub(crate) fn main(self, direction: FlexDirection) -> T {
143        if direction.is_row() {
144            self.width
145        } else {
146            self.height
147        }
148    }
149
150    pub(crate) fn cross(self, direction: FlexDirection) -> T {
151        if direction.is_row() {
152            self.height
153        } else {
154            self.width
155        }
156    }
157}
158
159impl Size<f32> {
160    #[must_use]
161    pub fn zero() -> Self {
162        Self { width: 0.0, height: 0.0 }
163    }
164}
165
166impl Size<Dimension> {
167    pub(crate) fn resolve(&self, parent: Size<Number>) -> Size<Number> {
168        Size { width: self.width.resolve(parent.width), height: self.height.resolve(parent.height) }
169    }
170}
171
172#[derive(Debug, Copy, Clone, PartialEq)]
173pub struct Point<T> {
174    pub x: T,
175    pub y: T,
176}
177
178impl Point<f32> {
179    #[must_use]
180    pub fn zero() -> Self {
181        Self { x: 0.0, y: 0.0 }
182    }
183}