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