1use bytemuck::{Pod, Zeroable};
2use core::fmt::Debug;
3use derive_more::{Add, AddAssign, Div, Mul, Sub, SubAssign};
4use refineable::Refineable;
5use std::ops::{Add, AddAssign, Mul, Sub, SubAssign};
6
7#[derive(
8 Refineable, Default, Add, AddAssign, Sub, SubAssign, Mul, Div, Copy, Debug, PartialEq, Eq, Hash,
9)]
10#[refineable(debug)]
11#[repr(C)]
12pub struct Point<T: Clone + Debug> {
13 pub x: T,
14 pub y: T,
15}
16
17pub fn point<T: Clone + Debug>(x: T, y: T) -> Point<T> {
18 Point { x, y }
19}
20
21impl<T: Clone + Debug> Point<T> {
22 pub fn new(x: T, y: T) -> Self {
23 Self { x, y }
24 }
25
26 pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Point<U> {
27 Point {
28 x: f(self.x.clone()),
29 y: f(self.y.clone()),
30 }
31 }
32}
33
34impl<T: Clone + Debug + Sub<Output = T>> SubAssign<Size<T>> for Point<T> {
35 fn sub_assign(&mut self, rhs: Size<T>) {
36 self.x = self.x.clone() - rhs.width;
37 self.y = self.y.clone() - rhs.height;
38 }
39}
40
41impl<T: Clone + Debug + Add<Output = T> + Copy> AddAssign<T> for Point<T> {
42 fn add_assign(&mut self, rhs: T) {
43 self.x = self.x.clone() + rhs;
44 self.y = self.y.clone() + rhs;
45 }
46}
47
48impl<T: Clone + Debug + std::cmp::PartialOrd> Point<T> {
49 pub fn max(&self, other: &Self) -> Self {
50 Point {
51 x: if self.x >= other.x {
52 self.x.clone()
53 } else {
54 other.x.clone()
55 },
56 y: if self.y >= other.y {
57 self.y.clone()
58 } else {
59 other.y.clone()
60 },
61 }
62 }
63}
64
65impl<T: Clone + Debug> Clone for Point<T> {
66 fn clone(&self) -> Self {
67 Self {
68 x: self.x.clone(),
69 y: self.y.clone(),
70 }
71 }
72}
73
74unsafe impl<T: Clone + Debug + Zeroable + Pod> Zeroable for Point<T> {}
75
76unsafe impl<T: Clone + Debug + Zeroable + Pod> Pod for Point<T> {}
77
78#[derive(Refineable, Default, Clone, Copy, Debug, PartialEq)]
79#[refineable(debug)]
80pub struct Size<T: Clone + Debug> {
81 pub width: T,
82 pub height: T,
83}
84
85pub fn size<T: Clone + Debug>(width: T, height: T) -> Size<T> {
86 Size { width, height }
87}
88
89impl<T: Clone + Debug> Size<T> {
90 pub fn map<U: Clone + Debug, F: Fn(T) -> U>(&self, f: F) -> Size<U> {
91 Size {
92 width: f(self.width.clone()),
93 height: f(self.height.clone()),
94 }
95 }
96}
97
98impl From<Size<Option<Pixels>>> for Size<Option<f32>> {
99 fn from(val: Size<Option<Pixels>>) -> Self {
100 Size {
101 width: val.width.map(|p| p.0 as f32),
102 height: val.height.map(|p| p.0 as f32),
103 }
104 }
105}
106
107impl Size<Length> {
108 pub fn full() -> Self {
109 Self {
110 width: relative(1.).into(),
111 height: relative(1.).into(),
112 }
113 }
114}
115
116impl Size<DefiniteLength> {
117 pub fn zero() -> Self {
118 Self {
119 width: px(0.).into(),
120 height: px(0.).into(),
121 }
122 }
123}
124
125impl Size<Length> {
126 pub fn auto() -> Self {
127 Self {
128 width: Length::Auto,
129 height: Length::Auto,
130 }
131 }
132}
133
134#[derive(Refineable, Clone, Default, Debug, PartialEq)]
135#[refineable(debug)]
136pub struct Bounds<T: Clone + Debug> {
137 pub origin: Point<T>,
138 pub size: Size<T>,
139}
140
141impl<T: Clone + Debug + Copy + Add<T, Output = T>> Bounds<T> {
142 pub fn upper_right(&self) -> Point<T> {
143 Point {
144 x: self.origin.x + self.size.width,
145 y: self.origin.y,
146 }
147 }
148
149 pub fn lower_right(&self) -> Point<T> {
150 Point {
151 x: self.origin.x + self.size.width,
152 y: self.origin.y + self.size.height,
153 }
154 }
155}
156
157impl<T: Clone + Debug + Copy + PartialOrd + Add<T, Output = T>> Bounds<T> {
158 pub fn contains_point(&self, point: Point<T>) -> bool {
159 point.x >= self.origin.x
160 && point.x <= self.origin.x + self.size.width
161 && point.y >= self.origin.y
162 && point.y <= self.origin.y + self.size.height
163 }
164}
165
166impl<T: Clone + Debug + Copy> Copy for Bounds<T> {}
167
168#[derive(Refineable, Clone, Default, Debug)]
169#[refineable(debug)]
170pub struct Edges<T: Clone + Debug> {
171 pub top: T,
172 pub right: T,
173 pub bottom: T,
174 pub left: T,
175}
176
177impl Edges<Length> {
178 pub fn auto() -> Self {
179 Self {
180 top: Length::Auto,
181 right: Length::Auto,
182 bottom: Length::Auto,
183 left: Length::Auto,
184 }
185 }
186
187 pub fn zero() -> Self {
188 Self {
189 top: px(0.).into(),
190 right: px(0.).into(),
191 bottom: px(0.).into(),
192 left: px(0.).into(),
193 }
194 }
195}
196
197impl Edges<DefiniteLength> {
198 pub fn zero() -> Self {
199 Self {
200 top: px(0.).into(),
201 right: px(0.).into(),
202 bottom: px(0.).into(),
203 left: px(0.).into(),
204 }
205 }
206}
207
208impl Edges<AbsoluteLength> {
209 pub fn zero() -> Self {
210 Self {
211 top: px(0.).into(),
212 right: px(0.).into(),
213 bottom: px(0.).into(),
214 left: px(0.).into(),
215 }
216 }
217
218 pub fn to_pixels(&self, rem_size: Pixels) -> Edges<Pixels> {
219 Edges {
220 top: self.top.to_pixels(rem_size),
221 right: self.right.to_pixels(rem_size),
222 bottom: self.bottom.to_pixels(rem_size),
223 left: self.left.to_pixels(rem_size),
224 }
225 }
226}
227
228impl Edges<Pixels> {
229 pub fn is_empty(&self) -> bool {
230 self.top == px(0.) && self.right == px(0.) && self.bottom == px(0.) && self.left == px(0.)
231 }
232}
233
234#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
235#[repr(transparent)]
236pub struct Pixels(pub(crate) f32);
237
238impl Pixels {
239 pub fn round(&self) -> Self {
240 Self(self.0.round())
241 }
242}
243
244impl Mul<f32> for Pixels {
245 type Output = Pixels;
246
247 fn mul(self, other: f32) -> Pixels {
248 Pixels(self.0 * other)
249 }
250}
251
252impl Mul<Pixels> for Pixels {
253 type Output = Pixels;
254
255 fn mul(self, rhs: Pixels) -> Self::Output {
256 Pixels(self.0 * rhs.0)
257 }
258}
259
260impl Eq for Pixels {}
261
262impl Ord for Pixels {
263 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
264 self.0.partial_cmp(&other.0).unwrap()
265 }
266}
267
268impl std::hash::Hash for Pixels {
269 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
270 self.0.to_bits().hash(state);
271 }
272}
273
274impl From<f64> for Pixels {
275 fn from(val: f64) -> Self {
276 Pixels(val as f32)
277 }
278}
279
280impl From<f32> for Pixels {
281 fn from(val: f32) -> Self {
282 Pixels(val)
283 }
284}
285
286unsafe impl bytemuck::Pod for Pixels {}
287unsafe impl bytemuck::Zeroable for Pixels {}
288
289impl Debug for Pixels {
290 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
291 write!(f, "{} px", self.0)
292 }
293}
294
295impl From<Pixels> for f32 {
296 fn from(pixels: Pixels) -> Self {
297 pixels.0
298 }
299}
300
301impl From<&Pixels> for f32 {
302 fn from(pixels: &Pixels) -> Self {
303 pixels.0
304 }
305}
306
307impl From<Pixels> for f64 {
308 fn from(pixels: Pixels) -> Self {
309 pixels.0 as f64
310 }
311}
312
313#[derive(Clone, Copy, Default, Add, Sub, Mul, Div)]
314pub struct Rems(f32);
315
316impl Mul<Pixels> for Rems {
317 type Output = Pixels;
318
319 fn mul(self, other: Pixels) -> Pixels {
320 Pixels(self.0 * other.0)
321 }
322}
323
324impl Debug for Rems {
325 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
326 write!(f, "{} rem", self.0)
327 }
328}
329
330#[derive(Clone, Copy, Debug)]
331pub enum AbsoluteLength {
332 Pixels(Pixels),
333 Rems(Rems),
334}
335
336impl From<Pixels> for AbsoluteLength {
337 fn from(pixels: Pixels) -> Self {
338 AbsoluteLength::Pixels(pixels)
339 }
340}
341
342impl From<Rems> for AbsoluteLength {
343 fn from(rems: Rems) -> Self {
344 AbsoluteLength::Rems(rems)
345 }
346}
347
348impl AbsoluteLength {
349 pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
350 match self {
351 AbsoluteLength::Pixels(pixels) => *pixels,
352 AbsoluteLength::Rems(rems) => *rems * rem_size,
353 }
354 }
355}
356
357impl Default for AbsoluteLength {
358 fn default() -> Self {
359 px(0.).into()
360 }
361}
362
363/// A non-auto length that can be defined in pixels, rems, or percent of parent.
364#[derive(Clone, Copy)]
365pub enum DefiniteLength {
366 Absolute(AbsoluteLength),
367 /// A fraction of the parent's size between 0 and 1.
368 Fraction(f32),
369}
370
371impl Debug for DefiniteLength {
372 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
373 match self {
374 DefiniteLength::Absolute(length) => Debug::fmt(length, f),
375 DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
376 }
377 }
378}
379
380impl From<Pixels> for DefiniteLength {
381 fn from(pixels: Pixels) -> Self {
382 Self::Absolute(pixels.into())
383 }
384}
385
386impl From<Rems> for DefiniteLength {
387 fn from(rems: Rems) -> Self {
388 Self::Absolute(rems.into())
389 }
390}
391
392impl From<AbsoluteLength> for DefiniteLength {
393 fn from(length: AbsoluteLength) -> Self {
394 Self::Absolute(length)
395 }
396}
397
398impl Default for DefiniteLength {
399 fn default() -> Self {
400 Self::Absolute(AbsoluteLength::default())
401 }
402}
403
404/// A length that can be defined in pixels, rems, percent of parent, or auto.
405#[derive(Clone, Copy)]
406pub enum Length {
407 Definite(DefiniteLength),
408 Auto,
409}
410
411impl Debug for Length {
412 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
413 match self {
414 Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
415 Length::Auto => write!(f, "auto"),
416 }
417 }
418}
419
420pub fn relative(fraction: f32) -> DefiniteLength {
421 DefiniteLength::Fraction(fraction).into()
422}
423
424pub fn rems(rems: f32) -> Rems {
425 Rems(rems)
426}
427
428pub fn px(pixels: f32) -> Pixels {
429 Pixels(pixels)
430}
431
432pub fn auto() -> Length {
433 Length::Auto
434}
435
436impl From<Pixels> for Length {
437 fn from(pixels: Pixels) -> Self {
438 Self::Definite(pixels.into())
439 }
440}
441
442impl From<Rems> for Length {
443 fn from(rems: Rems) -> Self {
444 Self::Definite(rems.into())
445 }
446}
447
448impl From<DefiniteLength> for Length {
449 fn from(length: DefiniteLength) -> Self {
450 Self::Definite(length)
451 }
452}
453
454impl From<AbsoluteLength> for Length {
455 fn from(length: AbsoluteLength) -> Self {
456 Self::Definite(length.into())
457 }
458}
459
460impl Default for Length {
461 fn default() -> Self {
462 Self::Definite(DefiniteLength::default())
463 }
464}
465
466impl From<()> for Length {
467 fn from(_: ()) -> Self {
468 Self::Definite(DefiniteLength::default())
469 }
470}