From 9f5ea80d526b5c06380a99d32241ff5008f69177 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Thu, 25 Jul 2024 15:23:53 +1000 Subject: [PATCH] 0.2.0 - autotiling --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/config.rs | 4 ++++ src/main.rs | 27 +++++++++++++++++++++++++-- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dc606dd..b6e8cfa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -653,7 +653,7 @@ dependencies = [ [[package]] name = "sway-flash-indicator" -version = "0.1.1" +version = "0.2.0" dependencies = [ "directories", "futures-util", diff --git a/Cargo.toml b/Cargo.toml index 7385d8c..033cf3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sway-flash-indicator" -version = "0.1.1" +version = "0.2.0" edition = "2021" [dependencies] diff --git a/src/config.rs b/src/config.rs index b8e72a4..de7f282 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,6 +10,8 @@ pub struct Config { pub refresh_rate: u64, #[serde(serialize_with = "lab_ser", deserialize_with = "lab_de")] pub flash_colour: Lab, + pub autosplit_enabled: bool, + pub autosplit_ratio: f64, } pub fn parse_config() -> Res<()> { @@ -51,6 +53,8 @@ impl Default for Config { a: 80.1, b: 67.2, }, + autosplit_enabled: true, + autosplit_ratio: 1.0, } } } diff --git a/src/main.rs b/src/main.rs index 6b629ca..3598a38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,11 +31,15 @@ async fn main() -> Res<()> { config::parse_config()?; let mut event_connection = swayipc_async::Connection::new().await?; + let mut autosplit_connection = swayipc_async::Connection::new().await?; get_sway_config(&mut event_connection).await?; let mut events = event_connection - .subscribe([swayipc_async::EventType::Binding]) + .subscribe([ + swayipc_async::EventType::Binding, + swayipc_async::EventType::Window, + ]) .await?; let mut fut: Option> = None; @@ -55,7 +59,26 @@ async fn main() -> Res<()> { })); } } - _ => unreachable!(), + swayipc_async::Event::Window(window) + // TODO: change on window closed also + // the node we're given is the one that closes + if window.change != swayipc_async::WindowChange::Mark => + { + let node = window.container; + + let (width, height) = (node.window_rect.width, node.window_rect.height); + let ratio = (width as f64) / (height as f64); + let autosplit_ratio = + CONFIG.get().ok_or(Error::NoMatchingConfig)?.autosplit_ratio; + if let Err(e) = if ratio > autosplit_ratio { + autosplit_connection.run_command("splith").await + } else { + autosplit_connection.run_command("splitv").await + } { + log::warn!("error {e:?} setting split"); + } + } + _ => {} } } }