proper tabbed autosplit

This commit is contained in:
Alex Janka 2024-09-24 14:46:25 +10:00
parent 0cb40e714e
commit 5e2ffa096c
4 changed files with 52 additions and 48 deletions

2
Cargo.lock generated
View file

@ -1041,7 +1041,7 @@ dependencies = [
[[package]] [[package]]
name = "sway-flash-indicator" name = "sway-flash-indicator"
version = "0.5.2" version = "0.6.0"
dependencies = [ dependencies = [
"directories", "directories",
"futures-util", "futures-util",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "sway-flash-indicator" name = "sway-flash-indicator"
version = "0.5.2" version = "0.6.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View file

@ -1,7 +1,7 @@
# Maintainer: Alex Janka <alex@alexjanka.com> # Maintainer: Alex Janka <alex@alexjanka.com>
pkgname=sway-flash-indicator pkgname=sway-flash-indicator
pkgver=0.5.2 pkgver=0.6.0
pkgrel=1 pkgrel=1
pkgdesc="flashes sway indicator border rather than always showing it" pkgdesc="flashes sway indicator border rather than always showing it"
arch=('x86_64' 'aarch64') arch=('x86_64' 'aarch64')

View file

@ -64,22 +64,10 @@ async fn main() -> Res<()> {
} else if binding.command.starts_with("focus parent") { } else if binding.command.starts_with("focus parent") {
let tree = command_connection.get_tree().await?; let tree = command_connection.get_tree().await?;
if let Some(node) = tree.find_focused_as_ref(|n| { if let Some(node) =
n.focused && n.node_type == swayipc_async::NodeType::Con tree.find_as_ref(|n| n.focused && n.nodes.iter().all(|c| !c.focused))
}) { {
let (width, height) = (node.window_rect.width, node.window_rect.height); autosplit(node, &tree, &mut command_connection).await?;
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");
}
} }
} }
} }
@ -97,7 +85,7 @@ async fn main() -> Res<()> {
.any(|name| name.eq_ignore_ascii_case(app_id)) .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 { if parent.layout != swayipc_async::NodeLayout::Tabbed {
command_connection.run_command("splith").await?; command_connection.run_command("splith").await?;
command_connection.run_command("layout tabbed").await?; command_connection.run_command("layout tabbed").await?;
@ -119,9 +107,8 @@ async fn main() -> Res<()> {
if let Some((_, id)) = recent_code { if let Some((_, id)) = recent_code {
if id == node.id && window.change == swayipc_async::WindowChange::Focus { if id == node.id && window.change == swayipc_async::WindowChange::Focus {
recent_code = None; recent_code = None;
log::info!("focused the window we want with id {id}");
if let Err(e) = code_trigger( if let Err(e) = code_trigger(
tree, &tree,
&mut command_connection, &mut command_connection,
id, id,
&CONFIG.get().await.output_blocklist, &CONFIG.get().await.output_blocklist,
@ -135,31 +122,8 @@ async fn main() -> Res<()> {
// TODO: also change on window closed - // TODO: also change on window closed -
// the node we're given is the one that closes // the node we're given is the one that closes
if node.node_type == swayipc_async::NodeType::Con { if window.change != swayipc_async::WindowChange::Close {
let config = CONFIG.get().await; autosplit(&node, &tree, &mut command_connection).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");
}
} }
} }
swayipc_async::Event::Shutdown(_) => { swayipc_async::Event::Shutdown(_) => {
@ -173,8 +137,48 @@ async fn main() -> Res<()> {
Ok(()) 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<T: AsRef<str> + std::cmp::PartialEq<str>>( async fn code_trigger<T: AsRef<str> + std::cmp::PartialEq<str>>(
tree: swayipc_async::Node, tree: &swayipc_async::Node,
connection: &mut swayipc_async::Connection, connection: &mut swayipc_async::Connection,
id: i64, id: i64,
output_blocklist: &[T], output_blocklist: &[T],