Skip to content

Commit dbd43d7

Browse files
committed
shell: Refactor out common code for removing workspace from set
1 parent bdd3e12 commit dbd43d7

File tree

1 file changed

+60
-35
lines changed

1 file changed

+60
-35
lines changed

src/shell/mod.rs

+60-35
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,54 @@ impl WorkspaceSet {
624624
workspace_set_idx(state, i as u8 + 1, &workspace.handle);
625625
}
626626
}
627+
628+
fn post_remove_workspace(
629+
&mut self,
630+
workspace_state: &mut WorkspaceUpdateGuard<'_, State>,
631+
previous_active_handle: &WorkspaceHandle,
632+
) {
633+
if self.workspaces.is_empty() {
634+
self.add_empty_workspace(workspace_state);
635+
}
636+
self.update_workspace_idxs(workspace_state);
637+
self.active = self
638+
.workspaces
639+
.iter()
640+
.position(|w| w.handle == *previous_active_handle)
641+
.unwrap_or_else(|| {
642+
let idx = self.workspaces.len() - 1;
643+
let workspace = &self.workspaces[idx];
644+
workspace_state.add_workspace_state(&workspace.handle, WState::Active);
645+
idx
646+
});
647+
}
648+
649+
// Remove a workspace from the set, and return it, for adding to a different
650+
// workspace set
651+
fn remove_workspace(
652+
&mut self,
653+
workspace_state: &mut WorkspaceUpdateGuard<'_, State>,
654+
handle: &WorkspaceHandle,
655+
) -> Option<Workspace> {
656+
let previous_active_handle = self.workspaces[self.active].handle;
657+
let idx = self.workspaces.iter().position(|w| w.handle == *handle)?;
658+
let workspace = self.workspaces.remove(idx);
659+
self.post_remove_workspace(workspace_state, &previous_active_handle);
660+
Some(workspace)
661+
}
662+
663+
// Remove all workspaces matched by the callback from the set
664+
fn remove_workspaces(
665+
&mut self,
666+
workspace_state: &mut WorkspaceUpdateGuard<'_, State>,
667+
cb: impl Fn(&Workspace) -> bool,
668+
) -> Vec<Workspace> {
669+
let previous_active_handle = self.workspaces[self.active].handle;
670+
let (prefers, doesnt) = self.workspaces.drain(..).partition(cb);
671+
self.workspaces = doesnt;
672+
self.post_remove_workspace(workspace_state, &previous_active_handle);
673+
prefers
674+
}
627675
}
628676

629677
#[derive(Debug)]
@@ -672,30 +720,13 @@ impl Workspaces {
672720
workspace_state.add_group_output(&set.group, &output);
673721

674722
// Remove workspaces that prefer this output from other sets
675-
let mut moved_workspaces = Vec::new();
676-
for other_set in self.sets.values_mut() {
677-
let active_handle = other_set.workspaces[other_set.active].handle;
678-
let (prefers, doesnt) = other_set
679-
.workspaces
680-
.drain(..)
681-
.partition(|w| w.prefers_output(output));
682-
moved_workspaces.extend(prefers);
683-
other_set.workspaces = doesnt;
684-
if other_set.workspaces.is_empty() {
685-
other_set.add_empty_workspace(workspace_state);
686-
}
687-
other_set.update_workspace_idxs(workspace_state);
688-
other_set.active = other_set
689-
.workspaces
690-
.iter()
691-
.position(|w| w.handle == active_handle)
692-
.unwrap_or_else(|| {
693-
let idx = other_set.workspaces.len() - 1;
694-
let workspace = &other_set.workspaces[idx];
695-
workspace_state.add_workspace_state(&workspace.handle, WState::Active);
696-
idx
697-
})
698-
}
723+
let mut moved_workspaces = self
724+
.sets
725+
.values_mut()
726+
.flat_map(|other_set| {
727+
other_set.remove_workspaces(workspace_state, |w| w.prefers_output(output))
728+
})
729+
.collect::<Vec<_>>();
699730

700731
// Add `moved_workspaces` to set, and update output and index of workspaces
701732
for workspace in &mut moved_workspaces {
@@ -820,17 +851,11 @@ impl Workspaces {
820851
return;
821852
}
822853

823-
if let Some(mut workspace) = self.sets.get_mut(from).and_then(|set| {
824-
let pos = set.workspaces.iter().position(|w| &w.handle == handle)?;
825-
let workspace = set.workspaces.remove(pos);
826-
if set.workspaces.is_empty() {
827-
set.add_empty_workspace(workspace_state);
828-
}
829-
set.active = set.active.max(set.workspaces.len() - 1);
830-
workspace_state.add_workspace_state(&set.workspaces[set.active].handle, WState::Active);
831-
set.update_workspace_idxs(workspace_state);
832-
Some(workspace)
833-
}) {
854+
if let Some(mut workspace) = self
855+
.sets
856+
.get_mut(from)
857+
.and_then(|set| set.remove_workspace(workspace_state, handle))
858+
{
834859
let new_set = self.sets.get_mut(to).unwrap();
835860
move_workspace_to_group(&mut workspace, &new_set.group, workspace_state);
836861
workspace.set_output(to);

0 commit comments

Comments
 (0)