diff --git a/Cargo.toml b/Cargo.toml index 30b0998..c7534c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,3 +22,4 @@ bitflags = "1.3.2" derivative = "2.2.0" serde = { version = "1.0", features = ["derive"] } toml = "0.5" +num-traits = "0.2.14" diff --git a/src/backends/window_event.rs b/src/backends/window_event.rs index 507f0af..92d1e95 100644 --- a/src/backends/window_event.rs +++ b/src/backends/window_event.rs @@ -129,42 +129,7 @@ impl KeyEvent { } } -#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)] -pub struct Point -where - I: Copy + Clone + PartialEq + PartialOrd, -{ - pub x: I, - pub y: I, -} -impl From<(I, I)> for Point -where - I: Copy + Clone + PartialEq + PartialOrd, -{ - fn from(value: (I, I)) -> Self { - Self::from_tuple(value) - } -} - -impl Point -where - I: Copy + Clone + PartialEq + PartialOrd, -{ - pub fn new(x: I, y: I) -> Self { - Self { x, y } - } - - pub fn from_tuple(tuple: (I, I)) -> Self { - Self { - x: tuple.0, - y: tuple.1, - } - } - - pub fn as_tuple(&self) -> (I, I) { - (self.x, self.y) - } -} +pub use crate::util::Point; #[derive(Debug)] pub struct ButtonEvent { diff --git a/src/clients.rs b/src/clients.rs index e641d8a..a01f22e 100644 --- a/src/clients.rs +++ b/src/clients.rs @@ -2,10 +2,10 @@ use std::num::NonZeroI32; use std::{ops::Rem, usize}; use indexmap::IndexMap; -use log::{error, info}; +use log::error; -use crate::backends::window_event::Point; use crate::util::BuildIdentityHasher; +use crate::util::{Point, Size}; mod client { use std::hash::{Hash, Hasher}; @@ -538,7 +538,7 @@ impl ClientState { let (new, old) = self.focus_client_inner(key); if !(new.is_vacant() && old.is_vacant()) { - info!("Swapping focus: new({:?}) old({:?})", new, old); + //info!("Swapping focus: new({:?}) old({:?})", new, old); } (new, old) diff --git a/src/util.rs b/src/util.rs index 4389ed7..75f9d78 100644 --- a/src/util.rs +++ b/src/util.rs @@ -22,3 +22,136 @@ impl Hasher for IdentityHasher { } pub type BuildIdentityHasher = BuildHasherDefault; + +pub use point::Point; +pub use size::Size; + +mod size { + #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)] + pub struct Size + where + I: num_traits::PrimInt + num_traits::Zero, + { + pub width: I, + pub height: I, + } + + impl Default for Size + where + I: num_traits::PrimInt + num_traits::Zero, + { + fn default() -> Self { + Self { + width: I::zero(), + height: I::zero(), + } + } + } + + impl From<(I, I)> for Size + where + I: num_traits::PrimInt + num_traits::Zero, + { + fn from(value: (I, I)) -> Self { + Self::from_tuple(value) + } + } + + impl From> for Size + where + I: num_traits::PrimInt + num_traits::Zero, + { + fn from(value: super::point::Point) -> Self { + Self::new(value.x, value.y) + } + } + + impl Size + where + I: num_traits::PrimInt + num_traits::Zero, + { + pub fn new(width: I, height: I) -> Self { + Self { width, height } + } + + pub fn from_tuple(tuple: (I, I)) -> Self { + Self::new(tuple.0, tuple.1) + } + + pub fn as_tuple(&self) -> (I, I) { + (self.width, self.height) + } + + pub fn map(self, f: F) -> Self + where + F: FnOnce(I, I) -> Self, + { + f(self.width, self.height) + } + } +} + +mod point { + #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)] + pub struct Point + where + I: num_traits::PrimInt + num_traits::Zero, + { + pub x: I, + pub y: I, + } + + impl Default for Point + where + I: num_traits::PrimInt + num_traits::Zero, + { + fn default() -> Self { + Self { + x: I::zero(), + y: I::zero(), + } + } + } + + impl From<(I, I)> for Point + where + I: num_traits::PrimInt + num_traits::Zero, + { + fn from(value: (I, I)) -> Self { + Self::from_tuple(value) + } + } + + impl From> for Point + where + I: num_traits::PrimInt + num_traits::Zero, + { + fn from(value: super::size::Size) -> Self { + Self::new(value.width, value.height) + } + } + + impl Point + where + I: num_traits::PrimInt + num_traits::Zero, + { + pub fn new(x: I, y: I) -> Self { + Self { x, y } + } + + pub fn from_tuple(tuple: (I, I)) -> Self { + Self::new(tuple.0, tuple.1) + } + + pub fn as_tuple(&self) -> (I, I) { + (self.x, self.y) + } + + pub fn map(self, f: F) -> T + where + F: FnOnce(I, I) -> T, + { + f(self.x, self.y) + } + } +}