From f7a827971f436d9d493cff9ecb7fe91958f71be1 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 27 Dec 2015 23:28:43 -0300 Subject: Fix some problems @ removing a window. When we wanted to remove the window idx 0 within the list. the current index went to usize::max_value always. This add a check for the case when we have more than one window and removes the window 0. --- src/desktop.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/desktop.rs b/src/desktop.rs index 520c196..8bd4a4c 100644 --- a/src/desktop.rs +++ b/src/desktop.rs @@ -27,6 +27,14 @@ impl Desktop { } } + /// Tells if the current index is valid for indexing the window list. + /// + /// This is a precondition that must be valid wherever one index that + /// list. + fn valid_idx(&self) -> bool { + self.cw_idx != usize::max_value() + } + pub fn current_window(&self) -> Option<&XWindow> { if self.cw_idx < self.window_list.len() { self.window_list.get(self.cw_idx) @@ -63,7 +71,11 @@ impl Desktop { match pos { Some(idx) => { - let new_idx = if idx == 0 { usize::max_value() } else { idx - 1 }; + let new_idx = if idx == 0 { + if self.window_list.len() == 1 { usize::max_value() } else { 0 } + } else { + idx - 1 + }; self.window_list.remove(idx); self.cw_idx = new_idx; self.focus_current_window(); @@ -93,7 +105,7 @@ impl Desktop { /// There're 3 posibilities. There's no window, there's one window or there /// are more windows. pub fn focus_next(&mut self) { - if self.window_list.len() > 0 { + if self.valid_idx() { // Unfocus the current window. let prev_win = &self.window_list[self.cw_idx]; prev_win.unfocus(); -- cgit v1.2.3-70-g09d2