diff --git a/src/config.rs b/src/config.rs index 996fee3..d7d6eab 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,6 +12,7 @@ pub struct Config { pub flash_colour: colour::Format, pub autosplit_enabled: bool, pub autosplit_ratio: f64, + pub force_tabbed: Vec, pub output_blocklist: Vec, #[serde(default = "default_log_dir")] pub log_dir: std::path::PathBuf, @@ -130,6 +131,7 @@ impl Default for Config { flash_colour: colour::parse_hex("#ff0000").unwrap(), autosplit_enabled: true, autosplit_ratio: 1.0, + force_tabbed: Vec::new(), output_blocklist: Vec::new(), log_dir: std::path::PathBuf::new(), } diff --git a/src/main.rs b/src/main.rs index 188278e..535eaa3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,12 +61,49 @@ async fn main() -> Res<()> { log::warn!("error {e:?}"); } })); + } else if binding.command.starts_with("focus parent") { + let tree = command_connection.get_tree().await?; + + if let Some(node) = tree.find_focused_as_ref(|n| { + n.focused && n.node_type == swayipc_async::NodeType::Con + }) { + let (width, height) = (node.window_rect.width, node.window_rect.height); + if width == 0 || height == 0 { + continue; + } + let ratio = (width as f64) / (height as f64); + let autosplit_ratio = CONFIG.get().await.autosplit_ratio; + if let Err(e) = if ratio > autosplit_ratio { + command_connection.run_command("splith").await + } else { + command_connection.run_command("splitv").await + } { + log::warn!("error {e:?} setting split"); + } + } } } swayipc_async::Event::Window(window) if window.change != swayipc_async::WindowChange::Mark => { let node = window.container; + let tree = command_connection.get_tree().await?; + let config = CONFIG.get().await; + if window.change == swayipc_async::WindowChange::New + && node.app_id.as_ref().is_some_and(|app_id| { + config + .force_tabbed + .iter() + .any(|name| name.eq_ignore_ascii_case(app_id)) + }) + { + if let Some(parent) = tree.clone().find(|n| n.focus.contains(&node.id)) { + if parent.layout != swayipc_async::NodeLayout::Tabbed { + command_connection.run_command("splith").await?; + command_connection.run_command("layout tabbed").await?; + } + } + } if window.change == swayipc_async::WindowChange::New && node.app_id == Some(String::from("code-url-handler")) @@ -84,6 +121,7 @@ async fn main() -> Res<()> { recent_code = None; log::info!("focused the window we want with id {id}"); if let Err(e) = code_trigger( + tree, &mut command_connection, id, &CONFIG.get().await.output_blocklist, @@ -99,7 +137,14 @@ async fn main() -> Res<()> { // the node we're given is the one that closes if node.node_type == swayipc_async::NodeType::Con { let config = CONFIG.get().await; - if !config.autosplit_enabled { + if !config.autosplit_enabled + || config.force_tabbed.iter().any(|blocked| { + node.app_id + .as_ref() + .map(|v| blocked.eq_ignore_ascii_case(v)) + .unwrap_or_default() + }) + { continue; } let (width, height) = (node.window_rect.width, node.window_rect.height); @@ -129,15 +174,15 @@ async fn main() -> Res<()> { } async fn code_trigger + std::cmp::PartialEq>( + tree: swayipc_async::Node, connection: &mut swayipc_async::Connection, id: i64, output_blocklist: &[T], ) -> Res<()> { - let tree = connection.get_tree().await?; - for workspace in connection.get_workspaces().await? { - let width = workspace.rect.width; if workspace.focused { + let width = workspace.rect.width; + let mut workspace = get_with_id(&tree, workspace.id)?; if workspace .output @@ -150,7 +195,16 @@ async fn code_trigger + std::cmp::PartialEq>( while workspace.focus.len() == 1 { workspace = get_with_id(workspace, workspace.focus[0])?; } - if workspace.layout == swayipc_async::NodeLayout::SplitH && workspace.focus.len() == 2 { + if workspace.layout == swayipc_async::NodeLayout::Tabbed { + for _ in 0..workspace.nodes.len() { + connection.run_command("move left").await?; + } + connection + .run_command(format!("resize set width {}px", width - 1220)) + .await?; + } else if workspace.layout == swayipc_async::NodeLayout::SplitH + && workspace.focus.len() == 2 + { let (code, other) = { let a = get_with_id(&tree, workspace.focus[0])?; let b = get_with_id(&tree, workspace.focus[1])?;