layout: Extract Monitor::remove_workspace_by_idx()

This commit is contained in:
Ivan Molodetskikh
2025-09-25 18:15:46 +03:00
parent 1484175408
commit 3b4cf1ec8f
2 changed files with 27 additions and 30 deletions

View File

@@ -3414,19 +3414,7 @@ impl<W: LayoutElement> Layout<W> {
return false;
}
if current.active_workspace_idx == current.workspaces.len() - 1 {
// Insert a new empty workspace.
current.add_workspace_bottom();
}
if current.options.layout.empty_workspace_above_first && current.active_workspace_idx == 0 {
current.add_workspace_top();
}
let mut ws = current.workspaces.remove(current.active_workspace_idx);
current.active_workspace_idx = current.active_workspace_idx.saturating_sub(1);
current.workspace_switch = None;
current.clean_up_workspaces();
let mut ws = current.remove_workspace_by_idx(current.active_workspace_idx);
ws.set_output(Some(output.clone()));
ws.original_output = OutputId::new(output);
@@ -3500,23 +3488,7 @@ impl<W: LayoutElement> Layout<W> {
let current_active_ws_idx = current.active_workspace_idx;
if old_idx == current.workspaces.len() - 1 {
// Insert a new empty workspace.
current.add_workspace_bottom();
}
let mut ws = current.workspaces.remove(old_idx);
if current.options.layout.empty_workspace_above_first && old_idx == 0 {
current.add_workspace_top();
}
if old_idx < current.active_workspace_idx {
current.active_workspace_idx -= 1;
}
current.workspace_switch = None;
current.clean_up_workspaces();
let mut ws = current.remove_workspace_by_idx(old_idx);
ws.set_output(Some(new_output.clone()));
ws.original_output = OutputId::new(&new_output);

View File

@@ -604,6 +604,31 @@ impl<W: LayoutElement> Monitor<W> {
true
}
pub fn remove_workspace_by_idx(&mut self, mut idx: usize) -> Workspace<W> {
if idx == self.workspaces.len() - 1 {
self.add_workspace_bottom();
}
if self.options.layout.empty_workspace_above_first && idx == 0 {
self.add_workspace_top();
idx += 1;
}
let mut ws = self.workspaces.remove(idx);
ws.set_output(None);
// For monitor current workspace removal, we focus previous rather than next (<= rather
// than <). This is different from columns and tiles, but it lets move-workspace-to-monitor
// back and forth to preserve position.
if idx <= self.active_workspace_idx && self.active_workspace_idx > 0 {
self.active_workspace_idx -= 1;
}
self.workspace_switch = None;
self.clean_up_workspaces();
ws
}
pub fn move_down_or_to_workspace_down(&mut self) {
if !self.active_workspace().move_down() {
self.move_to_workspace_down(true);