From 3b0aa9e4277cd038bf900013d7e4148651f39b29 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Thu, 30 Jan 2025 13:47:17 -0800 Subject: [PATCH] shell: Remove last workspace if it follows empty active workspace Partly fixes https://github.com/pop-os/cosmic-workspaces-epoch/issues/83, but it seems like there's at least one other issue with workspaces not being removed when they should be. The if condition got a bit complicated here, so I've split it up and inverted the condition. --- src/shell/mod.rs | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/shell/mod.rs b/src/shell/mod.rs index c0f4f97e..ef871567 100644 --- a/src/shell/mod.rs +++ b/src/shell/mod.rs @@ -553,34 +553,43 @@ impl WorkspaceSet { fn ensure_last_empty(&mut self, state: &mut WorkspaceUpdateGuard) { // add empty at the end, if necessary - if self - .workspaces - .last() - .map(|last| !last.is_empty()) - .unwrap_or(true) - { + if self.workspaces.last().map_or(true, |last| !last.is_empty()) { self.add_empty_workspace(state); } - // remove empty workspaces in between, if they are not active + // remove other empty workspaces let len = self.workspaces.len(); - let mut keep = vec![true; len]; - for (i, workspace) in self.workspaces.iter().enumerate() { - if workspace.is_empty() && i != self.active && i != len - 1 { - state.remove_workspace(workspace.handle); - keep[i] = false; - } - } + let kept: Vec = self + .workspaces + .iter() + .enumerate() + .map(|(i, workspace)| { + let previous_is_empty = + i > 0 && self.workspaces.get(i - 1).map_or(false, |w| w.is_empty()); + let keep = if workspace.is_empty() { + // Keep empty workspace if it's active, or it's the last workspace, + // and the previous worspace is not both active and empty. + i == self.active + || (i == len - 1 && !(i == self.active + 1 && previous_is_empty)) + } else { + true + }; + if !keep { + state.remove_workspace(workspace.handle); + } + keep + }) + .collect(); - let mut iter = keep.iter(); + let mut iter = kept.iter(); self.workspaces.retain(|_| *iter.next().unwrap()); - self.active -= keep + self.active -= kept .iter() .take(self.active + 1) - .filter(|keep| !**keep) + .filter(|kept| !**kept) .count(); - if keep.iter().any(|val| *val == false) { + if kept.iter().any(|val| *val == false) { for (i, workspace) in self.workspaces.iter().enumerate() { workspace_set_idx(state, i as u8 + 1, self.idx, &workspace.handle); }