From 05bfd598a060beafe1a20ff759e499987f1ce87b Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Mon, 9 Nov 2015 01:33:33 -0300 Subject: Focus handling. For now this have some bugs. Some complex programs (i.e. thunar, firefox) will spawn a lot of XCreateEvents and the last window it's not focused correctly. Also the enter notify should work on an input instead hovering the window --- src/dotwm.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'src/dotwm.rs') diff --git a/src/dotwm.rs b/src/dotwm.rs index 2d64584..77f09b3 100644 --- a/src/dotwm.rs +++ b/src/dotwm.rs @@ -104,14 +104,18 @@ impl DotWM { } pub fn add_window(&mut self, w: xlib::Window) { + self.unfocus_current_window(); if let Some(w) = XWindow::new(self.display, w) { + w.select_input(xlib::EnterWindowMask); self.window_list.push(w); // Last windows get focus. self.cw_idx = self.window_list.len() - 1; + self.focus_current_window(); } } pub fn current_window(&self) -> Option<&XWindow> { + println!("trying to get {} from list of len {}", self.cw_idx, self.window_list.len()); if self.cw_idx < self.window_list.len() { self.window_list.get(self.cw_idx) } else { @@ -121,15 +125,46 @@ impl DotWM { pub fn remove_window(&mut self, w: xlib::Window) { let pos = self.window_list.iter().position(|xw| xw.inner == w); + println!("Removing widnow {}: pos: {:?}", w, pos); match pos { Some(idx) => { + let new_idx = if idx == 0 { usize::max_value() } else { idx - 1 }; self.window_list.remove(idx); - self.cw_idx = usize::max_value(); + self.cw_idx = new_idx; + self.focus_current_window(); }, None => (), } } + + pub fn change_focus_of(&mut self, idx: usize) { + let w = &self.window_list[idx]; + self.unfocus_current_window(); + self.cw_idx = idx; + w.set_border_width(1); + w.set_border_color("red"); + w.raise(); + } + + pub fn find_window(&self, w: xlib::Window) -> Option { + self.window_list.iter().position(|xw| xw.inner == w) + } + + + fn focus_current_window(&self) { + if let Some(win) = self.current_window() { + win.set_border_width(1); + win.set_border_color("red"); + } + } + + fn unfocus_current_window(&self) { + if let Some(win) = self.current_window() { + win.set_border_width(1); + win.set_border_color("black"); + } + } } impl Drop for DotWM { -- cgit v1.2.3-70-g09d2