🔒 Repository is read-only – file editing is disabled.

PaganDE/smithay-0.7.0/src/utils/geometry.rs main

2029 linii Raw ← Powrót
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
use std::fmt;
use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign};

#[cfg(feature = "wayland_frontend")]
use wayland_server::protocol::wl_output::Transform as WlTransform;

/// Type-level marker for the logical coordinate space
#[derive(Debug)]
pub struct Logical;

/// Type-level marker for the client logical coordinate space
#[derive(Debug)]
#[cfg(feature = "wayland_frontend")]
pub(crate) struct Client;

/// Type-level marker for the physical coordinate space
#[derive(Debug)]
pub struct Physical;

/// Type-level marker for the buffer coordinate space
#[derive(Debug)]
pub struct Buffer;

/// Type-level marker for raw coordinate space, provided by input devices
#[derive(Debug)]
pub struct Raw;

/// Trait for types serving as a coordinate for other geometry utils
pub trait Coordinate:
    Sized + Add<Self, Output = Self> + Sub<Self, Output = Self> + PartialOrd + Default + Copy + fmt::Debug
{
    /// A Coordinate that is 0
    const ZERO: Self;
    /// Downscale the coordinate
    fn downscale(self, scale: Self) -> Self;
    /// Upscale the coordinate
    fn upscale(self, scale: Self) -> Self;
    /// Convert the coordinate to a f64
    fn to_f64(self) -> f64;
    /// Convert to this coordinate from a f64
    fn from_f64(v: f64) -> Self;
    /// Compare and return the smaller one
    fn min(self, other: Self) -> Self {
        if self < other {
            self
        } else {
            other
        }
    }
    /// Compare and return the larger one
    fn max(self, other: Self) -> Self {
        if self > other {
            self
        } else {
            other
        }
    }
    /// Test if the coordinate is not negative
    fn non_negative(self) -> bool;
    /// Returns the absolute value of this coordinate
    fn abs(self) -> Self;

    /// Saturating integer addition. Computes self + other, saturating at the numeric bounds instead of overflowing.
    fn saturating_add(self, other: Self) -> Self;
    /// Saturating integer subtraction. Computes self - other, saturating at the numeric bounds instead of overflowing.
    fn saturating_sub(self, other: Self) -> Self;
    /// Saturating integer multiplication. Computes self * other, saturating at the numeric bounds instead of overflowing.
    fn saturating_mul(self, other: Self) -> Self;
}

/// Implements Coordinate for an unsigned numerical type.
macro_rules! unsigned_coordinate_impl {
    ($ty:ty, $ ($tys:ty),* ) => {
        unsigned_coordinate_impl!($ty);
        $(
            unsigned_coordinate_impl!($tys);
        )*
    };

    ($ty:ty) => {
        impl Coordinate for $ty {
            const ZERO: $ty = 0;

            #[inline]
            fn downscale(self, scale: Self) -> Self {
                self / scale
            }

            #[inline]
            fn upscale(self, scale: Self) -> Self {
                self.saturating_mul(scale)
            }

            #[inline]
            fn to_f64(self) -> f64 {
                self as f64
            }

            #[inline]
            fn from_f64(v: f64) -> Self {
                v as Self
            }

            #[inline]
            fn non_negative(self) -> bool {
                true
            }

            #[inline]
            fn abs(self) -> Self {
                self
            }

            #[inline]
            fn saturating_add(self, other: Self) -> Self {
                self.saturating_add(other)
            }
            #[inline]
            fn saturating_sub(self, other: Self) -> Self {
                self.saturating_sub(other)
            }
            #[inline]
            fn saturating_mul(self, other: Self) -> Self {
                self.saturating_mul(other)
            }
        }
    };
}

unsigned_coordinate_impl! {
    u8,
    u16,
    u32,
    u64,
    u128
}

/// Implements Coordinate for an signed numerical type.
macro_rules! signed_coordinate_impl {
    ($ty:ty, $ ($tys:ty),* ) => {
        signed_coordinate_impl!($ty);
        $(
            signed_coordinate_impl!($tys);
        )*
    };

    ($ty:ty) => {
        impl Coordinate for $ty {
            const ZERO: $ty = 0;

            #[inline]
            fn downscale(self, scale: Self) -> Self {
                self / scale
            }

            #[inline]
            fn upscale(self, scale: Self) -> Self {
                self.saturating_mul(scale)
            }

            #[inline]
            fn to_f64(self) -> f64 {
                self as f64
            }

            #[inline]
            fn from_f64(v: f64) -> Self {
                v as Self
            }

            #[inline]
            fn non_negative(self) -> bool {
                self >= 0
            }

            #[inline]
            fn abs(self) -> Self {
                self.abs()
            }

            #[inline]
            fn saturating_add(self, other: Self) -> Self {
                self.saturating_add(other)
            }
            #[inline]
            fn saturating_sub(self, other: Self) -> Self {
                self.saturating_sub(other)
            }
            #[inline]
            fn saturating_mul(self, other: Self) -> Self {
                self.saturating_mul(other)
            }
        }
    };
}

signed_coordinate_impl! {
    i8,
    i16,
    i32,
    i64,
    i128
}

macro_rules! floating_point_coordinate_impl {
    ($ty:ty, $ ($tys:ty),* ) => {
        floating_point_coordinate_impl!($ty);
        $(
            floating_point_coordinate_impl!($tys);
        )*
    };

    ($ty:ty) => {
        impl Coordinate for $ty {
            const ZERO: $ty = 0.0;

            #[inline]
            fn downscale(self, scale: Self) -> Self {
                self / scale
            }

            #[inline]
            fn upscale(self, scale: Self) -> Self {
                self * scale
            }

            #[inline]
            fn to_f64(self) -> f64 {
                self as f64
            }

            #[inline]
            fn from_f64(v: f64) -> Self {
                v as Self
            }

            #[inline]
            fn non_negative(self) -> bool {
                self >= 0.0
            }

            #[inline]
            fn abs(self) -> Self {
                self.abs()
            }

            #[inline]
            fn saturating_add(self, other: Self) -> Self {
                self + other
            }
            #[inline]
            fn saturating_sub(self, other: Self) -> Self {
                self - other
            }
            #[inline]
            fn saturating_mul(self, other: Self) -> Self {
                self * other
            }
        }
    };
}

floating_point_coordinate_impl! {
    f32,
    f64
}

/*
 * Scale
 */

/// A two-dimensional scale that can be
/// used to scale [`Point`]s, [`Size`]s and
/// [`Rectangle`]s
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Scale<N: Coordinate> {
    /// The scale on the x axis
    pub x: N,
    /// The scale on the y axis
    pub y: N,
}

impl<N: Coordinate> Scale<N> {
    /// Convert the underlying numerical type to f64 for floating point manipulations
    #[inline]
    pub fn to_f64(self) -> Scale<f64> {
        Scale {
            x: self.x.to_f64(),
            y: self.y.to_f64(),
        }
    }
}

impl<N: Coordinate> From<N> for Scale<N> {
    #[inline]
    fn from(scale: N) -> Self {
        Scale { x: scale, y: scale }
    }
}

impl<N: Coordinate> From<(N, N)> for Scale<N> {
    #[inline]
    fn from((scale_x, scale_y): (N, N)) -> Self {
        Scale {
            x: scale_x,
            y: scale_y,
        }
    }
}

impl<N, T> Mul<T> for Scale<N>
where
    N: Coordinate,
    T: Into<Scale<N>>,
{
    type Output = Scale<N>;

    #[inline]
    fn mul(self, rhs: T) -> Self::Output {
        let rhs = rhs.into();
        Scale {
            x: self.x.upscale(rhs.x),
            y: self.y.upscale(rhs.y),
        }
    }
}

