Computer control: reach windows on other workspaces and virtual desktops
CI / test (push) Has been cancelled

Opening a web service in a browser whose window sat on another i3
workspace failed with "I could not select the intended window": the
window list only covered visible workspaces, so the browser window was
never found nor activated.

- i3: leaves carry their workspace; hidden_leaves() lists windows on
  other workspaces and in the scratchpad.
- KWin: the list script reports the desktop of non-current windows and
  activation switches to that desktop first.
- Desktop::state(true) now includes those windows (minimized = true,
  desktop = workspace/desktop name), after the on-screen ones;
  state(false) is unchanged, so input and capture still only target
  visible windows.
- App window lists given to the model tag them with otherDesktop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nocodeandClaude Opus 5.5 committed 2026-11-13 17:11:12 +01:00
1 parent 49349d6ecd
commit 8413198f42
4 files changed
+119 -37

No files matched your search

+41 -25
View File
@@ -98,32 +98,33 @@ pub struct Leaf {
pub output: String,
pub focused: bool,
pub floating: bool,
/// Workspace name (`__i3_scratch` for the scratchpad).
pub workspace: String,
}
/// Leaves on visible workspaces (or focused), scratchpad excluded, ordered fullscreen, floating,
/// tiled, focus breaking ties (stacking approximation).
pub fn leaves(tree: &Value, visible: &[String]) -> Vec<Leaf> {
fn walk(node: &Value, workspace: Option<&str>, output: &str, floating: bool, out: &mut Vec<(Leaf, Option<String>)>) {
let workspace = if node["type"] == "workspace" { node["name"].as_str() } else { workspace };
pub const SCRATCHPAD: &str = "__i3_scratch";
/// Every leaf of the tree in tree order, with its workspace.
fn collect(tree: &Value) -> Vec<Leaf> {
fn walk(node: &Value, workspace: &str, output: &str, floating: bool, out: &mut Vec<Leaf>) {
let workspace = if node["type"] == "workspace" { node["name"].as_str().unwrap_or("") } else { workspace };
let output = if node["type"] == "output" { node["name"].as_str().unwrap_or("") } else { output };
if let Some(window) = node["window"].as_u64().filter(|w| *w > 0)
&& node["type"] == "con"
{
let r = &node["rect"];
out.push((
Leaf {
window: window as u32,
title: node["name"].as_str().unwrap_or("").into(),
rect: (r["x"].as_i64().unwrap_or(0) as i32, r["y"].as_i64().unwrap_or(0) as i32, r["width"].as_u64().unwrap_or(0) as u32, r["height"].as_u64().unwrap_or(0) as u32),
fullscreen: node["fullscreen_mode"].as_i64().unwrap_or(0) != 0,
dialog: node["window_type"] == "dialog",
class: node["window_properties"]["class"].as_str().unwrap_or("").into(),
output: node["output"].as_str().unwrap_or(output).into(),
focused: node["focused"] == true,
floating,
},
workspace.map(String::from),
));
out.push(Leaf {
window: window as u32,
title: node["name"].as_str().unwrap_or("").into(),
rect: (r["x"].as_i64().unwrap_or(0) as i32, r["y"].as_i64().unwrap_or(0) as i32, r["width"].as_u64().unwrap_or(0) as u32, r["height"].as_u64().unwrap_or(0) as u32),
fullscreen: node["fullscreen_mode"].as_i64().unwrap_or(0) != 0,
dialog: node["window_type"] == "dialog",
class: node["window_properties"]["class"].as_str().unwrap_or("").into(),
output: node["output"].as_str().unwrap_or(output).into(),
focused: node["focused"] == true,
floating,
workspace: workspace.into(),
});
}
for child in node["nodes"].as_array().into_iter().flatten() {
walk(child, workspace, output, floating, out);
@@ -133,16 +134,29 @@ pub fn leaves(tree: &Value, visible: &[String]) -> Vec<Leaf> {
}
}
let mut found = Vec::new();
walk(tree, None, "", false, &mut found);
let mut result: Vec<Leaf> = found
.into_iter()
.filter(|(leaf, ws)| ws.as_deref() != Some("__i3_scratch") && (leaf.focused || ws.as_ref().is_some_and(|w| visible.contains(w))))
.map(|(leaf, _)| leaf)
.collect();
walk(tree, "", "", false, &mut found);
found
}
/// A leaf the owner can see: on a visible workspace (or focused), never in the scratchpad.
fn shown(leaf: &Leaf, visible: &[String]) -> bool {
leaf.workspace != SCRATCHPAD && (leaf.focused || visible.contains(&leaf.workspace))
}
/// Leaves on visible workspaces (or focused), scratchpad excluded, ordered fullscreen, floating,
/// tiled, focus breaking ties (stacking approximation).
pub fn leaves(tree: &Value, visible: &[String]) -> Vec<Leaf> {
let mut result: Vec<Leaf> = collect(tree).into_iter().filter(|l| shown(l, visible)).collect();
result.sort_by_key(|w| (!w.fullscreen, !w.floating, !w.focused));
result
}
/// Leaves on the other workspaces and in the scratchpad, in tree order: `[id=…] focus` switches
/// to their workspace (or shows the scratchpad window).
pub fn hidden_leaves(tree: &Value, visible: &[String]) -> Vec<Leaf> {
collect(tree).into_iter().filter(|l| !l.workspace.is_empty() && !shown(l, visible)).collect()
}
#[cfg(test)]
mod tests {
use super::*;
@@ -163,5 +177,7 @@ mod tests {
let l = leaves(&tree, &["1".into()]);
assert_eq!(l.iter().map(|w| w.window).collect::<Vec<_>>(), vec![13, 12, 11]);
assert!(l[0].dialog && l[0].floating && l[2].class == "Kate" && l[1].output == "DP-1");
let h = hidden_leaves(&tree, &["1".into()]);
assert_eq!(h.iter().map(|w| (w.window, w.workspace.as_str())).collect::<Vec<_>>(), vec![(14, "2"), (15, SCRATCHPAD)]);
}
}