The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 00:30 UTC
fix(desktop): skip auxiliary macOS snapshot windows (#12978)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Yoru committed
Oct 19, 2026 at 18:58 UTC
b4cebe8b1ddbcffec4c9b0ba9078202b31b75a2a
2 files changed
+191
-37
apps/desktop/src/snapShot/ActiveWindow.test.ts
+128
-13
@@ -28,19 +28,34 @@ function stubMacLookup(stdout: string) {
28
);
29
}
30
31
+function macLookup(
32
+ windows: ReadonlyArray<{
33
+ id: number;
34
+ title: string;
35
+ bounds: { x: number; y: number; width: number; height: number };
36
+ alpha?: number;
37
+ }>,
38
+) {
39
+ return JSON.stringify({
40
+ owner: {
41
+ name: "Editor",
42
+ processId: 123,
43
+ path: "/Applications/Editor.app",
44
+ bundleId: "com.example.editor",
45
+ },
46
+ windows: windows.map((window) => ({ ...window, alpha: window.alpha ?? 1 })),
47
+ });
48
+}
49
+
50
it("parses the frontmost macOS window from the osascript lookup", async () => {
51
stubMacLookup(
33
- JSON.stringify({
34
- id: 42,
35
- title: "main.ts",
36
- bounds: { x: 10, y: 20, width: 800, height: 600 },
37
- owner: {
38
- name: "Editor",
39
- processId: 123,
40
- path: "/Applications/Editor.app",
41
- bundleId: "com.example.editor",
52
+ macLookup([
53
+ {
54
+ id: 42,
55
+ title: "main.ts",
56
+ bounds: { x: 10, y: 20, width: 800, height: 600 },
57
},
43
- }) + "\n",
58
+ ]) + "\n",
59
);
60
61
const window = await activeWindow("darwin");
@@ -66,10 +81,15 @@ it("parses the frontmost macOS window from the osascript lookup", async () => {
81
it("omits an empty macOS bundle identifier", async () => {
82
stubMacLookup(
83
JSON.stringify({
69
- id: 7,
70
- title: "",
71
- bounds: { x: 0, y: 0, width: 1, height: 1 },
84
owner: { name: "cli", processId: 9, path: "", bundleId: "" },
85
+ windows: [
86
+ {
87
+ id: 7,
88
+ title: "",
89
+ bounds: { x: 0, y: 0, width: 1, height: 1 },
90
+ alpha: 1,
91
+ },
92
+ ],
93
}),
94
);
95
@@ -78,12 +98,107 @@ it("omits an empty macOS bundle identifier", async () => {
98
assert.deepEqual(window?.owner, { name: "cli", processId: 9, path: "" });
99
});
100
101
+it.each([
102
+ {
103
+ name: "transparent and untitled helpers",
104
+ helpers: [
105
+ {
106
+ id: 1,
107
+ title: "",
108
+ bounds: { x: -1, y: -44, width: 1_680, height: 44 },
109
+ alpha: 0,
110
+ },
111
+ { id: 2, title: "", bounds: { x: 0, y: 0, width: 1_680, height: 68 } },
112
+ ],
113
+ content: {
114
+ id: 3,
115
+ title: "Document",
116
+ bounds: { x: 0, y: 0, width: 1_680, height: 1_050 },
117
+ },
118
+ },
119
+ {
120
+ name: "Chrome strips stacked above the content window",
121
+ helpers: [
122
+ { id: 75, title: "", bounds: { x: 0, y: 39, width: 1_800, height: 41 } },
123
+ { id: 74, title: "", bounds: { x: 0, y: 80, width: 1_800, height: 81 } },
124
+ { id: 1691, title: "", bounds: { x: 0, y: 39, width: 1_800, height: 158 } },
125
+ ],
126
+ content: {
127
+ id: 71,
128
+ title: "(10) Video - YouTube",
129
+ bounds: { x: 0, y: 161, width: 1_800, height: 1_008 },
130
+ },
131
+ },
132
+ {
133
+ name: "a smaller front window's strips with a larger background window",
134
+ helpers: [
135
+ { id: 75, title: "", bounds: { x: 100, y: 139, width: 1_200, height: 41 } },
136
+ { id: 74, title: "", bounds: { x: 100, y: 180, width: 1_200, height: 81 } },
137
+ ],
138
+ content: {
139
+ id: 71,
140
+ title: "Video - YouTube",
141
+ bounds: { x: 100, y: 261, width: 1_200, height: 700 },
142
+ },
143
+ background: [
144
+ { id: 80, title: "Other document", bounds: { x: 0, y: 39, width: 1_800, height: 1_130 } },
145
+ ],
146
+ },
147
+])("selects the content window behind $name", async ({ helpers, content, background = [] }) => {
148
+ stubMacLookup(macLookup([...helpers, content, ...background]));
149
+
150
+ const window = await activeWindow("darwin");
151
+
152
+ assert.strictEqual(window?.id, content.id);
153
+ assert.deepEqual(window?.bounds, content.bounds);
154
+});
155
+
156
+it.each([
157
+ { name: "untitled dialog", title: "", bounds: { x: 300, y: 200, width: 400, height: 240 } },
158
+ { name: "titled palette", title: "Palette", bounds: { x: 0, y: 0, width: 800, height: 50 } },
159
+ { name: "offset untitled strip", title: "", bounds: { x: 100, y: 100, width: 800, height: 50 } },
160
+ { name: "tall untitled window", title: "", bounds: { x: 0, y: 0, width: 800, height: 900 } },
161
+])("keeps a frontmost $name ahead of a larger document", async ({ title, bounds }) => {
162
+ stubMacLookup(
163
+ macLookup([
164
+ { id: 1, title, bounds },
165
+ { id: 2, title: "Document", bounds: { x: 0, y: 0, width: 800, height: 1_000 } },
166
+ ]),
167
+ );
168
+
169
+ const window = await activeWindow("darwin");
170
+
171
+ assert.strictEqual(window?.id, 1);
172
+ assert.deepEqual(window?.bounds, bounds);
173
+});
174
+
175
+it("keeps an untitled content window ahead of a much taller background window", async () => {
176
+ const bounds = { x: 0, y: 0, width: 800, height: 900 };
177
+ stubMacLookup(
178
+ macLookup([
179
+ { id: 1, title: "", bounds },
180
+ { id: 2, title: "Background document", bounds: { x: 0, y: 0, width: 800, height: 4_000 } },
181
+ ]),
182
+ );
183
+
184
+ const window = await activeWindow("darwin");
185
+
186
+ assert.strictEqual(window?.id, 1);
187
+ assert.deepEqual(window?.bounds, bounds);
188
+});
189
+
190
it("resolves undefined when macOS has no frontmost window", async () => {
191
stubMacLookup("\n");
192
193
assert.isUndefined(await activeWindow("darwin"));
194
});
195
196
+it("resolves undefined when macOS reports no visible windows", async () => {
197
+ stubMacLookup(macLookup([]));
198
+
199
+ assert.isUndefined(await activeWindow("darwin"));
200
+});
201
+
202
it("composes the Windows foreground window from Win32 calls", async () => {
203
const api = {
204
getForegroundWindow: vi.fn(() => 0x1_f123_4567n),
apps/desktop/src/snapShot/ActiveWindow.ts
+63
-24
@@ -30,23 +30,32 @@ export type ActiveWindow = {
30
31
const MAC_LOOKUP_TIMEOUT_MS = 5_000;
32
33
-const MacActiveWindow = Schema.Struct({
34
- id: Schema.Number,
35
- title: Schema.String,
36
- bounds: Schema.Struct({
37
- x: Schema.Number,
38
- y: Schema.Number,
39
- width: Schema.Number,
40
- height: Schema.Number,
41
- }),
33
+const MAC_HELPER_STRIP_MAX_THICKNESS_RATIO = 0.25;
34
+
35
+const MacActiveWindowLookup = Schema.Struct({
36
owner: Schema.Struct({
37
name: Schema.String,
38
processId: Schema.Number,
39
path: Schema.String,
40
bundleId: Schema.String,
41
}),
42
+ windows: Schema.Array(
43
+ Schema.Struct({
44
+ id: Schema.Number,
45
+ title: Schema.String,
46
+ bounds: Schema.Struct({
47
+ x: Schema.Number,
48
+ y: Schema.Number,
49
+ width: Schema.Number,
50
+ height: Schema.Number,
51
+ }),
52
+ alpha: Schema.Number,
53
+ }),
54
+ ),
55
});
49
-const decodeMacActiveWindow = Schema.decodeUnknownSync(Schema.fromJsonString(MacActiveWindow));
56
+const decodeMacActiveWindowLookup = Schema.decodeUnknownSync(
57
+ Schema.fromJsonString(MacActiveWindowLookup),
58
+);
59
60
const MAC_LOOKUP_SCRIPT = `
61
ObjC.import("CoreGraphics");
@@ -55,30 +64,38 @@ function run() {
64
const app = $.NSWorkspace.sharedWorkspace.frontmostApplication;
65
if (app.isNil()) return "";
66
const pid = app.processIdentifier;
67
+ let ownerName = String(app.localizedName.js || "");
68
const list = $.CGWindowListCopyWindowInfo(
69
$.kCGWindowListOptionOnScreenOnly | $.kCGWindowListExcludeDesktopElements,
70
$.kCGNullWindowID,
71
);
72
$.CFMakeCollectable(list);
73
const count = $.CFArrayGetCount(list);
74
+ const windows = [];
75
for (let i = 0; i < count; i++) {
76
const w = ObjC.castRefToObject($.CFArrayGetValueAtIndex(list, i));
77
if (w.objectForKey("kCGWindowOwnerPID").js !== pid) continue;
78
if (w.objectForKey("kCGWindowLayer").js !== 0) continue;
79
+ if (!ownerName) {
80
+ ownerName = String(ObjC.unwrap(w.objectForKey("kCGWindowOwnerName")) || "");
81
+ }
82
const b = ObjC.deepUnwrap(w.objectForKey("kCGWindowBounds"));
69
- return JSON.stringify({
83
+ windows.push({
84
id: w.objectForKey("kCGWindowNumber").js,
85
title: String(ObjC.unwrap(w.objectForKey("kCGWindowName")) || ""),
86
bounds: { x: b.X, y: b.Y, width: b.Width, height: b.Height },
73
- owner: {
74
- name: String(app.localizedName.js || ObjC.unwrap(w.objectForKey("kCGWindowOwnerName")) || ""),
75
- processId: pid,
76
- path: String(app.bundleURL.path.js || ""),
77
- bundleId: String(app.bundleIdentifier.js || ""),
78
- },
87
+ alpha: w.objectForKey("kCGWindowAlpha").js,
88
});
89
}
81
- return "";
90
+ return JSON.stringify({
91
+ owner: {
92
+ name: ownerName,
93
+ processId: pid,
94
+ path: String(app.bundleURL.path.js || ""),
95
+ bundleId: String(app.bundleIdentifier.js || ""),
96
+ },
97
+ windows,
98
+ });
99
}`;
100
101
function runMacLookup(): Promise<string> {
@@ -86,7 +103,7 @@ function runMacLookup(): Promise<string> {
103
NodeChildProcess.execFile(
104
"/usr/bin/osascript",
105
["-l", "JavaScript", "-e", MAC_LOOKUP_SCRIPT],
89
- { timeout: MAC_LOOKUP_TIMEOUT_MS, maxBuffer: 64 * 1024 },
106
+ { timeout: MAC_LOOKUP_TIMEOUT_MS, maxBuffer: 1024 * 1024 },
107
(error, stdout) => {
108
if (error) reject(error);
109
else resolve(stdout);
@@ -98,17 +115,39 @@ function runMacLookup(): Promise<string> {
115
async function macActiveWindow(): Promise<ActiveWindow | undefined> {
116
const output = (await runMacLookup()).trim();
117
if (!output) return undefined;
101
- const window = decodeMacActiveWindow(output);
118
+ const lookup = decodeMacActiveWindowLookup(output);
119
+ const visible = lookup.windows.filter(
120
+ (window) => window.alpha > 0 && window.bounds.width > 0 && window.bounds.height > 0,
121
+ );
122
+ // Helpers belong to the content behind them, which may not be the app's largest window.
123
+ const window = visible.find((candidate, index) => {
124
+ if (candidate.title.trim() !== "") return true;
125
+ const bounds = candidate.bounds;
126
+ return !visible.some((reference, referenceIndex) => {
127
+ if (referenceIndex <= index) return false;
128
+ return (
129
+ (bounds.x === reference.bounds.x &&
130
+ bounds.width === reference.bounds.width &&
131
+ bounds.height <= bounds.width * MAC_HELPER_STRIP_MAX_THICKNESS_RATIO &&
132
+ bounds.height <= reference.bounds.height * MAC_HELPER_STRIP_MAX_THICKNESS_RATIO) ||
133
+ (bounds.y === reference.bounds.y &&
134
+ bounds.height === reference.bounds.height &&
135
+ bounds.width <= bounds.height * MAC_HELPER_STRIP_MAX_THICKNESS_RATIO &&
136
+ bounds.width <= reference.bounds.width * MAC_HELPER_STRIP_MAX_THICKNESS_RATIO)
137
+ );
138
+ });
139
+ });
140
+ if (!window) return undefined;
141
return {
142
platform: "macos",
143
id: window.id,
144
title: window.title,
145
bounds: window.bounds,
146
owner: {
108
- name: window.owner.name,
109
- processId: window.owner.processId,
110
- path: window.owner.path,
111
- ...(window.owner.bundleId ? { bundleId: window.owner.bundleId } : {}),
147
+ name: lookup.owner.name,
148
+ processId: lookup.owner.processId,
149
+ path: lookup.owner.path,
150
+ ...(lookup.owner.bundleId ? { bundleId: lookup.owner.bundleId } : {}),
151
},
152
};
153
}