chore: don't change i3 layout if workspace already uses that layout

This commit is contained in:
oddlama 2023-10-04 17:43:12 +02:00
parent c3b7cee555
commit a575d2e271
No known key found for this signature in database
GPG key ID: 14EFE510775FE39A

View file

@ -1,6 +1,6 @@
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use clap::Parser; use clap::Parser;
use log::{debug, info}; use log::{debug, info, warn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
@ -8,7 +8,7 @@ use tokio::{sync::mpsc, task::JoinHandle};
use tokio_i3ipc::{ use tokio_i3ipc::{
event::{Event, Subscribe, WindowChange, WorkspaceChange}, event::{Event, Subscribe, WindowChange, WorkspaceChange},
msg::Msg, msg::Msg,
reply::{Node, NodeType}, reply::{Node, NodeLayout, NodeType},
I3, I3,
}; };
use tokio_stream::StreamExt; use tokio_stream::StreamExt;
@ -33,7 +33,7 @@ struct Cli {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct Config { struct Config {
/// The workspace -> layout associations /// The workspace -> layout associations
layouts: HashMap<String, String>, layouts: HashMap<String, NodeLayout>,
} }
fn find_workspace_for_window(tree: &Node, window_id: usize) -> Option<&Node> { 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<String> {
// we can operate on the workspace. // 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!( debug!(
"Changing layout of workspace {:?} to {} (modifying con_id={})", "Changing layout of workspace {:?} to {} (modifying con_id={})",
&name, desired_layout, con.id &name, desired_layout, con.id