1use core::fmt::Debug;
2use derive_more::{Add, AddAssign, Div, DivAssign, Mul, Neg, Sub, SubAssign};
3use refineable::Refineable;
4use serde_derive::{Deserialize, Serialize};
5use std::{
6 cmp::{self, PartialOrd},
7 fmt,
8 ops::{Add, Div, Mul, MulAssign, Sub},
9};
10
11#[derive(Copy, Clone, PartialEq, Eq, Debug)]
12pub enum Axis {
13 Vertical,
14 Horizontal,
15}
16
17impl Axis {
18 pub fn invert(&self) -> Self {
19 match self {
20 Axis::Vertical => Axis::Horizontal,
21 Axis::Horizontal => Axis::Vertical,
22 }
23 }
24}
25
26pub trait Along {
27 type Unit;
28
29 fn along(&self, axis: Axis) -> Self::Unit;
30
31 fn apply_along(&self, axis: Axis, f: impl FnOnce(Self::Unit) -> Self::Unit) -> Self;
32}
33
34/// Describes a location in a 2D cartesian coordinate space.
35///
36/// It holds two public fields, `x` and `y`, which represent the coordinates in the space.
37/// The type `T` for the coordinates can be any type that implements `Default`, `Clone`, and `Debug`.
38///
39/// # Examples
40///
41/// ```
42/// # use zed::Point;
43/// let point = Point { x: 10, y: 20 };
44/// println!("{:?}", point); // Outputs: Point { x: 10, y: 20 }
45/// ```
46#[derive(Refineable, Default, Add, AddAssign, Sub, SubAssign, Copy, Debug, PartialEq, Eq, Hash)]
47#[refineable(Debug)]
48#[repr(C)]
49pub struct Point<T: Default + Clone + Debug> {
50 pub x: T,
51 pub y: T,
52}
53
54/// Constructs a new `Point<T>` with the given x and y coordinates.
55///
56/// # Arguments
57///
58/// * `x` - The x coordinate of the point.
59/// * `y` - The y coordinate of the point.
60///
61/// # Returns
62///
63/// Returns a `Point<T>` with the specified coordinates.
64///
65/// # Examples
66///
67/// ```
68/// # use zed::Point;
69/// let p = point(10, 20);
70/// assert_eq!(p.x, 10);
71/// assert_eq!(p.y, 20);
72/// ```
73pub fn point<T: Clone + Debug + Default>(x: T, y: T) -> Point<T> {
74 Point { x, y }
75}
76
77impl<T: Clone + Debug + Default> Point<T> {
78 /// Creates a new `Point` with the specified `x` and `y` coordinates.
79 ///
80 /// # Arguments
81 ///
82 /// * `x` - The horizontal coordinate of the point.
83 /// * `y` - The vertical coordinate of the point.
84 ///
85 /// # Examples
86 ///
87 /// ```
88 /// let p = Point::new(10, 20);
89 /// assert_eq!(p.x, 10);
90 /// assert_eq!(p.y, 20);
91 /// ```
92 pub const fn new(x: T, y: T) -> Self {
93 Self { x, y }
94 }
95
96 /// Transforms the point to a `Point<U>` by applying the given function to both coordinates.
97 ///
98 /// This method allows for converting a `Point<T>` to a `Point<U>` by specifying a closure
99 /// that defines how to convert between the two types. The closure is applied to both the `x`
100 /// and `y` coordinates, resulting in a new point of the desired type.
101 ///
102 /// # Arguments
103 ///
104 /// * `f` - A closure that takes a value of type `T` and returns a value of type `U`.
105 ///
106 /// # Examples
107 ///
108 /// ```
109 /// # use zed::Point;
110 /// let p = Point { x: 3, y: 4 };
111 /// let p_float = p.map(|coord| coord as f32);
112 /// assert_eq!(p_float, Point { x: 3.0, y: 4.0 });
113 /// ```
114 pub fn map<U: Clone + Default + Debug>(&self, f: impl Fn(T) -> U) -> Point<U> {
115 Point {
116 x: f(self.x.clone()),
117 y: f(self.y.clone()),
118 }
119 }
120}
121
122impl<T: Clone + Debug + Default> Along for Point<T> {
123 type Unit = T;
124
125 fn along(&self, axis: Axis) -> T {
126 match axis {
127 Axis::Horizontal => self.x.clone(),
128 Axis::Vertical => self.y.clone(),
129 }
130 }
131
132 fn apply_along(&self, axis: Axis, f: impl FnOnce(T) -> T) -> Point<T> {
133 match axis {
134 Axis::Horizontal => Point {
135 x: f(self.x.clone()),
136 y: self.y.clone(),
137 },
138 Axis::Vertical => Point {
139 x: self.x.clone(),
140 y: f(self.y.clone()),
141 },
142 }
143 }
144}
145
146impl Point<Pixels> {
147 /// Scales the point by a given factor, which is typically derived from the resolution
148 /// of a target display to ensure proper sizing of UI elements.
149 ///
150 /// # Arguments
151 ///
152 /// * `factor` - The scaling factor to apply to both the x and y coordinates.
153 ///
154 /// # Examples
155 ///
156 /// ```
157 /// # use zed::{Point, Pixels, ScaledPixels};
158 /// let p = Point { x: Pixels(10.0), y: Pixels(20.0) };
159 /// let scaled_p = p.scale(1.5);
160 /// assert_eq!(scaled_p, Point { x: ScaledPixels(15.0), y: ScaledPixels(30.0) });
161 /// ```
162 pub fn scale(&self, factor: f32) -> Point<ScaledPixels> {
163 Point {
164 x: self.x.scale(factor),
165 y: self.y.scale(factor),
166 }
167 }
168
169 /// Calculates the Euclidean distance from the origin (0, 0) to this point.
170 ///
171 /// # Examples
172 ///
173 /// ```
174 /// # use zed::Point;
175 /// # use zed::Pixels;
176 /// let p = Point { x: Pixels(3.0), y: Pixels(4.0) };
177 /// assert_eq!(p.magnitude(), 5.0);
178 /// ```
179 pub fn magnitude(&self) -> f64 {
180 ((self.x.0.powi(2) + self.y.0.powi(2)) as f64).sqrt()
181 }
182}
183
184impl<T, Rhs> Mul<Rhs> for Point<T>
185where
186 T: Mul<Rhs, Output = T> + Clone + Default + Debug,
187 Rhs: Clone + Debug,
188{
189 type Output = Point<T>;
190
191 fn mul(self, rhs: Rhs) -> Self::Output {
192 Point {
193 x: self.x * rhs.clone(),
194 y: self.y * rhs,
195 }
196 }
197}
198
199impl<T, S> MulAssign<S> for Point<T>
200where
201 T: Clone + Mul<S, Output = T> + Default + Debug,
202 S: Clone,
203{
204 fn mul_assign(&mut self, rhs: S) {
205 self.x = self.x.clone() * rhs.clone();
206 self.y = self.y.clone() * rhs;
207 }
208}
209
210impl<T, S> Div<S> for Point<T>
211where
212 T: Div<S, Output = T> + Clone + Default + Debug,
213 S: Clone,
214{
215 type Output = Self;
216
217 fn div(self, rhs: S) -> Self::Output {
218 Self {
219 x: self.x / rhs.clone(),
220 y: self.y / rhs,
221 }
222 }
223}
224
225impl<T> Point<T>
226where
227 T: PartialOrd + Clone + Default + Debug,
228{
229 /// Returns a new point with the maximum values of each dimension from `self` and `other`.
230 ///
231 /// # Arguments
232 ///
233 /// * `other` - A reference to another `Point` to compare with `self`.
234 ///
235 /// # Examples
236 ///
237 /// ```
238 /// # use zed::Point;
239 /// let p1 = Point { x: 3, y: 7 };
240 /// let p2 = Point { x: 5, y: 2 };
241 /// let max_point = p1.max(&p2);
242 /// assert_eq!(max_point, Point { x: 5, y: 7 });
243 /// ```
244 pub fn max(&self, other: &Self) -> Self {
245 Point {
246 x: if self.x > other.x {
247 self.x.clone()
248 } else {
249 other.x.clone()
250 },
251 y: if self.y > other.y {
252 self.y.clone()
253 } else {
254 other.y.clone()
255 },
256 }
257 }
258
259 /// Returns a new point with the minimum values of each dimension from `self` and `other`.
260 ///
261 /// # Arguments
262 ///
263 /// * `other` - A reference to another `Point` to compare with `self`.
264 ///
265 /// # Examples
266 ///
267 /// ```
268 /// # use zed::Point;
269 /// let p1 = Point { x: 3, y: 7 };
270 /// let p2 = Point { x: 5, y: 2 };
271 /// let min_point = p1.min(&p2);
272 /// assert_eq!(min_point, Point { x: 3, y: 2 });
273 /// ```
274 pub fn min(&self, other: &Self) -> Self {
275 Point {
276 x: if self.x <= other.x {
277 self.x.clone()
278 } else {
279 other.x.clone()
280 },
281 y: if self.y <= other.y {
282 self.y.clone()
283 } else {
284 other.y.clone()
285 },
286 }
287 }
288
289 /// Clamps the point to a specified range.
290 ///
291 /// Given a minimum point and a maximum point, this method constrains the current point
292 /// such that its coordinates do not exceed the range defined by the minimum and maximum points.
293 /// If the current point's coordinates are less than the minimum, they are set to the minimum.
294 /// If they are greater than the maximum, they are set to the maximum.
295 ///
296 /// # Arguments
297 ///
298 /// * `min` - A reference to a `Point` representing the minimum allowable coordinates.
299 /// * `max` - A reference to a `Point` representing the maximum allowable coordinates.
300 ///
301 /// # Examples
302 ///
303 /// ```
304 /// # use zed::Point;
305 /// let p = Point { x: 10, y: 20 };
306 /// let min = Point { x: 0, y: 5 };
307 /// let max = Point { x: 15, y: 25 };
308 /// let clamped_p = p.clamp(&min, &max);
309 /// assert_eq!(clamped_p, Point { x: 10, y: 20 });
310 ///
311 /// let p_out_of_bounds = Point { x: -5, y: 30 };
312 /// let clamped_p_out_of_bounds = p_out_of_bounds.clamp(&min, &max);
313 /// assert_eq!(clamped_p_out_of_bounds, Point { x: 0, y: 25 });
314 /// ```
315 pub fn clamp(&self, min: &Self, max: &Self) -> Self {
316 self.max(min).min(max)
317 }
318}
319
320impl<T: Clone + Default + Debug> Clone for Point<T> {
321 fn clone(&self) -> Self {
322 Self {
323 x: self.x.clone(),
324 y: self.y.clone(),
325 }
326 }
327}
328
329/// A structure representing a two-dimensional size with width and height in a given unit.
330///
331/// This struct is generic over the type `T`, which can be any type that implements `Clone`, `Default`, and `Debug`.
332/// It is commonly used to specify dimensions for elements in a UI, such as a window or element.
333#[derive(Refineable, Default, Clone, Copy, PartialEq, Div, Hash, Serialize, Deserialize)]
334#[refineable(Debug)]
335#[repr(C)]
336pub struct Size<T: Clone + Default + Debug> {
337 pub width: T,
338 pub height: T,
339}
340
341/// Constructs a new `Size<T>` with the provided width and height.
342///
343/// # Arguments
344///
345/// * `width` - The width component of the `Size`.
346/// * `height` - The height component of the `Size`.
347///
348/// # Examples
349///
350/// ```
351/// # use zed::Size;
352/// let my_size = size(10, 20);
353/// assert_eq!(my_size.width, 10);
354/// assert_eq!(my_size.height, 20);
355/// ```
356pub fn size<T>(width: T, height: T) -> Size<T>
357where
358 T: Clone + Default + Debug,
359{
360 Size { width, height }
361}
362
363impl<T> Size<T>
364where
365 T: Clone + Default + Debug,
366{
367 /// Applies a function to the width and height of the size, producing a new `Size<U>`.
368 ///
369 /// This method allows for converting a `Size<T>` to a `Size<U>` by specifying a closure
370 /// that defines how to convert between the two types. The closure is applied to both the `width`
371 /// and `height`, resulting in a new size of the desired type.
372 ///
373 /// # Arguments
374 ///
375 /// * `f` - A closure that takes a value of type `T` and returns a value of type `U`.
376 ///
377 /// # Examples
378 ///
379 /// ```
380 /// # use zed::Size;
381 /// let my_size = Size { width: 10, height: 20 };
382 /// let my_new_size = my_size.map(|dimension| dimension as f32 * 1.5);
383 /// assert_eq!(my_new_size, Size { width: 15.0, height: 30.0 });
384 /// ```
385 pub fn map<U>(&self, f: impl Fn(T) -> U) -> Size<U>
386 where
387 U: Clone + Default + Debug,
388 {
389 Size {
390 width: f(self.width.clone()),
391 height: f(self.height.clone()),
392 }
393 }
394}
395
396impl Size<Pixels> {
397 /// Scales the size by a given factor.
398 ///
399 /// This method multiplies both the width and height by the provided scaling factor,
400 /// resulting in a new `Size<ScaledPixels>` that is proportionally larger or smaller
401 /// depending on the factor.
402 ///
403 /// # Arguments
404 ///
405 /// * `factor` - The scaling factor to apply to the width and height.
406 ///
407 /// # Examples
408 ///
409 /// ```
410 /// # use zed::{Size, Pixels, ScaledPixels};
411 /// let size = Size { width: Pixels(100.0), height: Pixels(50.0) };
412 /// let scaled_size = size.scale(2.0);
413 /// assert_eq!(scaled_size, Size { width: ScaledPixels(200.0), height: ScaledPixels(100.0) });
414 /// ```
415 pub fn scale(&self, factor: f32) -> Size<ScaledPixels> {
416 Size {
417 width: self.width.scale(factor),
418 height: self.height.scale(factor),
419 }
420 }
421}
422
423impl<T> Along for Size<T>
424where
425 T: Clone + Default + Debug,
426{
427 type Unit = T;
428
429 fn along(&self, axis: Axis) -> T {
430 match axis {
431 Axis::Horizontal => self.width.clone(),
432 Axis::Vertical => self.height.clone(),
433 }
434 }
435
436 /// Returns the value of this size along the given axis.
437 fn apply_along(&self, axis: Axis, f: impl FnOnce(T) -> T) -> Self {
438 match axis {
439 Axis::Horizontal => Size {
440 width: f(self.width.clone()),
441 height: self.height.clone(),
442 },
443 Axis::Vertical => Size {
444 width: self.width.clone(),
445 height: f(self.height.clone()),
446 },
447 }
448 }
449}
450
451impl<T> Size<T>
452where
453 T: PartialOrd + Clone + Default + Debug,
454{
455 /// Returns a new `Size` with the maximum width and height from `self` and `other`.
456 ///
457 /// # Arguments
458 ///
459 /// * `other` - A reference to another `Size` to compare with `self`.
460 ///
461 /// # Examples
462 ///
463 /// ```
464 /// # use zed::Size;
465 /// let size1 = Size { width: 30, height: 40 };
466 /// let size2 = Size { width: 50, height: 20 };
467 /// let max_size = size1.max(&size2);
468 /// assert_eq!(max_size, Size { width: 50, height: 40 });
469 /// ```
470 pub fn max(&self, other: &Self) -> Self {
471 Size {
472 width: if self.width >= other.width {
473 self.width.clone()
474 } else {
475 other.width.clone()
476 },
477 height: if self.height >= other.height {
478 self.height.clone()
479 } else {
480 other.height.clone()
481 },
482 }
483 }
484}
485
486impl<T> Sub for Size<T>
487where
488 T: Sub<Output = T> + Clone + Default + Debug,
489{
490 type Output = Size<T>;
491
492 fn sub(self, rhs: Self) -> Self::Output {
493 Size {
494 width: self.width - rhs.width,
495 height: self.height - rhs.height,
496 }
497 }
498}
499
500impl<T> Add for Size<T>
501where
502 T: Add<Output = T> + Clone + Default + Debug,
503{
504 type Output = Size<T>;
505
506 fn add(self, rhs: Self) -> Self::Output {
507 Size {
508 width: self.width + rhs.width,
509 height: self.height + rhs.height,
510 }
511 }
512}
513
514impl<T, Rhs> Mul<Rhs> for Size<T>
515where
516 T: Mul<Rhs, Output = Rhs> + Clone + Default + Debug,
517 Rhs: Clone + Default + Debug,
518{
519 type Output = Size<Rhs>;
520
521 fn mul(self, rhs: Rhs) -> Self::Output {
522 Size {
523 width: self.width * rhs.clone(),
524 height: self.height * rhs,
525 }
526 }
527}
528
529impl<T, S> MulAssign<S> for Size<T>
530where
531 T: Mul<S, Output = T> + Clone + Default + Debug,
532 S: Clone,
533{
534 fn mul_assign(&mut self, rhs: S) {
535 self.width = self.width.clone() * rhs.clone();
536 self.height = self.height.clone() * rhs;
537 }
538}
539
540impl<T> Eq for Size<T> where T: Eq + Default + Debug + Clone {}
541
542impl<T> Debug for Size<T>
543where
544 T: Clone + Default + Debug,
545{
546 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
547 write!(f, "Size {{ {:?} × {:?} }}", self.width, self.height)
548 }
549}
550
551impl<T: Clone + Default + Debug> From<Point<T>> for Size<T> {
552 fn from(point: Point<T>) -> Self {
553 Self {
554 width: point.x,
555 height: point.y,
556 }
557 }
558}
559
560impl From<Size<Pixels>> for Size<GlobalPixels> {
561 fn from(size: Size<Pixels>) -> Self {
562 Size {
563 width: GlobalPixels(size.width.0),
564 height: GlobalPixels(size.height.0),
565 }
566 }
567}
568
569impl From<Size<Pixels>> for Size<DefiniteLength> {
570 fn from(size: Size<Pixels>) -> Self {
571 Size {
572 width: size.width.into(),
573 height: size.height.into(),
574 }
575 }
576}
577
578impl From<Size<Pixels>> for Size<AbsoluteLength> {
579 fn from(size: Size<Pixels>) -> Self {
580 Size {
581 width: size.width.into(),
582 height: size.height.into(),
583 }
584 }
585}
586
587impl Size<Length> {
588 /// Returns a `Size` with both width and height set to fill the available space.
589 ///
590 /// This function creates a `Size` instance where both the width and height are set to `Length::Definite(DefiniteLength::Fraction(1.0))`,
591 /// which represents 100% of the available space in both dimensions.
592 ///
593 /// # Returns
594 ///
595 /// A `Size<Length>` that will fill the available space when used in a layout.
596 pub fn full() -> Self {
597 Self {
598 width: relative(1.).into(),
599 height: relative(1.).into(),
600 }
601 }
602}
603
604impl Size<Length> {
605 /// Returns a `Size` with both width and height set to `auto`, which allows the layout engine to determine the size.
606 ///
607 /// This function creates a `Size` instance where both the width and height are set to `Length::Auto`,
608 /// indicating that their size should be computed based on the layout context, such as the content size or
609 /// available space.
610 ///
611 /// # Returns
612 ///
613 /// A `Size<Length>` with width and height set to `Length::Auto`.
614 pub fn auto() -> Self {
615 Self {
616 width: Length::Auto,
617 height: Length::Auto,
618 }
619 }
620}
621
622/// Represents a rectangular area in a 2D space with an origin point and a size.
623///
624/// The `Bounds` struct is generic over a type `T` which represents the type of the coordinate system.
625/// The origin is represented as a `Point<T>` which defines the upper-left corner of the rectangle,
626/// and the size is represented as a `Size<T>` which defines the width and height of the rectangle.
627///
628/// # Examples
629///
630/// ```
631/// # use zed::{Bounds, Point, Size};
632/// let origin = Point { x: 0, y: 0 };
633/// let size = Size { width: 10, height: 20 };
634/// let bounds = Bounds::new(origin, size);
635///
636/// assert_eq!(bounds.origin, origin);
637/// assert_eq!(bounds.size, size);
638/// ```
639#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
640#[refineable(Debug)]
641#[repr(C)]
642pub struct Bounds<T: Clone + Default + Debug> {
643 pub origin: Point<T>,
644 pub size: Size<T>,
645}
646
647impl<T> Bounds<T>
648where
649 T: Clone + Debug + Sub<Output = T> + Default,
650{
651 /// Constructs a `Bounds` from two corner points: the upper-left and lower-right corners.
652 ///
653 /// This function calculates the origin and size of the `Bounds` based on the provided corner points.
654 /// The origin is set to the upper-left corner, and the size is determined by the difference between
655 /// the x and y coordinates of the lower-right and upper-left points.
656 ///
657 /// # Arguments
658 ///
659 /// * `upper_left` - A `Point<T>` representing the upper-left corner of the rectangle.
660 /// * `lower_right` - A `Point<T>` representing the lower-right corner of the rectangle.
661 ///
662 /// # Returns
663 ///
664 /// Returns a `Bounds<T>` that encompasses the area defined by the two corner points.
665 ///
666 /// # Examples
667 ///
668 /// ```
669 /// # use zed::{Bounds, Point};
670 /// let upper_left = Point { x: 0, y: 0 };
671 /// let lower_right = Point { x: 10, y: 10 };
672 /// let bounds = Bounds::from_corners(upper_left, lower_right);
673 ///
674 /// assert_eq!(bounds.origin, upper_left);
675 /// assert_eq!(bounds.size.width, 10);
676 /// assert_eq!(bounds.size.height, 10);
677 /// ```
678 pub fn from_corners(upper_left: Point<T>, lower_right: Point<T>) -> Self {
679 let origin = Point {
680 x: upper_left.x.clone(),
681 y: upper_left.y.clone(),
682 };
683 let size = Size {
684 width: lower_right.x - upper_left.x,
685 height: lower_right.y - upper_left.y,
686 };
687 Bounds { origin, size }
688 }
689
690 /// Creates a new `Bounds` with the specified origin and size.
691 ///
692 /// # Arguments
693 ///
694 /// * `origin` - A `Point<T>` representing the origin of the bounds.
695 /// * `size` - A `Size<T>` representing the size of the bounds.
696 ///
697 /// # Returns
698 ///
699 /// Returns a `Bounds<T>` that has the given origin and size.
700 pub fn new(origin: Point<T>, size: Size<T>) -> Self {
701 Bounds { origin, size }
702 }
703}
704
705impl<T> Bounds<T>
706where
707 T: Clone + Debug + PartialOrd + Add<T, Output = T> + Sub<Output = T> + Default + Half,
708{
709 /// Checks if this `Bounds` intersects with another `Bounds`.
710 ///
711 /// Two `Bounds` instances intersect if they overlap in the 2D space they occupy.
712 /// This method checks if there is any overlapping area between the two bounds.
713 ///
714 /// # Arguments
715 ///
716 /// * `other` - A reference to another `Bounds` to check for intersection with.
717 ///
718 /// # Returns
719 ///
720 /// Returns `true` if there is any intersection between the two bounds, `false` otherwise.
721 ///
722 /// # Examples
723 ///
724 /// ```
725 /// # use zed::{Bounds, Point, Size};
726 /// let bounds1 = Bounds {
727 /// origin: Point { x: 0, y: 0 },
728 /// size: Size { width: 10, height: 10 },
729 /// };
730 /// let bounds2 = Bounds {
731 /// origin: Point { x: 5, y: 5 },
732 /// size: Size { width: 10, height: 10 },
733 /// };
734 /// let bounds3 = Bounds {
735 /// origin: Point { x: 20, y: 20 },
736 /// size: Size { width: 10, height: 10 },
737 /// };
738 ///
739 /// assert_eq!(bounds1.intersects(&bounds2), true); // Overlapping bounds
740 /// assert_eq!(bounds1.intersects(&bounds3), false); // Non-overlapping bounds
741 /// ```
742 pub fn intersects(&self, other: &Bounds<T>) -> bool {
743 let my_lower_right = self.lower_right();
744 let their_lower_right = other.lower_right();
745
746 self.origin.x < their_lower_right.x
747 && my_lower_right.x > other.origin.x
748 && self.origin.y < their_lower_right.y
749 && my_lower_right.y > other.origin.y
750 }
751
752 /// Dilates the bounds by a specified amount in all directions.
753 ///
754 /// This method expands the bounds by the given `amount`, increasing the size
755 /// and adjusting the origin so that the bounds grow outwards equally in all directions.
756 /// The resulting bounds will have its width and height increased by twice the `amount`
757 /// (since it grows in both directions), and the origin will be moved by `-amount`
758 /// in both the x and y directions.
759 ///
760 /// # Arguments
761 ///
762 /// * `amount` - The amount by which to dilate the bounds.
763 ///
764 /// # Examples
765 ///
766 /// ```
767 /// # use zed::{Bounds, Point, Size};
768 /// let mut bounds = Bounds {
769 /// origin: Point { x: 10, y: 10 },
770 /// size: Size { width: 10, height: 10 },
771 /// };
772 /// bounds.dilate(5);
773 /// assert_eq!(bounds, Bounds {
774 /// origin: Point { x: 5, y: 5 },
775 /// size: Size { width: 20, height: 20 },
776 /// });
777 /// ```
778 pub fn dilate(&mut self, amount: T) {
779 self.origin.x = self.origin.x.clone() - amount.clone();
780 self.origin.y = self.origin.y.clone() - amount.clone();
781 let double_amount = amount.clone() + amount;
782 self.size.width = self.size.width.clone() + double_amount.clone();
783 self.size.height = self.size.height.clone() + double_amount;
784 }
785
786 /// Returns the center point of the bounds.
787 ///
788 /// Calculates the center by taking the origin's x and y coordinates and adding half the width and height
789 /// of the bounds, respectively. The center is represented as a `Point<T>` where `T` is the type of the
790 /// coordinate system.
791 ///
792 /// # Returns
793 ///
794 /// A `Point<T>` representing the center of the bounds.
795 ///
796 /// # Examples
797 ///
798 /// ```
799 /// # use zed::{Bounds, Point, Size};
800 /// let bounds = Bounds {
801 /// origin: Point { x: 0, y: 0 },
802 /// size: Size { width: 10, height: 20 },
803 /// };
804 /// let center = bounds.center();
805 /// assert_eq!(center, Point { x: 5, y: 10 });
806 /// ```
807 pub fn center(&self) -> Point<T> {
808 Point {
809 x: self.origin.x.clone() + self.size.width.clone().half(),
810 y: self.origin.y.clone() + self.size.height.clone().half(),
811 }
812 }
813}
814
815impl<T: Clone + Default + Debug + PartialOrd + Add<T, Output = T> + Sub<Output = T>> Bounds<T> {
816 /// Calculates the intersection of two `Bounds` objects.
817 ///
818 /// This method computes the overlapping region of two `Bounds`. If the bounds do not intersect,
819 /// the resulting `Bounds` will have a size with width and height of zero.
820 ///
821 /// # Arguments
822 ///
823 /// * `other` - A reference to another `Bounds` to intersect with.
824 ///
825 /// # Returns
826 ///
827 /// Returns a `Bounds` representing the intersection area. If there is no intersection,
828 /// the returned `Bounds` will have a size with width and height of zero.
829 ///
830 /// # Examples
831 ///
832 /// ```
833 /// # use zed::{Bounds, Point, Size};
834 /// let bounds1 = Bounds {
835 /// origin: Point { x: 0, y: 0 },
836 /// size: Size { width: 10, height: 10 },
837 /// };
838 /// let bounds2 = Bounds {
839 /// origin: Point { x: 5, y: 5 },
840 /// size: Size { width: 10, height: 10 },
841 /// };
842 /// let intersection = bounds1.intersect(&bounds2);
843 ///
844 /// assert_eq!(intersection, Bounds {
845 /// origin: Point { x: 5, y: 5 },
846 /// size: Size { width: 5, height: 5 },
847 /// });
848 /// ```
849 pub fn intersect(&self, other: &Self) -> Self {
850 let upper_left = self.origin.max(&other.origin);
851 let lower_right = self.lower_right().min(&other.lower_right());
852 Self::from_corners(upper_left, lower_right)
853 }
854
855 /// Computes the union of two `Bounds`.
856 ///
857 /// This method calculates the smallest `Bounds` that contains both the current `Bounds` and the `other` `Bounds`.
858 /// The resulting `Bounds` will have an origin that is the minimum of the origins of the two `Bounds`,
859 /// and a size that encompasses the furthest extents of both `Bounds`.
860 ///
861 /// # Arguments
862 ///
863 /// * `other` - A reference to another `Bounds` to create a union with.
864 ///
865 /// # Returns
866 ///
867 /// Returns a `Bounds` representing the union of the two `Bounds`.
868 ///
869 /// # Examples
870 ///
871 /// ```
872 /// # use zed::{Bounds, Point, Size};
873 /// let bounds1 = Bounds {
874 /// origin: Point { x: 0, y: 0 },
875 /// size: Size { width: 10, height: 10 },
876 /// };
877 /// let bounds2 = Bounds {
878 /// origin: Point { x: 5, y: 5 },
879 /// size: Size { width: 15, height: 15 },
880 /// };
881 /// let union_bounds = bounds1.union(&bounds2);
882 ///
883 /// assert_eq!(union_bounds, Bounds {
884 /// origin: Point { x: 0, y: 0 },
885 /// size: Size { width: 20, height: 20 },
886 /// });
887 /// ```
888 pub fn union(&self, other: &Self) -> Self {
889 let top_left = self.origin.min(&other.origin);
890 let bottom_right = self.lower_right().max(&other.lower_right());
891 Bounds::from_corners(top_left, bottom_right)
892 }
893}
894
895impl<T, Rhs> Mul<Rhs> for Bounds<T>
896where
897 T: Mul<Rhs, Output = Rhs> + Clone + Default + Debug,
898 Point<T>: Mul<Rhs, Output = Point<Rhs>>,
899 Rhs: Clone + Default + Debug,
900{
901 type Output = Bounds<Rhs>;
902
903 fn mul(self, rhs: Rhs) -> Self::Output {
904 Bounds {
905 origin: self.origin * rhs.clone(),
906 size: self.size * rhs,
907 }
908 }
909}
910
911impl<T, S> MulAssign<S> for Bounds<T>
912where
913 T: Mul<S, Output = T> + Clone + Default + Debug,
914 S: Clone,
915{
916 fn mul_assign(&mut self, rhs: S) {
917 self.origin *= rhs.clone();
918 self.size *= rhs;
919 }
920}
921
922impl<T, S> Div<S> for Bounds<T>
923where
924 Size<T>: Div<S, Output = Size<T>>,
925 T: Div<S, Output = T> + Default + Clone + Debug,
926 S: Clone,
927{
928 type Output = Self;
929
930 fn div(self, rhs: S) -> Self {
931 Self {
932 origin: self.origin / rhs.clone(),
933 size: self.size / rhs,
934 }
935 }
936}
937
938impl<T> Bounds<T>
939where
940 T: Add<T, Output = T> + Clone + Default + Debug,
941{
942 /// Returns the top edge of the bounds.
943 ///
944 /// # Returns
945 ///
946 /// A value of type `T` representing the y-coordinate of the top edge of the bounds.
947 pub fn top(&self) -> T {
948 self.origin.y.clone()
949 }
950
951 /// Returns the bottom edge of the bounds.
952 ///
953 /// # Returns
954 ///
955 /// A value of type `T` representing the y-coordinate of the bottom edge of the bounds.
956 pub fn bottom(&self) -> T {
957 self.origin.y.clone() + self.size.height.clone()
958 }
959
960 /// Returns the left edge of the bounds.
961 ///
962 /// # Returns
963 ///
964 /// A value of type `T` representing the x-coordinate of the left edge of the bounds.
965 pub fn left(&self) -> T {
966 self.origin.x.clone()
967 }
968
969 /// Returns the right edge of the bounds.
970 ///
971 /// # Returns
972 ///
973 /// A value of type `T` representing the x-coordinate of the right edge of the bounds.
974 pub fn right(&self) -> T {
975 self.origin.x.clone() + self.size.width.clone()
976 }
977
978 /// Returns the upper-right corner point of the bounds.
979 ///
980 /// # Returns
981 ///
982 /// A `Point<T>` representing the upper-right corner of the bounds.
983 ///
984 /// # Examples
985 ///
986 /// ```
987 /// # use zed::{Bounds, Point, Size};
988 /// let bounds = Bounds {
989 /// origin: Point { x: 0, y: 0 },
990 /// size: Size { width: 10, height: 20 },
991 /// };
992 /// let upper_right = bounds.upper_right();
993 /// assert_eq!(upper_right, Point { x: 10, y: 0 });
994 /// ```
995 pub fn upper_right(&self) -> Point<T> {
996 Point {
997 x: self.origin.x.clone() + self.size.width.clone(),
998 y: self.origin.y.clone(),
999 }
1000 }
1001
1002 /// Returns the lower-right corner point of the bounds.
1003 ///
1004 /// # Returns
1005 ///
1006 /// A `Point<T>` representing the lower-right corner of the bounds.
1007 ///
1008 /// # Examples
1009 ///
1010 /// ```
1011 /// # use zed::{Bounds, Point, Size};
1012 /// let bounds = Bounds {
1013 /// origin: Point { x: 0, y: 0 },
1014 /// size: Size { width: 10, height: 20 },
1015 /// };
1016 /// let lower_right = bounds.lower_right();
1017 /// assert_eq!(lower_right, Point { x: 10, y: 20 });
1018 /// ```
1019 pub fn lower_right(&self) -> Point<T> {
1020 Point {
1021 x: self.origin.x.clone() + self.size.width.clone(),
1022 y: self.origin.y.clone() + self.size.height.clone(),
1023 }
1024 }
1025
1026 /// Returns the lower-left corner point of the bounds.
1027 ///
1028 /// # Returns
1029 ///
1030 /// A `Point<T>` representing the lower-left corner of the bounds.
1031 ///
1032 /// # Examples
1033 ///
1034 /// ```
1035 /// # use zed::{Bounds, Point, Size};
1036 /// let bounds = Bounds {
1037 /// origin: Point { x: 0, y: 0 },
1038 /// size: Size { width: 10, height: 20 },
1039 /// };
1040 /// let lower_left = bounds.lower_left();
1041 /// assert_eq!(lower_left, Point { x: 0, y: 20 });
1042 /// ```
1043 pub fn lower_left(&self) -> Point<T> {
1044 Point {
1045 x: self.origin.x.clone(),
1046 y: self.origin.y.clone() + self.size.height.clone(),
1047 }
1048 }
1049}
1050
1051impl<T> Bounds<T>
1052where
1053 T: Add<T, Output = T> + PartialOrd + Clone + Default + Debug,
1054{
1055 /// Checks if the given point is within the bounds.
1056 ///
1057 /// This method determines whether a point lies inside the rectangle defined by the bounds,
1058 /// including the edges. The point is considered inside if its x-coordinate is greater than
1059 /// or equal to the left edge and less than or equal to the right edge, and its y-coordinate
1060 /// is greater than or equal to the top edge and less than or equal to the bottom edge of the bounds.
1061 ///
1062 /// # Arguments
1063 ///
1064 /// * `point` - A reference to a `Point<T>` that represents the point to check.
1065 ///
1066 /// # Returns
1067 ///
1068 /// Returns `true` if the point is within the bounds, `false` otherwise.
1069 ///
1070 /// # Examples
1071 ///
1072 /// ```
1073 /// # use zed::{Point, Bounds};
1074 /// let bounds = Bounds {
1075 /// origin: Point { x: 0, y: 0 },
1076 /// size: Size { width: 10, height: 10 },
1077 /// };
1078 /// let inside_point = Point { x: 5, y: 5 };
1079 /// let outside_point = Point { x: 15, y: 15 };
1080 ///
1081 /// assert!(bounds.contains_point(&inside_point));
1082 /// assert!(!bounds.contains_point(&outside_point));
1083 /// ```
1084 pub fn contains(&self, point: &Point<T>) -> bool {
1085 point.x >= self.origin.x
1086 && point.x <= self.origin.x.clone() + self.size.width.clone()
1087 && point.y >= self.origin.y
1088 && point.y <= self.origin.y.clone() + self.size.height.clone()
1089 }
1090
1091 /// Applies a function to the origin and size of the bounds, producing a new `Bounds<U>`.
1092 ///
1093 /// This method allows for converting a `Bounds<T>` to a `Bounds<U>` by specifying a closure
1094 /// that defines how to convert between the two types. The closure is applied to the `origin` and
1095 /// `size` fields, resulting in new bounds of the desired type.
1096 ///
1097 /// # Arguments
1098 ///
1099 /// * `f` - A closure that takes a value of type `T` and returns a value of type `U`.
1100 ///
1101 /// # Returns
1102 ///
1103 /// Returns a new `Bounds<U>` with the origin and size mapped by the provided function.
1104 ///
1105 /// # Examples
1106 ///
1107 /// ```
1108 /// # use zed::{Bounds, Point, Size};
1109 /// let bounds = Bounds {
1110 /// origin: Point { x: 10.0, y: 10.0 },
1111 /// size: Size { width: 10.0, height: 20.0 },
1112 /// };
1113 /// let new_bounds = bounds.map(|value| value as f64 * 1.5);
1114 ///
1115 /// assert_eq!(new_bounds, Bounds {
1116 /// origin: Point { x: 15.0, y: 15.0 },
1117 /// size: Size { width: 15.0, height: 30.0 },
1118 /// });
1119 pub fn map<U>(&self, f: impl Fn(T) -> U) -> Bounds<U>
1120 where
1121 U: Clone + Default + Debug,
1122 {
1123 Bounds {
1124 origin: self.origin.map(&f),
1125 size: self.size.map(f),
1126 }
1127 }
1128}
1129
1130impl Bounds<Pixels> {
1131 /// Scales the bounds by a given factor, typically used to adjust for display scaling.
1132 ///
1133 /// This method multiplies the origin and size of the bounds by the provided scaling factor,
1134 /// resulting in a new `Bounds<ScaledPixels>` that is proportionally larger or smaller
1135 /// depending on the scaling factor. This can be used to ensure that the bounds are properly
1136 /// scaled for different display densities.
1137 ///
1138 /// # Arguments
1139 ///
1140 /// * `factor` - The scaling factor to apply to the origin and size, typically the display's scaling factor.
1141 ///
1142 /// # Returns
1143 ///
1144 /// Returns a new `Bounds<ScaledPixels>` that represents the scaled bounds.
1145 ///
1146 /// # Examples
1147 ///
1148 /// ```
1149 /// # use zed::{Bounds, Point, Size, Pixels};
1150 /// let bounds = Bounds {
1151 /// origin: Point { x: Pixels(10.0), y: Pixels(20.0) },
1152 /// size: Size { width: Pixels(30.0), height: Pixels(40.0) },
1153 /// };
1154 /// let display_scale_factor = 2.0;
1155 /// let scaled_bounds = bounds.scale(display_scale_factor);
1156 /// assert_eq!(scaled_bounds, Bounds {
1157 /// origin: Point { x: ScaledPixels(20.0), y: ScaledPixels(40.0) },
1158 /// size: Size { width: ScaledPixels(60.0), height: ScaledPixels(80.0) },
1159 /// });
1160 /// ```
1161 pub fn scale(&self, factor: f32) -> Bounds<ScaledPixels> {
1162 Bounds {
1163 origin: self.origin.scale(factor),
1164 size: self.size.scale(factor),
1165 }
1166 }
1167}
1168
1169impl<T: Clone + Debug + Copy + Default> Copy for Bounds<T> {}
1170
1171/// Represents the edges of a box in a 2D space, such as padding or margin.
1172///
1173/// Each field represents the size of the edge on one side of the box: `top`, `right`, `bottom`, and `left`.
1174///
1175/// # Examples
1176///
1177/// ```
1178/// # use zed::Edges;
1179/// let edges = Edges {
1180/// top: 10.0,
1181/// right: 20.0,
1182/// bottom: 30.0,
1183/// left: 40.0,
1184/// };
1185///
1186/// assert_eq!(edges.top, 10.0);
1187/// assert_eq!(edges.right, 20.0);
1188/// assert_eq!(edges.bottom, 30.0);
1189/// assert_eq!(edges.left, 40.0);
1190/// ```
1191#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
1192#[refineable(Debug)]
1193#[repr(C)]
1194pub struct Edges<T: Clone + Default + Debug> {
1195 pub top: T,
1196 pub right: T,
1197 pub bottom: T,
1198 pub left: T,
1199}
1200
1201impl<T> Mul for Edges<T>
1202where
1203 T: Mul<Output = T> + Clone + Default + Debug,
1204{
1205 type Output = Self;
1206
1207 fn mul(self, rhs: Self) -> Self::Output {
1208 Self {
1209 top: self.top.clone() * rhs.top,
1210 right: self.right.clone() * rhs.right,
1211 bottom: self.bottom.clone() * rhs.bottom,
1212 left: self.left.clone() * rhs.left,
1213 }
1214 }
1215}
1216
1217impl<T, S> MulAssign<S> for Edges<T>
1218where
1219 T: Mul<S, Output = T> + Clone + Default + Debug,
1220 S: Clone,
1221{
1222 fn mul_assign(&mut self, rhs: S) {
1223 self.top = self.top.clone() * rhs.clone();
1224 self.right = self.right.clone() * rhs.clone();
1225 self.bottom = self.bottom.clone() * rhs.clone();
1226 self.left = self.left.clone() * rhs;
1227 }
1228}
1229
1230impl<T: Clone + Default + Debug + Copy> Copy for Edges<T> {}
1231
1232impl<T: Clone + Default + Debug> Edges<T> {
1233 /// Constructs `Edges` where all sides are set to the same specified value.
1234 ///
1235 /// This function creates an `Edges` instance with the `top`, `right`, `bottom`, and `left` fields all initialized
1236 /// to the same value provided as an argument. This is useful when you want to have uniform edges around a box,
1237 /// such as padding or margin with the same size on all sides.
1238 ///
1239 /// # Arguments
1240 ///
1241 /// * `value` - The value to set for all four sides of the edges.
1242 ///
1243 /// # Returns
1244 ///
1245 /// An `Edges` instance with all sides set to the given value.
1246 ///
1247 /// # Examples
1248 ///
1249 /// ```
1250 /// # use zed::Edges;
1251 /// let uniform_edges = Edges::all(10.0);
1252 /// assert_eq!(uniform_edges.top, 10.0);
1253 /// assert_eq!(uniform_edges.right, 10.0);
1254 /// assert_eq!(uniform_edges.bottom, 10.0);
1255 /// assert_eq!(uniform_edges.left, 10.0);
1256 /// ```
1257 pub fn all(value: T) -> Self {
1258 Self {
1259 top: value.clone(),
1260 right: value.clone(),
1261 bottom: value.clone(),
1262 left: value,
1263 }
1264 }
1265
1266 /// Applies a function to each field of the `Edges`, producing a new `Edges<U>`.
1267 ///
1268 /// This method allows for converting an `Edges<T>` to an `Edges<U>` by specifying a closure
1269 /// that defines how to convert between the two types. The closure is applied to each field
1270 /// (`top`, `right`, `bottom`, `left`), resulting in new edges of the desired type.
1271 ///
1272 /// # Arguments
1273 ///
1274 /// * `f` - A closure that takes a reference to a value of type `T` and returns a value of type `U`.
1275 ///
1276 /// # Returns
1277 ///
1278 /// Returns a new `Edges<U>` with each field mapped by the provided function.
1279 ///
1280 /// # Examples
1281 ///
1282 /// ```
1283 /// # use zed::Edges;
1284 /// let edges = Edges { top: 10, right: 20, bottom: 30, left: 40 };
1285 /// let edges_float = edges.map(|&value| value as f32 * 1.1);
1286 /// assert_eq!(edges_float, Edges { top: 11.0, right: 22.0, bottom: 33.0, left: 44.0 });
1287 /// ```
1288 pub fn map<U>(&self, f: impl Fn(&T) -> U) -> Edges<U>
1289 where
1290 U: Clone + Default + Debug,
1291 {
1292 Edges {
1293 top: f(&self.top),
1294 right: f(&self.right),
1295 bottom: f(&self.bottom),
1296 left: f(&self.left),
1297 }
1298 }
1299
1300 /// Checks if any of the edges satisfy a given predicate.
1301 ///
1302 /// This method applies a predicate function to each field of the `Edges` and returns `true` if any field satisfies the predicate.
1303 ///
1304 /// # Arguments
1305 ///
1306 /// * `predicate` - A closure that takes a reference to a value of type `T` and returns a `bool`.
1307 ///
1308 /// # Returns
1309 ///
1310 /// Returns `true` if the predicate returns `true` for any of the edge values, `false` otherwise.
1311 ///
1312 /// # Examples
1313 ///
1314 /// ```
1315 /// # use zed::Edges;
1316 /// let edges = Edges {
1317 /// top: 10,
1318 /// right: 0,
1319 /// bottom: 5,
1320 /// left: 0,
1321 /// };
1322 ///
1323 /// assert!(edges.any(|value| *value == 0));
1324 /// assert!(edges.any(|value| *value > 0));
1325 /// assert!(!edges.any(|value| *value > 10));
1326 /// ```
1327 pub fn any<F: Fn(&T) -> bool>(&self, predicate: F) -> bool {
1328 predicate(&self.top)
1329 || predicate(&self.right)
1330 || predicate(&self.bottom)
1331 || predicate(&self.left)
1332 }
1333}
1334
1335impl Edges<Length> {
1336 /// Sets the edges of the `Edges` struct to `auto`, which is a special value that allows the layout engine to automatically determine the size of the edges.
1337 ///
1338 /// This is typically used in layout contexts where the exact size of the edges is not important, or when the size should be calculated based on the content or container.
1339 ///
1340 /// # Returns
1341 ///
1342 /// Returns an `Edges<Length>` with all edges set to `Length::Auto`.
1343 ///
1344 /// # Examples
1345 ///
1346 /// ```
1347 /// # use zed::Edges;
1348 /// let auto_edges = Edges::auto();
1349 /// assert_eq!(auto_edges.top, Length::Auto);
1350 /// assert_eq!(auto_edges.right, Length::Auto);
1351 /// assert_eq!(auto_edges.bottom, Length::Auto);
1352 /// assert_eq!(auto_edges.left, Length::Auto);
1353 /// ```
1354 pub fn auto() -> Self {
1355 Self {
1356 top: Length::Auto,
1357 right: Length::Auto,
1358 bottom: Length::Auto,
1359 left: Length::Auto,
1360 }
1361 }
1362
1363 /// Sets the edges of the `Edges` struct to zero, which means no size or thickness.
1364 ///
1365 /// This is typically used when you want to specify that a box (like a padding or margin area)
1366 /// should have no edges, effectively making it non-existent or invisible in layout calculations.
1367 ///
1368 /// # Returns
1369 ///
1370 /// Returns an `Edges<Length>` with all edges set to zero length.
1371 ///
1372 /// # Examples
1373 ///
1374 /// ```
1375 /// # use zed::Edges;
1376 /// let no_edges = Edges::zero();
1377 /// assert_eq!(no_edges.top, Length::Definite(DefiniteLength::from(Pixels(0.))));
1378 /// assert_eq!(no_edges.right, Length::Definite(DefiniteLength::from(Pixels(0.))));
1379 /// assert_eq!(no_edges.bottom, Length::Definite(DefiniteLength::from(Pixels(0.))));
1380 /// assert_eq!(no_edges.left, Length::Definite(DefiniteLength::from(Pixels(0.))));
1381 /// ```
1382 pub fn zero() -> Self {
1383 Self {
1384 top: px(0.).into(),
1385 right: px(0.).into(),
1386 bottom: px(0.).into(),
1387 left: px(0.).into(),
1388 }
1389 }
1390}
1391
1392impl Edges<DefiniteLength> {
1393 /// Sets the edges of the `Edges` struct to zero, which means no size or thickness.
1394 ///
1395 /// This is typically used when you want to specify that a box (like a padding or margin area)
1396 /// should have no edges, effectively making it non-existent or invisible in layout calculations.
1397 ///
1398 /// # Returns
1399 ///
1400 /// Returns an `Edges<DefiniteLength>` with all edges set to zero length.
1401 ///
1402 /// # Examples
1403 ///
1404 /// ```
1405 /// # use zed::Edges;
1406 /// let no_edges = Edges::zero();
1407 /// assert_eq!(no_edges.top, DefiniteLength::from(zed::px(0.)));
1408 /// assert_eq!(no_edges.right, DefiniteLength::from(zed::px(0.)));
1409 /// assert_eq!(no_edges.bottom, DefiniteLength::from(zed::px(0.)));
1410 /// assert_eq!(no_edges.left, DefiniteLength::from(zed::px(0.)));
1411 /// ```
1412 pub fn zero() -> Self {
1413 Self {
1414 top: px(0.).into(),
1415 right: px(0.).into(),
1416 bottom: px(0.).into(),
1417 left: px(0.).into(),
1418 }
1419 }
1420
1421 /// Converts the `DefiniteLength` to `Pixels` based on the parent size and the REM size.
1422 ///
1423 /// This method allows for a `DefiniteLength` value to be converted into pixels, taking into account
1424 /// the size of the parent element (for percentage-based lengths) and the size of a rem unit (for rem-based lengths).
1425 ///
1426 /// # Arguments
1427 ///
1428 /// * `parent_size` - `Size<AbsoluteLength>` representing the size of the parent element.
1429 /// * `rem_size` - `Pixels` representing the size of one REM unit.
1430 ///
1431 /// # Returns
1432 ///
1433 /// Returns an `Edges<Pixels>` representing the edges with lengths converted to pixels.
1434 ///
1435 /// # Examples
1436 ///
1437 /// ```
1438 /// # use zed::{Edges, DefiniteLength, px, AbsoluteLength, Size};
1439 /// let edges = Edges {
1440 /// top: DefiniteLength::Absolute(AbsoluteLength::Pixels(px(10.0))),
1441 /// right: DefiniteLength::Fraction(0.5),
1442 /// bottom: DefiniteLength::Absolute(AbsoluteLength::Rems(rems(2.0))),
1443 /// left: DefiniteLength::Fraction(0.25),
1444 /// };
1445 /// let parent_size = Size {
1446 /// width: AbsoluteLength::Pixels(px(200.0)),
1447 /// height: AbsoluteLength::Pixels(px(100.0)),
1448 /// };
1449 /// let rem_size = px(16.0);
1450 /// let edges_in_pixels = edges.to_pixels(parent_size, rem_size);
1451 ///
1452 /// assert_eq!(edges_in_pixels.top, px(10.0)); // Absolute length in pixels
1453 /// assert_eq!(edges_in_pixels.right, px(100.0)); // 50% of parent width
1454 /// assert_eq!(edges_in_pixels.bottom, px(32.0)); // 2 rems
1455 /// assert_eq!(edges_in_pixels.left, px(50.0)); // 25% of parent width
1456 /// ```
1457 pub fn to_pixels(&self, parent_size: Size<AbsoluteLength>, rem_size: Pixels) -> Edges<Pixels> {
1458 Edges {
1459 top: self.top.to_pixels(parent_size.height, rem_size),
1460 right: self.right.to_pixels(parent_size.width, rem_size),
1461 bottom: self.bottom.to_pixels(parent_size.height, rem_size),
1462 left: self.left.to_pixels(parent_size.width, rem_size),
1463 }
1464 }
1465}
1466
1467impl Edges<AbsoluteLength> {
1468 /// Sets the edges of the `Edges` struct to zero, which means no size or thickness.
1469 ///
1470 /// This is typically used when you want to specify that a box (like a padding or margin area)
1471 /// should have no edges, effectively making it non-existent or invisible in layout calculations.
1472 ///
1473 /// # Returns
1474 ///
1475 /// Returns an `Edges<AbsoluteLength>` with all edges set to zero length.
1476 ///
1477 /// # Examples
1478 ///
1479 /// ```
1480 /// # use zed::Edges;
1481 /// let no_edges = Edges::zero();
1482 /// assert_eq!(no_edges.top, AbsoluteLength::Pixels(Pixels(0.0)));
1483 /// assert_eq!(no_edges.right, AbsoluteLength::Pixels(Pixels(0.0)));
1484 /// assert_eq!(no_edges.bottom, AbsoluteLength::Pixels(Pixels(0.0)));
1485 /// assert_eq!(no_edges.left, AbsoluteLength::Pixels(Pixels(0.0)));
1486 /// ```
1487 pub fn zero() -> Self {
1488 Self {
1489 top: px(0.).into(),
1490 right: px(0.).into(),
1491 bottom: px(0.).into(),
1492 left: px(0.).into(),
1493 }
1494 }
1495
1496 /// Converts the `AbsoluteLength` to `Pixels` based on the `rem_size`.
1497 ///
1498 /// If the `AbsoluteLength` is already in pixels, it simply returns the corresponding `Pixels` value.
1499 /// If the `AbsoluteLength` is in rems, it multiplies the number of rems by the `rem_size` to convert it to pixels.
1500 ///
1501 /// # Arguments
1502 ///
1503 /// * `rem_size` - The size of one rem unit in pixels.
1504 ///
1505 /// # Returns
1506 ///
1507 /// Returns an `Edges<Pixels>` representing the edges with lengths converted to pixels.
1508 ///
1509 /// # Examples
1510 ///
1511 /// ```
1512 /// # use zed::{Edges, AbsoluteLength, Pixels, px};
1513 /// let edges = Edges {
1514 /// top: AbsoluteLength::Pixels(px(10.0)),
1515 /// right: AbsoluteLength::Rems(rems(1.0)),
1516 /// bottom: AbsoluteLength::Pixels(px(20.0)),
1517 /// left: AbsoluteLength::Rems(rems(2.0)),
1518 /// };
1519 /// let rem_size = px(16.0);
1520 /// let edges_in_pixels = edges.to_pixels(rem_size);
1521 ///
1522 /// assert_eq!(edges_in_pixels.top, px(10.0)); // Already in pixels
1523 /// assert_eq!(edges_in_pixels.right, px(16.0)); // 1 rem converted to pixels
1524 /// assert_eq!(edges_in_pixels.bottom, px(20.0)); // Already in pixels
1525 /// assert_eq!(edges_in_pixels.left, px(32.0)); // 2 rems converted to pixels
1526 /// ```
1527 pub fn to_pixels(&self, rem_size: Pixels) -> Edges<Pixels> {
1528 Edges {
1529 top: self.top.to_pixels(rem_size),
1530 right: self.right.to_pixels(rem_size),
1531 bottom: self.bottom.to_pixels(rem_size),
1532 left: self.left.to_pixels(rem_size),
1533 }
1534 }
1535}
1536
1537impl Edges<Pixels> {
1538 /// Scales the `Edges<Pixels>` by a given factor, returning `Edges<ScaledPixels>`.
1539 ///
1540 /// This method is typically used for adjusting the edge sizes for different display densities or scaling factors.
1541 ///
1542 /// # Arguments
1543 ///
1544 /// * `factor` - The scaling factor to apply to each edge.
1545 ///
1546 /// # Returns
1547 ///
1548 /// Returns a new `Edges<ScaledPixels>` where each edge is the result of scaling the original edge by the given factor.
1549 ///
1550 /// # Examples
1551 ///
1552 /// ```
1553 /// # use zed::{Edges, Pixels};
1554 /// let edges = Edges {
1555 /// top: Pixels(10.0),
1556 /// right: Pixels(20.0),
1557 /// bottom: Pixels(30.0),
1558 /// left: Pixels(40.0),
1559 /// };
1560 /// let scaled_edges = edges.scale(2.0);
1561 /// assert_eq!(scaled_edges.top, ScaledPixels(20.0));
1562 /// assert_eq!(scaled_edges.right, ScaledPixels(40.0));
1563 /// assert_eq!(scaled_edges.bottom, ScaledPixels(60.0));
1564 /// assert_eq!(scaled_edges.left, ScaledPixels(80.0));
1565 /// ```
1566 pub fn scale(&self, factor: f32) -> Edges<ScaledPixels> {
1567 Edges {
1568 top: self.top.scale(factor),
1569 right: self.right.scale(factor),
1570 bottom: self.bottom.scale(factor),
1571 left: self.left.scale(factor),
1572 }
1573 }
1574
1575 /// Returns the maximum value of any edge.
1576 ///
1577 /// # Returns
1578 ///
1579 /// The maximum `Pixels` value among all four edges.
1580 pub fn max(&self) -> Pixels {
1581 self.top.max(self.right).max(self.bottom).max(self.left)
1582 }
1583}
1584
1585impl From<f32> for Edges<Pixels> {
1586 fn from(val: f32) -> Self {
1587 Edges {
1588 top: val.into(),
1589 right: val.into(),
1590 bottom: val.into(),
1591 left: val.into(),
1592 }
1593 }
1594}
1595
1596/// Represents the corners of a box in a 2D space, such as border radius.
1597///
1598/// Each field represents the size of the corner on one side of the box: `top_left`, `top_right`, `bottom_right`, and `bottom_left`.
1599#[derive(Refineable, Clone, Default, Debug, Eq, PartialEq)]
1600#[refineable(Debug)]
1601#[repr(C)]
1602pub struct Corners<T: Clone + Default + Debug> {
1603 pub top_left: T,
1604 pub top_right: T,
1605 pub bottom_right: T,
1606 pub bottom_left: T,
1607}
1608
1609impl<T> Corners<T>
1610where
1611 T: Clone + Default + Debug,
1612{
1613 /// Constructs `Corners` where all sides are set to the same specified value.
1614 ///
1615 /// This function creates a `Corners` instance with the `top_left`, `top_right`, `bottom_right`, and `bottom_left` fields all initialized
1616 /// to the same value provided as an argument. This is useful when you want to have uniform corners around a box,
1617 /// such as a uniform border radius on a rectangle.
1618 ///
1619 /// # Arguments
1620 ///
1621 /// * `value` - The value to set for all four corners.
1622 ///
1623 /// # Returns
1624 ///
1625 /// An `Corners` instance with all corners set to the given value.
1626 ///
1627 /// # Examples
1628 ///
1629 /// ```
1630 /// # use zed::Corners;
1631 /// let uniform_corners = Corners::all(5.0);
1632 /// assert_eq!(uniform_corners.top_left, 5.0);
1633 /// assert_eq!(uniform_corners.top_right, 5.0);
1634 /// assert_eq!(uniform_corners.bottom_right, 5.0);
1635 /// assert_eq!(uniform_corners.bottom_left, 5.0);
1636 /// ```
1637 pub fn all(value: T) -> Self {
1638 Self {
1639 top_left: value.clone(),
1640 top_right: value.clone(),
1641 bottom_right: value.clone(),
1642 bottom_left: value,
1643 }
1644 }
1645}
1646
1647impl Corners<AbsoluteLength> {
1648 /// Converts the `AbsoluteLength` to `Pixels` based on the provided size and rem size, ensuring the resulting
1649 /// `Pixels` do not exceed half of the maximum of the provided size's width and height.
1650 ///
1651 /// This method is particularly useful when dealing with corner radii, where the radius in pixels should not
1652 /// exceed half the size of the box it applies to, to avoid the corners overlapping.
1653 ///
1654 /// # Arguments
1655 ///
1656 /// * `size` - The `Size<Pixels>` against which the maximum allowable radius is determined.
1657 /// * `rem_size` - The size of one REM unit in pixels, used for conversion if the `AbsoluteLength` is in REMs.
1658 ///
1659 /// # Returns
1660 ///
1661 /// Returns a `Corners<Pixels>` instance with each corner's length converted to pixels and clamped to the
1662 /// maximum allowable radius based on the provided size.
1663 ///
1664 /// # Examples
1665 ///
1666 /// ```
1667 /// # use zed::{Corners, AbsoluteLength, Pixels, Size};
1668 /// let corners = Corners {
1669 /// top_left: AbsoluteLength::Pixels(Pixels(15.0)),
1670 /// top_right: AbsoluteLength::Rems(Rems(1.0)),
1671 /// bottom_right: AbsoluteLength::Pixels(Pixels(20.0)),
1672 /// bottom_left: AbsoluteLength::Rems(Rems(2.0)),
1673 /// };
1674 /// let size = Size { width: Pixels(100.0), height: Pixels(50.0) };
1675 /// let rem_size = Pixels(16.0);
1676 /// let corners_in_pixels = corners.to_pixels(size, rem_size);
1677 ///
1678 /// // The resulting corners should not exceed half the size of the smallest dimension (50.0 / 2.0 = 25.0).
1679 /// assert_eq!(corners_in_pixels.top_left, Pixels(15.0));
1680 /// assert_eq!(corners_in_pixels.top_right, Pixels(16.0)); // 1 rem converted to pixels
1681 /// assert_eq!(corners_in_pixels.bottom_right, Pixels(20.0).min(Pixels(25.0))); // Clamped to 25.0
1682 /// assert_eq!(corners_in_pixels.bottom_left, Pixels(32.0).min(Pixels(25.0))); // 2 rems converted to pixels and clamped
1683 /// ```
1684 pub fn to_pixels(&self, size: Size<Pixels>, rem_size: Pixels) -> Corners<Pixels> {
1685 let max = size.width.max(size.height) / 2.;
1686 Corners {
1687 top_left: self.top_left.to_pixels(rem_size).min(max),
1688 top_right: self.top_right.to_pixels(rem_size).min(max),
1689 bottom_right: self.bottom_right.to_pixels(rem_size).min(max),
1690 bottom_left: self.bottom_left.to_pixels(rem_size).min(max),
1691 }
1692 }
1693}
1694
1695impl Corners<Pixels> {
1696 /// Scales the `Corners<Pixels>` by a given factor, returning `Corners<ScaledPixels>`.
1697 ///
1698 /// This method is typically used for adjusting the corner sizes for different display densities or scaling factors.
1699 ///
1700 /// # Arguments
1701 ///
1702 /// * `factor` - The scaling factor to apply to each corner.
1703 ///
1704 /// # Returns
1705 ///
1706 /// Returns a new `Corners<ScaledPixels>` where each corner is the result of scaling the original corner by the given factor.
1707 ///
1708 /// # Examples
1709 ///
1710 /// ```
1711 /// # use zed::{Corners, Pixels};
1712 /// let corners = Corners {
1713 /// top_left: Pixels(10.0),
1714 /// top_right: Pixels(20.0),
1715 /// bottom_right: Pixels(30.0),
1716 /// bottom_left: Pixels(40.0),
1717 /// };
1718 /// let scaled_corners = corners.scale(2.0);
1719 /// assert_eq!(scaled_corners.top_left, ScaledPixels(20.0));
1720 /// assert_eq!(scaled_corners.top_right, ScaledPixels(40.0));
1721 /// assert_eq!(scaled_corners.bottom_right, ScaledPixels(60.0));
1722 /// assert_eq!(scaled_corners.bottom_left, ScaledPixels(80.0));
1723 /// ```
1724 pub fn scale(&self, factor: f32) -> Corners<ScaledPixels> {
1725 Corners {
1726 top_left: self.top_left.scale(factor),
1727 top_right: self.top_right.scale(factor),
1728 bottom_right: self.bottom_right.scale(factor),
1729 bottom_left: self.bottom_left.scale(factor),
1730 }
1731 }
1732
1733 /// Returns the maximum value of any corner.
1734 ///
1735 /// # Returns
1736 ///
1737 /// The maximum `Pixels` value among all four corners.
1738 pub fn max(&self) -> Pixels {
1739 self.top_left
1740 .max(self.top_right)
1741 .max(self.bottom_right)
1742 .max(self.bottom_left)
1743 }
1744}
1745
1746impl<T: Clone + Default + Debug> Corners<T> {
1747 /// Applies a function to each field of the `Corners`, producing a new `Corners<U>`.
1748 ///
1749 /// This method allows for converting a `Corners<T>` to a `Corners<U>` by specifying a closure
1750 /// that defines how to convert between the two types. The closure is applied to each field
1751 /// (`top_left`, `top_right`, `bottom_right`, `bottom_left`), resulting in new corners of the desired type.
1752 ///
1753 /// # Arguments
1754 ///
1755 /// * `f` - A closure that takes a reference to a value of type `T` and returns a value of type `U`.
1756 ///
1757 /// # Returns
1758 ///
1759 /// Returns a new `Corners<U>` with each field mapped by the provided function.
1760 ///
1761 /// # Examples
1762 ///
1763 /// ```
1764 /// # use zed::{Corners, Pixels};
1765 /// let corners = Corners {
1766 /// top_left: Pixels(10.0),
1767 /// top_right: Pixels(20.0),
1768 /// bottom_right: Pixels(30.0),
1769 /// bottom_left: Pixels(40.0),
1770 /// };
1771 /// let corners_in_rems = corners.map(|&px| Rems(px.0 / 16.0));
1772 /// assert_eq!(corners_in_rems, Corners {
1773 /// top_left: Rems(0.625),
1774 /// top_right: Rems(1.25),
1775 /// bottom_right: Rems(1.875),
1776 /// bottom_left: Rems(2.5),
1777 /// });
1778 /// ```
1779 pub fn map<U>(&self, f: impl Fn(&T) -> U) -> Corners<U>
1780 where
1781 U: Clone + Default + Debug,
1782 {
1783 Corners {
1784 top_left: f(&self.top_left),
1785 top_right: f(&self.top_right),
1786 bottom_right: f(&self.bottom_right),
1787 bottom_left: f(&self.bottom_left),
1788 }
1789 }
1790}
1791
1792impl<T> Mul for Corners<T>
1793where
1794 T: Mul<Output = T> + Clone + Default + Debug,
1795{
1796 type Output = Self;
1797
1798 fn mul(self, rhs: Self) -> Self::Output {
1799 Self {
1800 top_left: self.top_left.clone() * rhs.top_left,
1801 top_right: self.top_right.clone() * rhs.top_right,
1802 bottom_right: self.bottom_right.clone() * rhs.bottom_right,
1803 bottom_left: self.bottom_left.clone() * rhs.bottom_left,
1804 }
1805 }
1806}
1807
1808impl<T, S> MulAssign<S> for Corners<T>
1809where
1810 T: Mul<S, Output = T> + Clone + Default + Debug,
1811 S: Clone,
1812{
1813 fn mul_assign(&mut self, rhs: S) {
1814 self.top_left = self.top_left.clone() * rhs.clone();
1815 self.top_right = self.top_right.clone() * rhs.clone();
1816 self.bottom_right = self.bottom_right.clone() * rhs.clone();
1817 self.bottom_left = self.bottom_left.clone() * rhs;
1818 }
1819}
1820
1821impl<T> Copy for Corners<T> where T: Copy + Clone + Default + Debug {}
1822
1823impl From<f32> for Corners<Pixels> {
1824 fn from(val: f32) -> Self {
1825 Corners {
1826 top_left: val.into(),
1827 top_right: val.into(),
1828 bottom_right: val.into(),
1829 bottom_left: val.into(),
1830 }
1831 }
1832}
1833
1834impl From<Pixels> for Corners<Pixels> {
1835 fn from(val: Pixels) -> Self {
1836 Corners {
1837 top_left: val,
1838 top_right: val,
1839 bottom_right: val,
1840 bottom_left: val,
1841 }
1842 }
1843}
1844
1845/// Represents a length in pixels, the base unit of measurement in the UI framework.
1846///
1847/// `Pixels` is a value type that represents an absolute length in pixels, which is used
1848/// for specifying sizes, positions, and distances in the UI. It is the fundamental unit
1849/// of measurement for all visual elements and layout calculations.
1850///
1851/// The inner value is an `f32`, allowing for sub-pixel precision which can be useful for
1852/// anti-aliasing and animations. However, when applied to actual pixel grids, the value
1853/// is typically rounded to the nearest integer.
1854///
1855/// # Examples
1856///
1857/// ```
1858/// use zed::Pixels;
1859///
1860/// // Define a length of 10 pixels
1861/// let length = Pixels(10.0);
1862///
1863/// // Define a length and scale it by a factor of 2
1864/// let scaled_length = length.scale(2.0);
1865/// assert_eq!(scaled_length, Pixels(20.0));
1866/// ```
1867#[derive(
1868 Clone,
1869 Copy,
1870 Default,
1871 Add,
1872 AddAssign,
1873 Sub,
1874 SubAssign,
1875 Neg,
1876 Div,
1877 DivAssign,
1878 PartialEq,
1879 Serialize,
1880 Deserialize,
1881)]
1882#[repr(transparent)]
1883pub struct Pixels(pub f32);
1884
1885impl std::ops::Div for Pixels {
1886 type Output = f32;
1887
1888 fn div(self, rhs: Self) -> Self::Output {
1889 self.0 / rhs.0
1890 }
1891}
1892
1893impl std::ops::DivAssign for Pixels {
1894 fn div_assign(&mut self, rhs: Self) {
1895 *self = Self(self.0 / rhs.0);
1896 }
1897}
1898
1899impl std::ops::RemAssign for Pixels {
1900 fn rem_assign(&mut self, rhs: Self) {
1901 self.0 %= rhs.0;
1902 }
1903}
1904
1905impl std::ops::Rem for Pixels {
1906 type Output = Self;
1907
1908 fn rem(self, rhs: Self) -> Self {
1909 Self(self.0 % rhs.0)
1910 }
1911}
1912
1913impl Mul<f32> for Pixels {
1914 type Output = Pixels;
1915
1916 fn mul(self, other: f32) -> Pixels {
1917 Pixels(self.0 * other)
1918 }
1919}
1920
1921impl Mul<usize> for Pixels {
1922 type Output = Pixels;
1923
1924 fn mul(self, other: usize) -> Pixels {
1925 Pixels(self.0 * other as f32)
1926 }
1927}
1928
1929impl Mul<Pixels> for f32 {
1930 type Output = Pixels;
1931
1932 fn mul(self, rhs: Pixels) -> Self::Output {
1933 Pixels(self * rhs.0)
1934 }
1935}
1936
1937impl MulAssign<f32> for Pixels {
1938 fn mul_assign(&mut self, other: f32) {
1939 self.0 *= other;
1940 }
1941}
1942
1943impl Pixels {
1944 /// Represents zero pixels.
1945 pub const ZERO: Pixels = Pixels(0.0);
1946 /// The maximum value that can be represented by `Pixels`.
1947 pub const MAX: Pixels = Pixels(f32::MAX);
1948
1949 /// Floors the `Pixels` value to the nearest whole number.
1950 ///
1951 /// # Returns
1952 ///
1953 /// Returns a new `Pixels` instance with the floored value.
1954 pub fn floor(&self) -> Self {
1955 Self(self.0.floor())
1956 }
1957
1958 /// Rounds the `Pixels` value to the nearest whole number.
1959 ///
1960 /// # Returns
1961 ///
1962 /// Returns a new `Pixels` instance with the rounded value.
1963 pub fn round(&self) -> Self {
1964 Self(self.0.round())
1965 }
1966
1967 /// Returns the ceiling of the `Pixels` value to the nearest whole number.
1968 ///
1969 /// # Returns
1970 ///
1971 /// Returns a new `Pixels` instance with the ceiling value.
1972 pub fn ceil(&self) -> Self {
1973 Self(self.0.ceil())
1974 }
1975
1976 /// Scales the `Pixels` value by a given factor, producing `ScaledPixels`.
1977 ///
1978 /// This method is used when adjusting pixel values for display scaling factors,
1979 /// such as high DPI (dots per inch) or Retina displays, where the pixel density is higher and
1980 /// thus requires scaling to maintain visual consistency and readability.
1981 ///
1982 /// The resulting `ScaledPixels` represent the scaled value which can be used for rendering
1983 /// calculations where display scaling is considered.
1984 pub fn scale(&self, factor: f32) -> ScaledPixels {
1985 ScaledPixels(self.0 * factor)
1986 }
1987
1988 /// Raises the `Pixels` value to a given power.
1989 ///
1990 /// # Arguments
1991 ///
1992 /// * `exponent` - The exponent to raise the `Pixels` value by.
1993 ///
1994 /// # Returns
1995 ///
1996 /// Returns a new `Pixels` instance with the value raised to the given exponent.
1997 pub fn pow(&self, exponent: f32) -> Self {
1998 Self(self.0.powf(exponent))
1999 }
2000
2001 /// Returns the absolute value of the `Pixels`.
2002 ///
2003 /// # Returns
2004 ///
2005 /// A new `Pixels` instance with the absolute value of the original `Pixels`.
2006 pub fn abs(&self) -> Self {
2007 Self(self.0.abs())
2008 }
2009}
2010
2011impl Mul<Pixels> for Pixels {
2012 type Output = Pixels;
2013
2014 fn mul(self, rhs: Pixels) -> Self::Output {
2015 Pixels(self.0 * rhs.0)
2016 }
2017}
2018
2019impl Eq for Pixels {}
2020
2021impl PartialOrd for Pixels {
2022 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
2023 self.0.partial_cmp(&other.0)
2024 }
2025}
2026
2027impl Ord for Pixels {
2028 fn cmp(&self, other: &Self) -> cmp::Ordering {
2029 self.partial_cmp(other).unwrap()
2030 }
2031}
2032
2033impl std::hash::Hash for Pixels {
2034 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
2035 self.0.to_bits().hash(state);
2036 }
2037}
2038
2039impl From<f64> for Pixels {
2040 fn from(pixels: f64) -> Self {
2041 Pixels(pixels as f32)
2042 }
2043}
2044
2045impl From<f32> for Pixels {
2046 fn from(pixels: f32) -> Self {
2047 Pixels(pixels)
2048 }
2049}
2050
2051impl Debug for Pixels {
2052 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2053 write!(f, "{} px", self.0)
2054 }
2055}
2056
2057impl From<Pixels> for f32 {
2058 fn from(pixels: Pixels) -> Self {
2059 pixels.0
2060 }
2061}
2062
2063impl From<&Pixels> for f32 {
2064 fn from(pixels: &Pixels) -> Self {
2065 pixels.0
2066 }
2067}
2068
2069impl From<Pixels> for f64 {
2070 fn from(pixels: Pixels) -> Self {
2071 pixels.0 as f64
2072 }
2073}
2074
2075impl From<Pixels> for u32 {
2076 fn from(pixels: Pixels) -> Self {
2077 pixels.0 as u32
2078 }
2079}
2080
2081impl From<u32> for Pixels {
2082 fn from(pixels: u32) -> Self {
2083 Pixels(pixels as f32)
2084 }
2085}
2086
2087impl From<Pixels> for usize {
2088 fn from(pixels: Pixels) -> Self {
2089 pixels.0 as usize
2090 }
2091}
2092
2093impl From<usize> for Pixels {
2094 fn from(pixels: usize) -> Self {
2095 Pixels(pixels as f32)
2096 }
2097}
2098
2099/// Represents physical pixels on the display.
2100///
2101/// `DevicePixels` is a unit of measurement that refers to the actual pixels on a device's screen.
2102/// This type is used when precise pixel manipulation is required, such as rendering graphics or
2103/// interfacing with hardware that operates on the pixel level. Unlike logical pixels that may be
2104/// affected by the device's scale factor, `DevicePixels` always correspond to real pixels on the
2105/// display.
2106#[derive(
2107 Add, AddAssign, Clone, Copy, Default, Div, Eq, Hash, Ord, PartialEq, PartialOrd, Sub, SubAssign,
2108)]
2109#[repr(transparent)]
2110pub struct DevicePixels(pub(crate) i32);
2111
2112impl DevicePixels {
2113 /// Converts the `DevicePixels` value to the number of bytes needed to represent it in memory.
2114 ///
2115 /// This function is useful when working with graphical data that needs to be stored in a buffer,
2116 /// such as images or framebuffers, where each pixel may be represented by a specific number of bytes.
2117 ///
2118 /// # Arguments
2119 ///
2120 /// * `bytes_per_pixel` - The number of bytes used to represent a single pixel.
2121 ///
2122 /// # Returns
2123 ///
2124 /// The number of bytes required to represent the `DevicePixels` value in memory.
2125 ///
2126 /// # Examples
2127 ///
2128 /// ```
2129 /// # use zed::DevicePixels;
2130 /// let pixels = DevicePixels(10); // 10 device pixels
2131 /// let bytes_per_pixel = 4; // Assume each pixel is represented by 4 bytes (e.g., RGBA)
2132 /// let total_bytes = pixels.to_bytes(bytes_per_pixel);
2133 /// assert_eq!(total_bytes, 40); // 10 pixels * 4 bytes/pixel = 40 bytes
2134 /// ```
2135 pub fn to_bytes(&self, bytes_per_pixel: u8) -> u32 {
2136 self.0 as u32 * bytes_per_pixel as u32
2137 }
2138}
2139
2140impl fmt::Debug for DevicePixels {
2141 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2142 write!(f, "{} px (device)", self.0)
2143 }
2144}
2145
2146impl From<DevicePixels> for i32 {
2147 fn from(device_pixels: DevicePixels) -> Self {
2148 device_pixels.0
2149 }
2150}
2151
2152impl From<i32> for DevicePixels {
2153 fn from(device_pixels: i32) -> Self {
2154 DevicePixels(device_pixels)
2155 }
2156}
2157
2158impl From<u32> for DevicePixels {
2159 fn from(device_pixels: u32) -> Self {
2160 DevicePixels(device_pixels as i32)
2161 }
2162}
2163
2164impl From<DevicePixels> for u32 {
2165 fn from(device_pixels: DevicePixels) -> Self {
2166 device_pixels.0 as u32
2167 }
2168}
2169
2170impl From<DevicePixels> for u64 {
2171 fn from(device_pixels: DevicePixels) -> Self {
2172 device_pixels.0 as u64
2173 }
2174}
2175
2176impl From<u64> for DevicePixels {
2177 fn from(device_pixels: u64) -> Self {
2178 DevicePixels(device_pixels as i32)
2179 }
2180}
2181
2182impl From<DevicePixels> for usize {
2183 fn from(device_pixels: DevicePixels) -> Self {
2184 device_pixels.0 as usize
2185 }
2186}
2187
2188impl From<usize> for DevicePixels {
2189 fn from(device_pixels: usize) -> Self {
2190 DevicePixels(device_pixels as i32)
2191 }
2192}
2193
2194/// Represents scaled pixels that take into account the device's scale factor.
2195///
2196/// `ScaledPixels` are used to ensure that UI elements appear at the correct size on devices
2197/// with different pixel densities. When a device has a higher scale factor (such as Retina displays),
2198/// a single logical pixel may correspond to multiple physical pixels. By using `ScaledPixels`,
2199/// dimensions and positions can be specified in a way that scales appropriately across different
2200/// display resolutions.
2201#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
2202#[repr(transparent)]
2203pub struct ScaledPixels(pub(crate) f32);
2204
2205impl ScaledPixels {
2206 /// Floors the `ScaledPixels` value to the nearest whole number.
2207 ///
2208 /// # Returns
2209 ///
2210 /// Returns a new `ScaledPixels` instance with the floored value.
2211 pub fn floor(&self) -> Self {
2212 Self(self.0.floor())
2213 }
2214
2215 /// Rounds the `ScaledPixels` value to the nearest whole number.
2216 ///
2217 /// # Returns
2218 ///
2219 /// Returns a new `ScaledPixels` instance with the rounded value.
2220 pub fn ceil(&self) -> Self {
2221 Self(self.0.ceil())
2222 }
2223}
2224
2225impl Eq for ScaledPixels {}
2226
2227impl Debug for ScaledPixels {
2228 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2229 write!(f, "{} px (scaled)", self.0)
2230 }
2231}
2232
2233impl From<ScaledPixels> for DevicePixels {
2234 fn from(scaled: ScaledPixels) -> Self {
2235 DevicePixels(scaled.0.ceil() as i32)
2236 }
2237}
2238
2239impl From<DevicePixels> for ScaledPixels {
2240 fn from(device: DevicePixels) -> Self {
2241 ScaledPixels(device.0 as f32)
2242 }
2243}
2244
2245impl From<ScaledPixels> for f64 {
2246 fn from(scaled_pixels: ScaledPixels) -> Self {
2247 scaled_pixels.0 as f64
2248 }
2249}
2250
2251/// Represents pixels in a global coordinate space, which can span across multiple displays.
2252///
2253/// `GlobalPixels` is used when dealing with a coordinate system that is not limited to a single
2254/// display's boundaries. This type is particularly useful in multi-monitor setups where
2255/// positioning and measurements need to be consistent and relative to a "global" origin point
2256/// rather than being relative to any individual display.
2257#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
2258#[repr(transparent)]
2259pub struct GlobalPixels(pub(crate) f32);
2260
2261impl Debug for GlobalPixels {
2262 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2263 write!(f, "{} px (global coordinate space)", self.0)
2264 }
2265}
2266
2267impl From<GlobalPixels> for f64 {
2268 fn from(global_pixels: GlobalPixels) -> Self {
2269 global_pixels.0 as f64
2270 }
2271}
2272
2273impl From<f64> for GlobalPixels {
2274 fn from(global_pixels: f64) -> Self {
2275 GlobalPixels(global_pixels as f32)
2276 }
2277}
2278
2279/// Represents a length in rems, a unit based on the font-size of the window, which can be assigned with [`WindowContext::set_rem_size`][set_rem_size].
2280///
2281/// Rems are used for defining lengths that are scalable and consistent across different UI elements.
2282/// The value of `1rem` is typically equal to the font-size of the root element (often the `<html>` element in browsers),
2283/// making it a flexible unit that adapts to the user's text size preferences. In this framework, `rems` serve a similar
2284/// purpose, allowing for scalable and accessible design that can adjust to different display settings or user preferences.
2285///
2286/// For example, if the root element's font-size is `16px`, then `1rem` equals `16px`. A length of `2rems` would then be `32px`.
2287///
2288/// [set_rem_size]: crate::WindowContext::set_rem_size
2289#[derive(Clone, Copy, Default, Add, Sub, Mul, Div, Neg, PartialEq)]
2290pub struct Rems(pub f32);
2291
2292impl Mul<Pixels> for Rems {
2293 type Output = Pixels;
2294
2295 fn mul(self, other: Pixels) -> Pixels {
2296 Pixels(self.0 * other.0)
2297 }
2298}
2299
2300impl Debug for Rems {
2301 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2302 write!(f, "{} rem", self.0)
2303 }
2304}
2305
2306/// Represents an absolute length in pixels or rems.
2307///
2308/// `AbsoluteLength` can be either a fixed number of pixels, which is an absolute measurement not
2309/// affected by the current font size, or a number of rems, which is relative to the font size of
2310/// the root element. It is used for specifying dimensions that are either independent of or
2311/// related to the typographic scale.
2312#[derive(Clone, Copy, Debug, Neg, PartialEq)]
2313pub enum AbsoluteLength {
2314 /// A length in pixels.
2315 Pixels(Pixels),
2316 /// A length in rems.
2317 Rems(Rems),
2318}
2319
2320impl AbsoluteLength {
2321 /// Checks if the absolute length is zero.
2322 pub fn is_zero(&self) -> bool {
2323 match self {
2324 AbsoluteLength::Pixels(px) => px.0 == 0.0,
2325 AbsoluteLength::Rems(rems) => rems.0 == 0.0,
2326 }
2327 }
2328}
2329
2330impl From<Pixels> for AbsoluteLength {
2331 fn from(pixels: Pixels) -> Self {
2332 AbsoluteLength::Pixels(pixels)
2333 }
2334}
2335
2336impl From<Rems> for AbsoluteLength {
2337 fn from(rems: Rems) -> Self {
2338 AbsoluteLength::Rems(rems)
2339 }
2340}
2341
2342impl AbsoluteLength {
2343 /// Converts an `AbsoluteLength` to `Pixels` based on a given `rem_size`.
2344 ///
2345 /// # Arguments
2346 ///
2347 /// * `rem_size` - The size of one rem in pixels.
2348 ///
2349 /// # Returns
2350 ///
2351 /// Returns the `AbsoluteLength` as `Pixels`.
2352 ///
2353 /// # Examples
2354 ///
2355 /// ```
2356 /// # use zed::{AbsoluteLength, Pixels};
2357 /// let length_in_pixels = AbsoluteLength::Pixels(Pixels(42.0));
2358 /// let length_in_rems = AbsoluteLength::Rems(Rems(2.0));
2359 /// let rem_size = Pixels(16.0);
2360 ///
2361 /// assert_eq!(length_in_pixels.to_pixels(rem_size), Pixels(42.0));
2362 /// assert_eq!(length_in_rems.to_pixels(rem_size), Pixels(32.0));
2363 /// ```
2364 pub fn to_pixels(&self, rem_size: Pixels) -> Pixels {
2365 match self {
2366 AbsoluteLength::Pixels(pixels) => *pixels,
2367 AbsoluteLength::Rems(rems) => *rems * rem_size,
2368 }
2369 }
2370}
2371
2372impl Default for AbsoluteLength {
2373 fn default() -> Self {
2374 px(0.).into()
2375 }
2376}
2377
2378/// A non-auto length that can be defined in pixels, rems, or percent of parent.
2379///
2380/// This enum represents lengths that have a specific value, as opposed to lengths that are automatically
2381/// determined by the context. It includes absolute lengths in pixels or rems, and relative lengths as a
2382/// fraction of the parent's size.
2383#[derive(Clone, Copy, Neg, PartialEq)]
2384pub enum DefiniteLength {
2385 /// An absolute length specified in pixels or rems.
2386 Absolute(AbsoluteLength),
2387 /// A relative length specified as a fraction of the parent's size, between 0 and 1.
2388 Fraction(f32),
2389}
2390
2391impl DefiniteLength {
2392 /// Converts the `DefiniteLength` to `Pixels` based on a given `base_size` and `rem_size`.
2393 ///
2394 /// If the `DefiniteLength` is an absolute length, it will be directly converted to `Pixels`.
2395 /// If it is a fraction, the fraction will be multiplied by the `base_size` to get the length in pixels.
2396 ///
2397 /// # Arguments
2398 ///
2399 /// * `base_size` - The base size in `AbsoluteLength` to which the fraction will be applied.
2400 /// * `rem_size` - The size of one rem in pixels, used to convert rems to pixels.
2401 ///
2402 /// # Returns
2403 ///
2404 /// Returns the `DefiniteLength` as `Pixels`.
2405 ///
2406 /// # Examples
2407 ///
2408 /// ```
2409 /// # use zed::{DefiniteLength, AbsoluteLength, Pixels, px, rems};
2410 /// let length_in_pixels = DefiniteLength::Absolute(AbsoluteLength::Pixels(px(42.0)));
2411 /// let length_in_rems = DefiniteLength::Absolute(AbsoluteLength::Rems(rems(2.0)));
2412 /// let length_as_fraction = DefiniteLength::Fraction(0.5);
2413 /// let base_size = AbsoluteLength::Pixels(px(100.0));
2414 /// let rem_size = px(16.0);
2415 ///
2416 /// assert_eq!(length_in_pixels.to_pixels(base_size, rem_size), Pixels(42.0));
2417 /// assert_eq!(length_in_rems.to_pixels(base_size, rem_size), Pixels(32.0));
2418 /// assert_eq!(length_as_fraction.to_pixels(base_size, rem_size), Pixels(50.0));
2419 /// ```
2420 pub fn to_pixels(&self, base_size: AbsoluteLength, rem_size: Pixels) -> Pixels {
2421 match self {
2422 DefiniteLength::Absolute(size) => size.to_pixels(rem_size),
2423 DefiniteLength::Fraction(fraction) => match base_size {
2424 AbsoluteLength::Pixels(px) => px * *fraction,
2425 AbsoluteLength::Rems(rems) => rems * rem_size * *fraction,
2426 },
2427 }
2428 }
2429}
2430
2431impl Debug for DefiniteLength {
2432 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2433 match self {
2434 DefiniteLength::Absolute(length) => Debug::fmt(length, f),
2435 DefiniteLength::Fraction(fract) => write!(f, "{}%", (fract * 100.0) as i32),
2436 }
2437 }
2438}
2439
2440impl From<Pixels> for DefiniteLength {
2441 fn from(pixels: Pixels) -> Self {
2442 Self::Absolute(pixels.into())
2443 }
2444}
2445
2446impl From<Rems> for DefiniteLength {
2447 fn from(rems: Rems) -> Self {
2448 Self::Absolute(rems.into())
2449 }
2450}
2451
2452impl From<AbsoluteLength> for DefiniteLength {
2453 fn from(length: AbsoluteLength) -> Self {
2454 Self::Absolute(length)
2455 }
2456}
2457
2458impl Default for DefiniteLength {
2459 fn default() -> Self {
2460 Self::Absolute(AbsoluteLength::default())
2461 }
2462}
2463
2464/// A length that can be defined in pixels, rems, percent of parent, or auto.
2465#[derive(Clone, Copy)]
2466pub enum Length {
2467 /// A definite length specified either in pixels, rems, or as a fraction of the parent's size.
2468 Definite(DefiniteLength),
2469 /// An automatic length that is determined by the context in which it is used.
2470 Auto,
2471}
2472
2473impl Debug for Length {
2474 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2475 match self {
2476 Length::Definite(definite_length) => write!(f, "{:?}", definite_length),
2477 Length::Auto => write!(f, "auto"),
2478 }
2479 }
2480}
2481
2482/// Constructs a `DefiniteLength` representing a relative fraction of a parent size.
2483///
2484/// This function creates a `DefiniteLength` that is a specified fraction of a parent's dimension.
2485/// The fraction should be a floating-point number between 0.0 and 1.0, where 1.0 represents 100% of the parent's size.
2486///
2487/// # Arguments
2488///
2489/// * `fraction` - The fraction of the parent's size, between 0.0 and 1.0.
2490///
2491/// # Returns
2492///
2493/// A `DefiniteLength` representing the relative length as a fraction of the parent's size.
2494pub fn relative(fraction: f32) -> DefiniteLength {
2495 DefiniteLength::Fraction(fraction)
2496}
2497
2498/// Returns the Golden Ratio, i.e. `~(1.0 + sqrt(5.0)) / 2.0`.
2499pub fn phi() -> DefiniteLength {
2500 relative(1.618_034)
2501}
2502
2503/// Constructs a `Rems` value representing a length in rems.
2504///
2505/// # Arguments
2506///
2507/// * `rems` - The number of rems for the length.
2508///
2509/// # Returns
2510///
2511/// A `Rems` representing the specified number of rems.
2512pub fn rems(rems: f32) -> Rems {
2513 Rems(rems)
2514}
2515
2516/// Constructs a `Pixels` value representing a length in pixels.
2517///
2518/// # Arguments
2519///
2520/// * `pixels` - The number of pixels for the length.
2521///
2522/// # Returns
2523///
2524/// A `Pixels` representing the specified number of pixels.
2525pub const fn px(pixels: f32) -> Pixels {
2526 Pixels(pixels)
2527}
2528
2529/// Returns a `Length` representing an automatic length.
2530///
2531/// The `auto` length is often used in layout calculations where the length should be determined
2532/// by the layout context itself rather than being explicitly set. This is commonly used in CSS
2533/// for properties like `width`, `height`, `margin`, `padding`, etc., where `auto` can be used
2534/// to instruct the layout engine to calculate the size based on other factors like the size of the
2535/// container or the intrinsic size of the content.
2536///
2537/// # Returns
2538///
2539/// A `Length` variant set to `Auto`.
2540pub fn auto() -> Length {
2541 Length::Auto
2542}
2543
2544impl From<Pixels> for Length {
2545 fn from(pixels: Pixels) -> Self {
2546 Self::Definite(pixels.into())
2547 }
2548}
2549
2550impl From<Rems> for Length {
2551 fn from(rems: Rems) -> Self {
2552 Self::Definite(rems.into())
2553 }
2554}
2555
2556impl From<DefiniteLength> for Length {
2557 fn from(length: DefiniteLength) -> Self {
2558 Self::Definite(length)
2559 }
2560}
2561
2562impl From<AbsoluteLength> for Length {
2563 fn from(length: AbsoluteLength) -> Self {
2564 Self::Definite(length.into())
2565 }
2566}
2567
2568impl Default for Length {
2569 fn default() -> Self {
2570 Self::Definite(DefiniteLength::default())
2571 }
2572}
2573
2574impl From<()> for Length {
2575 fn from(_: ()) -> Self {
2576 Self::Definite(DefiniteLength::default())
2577 }
2578}
2579
2580/// Provides a trait for types that can calculate half of their value.
2581///
2582/// The `Half` trait is used for types that can be evenly divided, returning a new instance of the same type
2583/// representing half of the original value. This is commonly used for types that represent measurements or sizes,
2584/// such as lengths or pixels, where halving is a frequent operation during layout calculations or animations.
2585pub trait Half {
2586 /// Returns half of the current value.
2587 ///
2588 /// # Returns
2589 ///
2590 /// A new instance of the implementing type, representing half of the original value.
2591 fn half(&self) -> Self;
2592}
2593
2594impl Half for f32 {
2595 fn half(&self) -> Self {
2596 self / 2.
2597 }
2598}
2599
2600impl Half for DevicePixels {
2601 fn half(&self) -> Self {
2602 Self(self.0 / 2)
2603 }
2604}
2605
2606impl Half for ScaledPixels {
2607 fn half(&self) -> Self {
2608 Self(self.0 / 2.)
2609 }
2610}
2611
2612impl Half for Pixels {
2613 fn half(&self) -> Self {
2614 Self(self.0 / 2.)
2615 }
2616}
2617
2618impl Half for Rems {
2619 fn half(&self) -> Self {
2620 Self(self.0 / 2.)
2621 }
2622}
2623
2624impl Half for GlobalPixels {
2625 fn half(&self) -> Self {
2626 Self(self.0 / 2.)
2627 }
2628}
2629
2630/// A trait for checking if a value is zero.
2631///
2632/// This trait provides a method to determine if a value is considered to be zero.
2633/// It is implemented for various numeric and length-related types where the concept
2634/// of zero is applicable. This can be useful for comparisons, optimizations, or
2635/// determining if an operation has a neutral effect.
2636pub trait IsZero {
2637 /// Determines if the value is zero.
2638 ///
2639 /// # Returns
2640 ///
2641 /// Returns `true` if the value is zero, `false` otherwise.
2642 fn is_zero(&self) -> bool;
2643}
2644
2645impl IsZero for DevicePixels {
2646 fn is_zero(&self) -> bool {
2647 self.0 == 0
2648 }
2649}
2650
2651impl IsZero for ScaledPixels {
2652 fn is_zero(&self) -> bool {
2653 self.0 == 0.
2654 }
2655}
2656
2657impl IsZero for Pixels {
2658 fn is_zero(&self) -> bool {
2659 self.0 == 0.
2660 }
2661}
2662
2663impl IsZero for Rems {
2664 fn is_zero(&self) -> bool {
2665 self.0 == 0.
2666 }
2667}
2668
2669impl IsZero for AbsoluteLength {
2670 fn is_zero(&self) -> bool {
2671 match self {
2672 AbsoluteLength::Pixels(pixels) => pixels.is_zero(),
2673 AbsoluteLength::Rems(rems) => rems.is_zero(),
2674 }
2675 }
2676}
2677
2678impl IsZero for DefiniteLength {
2679 fn is_zero(&self) -> bool {
2680 match self {
2681 DefiniteLength::Absolute(length) => length.is_zero(),
2682 DefiniteLength::Fraction(fraction) => *fraction == 0.,
2683 }
2684 }
2685}
2686
2687impl IsZero for Length {
2688 fn is_zero(&self) -> bool {
2689 match self {
2690 Length::Definite(length) => length.is_zero(),
2691 Length::Auto => false,
2692 }
2693 }
2694}
2695
2696impl<T: IsZero + Debug + Clone + Default> IsZero for Point<T> {
2697 fn is_zero(&self) -> bool {
2698 self.x.is_zero() && self.y.is_zero()
2699 }
2700}
2701
2702impl<T> IsZero for Size<T>
2703where
2704 T: IsZero + Default + Debug + Clone,
2705{
2706 fn is_zero(&self) -> bool {
2707 self.width.is_zero() || self.height.is_zero()
2708 }
2709}
2710
2711impl<T: IsZero + Debug + Clone + Default> IsZero for Bounds<T> {
2712 fn is_zero(&self) -> bool {
2713 self.size.is_zero()
2714 }
2715}
2716
2717impl<T> IsZero for Corners<T>
2718where
2719 T: IsZero + Clone + Default + Debug,
2720{
2721 fn is_zero(&self) -> bool {
2722 self.top_left.is_zero()
2723 && self.top_right.is_zero()
2724 && self.bottom_right.is_zero()
2725 && self.bottom_left.is_zero()
2726 }
2727}
2728
2729#[cfg(test)]
2730mod tests {
2731 use super::*;
2732
2733 #[test]
2734 fn test_bounds_intersects() {
2735 let bounds1 = Bounds {
2736 origin: Point { x: 0.0, y: 0.0 },
2737 size: Size {
2738 width: 5.0,
2739 height: 5.0,
2740 },
2741 };
2742 let bounds2 = Bounds {
2743 origin: Point { x: 4.0, y: 4.0 },
2744 size: Size {
2745 width: 5.0,
2746 height: 5.0,
2747 },
2748 };
2749 let bounds3 = Bounds {
2750 origin: Point { x: 10.0, y: 10.0 },
2751 size: Size {
2752 width: 5.0,
2753 height: 5.0,
2754 },
2755 };
2756
2757 // Test Case 1: Intersecting bounds
2758 assert_eq!(bounds1.intersects(&bounds2), true);
2759
2760 // Test Case 2: Non-Intersecting bounds
2761 assert_eq!(bounds1.intersects(&bounds3), false);
2762
2763 // Test Case 3: Bounds intersecting with themselves
2764 assert_eq!(bounds1.intersects(&bounds1), true);
2765 }
2766}