diff --git a/Cargo.toml b/Cargo.toml old mode 100644 new mode 100755 diff --git a/src/main.rs b/src/main.rs index 05436e6..f9f8c33 100755 --- a/src/main.rs +++ b/src/main.rs @@ -42,30 +42,59 @@ async fn main() { .unwrap(); while let Some(event) = workspace_event_rx.recv().await { + let sway_ipc_clone = sway_ipc.clone(); + + // Handle the new layout setup + let root = sway_ipc_clone.get_tree().await.unwrap(); + let workspaces = sway_ipc_clone.get_workspaces().await.unwrap(); + let leaves = get_current_leaves_here(&root, &workspaces); if let SwayEvent::Window(window_event) = event { - if let WindowChangeType::New = window_event.change { - let sway_ipc_clone = sway_ipc.clone(); - - // Handle the new layout setup - let root = sway_ipc_clone.get_tree().await.unwrap(); - let workspaces = sway_ipc_clone.get_workspaces().await.unwrap(); - let leaves = get_current_leaves_here(&root, &workspaces); - - if let (Some(first), Some(second)) = (leaves.first(), leaves.get(1)) { - let mut command = format!("[con_id={}] splith", first.id); - match leaves.get(2) { - Some(third) => { - command = format!( + match window_event.change { + WindowChangeType::New => { + if let (Some(first), Some(second)) = (leaves.first(), leaves.get(1)) { + let mut command = format!("[con_id={}] splith", first.id); + match leaves.get(2) { + Some(third) => { + command = format!( "{};[con_id={}] splitv;[con_id={}] mark target;[con_id={}] move container to mark target;[con_id={}] swap container with mark target;", command, second.id, third.id, second.id, second.id ); + } + None => { + command = format!("{};[con_id={}] splitv;", command, second.id); + } } - None => { - command = format!("{};[con_id={}] splitv;", command, second.id); - } + sway_ipc_clone.run_command(command).await.unwrap(); } - sway_ipc_clone.run_command(command).await.unwrap(); } + WindowChangeType::Close => { + println!("❌ Window closed: ID {}", window_event.container.id); + + if let Some(master) = leaves.first() { + println!("🔍 Current Master ID before closing: {}", master.id); + } else { + println!("⚠️ No Master found in current workspace!"); + } + + if leaves + .first() + .map_or(false, |master| master.id == window_event.container.id) + { + println!("🔥 Master was closed! Promoting next container..."); + + let command = " + focus right; + layout splitv; + focus left; + move up; + layout splith; + "; + sway_ipc_clone.run_command(command.to_string()).await.unwrap(); + } else { + println!("✅ Closed window was NOT the Master. No action needed."); + } + } + _ => {} } } } diff --git a/src/swayipc.rs b/src/swayipc.rs old mode 100644 new mode 100755