From a8a80eb514b9a68b467d9a776a99a7117d69819c Mon Sep 17 00:00:00 2001 From: Skia Date: Mon, 24 Mar 2025 20:15:21 +0100 Subject: [PATCH] Simplifications and bugfixes discovered when writing tests --- src/lib.rs | 86 ++++++++++++++++++++---------------------------------- 1 file changed, 31 insertions(+), 55 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1413fa0..b394360 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -371,58 +371,33 @@ impl SwaySome { } fn focus_to_workspace_absolute(&self, workspace_index: usize) { - let output_index = (workspace_index / MAX_GROUP_WS) as usize; - - // If the workspace already exists - match self.workspaces.iter().find(|w| w.num == workspace_index) { - Some(_) => { - let mut focus_cmd: String = "workspace number ".to_string(); - focus_cmd.push_str(&workspace_index.to_string()); - self.send_command(&focus_cmd); - } - None => { - let target_group = workspace_index / MAX_GROUP_WS; - let target_screen_index = match self - .workspaces + let target_group = workspace_index / MAX_GROUP_WS; + match self + .workspaces + .iter() + .find(|w| w.num / MAX_GROUP_WS == target_group) + { + // If other workspaces on the same group exists + Some(other_workspace) => { + // find the corresponding output and focus it + let target_output = self + .outputs .iter() - .find(|w| w.num / MAX_GROUP_WS == target_group) - { - // If other workspaces on the same group exists - Some(other_workspace) => Some( - self.outputs - .iter() - .enumerate() - .find(|i| i.1.name == other_workspace.output) - .unwrap() - .0, - ), - None => { - // Or if the targeted output is currently connected - if output_index < self.outputs.len() { - Some(output_index) - } else { - None - } - } - }; - - match target_screen_index { - // If we have to send it to another screen - Some(target_screen_index) => { - let target_output = &self.outputs[target_screen_index - 1]; - - let mut focus_cmd: String = "focus output ".to_string(); - focus_cmd.push_str(&target_output.name); - self.send_command(&focus_cmd); - } - None => {} - }; - // Then we focus the workspace - let mut focus_cmd: String = "workspace number ".to_string(); - focus_cmd.push_str(&workspace_index.to_string()); + .enumerate() + .find(|i| i.1.name == other_workspace.output) + .unwrap() + .1; + let mut focus_cmd: String = "focus output ".to_string(); + focus_cmd.push_str(&target_output.name); self.send_command(&focus_cmd); } - } + None => {} + }; + + // Then we focus the workspace + let mut focus_cmd: String = "workspace number ".to_string(); + focus_cmd.push_str(&workspace_index.to_string()); + self.send_command(&focus_cmd); } fn focus_to_workspace_relative(&self, workspace_index: usize) { @@ -644,12 +619,13 @@ impl SwaySome { focus_cmd.push_str(&workspace.num.to_string()); self.send_command(&focus_cmd); - let group_index = workspace.num / MAX_GROUP_WS; - if group_index <= self.outputs.len() - 1 { - let mut move_cmd = move_cmd_prefix.clone(); - move_cmd.push_str(&self.outputs[group_index.max(1) - 1].name); - self.send_command(&move_cmd); - } + let group_index = + (workspace.num / MAX_GROUP_WS + self.outputs.len() - 1) % self.outputs.len(); + // if group_index <= self.outputs.len() - 1 { + let mut move_cmd = move_cmd_prefix.clone(); + move_cmd.push_str(&self.outputs[group_index].name); + self.send_command(&move_cmd); + // } } } }