Skip to content

Commit 941b366

Browse files
fix(desktop): skip auxiliary macOS snapshot windows
1 parent 89f51fc commit 941b366

2 files changed

Lines changed: 115 additions & 136 deletions

File tree

‎apps/desktop/src/snapShot/ActiveWindow.test.ts‎

Lines changed: 54 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -98,136 +98,78 @@ it("omits an empty macOS bundle identifier", async () => {
9898
assert.deepEqual(window?.owner, { name: "cli", processId: 9, path: "" });
9999
});
100100

101-
it("skips transparent and tiny auxiliary windows before the main window", async () => {
102-
stubMacLookup(
103-
macLookup([
101+
it.each([
102+
{
103+
name: "transparent and untitled helpers",
104+
helpers: [
104105
{
105106
id: 1,
106107
title: "",
107-
bounds: { x: 0, y: -32, width: 1_680, height: 32 },
108+
bounds: { x: -1, y: -44, width: 1_680, height: 44 },
108109
alpha: 0,
109110
},
110-
{
111-
id: 2,
112-
title: "",
113-
bounds: { x: 0, y: 0, width: 1_680, height: 68 },
114-
},
115-
{
116-
id: 3,
117-
title: "Pull request · Browser",
118-
bounds: { x: 0, y: 0, width: 1_680, height: 1_050 },
119-
},
120-
]),
121-
);
122-
123-
const window = await activeWindow("darwin");
124-
125-
assert.strictEqual(window?.id, 3);
126-
assert.deepEqual(window?.bounds, { x: 0, y: 0, width: 1_680, height: 1_050 });
127-
});
128-
129-
it("skips untitled full-width strips stacked above the main window", async () => {
130-
stubMacLookup(
131-
macLookup([
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: [
132122
{ id: 75, title: "", bounds: { x: 0, y: 39, width: 1_800, height: 41 } },
133123
{ id: 74, title: "", bounds: { x: 0, y: 80, width: 1_800, height: 81 } },
134124
{ id: 1691, title: "", bounds: { x: 0, y: 39, width: 1_800, height: 158 } },
135-
{
136-
id: 71,
137-
title: "(10) Video - YouTube",
138-
bounds: { x: 0, y: 161, width: 1_800, height: 1_008 },
139-
},
140-
]),
141-
);
142-
143-
assert.strictEqual((await activeWindow("darwin"))?.id, 71);
144-
});
145-
146-
it("keeps a tall untitled window that shares the main window's width", async () => {
147-
stubMacLookup(
148-
macLookup([
149-
{ id: 12, title: "", bounds: { x: 0, y: 0, width: 800, height: 900 } },
150-
{ id: 13, title: "Document", bounds: { x: 0, y: 0, width: 800, height: 1_000 } },
151-
]),
152-
);
153-
154-
assert.strictEqual((await activeWindow("darwin"))?.id, 12);
155-
});
156-
157-
it("keeps an untitled frontmost dialog ahead of a titled parent", async () => {
158-
stubMacLookup(
159-
macLookup([
160-
{
161-
id: 4,
162-
title: "",
163-
bounds: { x: 300, y: 200, width: 400, height: 240 },
164-
},
165-
{
166-
id: 5,
167-
title: "Document",
168-
bounds: { x: 0, y: 0, width: 1_680, height: 1_050 },
169-
},
170-
]),
171-
);
172-
173-
assert.strictEqual((await activeWindow("darwin"))?.id, 4);
174-
});
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]));
175149

176-
it("keeps a small frontmost window ahead of a larger window", async () => {
177-
stubMacLookup(
178-
macLookup([
179-
{
180-
id: 6,
181-
title: "Widget",
182-
bounds: { x: 20, y: 20, width: 109, height: 68 },
183-
},
184-
{
185-
id: 7,
186-
title: "Document",
187-
bounds: { x: 0, y: 0, width: 1_680, height: 1_050 },
188-
},
189-
]),
190-
);
150+
const window = await activeWindow("darwin");
191151

192-
assert.strictEqual((await activeWindow("darwin"))?.id, 6);
152+
assert.strictEqual(window?.id, content.id);
153+
assert.deepEqual(window?.bounds, content.bounds);
193154
});
194155

195-
it("keeps a titled edge-strip window ahead of a larger window", async () => {
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 }) => {
196162
stubMacLookup(
197163
macLookup([
198-
{
199-
id: 8,
200-
title: "Palette",
201-
bounds: { x: 0, y: 0, width: 1_000, height: 50 },
202-
},
203-
{
204-
id: 9,
205-
title: "Document",
206-
bounds: { x: 0, y: 0, width: 1_000, height: 800 },
207-
},
164+
{ id: 1, title, bounds },
165+
{ id: 2, title: "Document", bounds: { x: 0, y: 0, width: 800, height: 1_000 } },
208166
]),
209167
);
210168

211-
assert.strictEqual((await activeWindow("darwin"))?.id, 8);
212-
});
213-
214-
it("keeps an offset untitled edge-strip window ahead of a larger window", async () => {
215-
stubMacLookup(
216-
macLookup([
217-
{
218-
id: 10,
219-
title: "",
220-
bounds: { x: 100, y: 100, width: 1_000, height: 50 },
221-
},
222-
{
223-
id: 11,
224-
title: "Document",
225-
bounds: { x: 0, y: 0, width: 1_000, height: 800 },
226-
},
227-
]),
228-
);
169+
const window = await activeWindow("darwin");
229170

230-
assert.strictEqual((await activeWindow("darwin"))?.id, 10);
171+
assert.strictEqual(window?.id, 1);
172+
assert.deepEqual(window?.bounds, bounds);
231173
});
232174

233175
it("resolves undefined when macOS has no frontmost window", async () => {

‎apps/desktop/src/snapShot/ActiveWindow.ts‎

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,32 @@ export type ActiveWindow = {
3030

3131
const MAC_LOOKUP_TIMEOUT_MS = 5_000;
3232

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({
4236
owner: Schema.Struct({
4337
name: Schema.String,
4438
processId: Schema.Number,
4539
path: Schema.String,
4640
bundleId: Schema.String,
4741
}),
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+
),
4855
});
49-
const decodeMacActiveWindow = Schema.decodeUnknownSync(Schema.fromJsonString(MacActiveWindow));
56+
const decodeMacActiveWindowLookup = Schema.decodeUnknownSync(
57+
Schema.fromJsonString(MacActiveWindowLookup),
58+
);
5059

5160
const MAC_LOOKUP_SCRIPT = `
5261
ObjC.import("CoreGraphics");
@@ -55,38 +64,46 @@ function run() {
5564
const app = $.NSWorkspace.sharedWorkspace.frontmostApplication;
5665
if (app.isNil()) return "";
5766
const pid = app.processIdentifier;
67+
let ownerName = String(app.localizedName.js || "");
5868
const list = $.CGWindowListCopyWindowInfo(
5969
$.kCGWindowListOptionOnScreenOnly | $.kCGWindowListExcludeDesktopElements,
6070
$.kCGNullWindowID,
6171
);
6272
$.CFMakeCollectable(list);
6373
const count = $.CFArrayGetCount(list);
74+
const windows = [];
6475
for (let i = 0; i < count; i++) {
6576
const w = ObjC.castRefToObject($.CFArrayGetValueAtIndex(list, i));
6677
if (w.objectForKey("kCGWindowOwnerPID").js !== pid) continue;
6778
if (w.objectForKey("kCGWindowLayer").js !== 0) continue;
79+
if (!ownerName) {
80+
ownerName = String(ObjC.unwrap(w.objectForKey("kCGWindowOwnerName")) || "");
81+
}
6882
const b = ObjC.deepUnwrap(w.objectForKey("kCGWindowBounds"));
69-
return JSON.stringify({
83+
windows.push({
7084
id: w.objectForKey("kCGWindowNumber").js,
7185
title: String(ObjC.unwrap(w.objectForKey("kCGWindowName")) || ""),
7286
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,
7988
});
8089
}
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+
});
8299
}`;
83100

84101
function runMacLookup(): Promise<string> {
85102
return new Promise((resolve, reject) => {
86103
NodeChildProcess.execFile(
87104
"/usr/bin/osascript",
88105
["-l", "JavaScript", "-e", MAC_LOOKUP_SCRIPT],
89-
{ timeout: MAC_LOOKUP_TIMEOUT_MS, maxBuffer: 64 * 1024 },
106+
{ timeout: MAC_LOOKUP_TIMEOUT_MS, maxBuffer: 1024 * 1024 },
90107
(error, stdout) => {
91108
if (error) reject(error);
92109
else resolve(stdout);
@@ -98,17 +115,37 @@ function runMacLookup(): Promise<string> {
98115
async function macActiveWindow(): Promise<ActiveWindow | undefined> {
99116
const output = (await runMacLookup()).trim();
100117
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 <= reference.bounds.height * MAC_HELPER_STRIP_MAX_THICKNESS_RATIO) ||
132+
(bounds.y === reference.bounds.y &&
133+
bounds.height === reference.bounds.height &&
134+
bounds.width <= reference.bounds.width * MAC_HELPER_STRIP_MAX_THICKNESS_RATIO)
135+
);
136+
});
137+
});
138+
if (!window) return undefined;
102139
return {
103140
platform: "macos",
104141
id: window.id,
105142
title: window.title,
106143
bounds: window.bounds,
107144
owner: {
108-
name: window.owner.name,
109-
processId: window.owner.processId,
110-
path: window.owner.path,
111-
...(window.owner.bundleId ? { bundleId: window.owner.bundleId } : {}),
145+
name: lookup.owner.name,
146+
processId: lookup.owner.processId,
147+
path: lookup.owner.path,
148+
...(lookup.owner.bundleId ? { bundleId: lookup.owner.bundleId } : {}),
112149
},
113150
};
114151
}

0 commit comments

Comments
 (0)