tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 231Rust 9TOML 3CSS 1345 Definiciones

⎇ 7d9382ab959f495fb127584399448d7d7e7456de src/geometry.rs Rust · 180 líneas · 3.9 KB Historia Quién tocó qué crudo

Remove serde_camel_case and serde_kebab_case features (#84) 5dde45d20 · Alice Cecile ·

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    pub fn undefined() -> Size<Number> {
113        Size { width: Number::Undefined, height: Number::Undefined }
114    }
115}
116
117impl<T> Size<T> {
118    pub fn map<R, F>(self, f: F) -> Size<R>
119    where
120        F: Fn(T) -> R,
121    {
122        Size { width: f(self.width), height: f(self.height) }
123    }
124
125    pub(crate) fn set_main(&mut self, direction: FlexDirection, value: T) {
126        if direction.is_row() {
127            self.width = value
128        } else {
129            self.height = value
130        }
131    }
132
133    pub(crate) fn set_cross(&mut self, direction: FlexDirection, value: T) {
134        if direction.is_row() {
135            self.height = value
136        } else {
137            self.width = value
138        }
139    }
140
141    pub(crate) fn main(self, direction: FlexDirection) -> T {
142        if direction.is_row() {
143            self.width
144        } else {
145            self.height
146        }
147    }
148
149    pub(crate) fn cross(self, direction: FlexDirection) -> T {
150        if direction.is_row() {
151            self.height
152        } else {
153            self.width
154        }
155    }
156}
157
158impl Size<f32> {
159    pub fn zero() -> Self {
160        Self { width: 0.0, height: 0.0 }
161    }
162}
163
164impl Size<Dimension> {
165    pub(crate) fn resolve(&self, parent: Size<Number>) -> Size<Number> {
166        Size { width: self.width.resolve(parent.width), height: self.height.resolve(parent.height) }
167    }
168}
169
170#[derive(Debug, Copy, Clone, PartialEq)]
171pub struct Point<T> {
172    pub x: T,
173    pub y: T,
174}
175
176impl Point<f32> {
177    pub fn zero() -> Self {
178        Self { x: 0.0, y: 0.0 }
179    }
180}