From 5e2ffa096cfa6f8af6a3772bba1ca48a68e1bfbf Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Tue, 24 Sep 2024 14:46:25 +1000 Subject: [PATCH] proper tabbed autosplit --- Cargo.lock | 2 +- Cargo.toml | 2 +- packaging/PKGBUILD | 2 +- src/main.rs | 94 ++++++++++++++++++++++++---------------------- 4 files changed, 52 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 67b536a..59836a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1041,7 +1041,7 @@ dependencies = [ [[package]] name = "sway-flash-indicator" -version = "0.5.2" +version = "0.6.0" dependencies = [ "directories", "futures-util", diff --git a/Cargo.toml b/Cargo.toml index bef4442..0f74ba6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sway-flash-indicator" -version = "0.5.2" +version = "0.6.0" edition = "2021" [dependencies] diff --git a/packaging/PKGBUILD b/packaging/PKGBUILD index 6ca93d3..81ae13f 100644 --- a/packaging/PKGBUILD +++ b/packaging/PKGBUILD @@ -1,7 +1,7 @@ # Maintainer: Alex Janka pkgname=sway-flash-indicator -pkgver=0.5.2 +pkgver=0.6.0 pkgrel=1 pkgdesc="flashes sway indicator border rather than always showing it" arch=('x86_64' 'aarch64') diff --git a/src/main.rs b/src/main.rs index 535eaa3..4c49229 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,22 +64,10 @@ async fn main() -> Res<()> { } 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"); - } + if let Some(node) = + tree.find_as_ref(|n| n.focused && n.nodes.iter().all(|c| !c.focused)) + { + autosplit(node, &tree, &mut command_connection).await?; } } } @@ -97,7 +85,7 @@ async fn main() -> Res<()> { .any(|name| name.eq_ignore_ascii_case(app_id)) }) { - if let Some(parent) = tree.clone().find(|n| n.focus.contains(&node.id)) { + if let Some(parent) = tree.find_as_ref(|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?; @@ -119,9 +107,8 @@ async fn main() -> Res<()> { if let Some((_, id)) = recent_code { if id == node.id && window.change == swayipc_async::WindowChange::Focus { recent_code = None; - log::info!("focused the window we want with id {id}"); if let Err(e) = code_trigger( - tree, + &tree, &mut command_connection, id, &CONFIG.get().await.output_blocklist, @@ -135,31 +122,8 @@ async fn main() -> Res<()> { // TODO: also change on window closed - // 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 - || 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); - if width == 0 || height == 0 { - continue; - } - let ratio = (width as f64) / (height as f64); - let autosplit_ratio = config.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"); - } + if window.change != swayipc_async::WindowChange::Close { + autosplit(&node, &tree, &mut command_connection).await?; } } swayipc_async::Event::Shutdown(_) => { @@ -173,8 +137,48 @@ async fn main() -> Res<()> { Ok(()) } +async fn autosplit( + node: &swayipc_async::Node, + tree: &swayipc_async::Node, + connection: &mut swayipc_async::Connection, +) -> Res<()> { + if node.node_type == swayipc_async::NodeType::Con { + let (width, height) = (node.window_rect.width, node.window_rect.height); + if width == 0 || height == 0 { + return Ok(()); + } + if tree + .find_as_ref(|n| n.focus.contains(&node.id)) + .is_some_and(|parent| parent.node_type != swayipc_async::NodeType::Workspace) + { + return Ok(()); + } + let config = CONFIG.get().await; + 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() + }) + { + return Ok(()); + } + let ratio = (width as f64) / (height as f64); + let autosplit_ratio = config.autosplit_ratio; + if let Err(e) = if ratio > autosplit_ratio { + connection.run_command("splith").await + } else { + connection.run_command("splitv").await + } { + log::warn!("error {e:?} setting split"); + } + } + Ok(()) +} + async fn code_trigger + std::cmp::PartialEq>( - tree: swayipc_async::Node, + tree: &swayipc_async::Node, connection: &mut swayipc_async::Connection, id: i64, output_blocklist: &[T],