From 6d54483c6f86f42405cf6db9d8fdb19e9397267b Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Wed, 27 Jan 2016 18:49:01 -0300 Subject: Refactor resize sticky code --- src/command.rs | 49 ++++++++++--------------------------------------- src/safe_x11/window.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/command.rs b/src/command.rs index 74a7255..f5e3e83 100644 --- a/src/command.rs +++ b/src/command.rs @@ -150,54 +150,25 @@ pub fn resize_win(wm: &mut DotWM, _: xlib::XEvent, args: &[String]) -> bool { /// movement between `left` and `up` pub fn resize_win_sticky(wm: &mut DotWM, _: xlib::XEvent, args: &[String]) -> bool { if let Some(ref win) = wm.current_window() { - let attrs = win.attributes(); + let mut xgeometries = wm.x_geometries(); + let mut ygeometries = wm.y_geometries(); match &*args[0] { "left" => { - let mut xgeo = wm.x_geometries(); - xgeo.sort_by(|a,b| b.cmp(a)); - - let val = xgeo.iter().skip_while(|&a| *a >= attrs.x + attrs.width).next(); - if let Some(v) = val { - let diff = attrs.width - (*v - attrs.x); - if *v - attrs.x > 0 { - let _ = win.resize_to(attrs.width - diff, attrs.height); - } - } + xgeometries.sort_by(|a,b| b.cmp(a)); + win.resize_sticky_left(&xgeometries); }, "right" => { - let mut xgeo = wm.x_geometries(); - let win_right = attrs.x + attrs.width; - xgeo.sort_by(|a,b| a.cmp(b)); - - let val = xgeo.iter().skip_while(|&a| *a <= win_right).next(); - if let Some(xpoint) = val { - let new_width = *xpoint - attrs.x; - let _ = win.resize_to(new_width, attrs.height); - } + xgeometries.sort_by(|a,b| a.cmp(b)); + win.resize_sticky_right(&xgeometries); }, "up" => { - let mut ygeo = wm.y_geometries(); - ygeo.sort_by(|a,b| b.cmp(a)); - - let val = ygeo.iter().skip_while(|&a| *a >= attrs.y + attrs.height).next(); - if let Some(v) = val { - let diff = attrs.height - (*v - attrs.y); - if *v - attrs.y > 0 { - let _ = win.resize_to(attrs.width, attrs.height - diff); - } - } + ygeometries.sort_by(|a,b| b.cmp(a)); + win.resize_sticky_up(&ygeometries); }, "down" => { - let mut ygeo = wm.y_geometries(); - let win_bot = attrs.y + attrs.height; - ygeo.sort_by(|a,b| a.cmp(b)); - - let val = ygeo.iter().skip_while(|&a| *a <= win_bot).next(); - if let Some(v) = val { - let new_height = *v - attrs.y; - let _ = win.resize_to(attrs.width, new_height); - } + ygeometries.sort_by(|a,b| a.cmp(b)); + win.resize_sticky_down(&ygeometries); }, _ => (), } diff --git a/src/safe_x11/window.rs b/src/safe_x11/window.rs index c889dfc..6d1e54e 100644 --- a/src/safe_x11/window.rs +++ b/src/safe_x11/window.rs @@ -252,6 +252,56 @@ impl XWindow { } } + pub fn resize_sticky_left(&self, boundaries: &[i32]) { + let attrs = self.attributes(); + + if let Some(v) = boundaries.iter() + .skip_while(|&a| *a >= attrs.x + attrs.width) + .next() { + let diff = attrs.width - (*v - attrs.x); + if *v - attrs.x > 0 { + let _ = self.resize_to(attrs.width - diff, attrs.height); + } + } + } + + pub fn resize_sticky_right(&self, boundaries: &[i32]) { + let attrs = self.attributes(); + let win_bound = attrs.x + attrs.width; + + if let Some(v) = boundaries.iter() + .skip_while(|&a| *a <= win_bound) + .next() { + let new_width = *v - attrs.x; + let _ = self.resize_to(new_width, attrs.height); + } + } + + pub fn resize_sticky_up(&self, boundaries: &[i32]) { + let attrs = self.attributes(); + + if let Some(v) = boundaries.iter() + .skip_while(|&a| *a >= attrs.y + attrs.height) + .next() { + let diff = attrs.height - (*v - attrs.y); + if *v - attrs.y > 0 { + let _ = self.resize_to(attrs.width, attrs.height - diff); + } + } + } + + pub fn resize_sticky_down(&self, boundaries: &[i32]) { + let attrs = self.attributes(); + let win_bound = attrs.y + attrs.height; + + if let Some(v) = boundaries.iter() + .skip_while(|&a| *a <= win_bound) + .next() { + let new_height = *v - attrs.y; + let _ = self.resize_to(attrs.width, new_height); + } + } + /// Raise the focus of the window and set a border. pub fn focus(&self) { let attrs = self.attributes(); -- cgit v1.2.3-70-g09d2