Skip to content

Commit

Permalink
shell: Remove last workspace if it follows empty active workspace
Browse files Browse the repository at this point in the history
Partly fixes
pop-os/cosmic-workspaces-epoch#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.
  • Loading branch information
ids1024 committed Jan 30, 2025
1 parent 63995c4 commit 0527b26
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,34 +553,43 @@ impl WorkspaceSet {

fn ensure_last_empty(&mut self, state: &mut WorkspaceUpdateGuard<State>) {
// 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<bool> = 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);
}
Expand Down

0 comments on commit 0527b26

Please sign in to comment.