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]]
name = "sway-flash-indicator"
version = "0.5.2"
version = "0.6.0"
dependencies = [
"directories",
"futures-util",

View file

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

View file

@ -1,7 +1,7 @@
# Maintainer: Alex Janka <alex@alexjanka.com>
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')

View file

@ -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<T: AsRef<str> + std::cmp::PartialEq<str>>(
tree: swayipc_async::Node,
tree: &swayipc_async::Node,
connection: &mut swayipc_async::Connection,
id: i64,
output_blocklist: &[T],