diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 60c0f1fe..084c0236 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -3414,19 +3414,7 @@ impl Layout { 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 Layout { 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); diff --git a/src/layout/monitor.rs b/src/layout/monitor.rs index 600907cc..3c258d49 100644 --- a/src/layout/monitor.rs +++ b/src/layout/monitor.rs @@ -604,6 +604,31 @@ impl Monitor { true } + pub fn remove_workspace_by_idx(&mut self, mut idx: usize) -> Workspace { + 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);