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