diff --git a/src/main.rs b/src/main.rs index 08319f5..994f140 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,17 +29,17 @@ struct Cli { #[derive(Subcommand, Debug)] enum Command { - #[clap(about = "Initialize the workspaces for all the outputs")] + #[clap(about = "Initialize the workspace groups for all the outputs")] Init(InitAction), - #[clap(about = "Move the focused container to another workspace on the same output")] + #[clap(about = "Move the focused container to another workspace on the same workspace group")] Move(MoveAction), - #[clap(about = "Focus to another workspace on the same output")] + #[clap(about = "Focus to another workspace on the same workspace group")] Focus(FocusAction), - #[clap(about = "Focus to output")] - FocusOutput(FocusAction), + #[clap(about = "Focus to workspace group")] + FocusGroup(FocusAction), #[clap(about = "Focus to another workspace on all the outputs")] FocusAllOutputs(FocusAction), @@ -231,12 +231,7 @@ fn get_current_workspace(stream: &UnixStream) -> Workspace { fn move_container_to_workspace(stream: &UnixStream, workspace_index: usize) { if workspace_index < 10 { - let mut cmd: String = "move container to workspace number ".to_string(); - let full_ws_name = format!("{}{}", get_current_output_index(stream), workspace_index) - .parse::() - .unwrap(); - cmd.push_str(&full_ws_name.to_string()); - send_command(stream, &cmd); + move_container_to_workspace_relative(stream, workspace_index); } else { move_container_to_workspace_absolute(stream, workspace_index); } @@ -324,14 +319,21 @@ fn move_container_to_workspace_absolute(stream: &UnixStream, workspace_index: us } } +fn move_container_to_workspace_relative(stream: &UnixStream, workspace_index: usize) { + let current_workspace_index: usize = get_current_workspace(stream).num; + let focused_output_index = current_workspace_index / 10; + + let mut cmd: String = "move container to workspace number ".to_string(); + let full_ws_name = format!("{}{}", focused_output_index, workspace_index) + .parse::() + .unwrap(); + cmd.push_str(&full_ws_name.to_string()); + send_command(stream, &cmd); +} + fn focus_to_workspace(stream: &UnixStream, workspace_index: usize) { if workspace_index < 10 { - let mut cmd: String = "workspace number ".to_string(); - let full_ws_name = format!("{}{}", get_current_output_index(stream), &workspace_index) - .parse::() - .unwrap(); - cmd.push_str(&full_ws_name.to_string()); - send_command(stream, &cmd); + focus_to_workspace_relative(stream, workspace_index); } else { focus_to_workspace_absolute(stream, workspace_index); } @@ -390,7 +392,7 @@ fn focus_to_workspace_absolute(stream: &UnixStream, workspace_index: usize) { } } -fn focus_to_workspace_virtual_output(stream: &UnixStream, workspace_index: usize) { +fn focus_to_workspace_relative(stream: &UnixStream, workspace_index: usize) { let current_workspace_index: usize = get_current_workspace(stream).num; let focused_output_index = current_workspace_index / 10; @@ -402,17 +404,60 @@ fn focus_to_workspace_virtual_output(stream: &UnixStream, workspace_index: usize send_command(stream, &cmd); } -fn focus_to_output(stream: &UnixStream, target_output_index: usize) { +fn focus_to_group(stream: &UnixStream, group_index: usize) { let outputs = get_outputs(stream); - if target_output_index < outputs.len() { - let mut cmd: String = "focus output ".to_string(); - cmd.push_str(&outputs[target_output_index - 1].name); - send_command(stream, &cmd); - } else { - let current_workspace_index = get_current_workspace(stream).num; - let contextual_workspace_index = - current_workspace_index - (current_workspace_index / 10) * 10; - focus_to_workspace_virtual_output(stream, contextual_workspace_index); + let workspaces = get_workspaces(stream); + + let current_workspace_index: usize = get_current_workspace(stream).num; + let contextual_target_workspace_index = + current_workspace_index - (current_workspace_index / 10) * 10; + + let target_workspace_index = group_index * 10 + contextual_target_workspace_index; + + // If the workspace already exists + match workspaces.iter().find(|w| w.num == target_workspace_index) { + Some(_) => { + let mut focus_cmd: String = "workspace number ".to_string(); + focus_cmd.push_str(&target_workspace_index.to_string()); + send_command(stream, &focus_cmd); + } + None => { + let target_screen_index = match workspaces.iter().find(|w| w.num / 10 == group_index) { + // If other workspaces on the same group exists + Some(other_workspace) => Some( + outputs + .iter() + .enumerate() + .find(|i| i.1.name == other_workspace.output) + .unwrap() + .0, + ), + None => { + // Or if the targeted output is currently connected + if group_index < outputs.len() { + Some(group_index) + } else { + None + } + } + }; + + match target_screen_index { + // If we have to send it to another screen + Some(target_screen_index) => { + let target_output = &outputs[target_screen_index]; + + let mut focus_cmd: String = "focus output ".to_string(); + focus_cmd.push_str(&target_output.name); + send_command(stream, &focus_cmd); + } + None => {} + }; + // Then we focus the workspace + let mut focus_cmd: String = "workspace number ".to_string(); + focus_cmd.push_str(&target_workspace_index.to_string()); + send_command(stream, &focus_cmd); + } } } @@ -515,7 +560,13 @@ fn init_workspaces(stream: &UnixStream, workspace_index: usize) { cmd.push_str(output.name.as_str()); println!("{:?}", cmd.clone()); send_command(stream, &cmd); - focus_to_workspace(stream, workspace_index); + + let mut cmd: String = "workspace number ".to_string(); + let full_ws_name = format!("{}{}", get_current_output_index(stream), &workspace_index) + .parse::() + .unwrap(); + cmd.push_str(&full_ws_name.to_string()); + send_command(stream, &cmd); } } @@ -553,8 +604,8 @@ fn main() { Command::Focus(action) => { focus_to_workspace(&stream, action.index); } - Command::FocusOutput(action) => { - focus_to_output(&stream, action.index); + Command::FocusGroup(action) => { + focus_to_group(&stream, action.index); } Command::FocusAllOutputs(action) => { focus_all_outputs_to_workspace(&stream, action.index);