/*
 * Point
 */

/// A point as defined by its x and y coordinates
///
/// Operations on points are saturating.
#[repr(C)]
pub struct Point<N, Kind> {
    /// horizontal coordinate
    pub x: N,
    /// vertical coordinate
    pub y: N,
    _kind: std::marker::PhantomData<Kind>,
}

impl<N, Kind> Point<N, Kind> {
    /// Create a new Point
    pub const fn new(x: N, y: N) -> Point<N, Kind> {
        Point {
            x,
            y,
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> Point<N, Kind> {
    /// Convert this [`Point`] to a [`Size`] with the same coordinates
    ///
    /// Checks that the coordinates are positive with a `debug_assert!()`.
    #[inline]
    pub fn to_size(self) -> Size<N, Kind> {
        debug_assert!(
            self.x.non_negative() && self.y.non_negative(),
            "Attempting to create a `Size` of negative size: {:?}",
            (self.x, self.y)
        );
        Size {
            w: self.x,
            h: self.y,
            _kind: std::marker::PhantomData,
        }
    }

    /// Convert this [`Point`] to a [`Size`] with the same coordinates
    ///
    /// Ensures that the coordinates are positive by taking their absolute value
    #[inline]
    pub fn to_size_abs(self) -> Size<N, Kind> {
        Size {
            w: self.x.abs(),
            h: self.y.abs(),
            _kind: std::marker::PhantomData,
        }
    }

    /// Upscale this [`Point`] by a specified [`Scale`]
    #[inline]
    pub fn upscale(self, scale: impl Into<Scale<N>>) -> Point<N, Kind> {
        let scale = scale.into();
        Point {
            x: self.x.upscale(scale.x),
            y: self.y.upscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }

    /// Downscale this [`Point`] by a specified [`Scale`]
    #[inline]
    pub fn downscale(self, scale: impl Into<Scale<N>>) -> Point<N, Kind> {
        let scale = scale.into();
        Point {
            x: self.x.downscale(scale.x),
            y: self.y.downscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> Point<N, Kind> {
    /// Constrain this [`Point`] within a [`Rectangle`] with the same coordinates
    ///
    /// The [`Point`] returned is guaranteed to be not smaller than the [`Rectangle`]
    /// location and not greater than the [`Rectangle`] location plus size.
    #[inline]
    pub fn constrain(self, rect: impl Into<Rectangle<N, Kind>>) -> Point<N, Kind> {
        let rect = rect.into();

        Point {
            x: self.x.max(rect.loc.x).min(rect.loc.x + rect.size.w),
            y: self.y.max(rect.loc.y).min(rect.loc.y + rect.size.h),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> Point<N, Kind> {
    /// Convert the underlying numerical type to f64 for floating point manipulations
    #[inline]
    pub fn to_f64(self) -> Point<f64, Kind> {
        Point {
            x: self.x.to_f64(),
            y: self.y.to_f64(),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<Kind> Point<f64, Kind> {
    /// Convert to i32 for integer-space manipulations by rounding float values
    #[inline]
    pub fn to_i32_round<N: Coordinate>(self) -> Point<N, Kind> {
        Point {
            x: N::from_f64(self.x.round()),
            y: N::from_f64(self.y.round()),
            _kind: std::marker::PhantomData,
        }
    }

    /// Convert to i32 for integer-space manipulations by flooring float values
    #[inline]
    pub fn to_i32_floor<N: Coordinate>(self) -> Point<N, Kind> {
        Point {
            x: N::from_f64(self.x.floor()),
            y: N::from_f64(self.y.floor()),
            _kind: std::marker::PhantomData,
        }
    }

    /// Convert to i32 for integer-space manipulations by ceiling float values
    #[inline]
    pub fn to_i32_ceil<N: Coordinate>(self) -> Point<N, Kind> {
        Point {
            x: N::from_f64(self.x.ceil()),
            y: N::from_f64(self.y.ceil()),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: fmt::Debug, S> fmt::Debug for Point<N, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_fmt(format_args!("Point<{}>", std::any::type_name::<S>()))?;
        f.debug_struct("")
            .field("x", &self.x)
            .field("y", &self.y)
            .finish()
    }
}

impl<N: Coordinate> Point<N, Logical> {
    #[inline]
    #[cfg(feature = "wayland_frontend")]
    pub(crate) fn to_client(self, scale: impl Into<Scale<N>>) -> Point<N, Client> {
        let scale: Scale<N> = scale.into();
        Point {
            x: self.x.upscale(scale.x),
            y: self.y.upscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }

    #[inline]
    #[cfg(feature = "wayland_frontend")]
    pub(crate) fn to_client_precise_round<S: Coordinate, R: Coordinate>(
        self,
        scale: impl Into<Scale<S>>,
    ) -> Point<R, Client> {
        self.to_f64().to_client(scale.into().to_f64()).to_i32_round()
    }

    #[inline]
    /// Convert this logical point to physical coordinate space according to given scale factor
    pub fn to_physical(self, scale: impl Into<Scale<N>>) -> Point<N, Physical> {
        let scale = scale.into();
        Point {
            x: self.x.upscale(scale.x),
            y: self.y.upscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }

    /// Convert this logical point to physical coordinate space according to given scale factor
    /// and round the result
    #[inline]
    pub fn to_physical_precise_round<S: Coordinate, R: Coordinate>(
        self,
        scale: impl Into<Scale<S>>,
    ) -> Point<R, Physical> {
        self.to_f64().to_physical(scale.into().to_f64()).to_i32_round()
    }

    /// Convert this logical point to physical coordinate space according to given scale factor
    /// and ceil the result
    #[inline]
    pub fn to_physical_precise_ceil<S: Coordinate, R: Coordinate>(
        &self,
        scale: impl Into<Scale<S>>,
    ) -> Point<R, Physical> {
        self.to_f64().to_physical(scale.into().to_f64()).to_i32_ceil()
    }

    /// Convert this logical point to physical coordinate space according to given scale factor
    /// and floor the result
    #[inline]
    pub fn to_physical_precise_floor<S: Coordinate, R: Coordinate>(
        &self,
        scale: impl Into<Scale<S>>,
    ) -> Point<R, Physical> {
        self.to_f64().to_physical(scale.into().to_f64()).to_i32_floor()
    }

    #[inline]
    /// Convert this logical point to buffer coordinate space according to given scale factor
    pub fn to_buffer(
        self,
        scale: impl Into<Scale<N>>,
        transformation: Transform,
        area: &Size<N, Logical>,
    ) -> Point<N, Buffer> {
        let point = transformation.transform_point_in(self, area);
        let scale = scale.into();
        Point {
            x: point.x.upscale(scale.x),
            y: point.y.upscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }
}

#[cfg(feature = "wayland_frontend")]
impl<N: Coordinate> Point<N, Client> {
    #[inline]
    pub(crate) fn to_logical(self, scale: impl Into<Scale<N>>) -> Point<N, Logical> {
        let scale = scale.into();
        Point {
            x: self.x.downscale(scale.x),
            y: self.y.downscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate> Point<N, Physical> {
    #[inline]
    /// Convert this physical point to logical coordinate space according to given scale factor
    pub fn to_logical(self, scale: impl Into<Scale<N>>) -> Point<N, Logical> {
        let scale = scale.into();
        Point {
            x: self.x.downscale(scale.x),
            y: self.y.downscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate> Point<N, Buffer> {
    #[inline]
    /// Convert this physical point to logical coordinate space according to given scale factor
    pub fn to_logical(
        self,
        scale: impl Into<Scale<N>>,
        transform: Transform,
        area: &Size<N, Buffer>,
    ) -> Point<N, Logical> {
        let point = transform.invert().transform_point_in(self, area);
        let scale = scale.into();
        Point {
            x: point.x.downscale(scale.x),
            y: point.y.downscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N, Kind> From<(N, N)> for Point<N, Kind> {
    #[inline]
    fn from((x, y): (N, N)) -> Point<N, Kind> {
        Point::new(x, y)
    }
}

impl<N, Kind> From<Point<N, Kind>> for (N, N) {
    #[inline]
    fn from(point: Point<N, Kind>) -> (N, N) {
        (point.x, point.y)
    }
}

impl<N: Coordinate, Kind> Add for Point<N, Kind> {
    type Output = Point<N, Kind>;
    #[inline]
    fn add(self, other: Point<N, Kind>) -> Point<N, Kind> {
        Point {
            x: self.x.saturating_add(other.x),
            y: self.y.saturating_add(other.y),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> AddAssign for Point<N, Kind> {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        self.x = self.x.saturating_add(rhs.x);
        self.y = self.y.saturating_add(rhs.y);
    }
}

impl<N: Coordinate, Kind> SubAssign for Point<N, Kind> {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        self.x = self.x.saturating_sub(rhs.x);
        self.y = self.y.saturating_sub(rhs.y);
    }
}

impl<N: Coordinate, Kind> Sub for Point<N, Kind> {
    type Output = Point<N, Kind>;
    #[inline]
    fn sub(self, other: Point<N, Kind>) -> Point<N, Kind> {
        Point {
            x: self.x.saturating_sub(other.x),
            y: self.y.saturating_sub(other.y),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Clone, Kind> Clone for Point<N, Kind> {
    #[inline]
    fn clone(&self) -> Self {
        Point {
            x: self.x.clone(),
            y: self.y.clone(),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Copy, Kind> Copy for Point<N, Kind> {}

impl<N: PartialEq, Kind> PartialEq for Point<N, Kind> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.x == other.x && self.y == other.y
    }
}

impl<N: Eq, Kind> Eq for Point<N, Kind> {}

impl<N: Default, Kind> Default for Point<N, Kind> {
    #[inline]
    fn default() -> Self {
        Point {
            x: N::default(),
            y: N::default(),
            _kind: std::marker::PhantomData,
        }
    }
}

/*
 * Size
 */

/// A size as defined by its width and height
///
/// Constructors of this type ensure that the values are always positive via
/// `debug_assert!()`, however manually changing the values of the fields
/// can break this invariant.
///
/// Operations on sizes are saturating.
#[repr(C)]
pub struct Size<N, Kind> {
    /// horizontal coordinate
    pub w: N,
    /// vertical coordinate
    pub h: N,
    _kind: std::marker::PhantomData<Kind>,
}

impl<N: Coordinate, Kind> Size<N, Kind> {
    /// Create a new Size
    pub fn new(w: N, h: N) -> Size<N, Kind> {
        debug_assert!(
            w.non_negative() && h.non_negative(),
            "Attempting to create a `Size` of negative size: {:?}",
            (w, h)
        );
        Size {
            w,
            h,
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> Size<N, Kind> {
    /// Convert this [`Size`] to a [`Point`] with the same coordinates
    #[inline]
    pub fn to_point(self) -> Point<N, Kind> {
        Point {
            x: self.w,
            y: self.h,
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> Size<N, Kind> {
    /// Restrict this [`Size`] to min and max [`Size`] with the same coordinates
    pub fn clamp(self, min: impl Into<Size<N, Kind>>, max: impl Into<Size<N, Kind>>) -> Size<N, Kind> {
        let min = min.into();
        let max = max.into();

        Size {
            w: self.w.max(min.w).min(max.w),
            h: self.h.max(min.h).min(max.h),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> Size<N, Kind> {
    /// Convert the underlying numerical type to f64 for floating point manipulations
    #[inline]
    pub fn to_f64(self) -> Size<f64, Kind> {
        Size {
            w: self.w.to_f64(),
            h: self.h.to_f64(),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> Size<N, Kind> {
    /// Upscale this [`Size`] by a specified [`Scale`]
    #[inline]
    pub fn upscale(self, scale: impl Into<Scale<N>>) -> Size<N, Kind> {
        let scale = scale.into();
        Size {
            w: self.w.upscale(scale.x),
            h: self.h.upscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }

    /// Downscale this [`Size`] by a specified [`Scale`]
    #[inline]
    pub fn downscale(self, scale: impl Into<Scale<N>>) -> Size<N, Kind> {
        let scale = scale.into();
        Size {
            w: self.w.downscale(scale.x),
            h: self.h.downscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }

    /// Check if this [`Size`] is empty
    ///
    /// Returns true if either the width or the height is zero
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.w == N::default() || self.h == N::default()
    }
}

impl<Kind> Size<f64, Kind> {
    /// Convert to i32 for integer-space manipulations by rounding float values
    #[inline]
    pub fn to_i32_round<N: Coordinate>(self) -> Size<N, Kind> {
        Size {
            w: N::from_f64(self.w.round()),
            h: N::from_f64(self.h.round()),
            _kind: std::marker::PhantomData,
        }
    }

    /// Convert to i32 for integer-space manipulations by flooring float values
    #[inline]
    pub fn to_i32_floor<N: Coordinate>(self) -> Size<N, Kind> {
        Size {
            w: N::from_f64(self.w.floor()),
            h: N::from_f64(self.h.floor()),
            _kind: std::marker::PhantomData,
        }
    }

    /// Convert to i32 for integer-space manipulations by ceiling float values
    #[inline]
    pub fn to_i32_ceil<N: Coordinate>(self) -> Size<N, Kind> {
        Size {
            w: N::from_f64(self.w.ceil()),
            h: N::from_f64(self.h.ceil()),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: fmt::Debug, S> fmt::Debug for Size<N, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_fmt(format_args!("Size<{}>", std::any::type_name::<S>()))?;
        f.debug_struct("")
            .field("w", &self.w)
            .field("h", &self.h)
            .finish()
    }
}

impl<N: Coordinate> Size<N, Logical> {
    #[inline]
    #[cfg(feature = "wayland_frontend")]
    pub(crate) fn to_client(self, scale: impl Into<Scale<N>>) -> Size<N, Client> {
        let scale = scale.into();
        Size {
            w: self.w.upscale(scale.x),
            h: self.h.upscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }

    #[inline]
    /// Convert this logical size to physical coordinate space according to given scale factor
    pub fn to_physical(self, scale: impl Into<Scale<N>>) -> Size<N, Physical> {
        let scale = scale.into();
        Size {
            w: self.w.upscale(scale.x),
            h: self.h.upscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }

    /// Convert this logical size to physical coordinate space according to given scale factor
    /// and round the result
    #[inline]
    pub fn to_physical_precise_round<S: Coordinate, R: Coordinate>(
        self,
        scale: impl Into<Scale<S>>,
    ) -> Size<R, Physical> {
        self.to_f64().to_physical(scale.into().to_f64()).to_i32_round()
    }

    /// Convert this logical size to physical coordinate space according to given scale factor
    /// and ceil the result
    #[inline]
    pub fn to_physical_precise_ceil<S: Coordinate, R: Coordinate>(
        &self,
        scale: impl Into<Scale<S>>,
    ) -> Size<R, Physical> {
        self.to_f64().to_physical(scale.into().to_f64()).to_i32_ceil()
    }

    /// Convert this logical size to physical coordinate space according to given scale factor
    /// and floor the result
    #[inline]
    pub fn to_physical_precise_floor<S: Coordinate, R: Coordinate>(
        &self,
        scale: impl Into<Scale<S>>,
    ) -> Size<R, Physical> {
        self.to_f64().to_physical(scale.into().to_f64()).to_i32_floor()
    }

    #[inline]
    /// Convert this logical size to buffer coordinate space according to given scale factor
    pub fn to_buffer(self, scale: impl Into<Scale<N>>, transformation: Transform) -> Size<N, Buffer> {
        let scale = scale.into();
        transformation.transform_size(Size {
            w: self.w.upscale(scale.x),
            h: self.h.upscale(scale.y),
            _kind: std::marker::PhantomData,
        })
    }
}

#[cfg(feature = "wayland_frontend")]
impl<N: Coordinate> Size<N, Client> {
    #[inline]
    pub(crate) fn to_logical(self, scale: impl Into<Scale<N>>) -> Size<N, Logical> {
        let scale = scale.into();
        Size {
            w: self.w.downscale(scale.x),
            h: self.h.downscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate> Size<N, Physical> {
    #[inline]
    /// Convert this physical point to logical coordinate space according to given scale factor
    pub fn to_logical(self, scale: impl Into<Scale<N>>) -> Size<N, Logical> {
        let scale = scale.into();
        Size {
            w: self.w.downscale(scale.x),
            h: self.h.downscale(scale.y),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate> Size<N, Buffer> {
    #[inline]
    /// Convert this physical point to logical coordinate space according to given scale factor
    pub fn to_logical(self, scale: impl Into<Scale<N>>, transformation: Transform) -> Size<N, Logical> {
        let scale = scale.into();
        transformation.invert().transform_size(Size {
            w: self.w.downscale(scale.x),
            h: self.h.downscale(scale.y),
            _kind: std::marker::PhantomData,
        })
    }
}

impl<N: Coordinate, Kind> From<(N, N)> for Size<N, Kind> {
    #[inline]
    fn from((w, h): (N, N)) -> Size<N, Kind> {
        Size::new(w, h)
    }
}

impl<N, Kind> From<Size<N, Kind>> for (N, N) {
    #[inline]
    fn from(point: Size<N, Kind>) -> (N, N) {
        (point.w, point.h)
    }
}

impl<N: Coordinate, Kind> Add for Size<N, Kind> {
    type Output = Size<N, Kind>;
    #[inline]
    fn add(self, other: Size<N, Kind>) -> Size<N, Kind> {
        Size {
            w: self.w.saturating_add(other.w),
            h: self.h.saturating_add(other.h),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> AddAssign for Size<N, Kind> {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        self.w = self.w.saturating_add(rhs.w);
        self.h = self.h.saturating_add(rhs.h);
    }
}

impl<N: Coordinate, Kind> Sub for Size<N, Kind> {
    type Output = Size<N, Kind>;
    #[inline]
    fn sub(self, rhs: Self) -> Self::Output {
        Size {
            w: self.w.saturating_sub(rhs.w).max(N::ZERO),
            h: self.h.saturating_sub(rhs.h).max(N::ZERO),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> SubAssign for Size<N, Kind> {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        self.w = self.w.saturating_sub(rhs.w).max(N::ZERO);
        self.h = self.h.saturating_sub(rhs.h).max(N::ZERO);
    }
}

impl<N: Coordinate + Div<Output = N>, KindLhs, KindRhs> Div<Size<N, KindRhs>> for Size<N, KindLhs> {
    type Output = Scale<N>;

    #[inline]
    fn div(self, rhs: Size<N, KindRhs>) -> Self::Output {
        Scale {
            x: self.w / rhs.w,
            y: self.h / rhs.h,
        }
    }
}

impl<N: Coordinate + Div, Kind> Div<N> for Size<N, Kind> {
    type Output = Size<<N as Div>::Output, Kind>;

    #[inline]
    fn div(self, rhs: N) -> Self::Output {
        Size {
            w: self.w / rhs,
            h: self.h / rhs,
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate + Mul, Kind> Mul<N> for Size<N, Kind> {
    type Output = Size<<N as Mul>::Output, Kind>;

    #[inline]
    fn mul(self, rhs: N) -> Self::Output {
        Size {
            w: self.w * rhs,
            h: self.h * rhs,
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Clone, Kind> Clone for Size<N, Kind> {
    #[inline]
    fn clone(&self) -> Self {
        Size {
            w: self.w.clone(),
            h: self.h.clone(),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Copy, Kind> Copy for Size<N, Kind> {}

impl<N: PartialEq, Kind> PartialEq for Size<N, Kind> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.w == other.w && self.h == other.h
    }
}

impl<N: Eq, Kind> Eq for Size<N, Kind> {}

impl<N: Default, Kind> Default for Size<N, Kind> {
    #[inline]
    fn default() -> Self {
        Size {
            w: N::default(),
            h: N::default(),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> Add<Size<N, Kind>> for Point<N, Kind> {
    type Output = Point<N, Kind>;
    #[inline]
    fn add(self, other: Size<N, Kind>) -> Point<N, Kind> {
        Point {
            x: self.x.saturating_add(other.w),
            y: self.y.saturating_add(other.h),
            _kind: std::marker::PhantomData,
        }
    }
}

impl<N: Coordinate, Kind> Sub<Size<N, Kind>> for Point<N, Kind> {
    type Output = Point<N, Kind>;
    #[inline]
    fn sub(self, other: Size<N, Kind>) -> Point<N, Kind> {
        Point {
            x: self.x.saturating_sub(other.w),
            y: self.y.saturating_sub(other.h),
            _kind: std::marker::PhantomData,
        }
    }
}

/// A rectangle defined by its top-left corner and dimensions
///
/// Operations on rectangles are saturating.
#[repr(C)]
pub struct Rectangle<N, Kind> {
    /// Location of the top-left corner of the rectangle
    pub loc: Point<N, Kind>,
    /// Size of the rectangle, as (width, height)
    pub size: Size<N, Kind>,
}

impl<N: Coordinate, Kind> Rectangle<N, Kind> {
    /// Convert the underlying numerical type to another
    pub fn to_f64(self) -> Rectangle<f64, Kind> {
        Rectangle {
            loc: self.loc.to_f64(),
            size: self.size.to_f64(),
        }
    }
}

impl<N: Coordinate, Kind> Rectangle<N, Kind> {
    /// Upscale this [`Rectangle`] by the supplied [`Scale`]
    pub fn upscale(self, scale: impl Into<Scale<N>>) -> Rectangle<N, Kind> {
        let scale = scale.into();
        Rectangle {
            loc: self.loc.upscale(scale),
            size: self.size.upscale(scale),
        }
    }

    /// Downscale this [`Rectangle`] by the supplied [`Scale`]
    pub fn downscale(self, scale: impl Into<Scale<N>>) -> Rectangle<N, Kind> {
        let scale = scale.into();
        Rectangle {
            loc: self.loc.downscale(scale),
            size: self.size.downscale(scale),
        }
    }

    /// Check if this [`Rectangle`] is empty
    ///
    /// Returns true if either the width or the height
    /// of the [`Size`] is zero
    pub fn is_empty(&self) -> bool {
        self.size.is_empty()
    }
}

impl<Kind> Rectangle<f64, Kind> {
    /// Convert to i32 for integer-space manipulations by rounding float values
    #[inline]
    pub fn to_i32_round<N: Coordinate>(self) -> Rectangle<N, Kind> {
        Rectangle {
            loc: self.loc.to_i32_round(),
            size: self.size.to_i32_round(),
        }
    }

    /// Convert to i32 by returning the largest integer-space rectangle fitting into the float-based rectangle
    #[inline]
    pub fn to_i32_down<N: Coordinate>(self) -> Rectangle<N, Kind> {
        Rectangle::from_extremities(self.loc.to_i32_ceil(), (self.loc + self.size).to_i32_floor())
    }

    /// Convert to i32 by returning the smallest integet-space rectangle encapsulating the float-based rectangle
    #[inline]
    pub fn to_i32_up<N: Coordinate>(self) -> Rectangle<N, Kind> {
        Rectangle::from_extremities(self.loc.to_i32_floor(), (self.loc + self.size).to_i32_ceil())
    }
}

impl<N: Coordinate, Kind> Rectangle<N, Kind> {
    /// Create a new [`Rectangle`] from the coordinates of its top-left corner and its dimensions
    #[deprecated(note = "use new or from_size")]
    #[inline]
    pub fn from_loc_and_size(loc: impl Into<Point<N, Kind>>, size: impl Into<Size<N, Kind>>) -> Self {
        Rectangle {
            loc: loc.into(),
            size: size.into(),
        }
    }

    /// Create a new [`Rectangle`] from the coordinates of its top-left corner and its dimensions
    #[inline]
    pub fn new(loc: Point<N, Kind>, size: Size<N, Kind>) -> Self {
        Rectangle { loc, size }
    }

    /// Create a new [`Rectangle`] from its dimensions, with location zero
    #[inline]
    pub fn from_size(size: Size<N, Kind>) -> Self {
        Rectangle {
            loc: (N::ZERO, N::ZERO).into(),
            size,
        }
    }

    /// Create a new [`Rectangle`] with location and size zero
    #[inline]
    pub fn zero() -> Self {
        Rectangle {
            loc: (N::ZERO, N::ZERO).into(),
            size: (N::ZERO, N::ZERO).into(),
        }
    }

    /// Create a new [`Rectangle`] from the coordinates of its top-left corner and its bottom-right corner
    #[inline]
    #[deprecated = "use Rectangle::from_extremities instead"]
    #[doc(hidden)]
    pub fn from_extemities(
        topleft: impl Into<Point<N, Kind>>,
        bottomright: impl Into<Point<N, Kind>>,
    ) -> Self {
        Rectangle::from_extremities(topleft, bottomright)
    }

    /// Create a new [`Rectangle`] from the coordinates of its top-left corner and its bottom-right corner
    #[inline]
    pub fn from_extremities(
        topleft: impl Into<Point<N, Kind>>,
        bottomright: impl Into<Point<N, Kind>>,
    ) -> Self {
        let topleft = topleft.into();
        let bottomright = bottomright.into();
        Rectangle {
            loc: topleft,
            size: (bottomright - topleft).to_size(),
        }
    }

    /// Checks whether given [`Point`] is inside the rectangle
    #[inline]
    pub fn contains<P: Into<Point<N, Kind>>>(self, point: P) -> bool {
        let p: Point<N, Kind> = point.into();
        (p.x >= self.loc.x)
            && (p.x < self.loc.x.saturating_add(self.size.w))
            && (p.y >= self.loc.y)
            && (p.y < self.loc.y.saturating_add(self.size.h))
    }

    /// Checks whether given [`Rectangle`] is inside the rectangle
    ///
    /// A rectangle is considered inside another rectangle
    /// if its location is inside the other rectangle and it does not
    /// extend outside the other rectangle.
    /// This includes rectangles with the same location and size
    #[inline]
    pub fn contains_rect<R: Into<Rectangle<N, Kind>>>(self, rect: R) -> bool {
        let r: Rectangle<N, Kind> = rect.into();
        r.loc.x >= self.loc.x
            && r.loc.y >= self.loc.y
            && r.loc.x.saturating_add(r.size.w) <= self.loc.x.saturating_add(self.size.w)
            && r.loc.y.saturating_add(r.size.h) <= self.loc.y.saturating_add(self.size.h)
    }

    /// Checks whether a given [`Rectangle`] overlaps with this one
    ///
    /// Note: This operation is exclusive, touching only rectangles will return `false`.
    /// For inclusive overlap test see [`overlaps_or_touches`](Rectangle::overlaps_or_touches)
    #[inline]
    pub fn overlaps(self, other: impl Into<Rectangle<N, Kind>>) -> bool {
        let other = other.into();

        self.loc.x < other.loc.x.saturating_add(other.size.w)
            && other.loc.x < self.loc.x.saturating_add(self.size.w)
            && self.loc.y < other.loc.y.saturating_add(other.size.h)
            && other.loc.y < self.loc.y.saturating_add(self.size.h)
    }

    /// Checks whether a given [`Rectangle`] overlaps with this one or touches it
    ///
    /// Note: This operation is inclusive, touching only rectangles will return `true`.
    /// For exclusive overlap test see [`overlaps`](Rectangle::overlaps)
    #[inline]
    pub fn overlaps_or_touches(self, other: impl Into<Rectangle<N, Kind>>) -> bool {
        let other = other.into();

        self.loc.x <= other.loc.x.saturating_add(other.size.w)
            && other.loc.x <= self.loc.x.saturating_add(self.size.w)
            && self.loc.y <= other.loc.y.saturating_add(other.size.h)
            && other.loc.y <= self.loc.y.saturating_add(self.size.h)
    }

    /// Clamp rectangle to min and max corners resulting in the overlapping area of two rectangles
    ///
    /// Returns `None` if the two rectangles don't overlap
    #[inline]
    pub fn intersection(self, other: impl Into<Rectangle<N, Kind>>) -> Option<Self> {
        let other = other.into();
        if !self.overlaps(other) {
            return None;
        }
        Some(Rectangle::from_extremities(
            (self.loc.x.max(other.loc.x), self.loc.y.max(other.loc.y)),
            (
                (self.loc.x.saturating_add(self.size.w)).min(other.loc.x.saturating_add(other.size.w)),
                (self.loc.y.saturating_add(self.size.h)).min(other.loc.y.saturating_add(other.size.h)),
            ),
        ))
    }

    /// Compute the bounding box of a given set of points
    pub fn bounding_box(points: impl IntoIterator<Item = Point<N, Kind>>) -> Self {
        let ret = points.into_iter().fold(None, |acc, point| match acc {
            None => Some((point, point)),
            Some((min_point, max_point)) => Some((
                (point.x.min(min_point.x), point.y.min(min_point.y)).into(),
                (point.x.max(max_point.x), point.y.max(max_point.y)).into(),
            )),
        });

        match ret {
            None => Rectangle::default(),
            Some((min_point, max_point)) => Rectangle::from_extremities(min_point, max_point),
        }
    }

    /// Merge two [`Rectangle`] by producing the smallest rectangle that contains both
    #[inline]
    pub fn merge(self, other: Self) -> Self {
        Self::bounding_box([self.loc, self.loc + self.size, other.loc, other.loc + other.size])
    }

    /// Subtract another [`Rectangle`] from this [`Rectangle`]
    ///
    /// If the rectangles to not overlap the original rectangle will
    /// be returned.
    /// If the other rectangle contains self no rectangle will be returned,
    /// otherwise up to 4 rectangles will be returned.
    pub fn subtract_rect(self, other: Self) -> Vec<Self> {
        self.subtract_rects([other])
    }

    /// Subtract a set of [`Rectangle`]s from this [`Rectangle`]
    pub fn subtract_rects(self, others: impl IntoIterator<Item = Self>) -> Vec<Self> {
        let mut remaining = Vec::with_capacity(4);
        remaining.push(self);
        Self::subtract_rects_many_in_place(remaining, others)
    }

    /// Subtract a set of [`Rectangle`]s from a set [`Rectangle`]s
    pub fn subtract_rects_many(
        rects: impl IntoIterator<Item = Self>,
        others: impl IntoIterator<Item = Self>,
    ) -> Vec<Self> {
        let remaining = rects.into_iter().collect::<Vec<_>>();
        Self::subtract_rects_many_in_place(remaining, others)
    }

    /// Subtract a set of [`Rectangle`]s from a set [`Rectangle`]s in-place
    pub fn subtract_rects_many_in_place(
        mut rects: Vec<Self>,
        others: impl IntoIterator<Item = Self>,
    ) -> Vec<Self> {
        for other in others {
            let items = rects.len();
            let mut checked = 0usize;
            let mut index = 0usize;

            // If there is nothing left we can stop,
            // we won't be able to subtract any further
            if items == 0 {
                return rects;
            }

            while checked != items {
                checked += 1;

                // If there is no overlap there is nothing to subtract
                let Some(intersection) = rects[index].intersection(other) else {
                    index += 1;
                    continue;
                };

                // We now know that we have to subtract the other rect
                let item = rects.remove(index);

                // If we are completely contained then nothing is left
                if other.contains_rect(item) {
                    continue;
                }

                let top_rect = Rectangle::new(
                    item.loc,
                    (item.size.w, intersection.loc.y.saturating_sub(item.loc.y)).into(),
                );
                let left_rect: Rectangle<N, Kind> = Rectangle::new(
                    (item.loc.x, intersection.loc.y).into(),
                    (intersection.loc.x.saturating_sub(item.loc.x), intersection.size.h).into(),
                );
                let right_rect: Rectangle<N, Kind> = Rectangle::new(
                    (
                        intersection.loc.x.saturating_add(intersection.size.w),
                        intersection.loc.y,
                    )
                        .into(),
                    (
                        (item.loc.x.saturating_add(item.size.w))
                            .saturating_sub(intersection.loc.x.saturating_add(intersection.size.w)),
                        intersection.size.h,
                    )
                        .into(),
                );
                let bottom_rect: Rectangle<N, Kind> = Rectangle::new(
                    (item.loc.x, intersection.loc.y.saturating_add(intersection.size.h)).into(),
                    (
                        item.size.w,
                        (item.loc.y.saturating_add(item.size.h))
                            .saturating_sub(intersection.loc.y.saturating_add(intersection.size.h)),
                    )
                        .into(),
                );

                if !top_rect.is_empty() {
                    rects.push(top_rect);
                }

                if !left_rect.is_empty() {
                    rects.push(left_rect);
                }

                if !right_rect.is_empty() {
                    rects.push(right_rect);
                }

                if !bottom_rect.is_empty() {
                    rects.push(bottom_rect);
                }
            }
        }

        rects
    }
}

impl<N: Coordinate> Rectangle<N, Logical> {
    #[inline]
    #[cfg(feature = "xwayland")]
    pub(crate) fn to_client(self, scale: impl Into<Scale<N>>) -> Rectangle<N, Client> {
        let scale = scale.into();
        Rectangle {
            loc: self.loc.to_client(scale),
            size: self.size.to_client(scale),
        }
    }

    #[inline]
    #[cfg(feature = "xwayland")]
    pub(crate) fn to_client_precise_round<S: Coordinate, R: Coordinate>(
        self,
        scale: impl Into<Scale<S>>,
    ) -> Rectangle<R, Client> {
        self.to_f64().to_client(scale.into().to_f64()).to_i32_round()
    }

    /// Convert this logical rectangle to physical coordinate space according to given scale factor
    #[inline]
    pub fn to_physical(self, scale: impl Into<Scale<N>>) -> Rectangle<N, Physical> {
        let scale = scale.into();
        Rectangle {
            loc: self.loc.to_physical(scale),
            size: self.size.to_physical(scale),
        }
    }

    /// Convert this logical rectangle to physical coordinate space according to given scale factor
    /// and round the result
    #[inline]
    pub fn to_physical_precise_round<S: Coordinate, R: Coordinate>(
        self,
        scale: impl Into<Scale<S>>,
    ) -> Rectangle<R, Physical> {
        self.to_f64().to_physical(scale.into().to_f64()).to_i32_round()
    }

    /// Convert this logical rectangle to physical coordinate space according to given scale factor,
    /// returning the largest N-space rectangle fitting into the N-based rectangle
    ///
    /// This will ceil the location and floor the size after applying the scale
    #[inline]
    pub fn to_physical_precise_down<S: Coordinate, R: Coordinate>(
        &self,
        scale: impl Into<Scale<S>>,
    ) -> Rectangle<R, Physical> {
        self.to_f64().to_physical(scale.into().to_f64()).to_i32_down()
    }

    /// Convert this logical rectangle to physical coordinate space according to given scale factor,
    /// returning the smallest N-space rectangle encapsulating the N-based rectangle
    ///
    /// This will floor the location and ceil the size after applying the scale
    #[inline]
    pub fn to_physical_precise_up<S: Coordinate, R: Coordinate>(
        &self,
        scale: impl Into<Scale<S>>,
    ) -> Rectangle<R, Physical> {
        self.to_f64().to_physical(scale.into().to_f64()).to_i32_up()
    }

    /// Convert this logical rectangle to buffer coordinate space according to given scale factor
    #[inline]
    pub fn to_buffer(
        self,
        scale: impl Into<Scale<N>>,
        transformation: Transform,
        area: &Size<N, Logical>,
    ) -> Rectangle<N, Buffer> {
        let rect = transformation.transform_rect_in(self, area);
        let scale = scale.into();
        Rectangle {
            loc: Point {
                x: rect.loc.x.upscale(scale.x),
                y: rect.loc.y.upscale(scale.y),
                _kind: std::marker::PhantomData,
            },
            size: Size {
                w: rect.size.w.upscale(scale.x),
                h: rect.size.h.upscale(scale.y),
                _kind: std::marker::PhantomData,
            },
        }
    }
}

#[cfg(feature = "wayland_frontend")]
impl<N: Coordinate> Rectangle<N, Client> {
    #[inline]
    pub(crate) fn to_logical(self, scale: impl Into<Scale<N>>) -> Rectangle<N, Logical> {
        let scale = scale.into();
        Rectangle {
            loc: self.loc.to_logical(scale),
            size: self.size.to_logical(scale),
        }
    }
}

impl<N: Coordinate> Rectangle<N, Physical> {
    /// Convert this physical rectangle to logical coordinate space according to given scale factor
    #[inline]
    pub fn to_logical(self, scale: impl Into<Scale<N>>) -> Rectangle<N, Logical> {
        let scale = scale.into();
        Rectangle {
            loc: self.loc.to_logical(scale),
            size: self.size.to_logical(scale),
        }
    }
}

impl<N: Coordinate> Rectangle<N, Buffer> {
    /// Convert this physical rectangle to logical coordinate space according to given scale factor
    #[inline]
    pub fn to_logical(
        self,
        scale: impl Into<Scale<N>>,
        transformation: Transform,
        area: &Size<N, Buffer>,
    ) -> Rectangle<N, Logical> {
        let rect = transformation.invert().transform_rect_in(self, area);
        let scale = scale.into();
        Rectangle {
            loc: Point {
                x: rect.loc.x.downscale(scale.x),
                y: rect.loc.y.downscale(scale.y),
                _kind: std::marker::PhantomData,
            },
            size: Size {
                w: rect.size.w.downscale(scale.x),
                h: rect.size.h.downscale(scale.y),
                _kind: std::marker::PhantomData,
            },
        }
    }
}

impl<N: fmt::Debug, S> fmt::Debug for Rectangle<N, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_fmt(format_args!("Rectangle<{}>", std::any::type_name::<S>()))?;
        f.debug_struct("")
            .field("x", &self.loc.x)
            .field("y", &self.loc.y)
            .field("width", &self.size.w)
            .field("height", &self.size.h)
            .finish()
    }
}

impl<N: Clone, Kind> Clone for Rectangle<N, Kind> {
    #[inline]
    fn clone(&self) -> Self {
        Rectangle {
            loc: self.loc.clone(),
            size: self.size.clone(),
        }
    }
}

impl<N: Copy, Kind> Copy for Rectangle<N, Kind> {}

impl<N: PartialEq, Kind> PartialEq for Rectangle<N, Kind> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.loc == other.loc && self.size == other.size
    }
}

impl<N: Eq, Kind> Eq for Rectangle<N, Kind> {}

impl<N: Default, Kind> Default for Rectangle<N, Kind> {
    #[inline]
    fn default() -> Self {
        Rectangle {
            loc: Default::default(),
            size: Default::default(),
        }
    }
}

#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
/// Possible transformations to two-dimensional planes
#[derive(Default)]
pub enum Transform {
    /// Identity transformation (plane is unaltered when applied)
    #[default]
    Normal,
    /// Plane is rotated by 90 degrees
    _90,
    /// Plane is rotated by 180 degrees
    _180,
    /// Plane is rotated by 270 degrees
    _270,
    /// Plane is flipped vertically
    Flipped,
    /// Plane is flipped vertically and rotated by 90 degrees
    Flipped90,
    /// Plane is flipped vertically and rotated by 180 degrees
    Flipped180,
    /// Plane is flipped vertically and rotated by 270 degrees
    Flipped270,
}

impl Transform {
    /// Inverts any 90-degree transformation into 270-degree transformations and vise versa.
    ///
    /// Flipping is preserved and 180/Normal transformation are uneffected.
    #[inline]
    pub fn invert(&self) -> Transform {
        match self {
            Transform::Normal => Transform::Normal,
            Transform::Flipped => Transform::Flipped,
            Transform::_90 => Transform::_270,
            Transform::_180 => Transform::_180,
            Transform::_270 => Transform::_90,
            Transform::Flipped90 => Transform::Flipped270,
            Transform::Flipped180 => Transform::Flipped180,
            Transform::Flipped270 => Transform::Flipped90,
        }
    }

    /// Transforms a point inside an area of a given size by applying this transformation.
    pub fn transform_point_in<N: Coordinate, Kind>(
        &self,
        point: Point<N, Kind>,
        area: &Size<N, Kind>,
    ) -> Point<N, Kind> {
        match *self {
            Transform::Normal => point,
            Transform::_90 => (area.h - point.y, point.x).into(),
            Transform::_180 => (area.w - point.x, area.h - point.y).into(),
            Transform::_270 => (point.y, area.w - point.x).into(),
            Transform::Flipped => (area.w - point.x, point.y).into(),
            Transform::Flipped90 => (point.y, point.x).into(),
            Transform::Flipped180 => (point.x, area.h - point.y).into(),
            Transform::Flipped270 => (area.h - point.y, area.w - point.x).into(),
        }
    }

    /// Transformed size after applying this transformation.
    pub fn transform_size<N: Coordinate, Kind>(&self, size: Size<N, Kind>) -> Size<N, Kind> {
        if *self == Transform::_90
            || *self == Transform::_270
            || *self == Transform::Flipped90
            || *self == Transform::Flipped270
        {
            (size.h, size.w).into()
        } else {
            size
        }
    }

    /// Transforms a rectangle inside an area of a given size by applying this transformation.
    pub fn transform_rect_in<N: Coordinate, Kind>(
        &self,
        rect: Rectangle<N, Kind>,
        area: &Size<N, Kind>,
    ) -> Rectangle<N, Kind> {
        let size = self.transform_size(rect.size);

        let loc = match *self {
            Transform::Normal => rect.loc,
            Transform::_90 => (area.h - rect.loc.y - rect.size.h, rect.loc.x).into(),
            Transform::_180 => (
                area.w - rect.loc.x - rect.size.w,
                area.h - rect.loc.y - rect.size.h,
            )
                .into(),
            Transform::_270 => (rect.loc.y, area.w - rect.loc.x - rect.size.w).into(),
            Transform::Flipped => (area.w - rect.loc.x - rect.size.w, rect.loc.y).into(),
            Transform::Flipped90 => (
                area.h - rect.loc.y - rect.size.h,
                area.w - rect.loc.x - rect.size.w,
            )
                .into(),
            Transform::Flipped180 => (rect.loc.x, area.h - rect.loc.y - rect.size.h).into(),
            Transform::Flipped270 => (rect.loc.y, rect.loc.x).into(),
        };

        Rectangle::new(loc, size)
    }

    /// Returns true if the transformation would flip contents
    pub fn flipped(&self) -> bool {
        !matches!(
            self,
            Transform::Normal | Transform::_90 | Transform::_180 | Transform::_270
        )
    }

    /// Returns the angle (in degrees) of the transformation
    #[inline]
    pub fn degrees(&self) -> u32 {
        match self {
            Transform::Normal | Transform::Flipped => 0,
            Transform::_90 | Transform::Flipped90 => 90,
            Transform::_180 | Transform::Flipped180 => 180,
            Transform::_270 | Transform::Flipped270 => 270,
        }
    }
}

impl std::ops::Add for Transform {
    type Output = Self;

    #[inline]
    fn add(self, other: Self) -> Self {
        let flipped = matches!((self.flipped(), other.flipped()), (true, false) | (false, true));
        let degrees = (self.degrees() + other.degrees()) % 360;
        match (flipped, degrees) {
            (false, 0) => Transform::Normal,
            (false, 90) => Transform::_90,
            (false, 180) => Transform::_180,
            (false, 270) => Transform::_270,
            (true, 0) => Transform::Flipped,
            (true, 90) => Transform::Flipped90,
            (true, 180) => Transform::Flipped180,
            (true, 270) => Transform::Flipped270,
            _ => unreachable!(),
        }
    }
}

#[cfg(feature = "wayland_frontend")]
impl From<Transform> for WlTransform {
    #[inline]
    fn from(transform: Transform) -> Self {
        match transform {
            Transform::Normal => WlTransform::Normal,
            Transform::_90 => WlTransform::_90,
            Transform::_180 => WlTransform::_180,
            Transform::_270 => WlTransform::_270,
            Transform::Flipped => WlTransform::Flipped,
            Transform::Flipped90 => WlTransform::Flipped90,
            Transform::Flipped180 => WlTransform::Flipped180,
            Transform::Flipped270 => WlTransform::Flipped270,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{Logical, Rectangle, Size, Transform};

    #[test]
    fn transform_rect_ident() {
        let rect = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 40).into());
        let size = Size::from((70, 90));
        let transform = Transform::Normal;

        assert_eq!(rect, transform.transform_rect_in(rect, &size))
    }

    #[test]
    fn transform_rect_90() {
        let rect = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 40).into());
        let size = Size::from((70, 90));
        let transform = Transform::_90;

        assert_eq!(
            Rectangle::new((30, 10).into(), (40, 30).into()),
            transform.transform_rect_in(rect, &size)
        )
    }

    #[test]
    fn transform_rect_180() {
        let rect = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 40).into());
        let size = Size::from((70, 90));
        let transform = Transform::_180;

        assert_eq!(
            Rectangle::new((30, 30).into(), (30, 40).into()),
            transform.transform_rect_in(rect, &size)
        )
    }

    #[test]
    fn transform_rect_270() {
        let rect = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 40).into());
        let size = Size::from((70, 90));
        let transform = Transform::_270;

        assert_eq!(
            Rectangle::new((20, 30).into(), (40, 30).into()),
            transform.transform_rect_in(rect, &size)
        )
    }

    #[test]
    fn transform_rect_f() {
        let rect = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 40).into());
        let size = Size::from((70, 90));
        let transform = Transform::Flipped;

        assert_eq!(
            Rectangle::new((30, 20).into(), (30, 40).into()),
            transform.transform_rect_in(rect, &size)
        )
    }

    #[test]
    fn transform_rect_f90() {
        let rect = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 40).into());
        let size = Size::from((70, 80));
        let transform = Transform::Flipped90;

        assert_eq!(
            Rectangle::new((20, 30).into(), (40, 30).into()),
            transform.transform_rect_in(rect, &size)
        )
    }

    #[test]
    fn transform_rect_f180() {
        let rect = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 40).into());
        let size = Size::from((70, 90));
        let transform = Transform::Flipped180;

        assert_eq!(
            Rectangle::new((10, 30).into(), (30, 40).into()),
            transform.transform_rect_in(rect, &size)
        )
    }

    #[test]
    fn transform_rect_f270() {
        let rect = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 40).into());
        let size = Size::from((70, 90));
        let transform = Transform::Flipped270;

        assert_eq!(
            Rectangle::new((20, 10).into(), (40, 30).into()),
            transform.transform_rect_in(rect, &size)
        )
    }

    #[test]
    fn rectangle_contains_rect_itself() {
        let rect = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 40).into());
        assert!(rect.contains_rect(rect));
    }

    #[test]
    fn rectangle_contains_rect_outside() {
        let first = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 40).into());
        let second = Rectangle::<i32, Logical>::new((41, 61).into(), (30, 40).into());
        assert!(!first.contains_rect(second));
    }

    #[test]
    fn rectangle_contains_rect_extends() {
        let first = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 40).into());
        let second = Rectangle::<i32, Logical>::new((10, 20).into(), (30, 45).into());
        assert!(!first.contains_rect(second));
    }

    #[test]
    fn rectangle_subtract_full() {
        let outer = Rectangle::<i32, Logical>::from_size((100, 100).into());
        let inner = Rectangle::<i32, Logical>::new((-10, -10).into(), (1000, 1000).into());

        let rects = outer.subtract_rect(inner);
        assert_eq!(rects, vec![])
    }

    #[test]
    fn rectangle_subtract_center_hole() {
        let outer = Rectangle::<i32, Logical>::from_size((100, 100).into());
        let inner = Rectangle::<i32, Logical>::new((10, 10).into(), (80, 80).into());

        let rects = outer.subtract_rect(inner);
        assert_eq!(
            rects,
            vec![
                // Top rect
                Rectangle::<i32, Logical>::from_size((100, 10).into()),
                // Left rect
                Rectangle::<i32, Logical>::new((0, 10).into(), (10, 80).into()),
                // Right rect
                Rectangle::<i32, Logical>::new((90, 10).into(), (10, 80).into()),
                // Bottom rect
                Rectangle::<i32, Logical>::new((0, 90).into(), (100, 10).into()),
            ]
        )
    }

    #[test]
    fn rectangle_subtract_full_top() {
        let outer = Rectangle::<i32, Logical>::from_size((100, 100).into());
        let inner = Rectangle::<i32, Logical>::new((0, -20).into(), (100, 100).into());

        let rects = outer.subtract_rect(inner);
        assert_eq!(
            rects,
            vec![
                // Bottom rect
                Rectangle::<i32, Logical>::new((0, 80).into(), (100, 20).into()),
            ]
        )
    }

    #[test]
    fn rectangle_subtract_full_bottom() {
        let outer = Rectangle::<i32, Logical>::from_size((100, 100).into());
        let inner = Rectangle::<i32, Logical>::new((0, 20).into(), (100, 100).into());

        let rects = outer.subtract_rect(inner);
        assert_eq!(
            rects,
            vec![
                // Top rect
                Rectangle::<i32, Logical>::from_size((100, 20).into()),
            ]
        )
    }

    #[test]
    fn rectangle_subtract_full_left() {
        let outer = Rectangle::<i32, Logical>::from_size((100, 100).into());
        let inner = Rectangle::<i32, Logical>::new((-20, 0).into(), (100, 100).into());

        let rects = outer.subtract_rect(inner);
        assert_eq!(
            rects,
            vec![
                // Right rect
                Rectangle::<i32, Logical>::new((80, 0).into(), (20, 100).into()),
            ]
        )
    }

    #[test]
    fn rectangle_subtract_full_right() {
        let outer = Rectangle::<i32, Logical>::from_size((100, 100).into());
        let inner = Rectangle::<i32, Logical>::new((20, 0).into(), (100, 100).into());

        let rects = outer.subtract_rect(inner);
        assert_eq!(
            rects,
            vec![
                // Left rect
                Rectangle::<i32, Logical>::from_size((20, 100).into()),
            ]
        )
    }

    #[test]
    fn rectangle_overlaps_or_touches_top() {
        let top = Rectangle::<i32, Logical>::new((0, -24).into(), (800, 24).into());
        let main = Rectangle::<i32, Logical>::from_size((800, 600).into());
        assert!(main.overlaps_or_touches(top));
    }

    #[test]
    fn rectangle_overlaps_or_touches_left() {
        let left = Rectangle::<i32, Logical>::new((-4, -24).into(), (4, 624).into());
        let main = Rectangle::<i32, Logical>::from_size((800, 600).into());
        assert!(main.overlaps_or_touches(left));
    }

    #[test]
    fn rectangle_overlaps_or_touches_right() {
        let right = Rectangle::<i32, Logical>::new((800, -24).into(), (4, 624).into());
        let main = Rectangle::<i32, Logical>::from_size((800, 600).into());
        assert!(main.overlaps_or_touches(right));
    }

    #[test]
    fn rectangle_no_overlap_top() {
        let top = Rectangle::<i32, Logical>::new((0, -24).into(), (800, 24).into());
        let main = Rectangle::<i32, Logical>::from_size((800, 600).into());
        assert!(!main.overlaps(top));
    }

    #[test]
    fn rectangle_no_overlap_left() {
        let left = Rectangle::<i32, Logical>::new((-4, -24).into(), (4, 624).into());
        let main = Rectangle::<i32, Logical>::from_size((800, 600).into());
        assert!(!main.overlaps(left));
    }

    #[test]
    fn rectangle_no_overlap_right() {
        let right = Rectangle::<i32, Logical>::new((800, -24).into(), (4, 624).into());
        let main = Rectangle::<i32, Logical>::from_size((800, 600).into());
        assert!(!main.overlaps(right));
    }

    #[test]
    fn size_sub_saturate_zero() {
        let bigger = Size::<_, Logical>::from((10, 10));
        let mut smaller = Size::<_, Logical>::from((5, 5));

        assert_eq!(smaller - bigger, Size::from((0, 0)));
        smaller -= bigger;
        assert_eq!(smaller, Size::from((0, 0)));
    }
}