From 852da5714affd067de731599136ed619dc3bba40 Mon Sep 17 00:00:00 2001 From: Kirottu <56396750+Kirottu@users.noreply.github.com> Date: Sat, 25 Jan 2025 10:49:51 +0200 Subject: Add move-workspace-to-index and move-workspace-to-monitor actions (#1007) * Added move-workspace-to-index and move-workspace-to-monitor IPC actions * Added redraws to the workspace handling actions, fixed tests that panicked, fixed other mentioned problems. * Fixed workspace focusing and handling numbered workspaces with `move-workspace-to-index` * Fixed more inconsistencies with move-workspace-to-monitor * Added back `self.workspace_switch = None` * Reordered some workspace cleanup logic * Fix formatting * Add missing blank lines * Fix moving workspace to same monitor and wrong current index updating * Move function up and add fixme comment --------- Co-authored-by: Ivan Molodetskikh --- src/layout/monitor.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/layout/monitor.rs') diff --git a/src/layout/monitor.rs b/src/layout/monitor.rs index 8bb92a33..3eaac97f 100644 --- a/src/layout/monitor.rs +++ b/src/layout/monitor.rs @@ -858,6 +858,53 @@ impl Monitor { self.clean_up_workspaces(); } + pub fn move_workspace_to_idx(&mut self, old_idx: usize, new_idx: usize) { + let mut new_idx = new_idx.clamp(0, self.workspaces.len() - 1); + if old_idx == new_idx { + return; + } + + let ws = self.workspaces.remove(old_idx); + self.workspaces.insert(new_idx, ws); + + if new_idx > old_idx { + if new_idx == self.workspaces.len() - 1 { + // Insert a new empty workspace. + self.add_workspace_bottom(); + } + + if self.options.empty_workspace_above_first && old_idx == 0 { + self.add_workspace_top(); + new_idx += 1; + } + } else { + if old_idx == self.workspaces.len() - 1 { + // Insert a new empty workspace. + self.add_workspace_bottom(); + } + + if self.options.empty_workspace_above_first && new_idx == 0 { + self.add_workspace_top(); + new_idx += 1; + } + } + + // Only refocus the workspace if it was already focused + if self.active_workspace_idx == old_idx { + self.active_workspace_idx = new_idx; + // If the workspace order was switched so that the current workspace moved down the + // workspace stack, focus correctly + } else if new_idx <= self.active_workspace_idx && old_idx > self.active_workspace_idx { + self.active_workspace_idx += 1; + } else if new_idx >= self.active_workspace_idx && old_idx < self.active_workspace_idx { + self.active_workspace_idx = self.active_workspace_idx.saturating_sub(1); + } + + self.workspace_switch = None; + + self.clean_up_workspaces(); + } + /// Returns the geometry of the active tile relative to and clamped to the output. /// /// During animations, assumes the final view position. -- cgit