aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatias Linares <matiaslina@openmailbox.org>2016-01-27 18:49:01 -0300
committerMatias Linares <matiaslina@openmailbox.org>2016-01-27 18:49:01 -0300
commit6d54483c6f86f42405cf6db9d8fdb19e9397267b (patch)
tree4205f07a8732f60d9c6f6cf8544bd34102ff8221 /src
parent5d7d8a19f6364dfb6e6583125ced3103d069f2b3 (diff)
downloaddotwm-6d54483c6f86f42405cf6db9d8fdb19e9397267b.tar.gz
Refactor resize sticky code
Diffstat (limited to 'src')
-rw-r--r--src/command.rs49
-rw-r--r--src/safe_x11/window.rs50
2 files changed, 60 insertions, 39 deletions
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();