diff --git a/src/backends/traits.rs b/src/backends/traits.rs index 603a3b8..1698c69 100644 --- a/src/backends/traits.rs +++ b/src/backends/traits.rs @@ -1,7 +1,5 @@ -use super::{ - window_event, - window_event::{KeyOrMouseBind, Point}, -}; +use super::window_event::{self, KeyOrMouseBind}; +use crate::util::{Point, Size}; pub trait WindowServerBackend { type Window; @@ -26,13 +24,13 @@ pub trait WindowServerBackend { fn configure_window( &self, window: Self::Window, - new_size: Option>, + new_size: Option>, new_pos: Option>, new_border: Option, ); - fn screen_size(&self) -> Point; - fn get_window_size(&self, window: Self::Window) -> Option>; + fn screen_size(&self) -> Size; + fn get_window_size(&self, window: Self::Window) -> Option>; fn grab_cursor(&self); fn ungrab_cursor(&self); @@ -43,7 +41,7 @@ pub trait WindowServerBackend { fn set_active_window_border_color(&mut self, color_name: &str); fn set_inactive_window_border_color(&mut self, color_name: &str); - fn resize_window(&self, window: Self::Window, new_size: Point) { + fn resize_window(&self, window: Self::Window, new_size: Size) { self.configure_window(window, Some(new_size), None, None); } diff --git a/src/backends/window_event.rs b/src/backends/window_event.rs index 92d1e95..44ddb1e 100644 --- a/src/backends/window_event.rs +++ b/src/backends/window_event.rs @@ -1,6 +1,7 @@ #![allow(dead_code)] use super::keycodes::{KeyOrButton, MouseButton, VirtualKeyCode}; +use crate::util::{Point, Size}; use bitflags::bitflags; #[derive(Debug)] @@ -129,8 +130,6 @@ impl KeyEvent { } } -pub use crate::util::Point; - #[derive(Debug)] pub struct ButtonEvent { pub window: Window, @@ -200,11 +199,11 @@ impl DestroyEvent { pub struct CreateEvent { pub window: Window, pub position: Point, - pub size: Point, + pub size: Size, } impl CreateEvent { - pub fn new(window: Window, position: Point, size: Point) -> Self { + pub fn new(window: Window, position: Point, size: Size) -> Self { Self { window, position, @@ -217,11 +216,11 @@ impl CreateEvent { pub struct ConfigureEvent { pub window: Window, pub position: Point, - pub size: Point, + pub size: Size, } impl ConfigureEvent { - pub fn new(window: Window, position: Point, size: Point) -> Self { + pub fn new(window: Window, position: Point, size: Size) -> Self { Self { window, position, diff --git a/src/backends/xlib/mod.rs b/src/backends/xlib/mod.rs index 22ee3de..4d222d1 100644 --- a/src/backends/xlib/mod.rs +++ b/src/backends/xlib/mod.rs @@ -1,4 +1,5 @@ use log::{error, warn}; +use num_traits::Zero; use std::{ffi::CString, mem::MaybeUninit, rc::Rc}; use thiserror::Error; @@ -19,10 +20,11 @@ use super::{ window_event::{ ButtonEvent, ConfigureEvent, DestroyEvent, EnterEvent, FullscreenEvent, FullscreenState, KeyEvent, KeyOrMouseBind, KeyState, MapEvent, - ModifierState, MotionEvent, Point, UnmapEvent, WindowEvent, + ModifierState, MotionEvent, UnmapEvent, WindowEvent, }, WindowServerBackend, }; +use crate::util::{Point, Size}; pub mod color; pub mod keysym; @@ -804,8 +806,8 @@ impl WindowServerBackend for XLib { } fn hide_window(&self, window: Self::Window) { - let screen_size = self.screen_size(); - self.move_window(window, screen_size); + let screen_size = self.screen_size() + Size::new(100, 100); + self.move_window(window, screen_size.into()); } fn kill_window(&self, window: Self::Window) { @@ -831,17 +833,17 @@ impl WindowServerBackend for XLib { fn configure_window( &self, window: Self::Window, - new_size: Option>, - new_pos: Option>, + new_size: Option>, + new_pos: Option>, new_border: Option, ) { - let position = new_pos.unwrap_or(Point::new(0, 0)); - let size = new_size.unwrap_or(Point::new(0, 0)); + let position = new_pos.unwrap_or(Point::zero()); + let size = new_size.unwrap_or(Size::zero()); let mut wc = xlib::XWindowChanges { x: position.x, y: position.y, - width: size.x, - height: size.y, + width: size.width, + height: size.height, border_width: new_border.unwrap_or(0), sibling: 0, stack_mode: 0, @@ -867,7 +869,7 @@ impl WindowServerBackend for XLib { } } - fn screen_size(&self) -> Point { + fn screen_size(&self) -> Size { unsafe { let mut wa = std::mem::MaybeUninit::::zeroed(); @@ -880,7 +882,7 @@ impl WindowServerBackend for XLib { } } - fn get_window_size(&self, window: Self::Window) -> Option> { + fn get_window_size(&self, window: Self::Window) -> Option> { self.get_window_attributes(window) .map(|wa| (wa.width, wa.height).into()) } diff --git a/src/clients.rs b/src/clients.rs index e806c08..6676d57 100644 --- a/src/clients.rs +++ b/src/clients.rs @@ -9,14 +9,13 @@ use crate::util::{Point, Size}; mod client { use std::hash::{Hash, Hasher}; + use crate::util::{Point, Size}; use x11::xlib::Window; - use crate::backends::window_event::Point; - #[derive(Clone, Debug)] pub struct Client { pub(crate) window: Window, - pub(crate) size: Point, + pub(crate) size: Size, pub(crate) position: Point, pub(crate) transient_for: Option, pub(crate) fullscreen: bool, @@ -38,7 +37,7 @@ mod client { #[allow(dead_code)] pub fn new( window: Window, - size: Point, + size: Size, position: Point, ) -> Self { Self { @@ -51,7 +50,7 @@ mod client { pub fn new_transient( window: Window, - size: Point, + size: Size, transient: Window, ) -> Self { Self { @@ -165,7 +164,7 @@ pub struct ClientState { pub(self) virtual_screens: VirtualScreenStore, pub(self) gap: i32, - pub(self) screen_size: Point, + pub(self) screen_size: Size, pub(self) master_size: f32, border_size: i32, } @@ -214,7 +213,7 @@ impl ClientState { } } - pub fn with_screen_size(self, screen_size: Point) -> Self { + pub fn with_screen_size(self, screen_size: Size) -> Self { Self { screen_size, ..self @@ -248,9 +247,9 @@ impl ClientState { client.position = { ( transient.position.x - + (transient.size.x - client.size.x) / 2, + + (transient.size.width - client.size.width) / 2, transient.position.y - + (transient.size.y - client.size.y) / 2, + + (transient.size.height - client.size.height) / 2, ) .into() }; diff --git a/src/state.rs b/src/state.rs index bd79771..4eadd27 100644 --- a/src/state.rs +++ b/src/state.rs @@ -4,13 +4,14 @@ use log::{error, info}; use x11::xlib::{self, Window}; +use crate::backends::window_event::{FullscreenEvent, FullscreenState}; +use crate::util::{Point, Size}; use crate::{ backends::{ keycodes::{MouseButton, VirtualKeyCode}, window_event::{ - ButtonEvent, ConfigureEvent, FullscreenEvent, FullscreenState, - KeyBind, KeyEvent, KeyState, MapEvent, ModifierKey, ModifierState, - MotionEvent, MouseBind, Point, WindowEvent, + ButtonEvent, ConfigureEvent, KeyBind, KeyEvent, KeyState, MapEvent, + ModifierKey, ModifierState, MotionEvent, MouseBind, WindowEvent, }, xlib::XLib, WindowServerBackend, @@ -105,7 +106,7 @@ struct MoveInfoInner { struct ResizeInfoInner { window: Window, starting_cursor_pos: Point, - starting_window_size: Point, + starting_window_size: Size, } use derivative::*; @@ -780,12 +781,7 @@ where let client = self.clients.get(&window).unwrap(); - let corner_pos = { - ( - client.position.x + client.size.x, - client.position.y + client.size.y, - ) - }; + let corner_pos = client.position + client.size.into(); self.backend.move_cursor(None, corner_pos.into()); self.backend.grab_cursor(); @@ -844,8 +840,10 @@ where { let size = &mut client.size; - size.x = std::cmp::max(1, info.starting_window_size.x + x); - size.y = std::cmp::max(1, info.starting_window_size.y + y); + size.width = + std::cmp::max(1, info.starting_window_size.width + x); + size.height = + std::cmp::max(1, info.starting_window_size.height + y); self.backend.resize_window(client.window, client.size); } diff --git a/src/util.rs b/src/util.rs index 75f9d78..bfc5fc2 100644 --- a/src/util.rs +++ b/src/util.rs @@ -36,6 +36,47 @@ mod size { pub height: I, } + impl std::ops::Add for Size + where + I: num_traits::PrimInt + num_traits::Zero, + { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + Self { + width: self.width + rhs.width, + height: self.height + rhs.height, + } + } + } + + impl std::ops::Sub for Size + where + I: num_traits::PrimInt + num_traits::Zero, + { + type Output = Self; + + fn sub(self, rhs: Self) -> Self::Output { + Self { + width: self.width - rhs.width, + height: self.height - rhs.height, + } + } + } + + impl num_traits::Zero for Size + where + I: num_traits::PrimInt + num_traits::Zero, + { + fn zero() -> Self { + Self::default() + } + + fn is_zero(&self) -> bool { + self.width == I::zero() && self.height == I::zero() + } + } + impl Default for Size where I: num_traits::PrimInt + num_traits::Zero, @@ -101,6 +142,47 @@ mod point { pub y: I, } + impl std::ops::Add for Point + where + I: num_traits::PrimInt + num_traits::Zero, + { + type Output = Self; + + fn add(self, rhs: Self) -> Self::Output { + Self { + x: self.x + rhs.x, + y: self.y + rhs.y, + } + } + } + + impl std::ops::Sub for Point + where + I: num_traits::PrimInt + num_traits::Zero, + { + type Output = Self; + + fn sub(self, rhs: Self) -> Self::Output { + Self { + x: self.x - rhs.x, + y: self.y - rhs.y, + } + } + } + + impl num_traits::Zero for Point + where + I: num_traits::PrimInt + num_traits::Zero, + { + fn zero() -> Self { + Self::default() + } + + fn is_zero(&self) -> bool { + self.x == I::zero() && self.y == I::zero() + } + } + impl Default for Point where I: num_traits::PrimInt + num_traits::Zero,