From f0c7df316763f56ff7f37e2c941f6c67def3331d Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Mon, 14 Oct 2024 09:56:25 +1100 Subject: [PATCH] associate app ids with workspaces --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/config.rs | 3 +++ src/main.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5bba7d3..487d19d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1154,7 +1154,7 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "sway-flash-indicator" -version = "0.6.6" +version = "0.7.0" dependencies = [ "clap", "directories", diff --git a/Cargo.toml b/Cargo.toml index 815be6e..b13e329 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sway-flash-indicator" -version = "0.6.6" +version = "0.7.0" edition = "2021" [dependencies] diff --git a/src/config.rs b/src/config.rs index 2828443..b073879 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ use crate::prelude::*; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] #[serde(default)] @@ -14,6 +15,7 @@ pub struct Config { pub force_stacking: Vec, pub force_tabbed: Vec, pub output_blocklist: Vec, + pub associate_id_to_workspace: HashMap, #[serde(default = "default_log_dir")] pub log_dir: std::path::PathBuf, } @@ -134,6 +136,7 @@ impl Default for Config { force_stacking: Vec::new(), force_tabbed: Vec::new(), output_blocklist: Vec::new(), + associate_id_to_workspace: HashMap::new(), log_dir: std::path::PathBuf::new(), } } diff --git a/src/main.rs b/src/main.rs index d01835d..2939f53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,6 +120,33 @@ async fn main() -> Res<()> { command_connection.run_command("layout stacking").await?; } } + } else if let Some(workspace) = node + .app_id + .as_ref() + .and_then(|id| config.associate_id_to_workspace.get(id)) + { + // TODO: case-insensitive app_id match? + + let mut last_node = &node; + let v = 'ws: loop { + // find the workspace containing this node + let Some(parent) = + tree.find_as_ref(|n| n.focus.contains(&last_node.id)) + else { + break 'ws None; + }; + if parent.node_type == swayipc_async::NodeType::Workspace { + break 'ws parent.name.clone(); + } + last_node = &parent; + }; + + if !v.is_some_and(|w| w.eq_ignore_ascii_case(&workspace)) { + // only run the command if we're on a different workspace + command_connection + .run_command(format!("move container to workspace {workspace}")) + .await?; + } } if node