geometry.rs

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