utils: New geometry spaces local/global

This commit is contained in:
Victoria Brekenfeld 2023-10-25 19:24:51 +02:00
parent c38a236ffa
commit 42aaafe586
18 changed files with 516 additions and 300 deletions

149
src/utils/geometry.rs Normal file
View file

@ -0,0 +1,149 @@
use smithay::{
output::Output,
utils::{Coordinate, Logical, Point, Rectangle, Size},
};
use super::prelude::OutputExt;
/// Marker type for coordinates in global space
#[derive(Debug)]
pub struct Global;
/// Marker type for coordinates in workspace local space
#[derive(Debug)]
pub struct Local;
pub trait PointExt<C: Coordinate> {
fn as_global(self) -> Point<C, Global>;
fn as_local(self) -> Point<C, Local>;
}
pub trait PointGlobalExt<C: Coordinate> {
fn to_local(self, output: &Output) -> Point<C, Local>;
fn as_logical(self) -> Point<C, Logical>;
}
pub trait PointLocalExt<C: Coordinate> {
fn to_global(self, output: &Output) -> Point<C, Global>;
fn as_logical(self) -> Point<C, Logical>;
}
pub trait SizeExt<C: Coordinate> {
fn as_logical(self) -> Size<C, Logical>;
fn as_local(self) -> Size<C, Local>;
fn as_global(self) -> Size<C, Global>;
}
pub trait RectExt<C: Coordinate> {
fn as_global(self) -> Rectangle<C, Global>;
fn as_local(self) -> Rectangle<C, Local>;
}
pub trait RectGlobalExt<C: Coordinate> {
fn to_local(self, output: &Output) -> Rectangle<C, Local>;
fn as_logical(self) -> Rectangle<C, Logical>;
}
pub trait RectLocalExt<C: Coordinate> {
fn to_global(self, output: &Output) -> Rectangle<C, Global>;
fn as_logical(self) -> Rectangle<C, Logical>;
}
impl<C: Coordinate> PointExt<C> for Point<C, Logical> {
fn as_global(self) -> Point<C, Global> {
(self.x, self.y).into()
}
fn as_local(self) -> Point<C, Local> {
(self.x, self.y).into()
}
}
impl<C: Coordinate> PointGlobalExt<C> for Point<C, Global> {
fn to_local(self, output: &Output) -> Point<C, Local> {
let point = (self.to_f64() - output.geometry().loc.to_f64()).as_logical();
(C::from_f64(point.x), C::from_f64(point.y)).into()
}
fn as_logical(self) -> Point<C, Logical> {
(self.x, self.y).into()
}
}
impl<C: Coordinate> PointLocalExt<C> for Point<C, Local> {
fn to_global(self, output: &Output) -> Point<C, Global> {
let point =
(self.to_f64().as_logical() + output.geometry().loc.to_f64().as_logical()).as_global();
(C::from_f64(point.x), C::from_f64(point.y)).into()
}
fn as_logical(self) -> Point<C, Logical> {
(self.x, self.y).into()
}
}
impl<C: Coordinate> SizeExt<C> for Size<C, Global> {
fn as_logical(self) -> Size<C, Logical> {
(self.w, self.h).into()
}
fn as_global(self) -> Size<C, Global> {
self
}
fn as_local(self) -> Size<C, Local> {
(self.w, self.h).into()
}
}
impl<C: Coordinate> SizeExt<C> for Size<C, Local> {
fn as_logical(self) -> Size<C, Logical> {
(self.w, self.h).into()
}
fn as_global(self) -> Size<C, Global> {
(self.w, self.h).into()
}
fn as_local(self) -> Size<C, Local> {
self
}
}
impl<C: Coordinate> SizeExt<C> for Size<C, Logical> {
fn as_logical(self) -> Size<C, Logical> {
self
}
fn as_global(self) -> Size<C, Global> {
(self.w, self.h).into()
}
fn as_local(self) -> Size<C, Local> {
(self.w, self.h).into()
}
}
impl<C: Coordinate> RectExt<C> for Rectangle<C, Logical> {
fn as_global(self) -> Rectangle<C, Global> {
Rectangle::from_loc_and_size(self.loc.as_global(), (self.size.w, self.size.h))
}
fn as_local(self) -> Rectangle<C, Local> {
Rectangle::from_loc_and_size(self.loc.as_local(), (self.size.w, self.size.h))
}
}
impl<C: Coordinate> RectGlobalExt<C> for Rectangle<C, Global> {
fn to_local(self, output: &Output) -> Rectangle<C, Local> {
Rectangle::from_loc_and_size(self.loc.to_local(output), (self.size.w, self.size.h))
}
fn as_logical(self) -> Rectangle<C, Logical> {
Rectangle::from_loc_and_size(self.loc.as_logical(), self.size.as_logical())
}
}
impl<C: Coordinate> RectLocalExt<C> for Rectangle<C, Local> {
fn to_global(self, output: &Output) -> Rectangle<C, Global> {
Rectangle::from_loc_and_size(self.loc.to_global(output), (self.size.w, self.size.h))
}
fn as_logical(self) -> Rectangle<C, Logical> {
Rectangle::from_loc_and_size(self.loc.as_logical(), self.size.as_logical())
}
}

View file

@ -2,6 +2,7 @@
mod ids;
pub(crate) use self::ids::id_gen;
pub mod geometry;
pub mod iced;
pub mod prelude;
pub mod tween;

View file

@ -11,20 +11,21 @@ use smithay::{
Seat,
},
output::Output,
utils::{Buffer, IsAlive, Logical, Monotonic, Point, Rectangle, Time, Transform},
utils::{Buffer, IsAlive, Monotonic, Point, Rectangle, Time, Transform},
wayland::compositor::with_states,
};
pub use super::geometry::*;
pub use crate::shell::{Shell, Workspace};
pub use crate::state::{Common, State};
pub use crate::wayland::handlers::xdg_shell::popup::update_reactive_popups;
pub trait OutputExt {
fn geometry(&self) -> Rectangle<i32, Logical>;
fn geometry(&self) -> Rectangle<i32, Global>;
}
impl OutputExt for Output {
fn geometry(&self) -> Rectangle<i32, Logical> {
fn geometry(&self) -> Rectangle<i32, Global> {
Rectangle::from_loc_and_size(self.current_location(), {
Transform::from(self.current_transform())
.transform_size(
@ -36,6 +37,7 @@ impl OutputExt for Output {
.to_logical(self.current_scale().fractional_scale())
.to_i32_round()
})
.as_global()
}
}