geometry.rs

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