From 81ef4d5ddde26cb048d5824c4e26cf882222cab6 Mon Sep 17 00:00:00 2001 From: Victoria Brekenfeld Date: Wed, 21 Aug 2024 17:54:05 +0200 Subject: [PATCH] geometry: Add new private `Client` coordinate space --- src/utils/geometry.rs | 75 +++++++++++++++++++++++++++++++++++++++++++ src/utils/mod.rs | 2 ++ 2 files changed, 77 insertions(+) diff --git a/src/utils/geometry.rs b/src/utils/geometry.rs index f0ec6ddc34..2f77f6ef5c 100644 --- a/src/utils/geometry.rs +++ b/src/utils/geometry.rs @@ -8,6 +8,11 @@ use wayland_server::protocol::wl_output::Transform as WlTransform; #[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; @@ -453,6 +458,17 @@ impl fmt::Debug for Point { } impl Point { + #[inline] + #[cfg(feature = "wayland_frontend")] + pub(crate) fn to_client(self, scale: impl Into>) -> Point { + let scale: Scale = scale.into(); + Point { + x: self.x.upscale(scale.x), + y: self.y.upscale(scale.y), + _kind: std::marker::PhantomData, + } + } + #[inline] /// Convert this logical point to physical coordinate space according to given scale factor pub fn to_physical(self, scale: impl Into>) -> Point { @@ -512,6 +528,19 @@ impl Point { } } +#[cfg(feature = "wayland_frontend")] +impl Point { + #[inline] + pub(crate) fn to_logical(self, scale: impl Into>) -> Point { + let scale = scale.into(); + Point { + x: self.x.downscale(scale.x), + y: self.y.downscale(scale.y), + _kind: std::marker::PhantomData, + } + } +} + impl Point { #[inline] /// Convert this physical point to logical coordinate space according to given scale factor @@ -768,6 +797,17 @@ impl fmt::Debug for Size { } impl Size { + #[inline] + #[cfg(feature = "wayland_frontend")] + pub(crate) fn to_client(self, scale: impl Into>) -> Size { + 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>) -> Size { @@ -821,6 +861,19 @@ impl Size { } } +#[cfg(feature = "wayland_frontend")] +impl Size { + #[inline] + pub(crate) fn to_logical(self, scale: impl Into>) -> Size { + let scale = scale.into(); + Size { + w: self.w.downscale(scale.x), + h: self.h.downscale(scale.y), + _kind: std::marker::PhantomData, + } + } +} + impl Size { #[inline] /// Convert this physical point to logical coordinate space according to given scale factor @@ -1273,6 +1326,16 @@ impl Rectangle { } impl Rectangle { + #[inline] + #[cfg(feature = "xwayland")] + pub(crate) fn to_client(self, scale: impl Into>) -> Rectangle { + let scale = scale.into(); + Rectangle { + loc: self.loc.to_client(scale), + size: self.size.to_client(scale), + } + } + /// Convert this logical rectangle to physical coordinate space according to given scale factor #[inline] pub fn to_physical(self, scale: impl Into>) -> Rectangle { @@ -1342,6 +1405,18 @@ impl Rectangle { } } +#[cfg(feature = "wayland_frontend")] +impl Rectangle { + #[inline] + pub(crate) fn to_logical(self, scale: impl Into>) -> Rectangle { + let scale = scale.into(); + Rectangle { + loc: self.loc.to_logical(scale), + size: self.size.to_logical(scale), + } + } +} + impl Rectangle { /// Convert this physical rectangle to logical coordinate space according to given scale factor #[inline] diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 12f7cc33ef..faa9e9bbf6 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -18,6 +18,8 @@ pub use fd::*; #[cfg(feature = "wayland_frontend")] pub(crate) mod sealed_file; +#[cfg(feature = "wayland_frontend")] +pub(crate) use self::geometry::Client; pub use self::geometry::{ Buffer, Coordinate, Logical, Physical, Point, Raw, Rectangle, Scale, Size, Transform, };