From a575d2e271bbd06e89a9bb8b793d5f9d998faa90 Mon Sep 17 00:00:00 2001 From: oddlama Date: Wed, 4 Oct 2023 17:43:12 +0200 Subject: [PATCH] chore: don't change i3 layout if workspace already uses that layout --- .../i3-per-workspace-layout/src/main.rs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/users/myuser/graphical/i3-per-workspace-layout/src/main.rs b/users/myuser/graphical/i3-per-workspace-layout/src/main.rs index 6babe7b..4cd8c53 100644 --- a/users/myuser/graphical/i3-per-workspace-layout/src/main.rs +++ b/users/myuser/graphical/i3-per-workspace-layout/src/main.rs @@ -1,6 +1,6 @@ use anyhow::{anyhow, Context, Result}; use clap::Parser; -use log::{debug, info}; +use log::{debug, info, warn}; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::path::PathBuf; @@ -8,7 +8,7 @@ use tokio::{sync::mpsc, task::JoinHandle}; use tokio_i3ipc::{ event::{Event, Subscribe, WindowChange, WorkspaceChange}, msg::Msg, - reply::{Node, NodeType}, + reply::{Node, NodeLayout, NodeType}, I3, }; use tokio_stream::StreamExt; @@ -33,7 +33,7 @@ struct Cli { #[derive(Debug, Serialize, Deserialize)] struct Config { /// The workspace -> layout associations - layouts: HashMap, + layouts: HashMap, } fn find_workspace_for_window(tree: &Node, window_id: usize) -> Option<&Node> { @@ -144,6 +144,22 @@ fn cmd_toggle_layout(config: &Config, workspace: &Node) -> Option { // we can operate on the workspace. } + // Don't do anything if the layout is already correct + if &con.layout == desired_layout { + return None + } + + let desired_layout = match desired_layout { + NodeLayout::SplitH => "splith", + NodeLayout::SplitV => "splitv", + NodeLayout::Stacked => "stacked", + NodeLayout::Tabbed => "tabbed", + x => { + warn!("Encountered invalid layout in configuration: {:?}", x); + return None; + } + }; + debug!( "Changing layout of workspace {:?} to {} (modifying con_id={})", &name, desired_layout, con.id