@devries-cameron20 / t3code-1 / commits / e20b1d96ed
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(desktop): keep Hyprland SnapShot metadata when a CLI title spinner ticks (#22116)

Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>

Alex Southwell committed Oct 31, 2026 at 08:46 UTC e20b1d96ed35ab41797e7031b59b87e4fec72699
2 files changed +131 -4
native/hyprland-snap-shot/src/ipc.rs
+27
@@ -62,6 +62,33 @@ impl Window {
62 serde_json::json!({"title": self.title, "appName": self.class, "appIdentifier": self.class,
63 "processId": self.pid, "bounds": self.bounds(), "clientBounds": self.bounds()})
64 }
65 + /// Same window, geometry and state; only a CLI title spinner may differ.
66 + pub fn matches_after_capture(&self, current: &Self) -> bool {
67 + self.address == current.address
68 + && self.pid == current.pid
69 + && self.class == current.class
70 + && self.at == current.at
71 + && self.size == current.size
72 + && self.mapped == current.mapped
73 + && self.hidden == current.hidden
74 + && capture_title(&self.title) == capture_title(&current.title)
75 + }
76 +}
77 +
78 +/// Match the same leading CLI spinner accepted by the Wayland AT-SPI window matcher.
79 +fn capture_title(title: &str) -> &str {
80 + let mut chars = title.chars();
81 + if matches!(
82 + chars.next(),
83 + Some('⠋' | '⠙' | '⠹' | '⠸' | '⠼' | '⠴' | '⠦' | '⠧' | '⠇' | '⠏')
84 + ) && chars.clone().next().is_some_and(char::is_whitespace)
85 + {
86 + let label = chars.as_str().trim_start();
87 + if !label.is_empty() {
88 + return label;
89 + }
90 + }
91 + title
92 }
93
94 pub fn session_directory() -> Result<PathBuf> {
native/hyprland-snap-shot/src/main.rs
+104 -4
@@ -14,6 +14,16 @@ fn emit(value: serde_json::Value) -> Result<()> {
14 Ok(())
15 }
16
17 +/// Metadata of the captured window, or none if it changed while the capture ran.
18 +fn captured_metadata(captured: &ipc::Window, after: Vec<ipc::Window>) -> Option<serde_json::Value> {
19 + // Never pair text with a different/closed window or stale geometry.
20 + after
21 + .into_iter()
22 + .find(|w| w.address == captured.address)
23 + .filter(|w| captured.matches_after_capture(w))
24 + .map(|_| captured.metadata())
25 +}
26 +
27 fn run() -> Result<()> {
28 ipc::session_directory()?;
29 let args: Vec<String> = std::env::args().collect();
@@ -24,10 +34,7 @@ fn run() -> Result<()> {
34 let window = ipc::active_window()?;
35 capture::capture(window.address()?, Path::new(&args[2]))?;
36 ipc::ensure_unlocked()?;
27 - let after = ipc::windows()?.into_iter().find(|w| w.address == window.address);
28 - // Never pair text with a different/closed window or stale geometry.
29 - let metadata = after.filter(|w| w == &window).map(|w| w.metadata());
30 - emit(serde_json::json!({"window": metadata}))
37 + emit(serde_json::json!({"window": captured_metadata(&window, ipc::windows()?)}))
38 }
39 Some("activate") if args.len() == 4 => {
40 ipc::activate(args[2].parse()?, &args[3])?;
@@ -46,3 +53,96 @@ fn main() {
53 std::process::exit(1);
54 }
55 }
56 +
57 +#[cfg(test)]
58 +mod tests {
59 + use super::*;
60 + use serde_json::{Value, json};
61 +
62 + // Shaped like Hyprland's j/activewindow and j/clients replies, including fields we ignore.
63 + fn client(address: &str, title: &str, patch: Value) -> Value {
64 + let mut client = json!({
65 + "address": address, "mapped": true, "hidden": false,
66 + "at": [-1920, 20], "size": [1000, 800], "workspace": {"id": 2, "name": "2"},
67 + "floating": false, "monitor": 1, "class": "kitty", "title": title,
68 + "initialClass": "kitty", "initialTitle": "kitty", "pid": 4242, "xwayland": false,
69 + });
70 + client
71 + .as_object_mut()
72 + .unwrap()
73 + .extend(patch.as_object().unwrap().clone());
74 + client
75 + }
76 +
77 + fn parse<T: serde::de::DeserializeOwned>(reply: Value) -> T {
78 + serde_json::from_str(&reply.to_string()).unwrap()
79 + }
80 +
81 + fn metadata_after(before: Value, after: Vec<Value>) -> Option<Value> {
82 + captured_metadata(&parse(before), parse(Value::Array(after)))
83 + }
84 +
85 + #[test]
86 + fn keeps_captured_metadata_when_only_the_title_spinner_ticks() {
87 + for (before, after) in [("⠋ Document", "⠙ Document"), ("⠏ Document", "Document")] {
88 + let metadata = metadata_after(
89 + client("0xabc", before, json!({})),
90 + vec![
91 + client("0xdef", "Other", json!({})),
92 + client("0xabc", after, json!({})),
93 + ],
94 + )
95 + .unwrap_or_else(|| panic!("{before:?} -> {after:?} dropped metadata"));
96 + assert_eq!(metadata["title"], before);
97 + assert_eq!(metadata["processId"], 4242);
98 + assert_eq!(metadata["appName"], "kitty");
99 + assert_eq!(
100 + metadata["bounds"],
101 + json!({"x": -1920.0, "y": 20.0, "width": 1000.0, "height": 800.0})
102 + );
103 + }
104 + }
105 +
106 + #[test]
107 + fn drops_metadata_when_title_whitespace_changes() {
108 + for (before, after) in [
109 + ("Document", " Document "),
110 + ("Document", "Document "),
111 + (" Document", "Document"),
112 + ("⠋ Document", "⠙ Document "),
113 + ] {
114 + assert_eq!(
115 + metadata_after(
116 + client("0xabc", before, json!({})),
117 + vec![client("0xabc", after, json!({}))],
118 + ),
119 + None,
120 + "{before:?} -> {after:?}",
121 + );
122 + }
123 + }
124 +
125 + #[test]
126 + fn drops_metadata_when_the_window_identity_or_state_changed() {
127 + let before = || client("0xabc", "⠋ Document", json!({}));
128 + for after in [
129 + client("0xabc", "⠙ Other", json!({})),
130 + client("0xabc", "⠁ Document", json!({})),
131 + client("0xabc", "⠙Document", json!({})),
132 + client("0xabc", "⠙ Document", json!({"at": [-1900, 20]})),
133 + client("0xabc", "⠙ Document", json!({"size": [1000, 700]})),
134 + client("0xabc", "⠙ Document", json!({"pid": 99})),
135 + client("0xabc", "⠙ Document", json!({"class": "foot"})),
136 + client("0xabc", "⠙ Document", json!({"hidden": true})),
137 + client("0xabc", "⠙ Document", json!({"mapped": false})),
138 + client("0xdef", "⠋ Document", json!({})),
139 + ] {
140 + assert_eq!(
141 + metadata_after(before(), vec![after.clone()]),
142 + None,
143 + "{after}"
144 + );
145 + }
146 + assert_eq!(metadata_after(before(), vec![]), None);
147 + }
148 +}