Skip to content

Commit 3529723

Browse files
fix(desktop): skip auxiliary macOS snapshot windows (#22624)
Co-authored-by: maria-rcks <[email protected]>
1 parent 40f0649 commit 3529723

2 files changed

Lines changed: 271 additions & 24 deletions

File tree

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import { assert, beforeEach, it, vi } from "vite-plus/test";
2+
3+
const { execFileMock } = vi.hoisted(() => ({ execFileMock: vi.fn() }));
4+
5+
vi.mock("node:child_process", () => ({ execFile: execFileMock }));
6+
7+
import { activeWindow } from "./ActiveWindow.ts";
8+
9+
beforeEach(() => {
10+
execFileMock.mockReset();
11+
});
12+
13+
function stubMacLookup(stdout: string) {
14+
execFileMock.mockImplementation(
15+
(
16+
_file: string,
17+
_args: ReadonlyArray<string>,
18+
_options: unknown,
19+
callback: (error: Error | null, stdout: string) => void,
20+
) => callback(null, stdout),
21+
);
22+
}
23+
24+
function macLookup(
25+
windows: ReadonlyArray<{
26+
id: number;
27+
title: string;
28+
bounds: { x: number; y: number; width: number; height: number };
29+
alpha?: number;
30+
}>,
31+
) {
32+
return JSON.stringify({
33+
owner: {
34+
name: "Editor",
35+
processId: 123,
36+
path: "/Applications/Editor.app",
37+
bundleId: "com.example.editor",
38+
},
39+
windows: windows.map((window) => ({ ...window, alpha: window.alpha ?? 1 })),
40+
});
41+
}
42+
43+
it("parses the frontmost macOS window from the osascript lookup", async () => {
44+
stubMacLookup(
45+
macLookup([
46+
{
47+
id: 42,
48+
title: "main.ts",
49+
bounds: { x: 10, y: 20, width: 800, height: 600 },
50+
},
51+
]) + "\n",
52+
);
53+
54+
const window = await activeWindow("darwin");
55+
56+
assert.deepEqual(window, {
57+
platform: "macos",
58+
id: 42,
59+
title: "main.ts",
60+
bounds: { x: 10, y: 20, width: 800, height: 600 },
61+
owner: {
62+
name: "Editor",
63+
processId: 123,
64+
path: "/Applications/Editor.app",
65+
bundleId: "com.example.editor",
66+
},
67+
});
68+
const [file, args] = execFileMock.mock.calls[0]!;
69+
assert.strictEqual(file, "/usr/bin/osascript");
70+
assert.deepEqual(args.slice(0, 3), ["-l", "JavaScript", "-e"]);
71+
});
72+
73+
it.each([
74+
{
75+
name: "transparent and untitled helpers",
76+
helpers: [
77+
{
78+
id: 1,
79+
title: "",
80+
bounds: { x: -1, y: -44, width: 1_680, height: 44 },
81+
alpha: 0,
82+
},
83+
{ id: 2, title: "", bounds: { x: 0, y: 0, width: 1_680, height: 68 } },
84+
],
85+
content: {
86+
id: 3,
87+
title: "Document",
88+
bounds: { x: 0, y: 0, width: 1_680, height: 1_050 },
89+
},
90+
},
91+
{
92+
name: "Chrome strips stacked above the content window",
93+
helpers: [
94+
{ id: 75, title: "", bounds: { x: 0, y: 39, width: 1_800, height: 41 } },
95+
{ id: 74, title: "", bounds: { x: 0, y: 80, width: 1_800, height: 81 } },
96+
{ id: 1691, title: "", bounds: { x: 0, y: 39, width: 1_800, height: 158 } },
97+
],
98+
content: {
99+
id: 71,
100+
title: "(10) Video - YouTube",
101+
bounds: { x: 0, y: 161, width: 1_800, height: 1_008 },
102+
},
103+
},
104+
{
105+
name: "vertical strips at a negative desktop origin",
106+
helpers: [
107+
{ id: 1, title: "", bounds: { x: -122, y: -100, width: 41, height: 1_000 } },
108+
{ id: 2, title: "", bounds: { x: -81, y: -100, width: 81, height: 1_000 } },
109+
],
110+
content: {
111+
id: 3,
112+
title: "Document",
113+
bounds: { x: 0, y: -100, width: 1_008, height: 1_000 },
114+
},
115+
},
116+
{
117+
name: "a smaller front window's strips with a larger background window",
118+
helpers: [
119+
{ id: 75, title: "", bounds: { x: 100, y: 139, width: 1_200, height: 41 } },
120+
{ id: 74, title: "", bounds: { x: 100, y: 180, width: 1_200, height: 81 } },
121+
],
122+
content: {
123+
id: 71,
124+
title: "Video - YouTube",
125+
bounds: { x: 100, y: 261, width: 1_200, height: 700 },
126+
},
127+
background: [
128+
{ id: 80, title: "Other document", bounds: { x: 0, y: 39, width: 1_800, height: 1_130 } },
129+
],
130+
},
131+
])("selects the content window behind $name", async ({ helpers, content, background = [] }) => {
132+
stubMacLookup(macLookup([...helpers, content, ...background]));
133+
134+
const window = await activeWindow("darwin");
135+
136+
assert.strictEqual(window?.id, content.id);
137+
assert.deepEqual(window?.bounds, content.bounds);
138+
});
139+
140+
it.each([
141+
{ name: "untitled dialog", title: "", bounds: { x: 300, y: 200, width: 400, height: 240 } },
142+
{ name: "titled palette", title: "Palette", bounds: { x: 0, y: 0, width: 800, height: 50 } },
143+
{ name: "offset untitled strip", title: "", bounds: { x: 100, y: 100, width: 800, height: 50 } },
144+
{ name: "tall untitled window", title: "", bounds: { x: 0, y: 0, width: 800, height: 900 } },
145+
{
146+
name: "horizontal strip below the document",
147+
title: "",
148+
bounds: { x: 0, y: 1_200, width: 800, height: 50 },
149+
},
150+
{
151+
name: "horizontal strip inside the document",
152+
title: "",
153+
bounds: { x: 0, y: 600, width: 800, height: 50 },
154+
},
155+
{
156+
name: "horizontal strip far above the document",
157+
title: "",
158+
bounds: { x: 0, y: -300, width: 800, height: 50 },
159+
},
160+
{
161+
name: "vertical strip right of the document",
162+
title: "",
163+
bounds: { x: 900, y: 0, width: 50, height: 1_000 },
164+
},
165+
{
166+
name: "vertical strip inside the document",
167+
title: "",
168+
bounds: { x: 400, y: 0, width: 50, height: 1_000 },
169+
},
170+
{
171+
name: "vertical strip far left of the document",
172+
title: "",
173+
bounds: { x: -250, y: 0, width: 50, height: 1_000 },
174+
},
175+
])("keeps a frontmost $name ahead of a larger document", async ({ title, bounds }) => {
176+
stubMacLookup(
177+
macLookup([
178+
{ id: 1, title, bounds },
179+
{ id: 2, title: "Document", bounds: { x: 0, y: 0, width: 800, height: 1_000 } },
180+
]),
181+
);
182+
183+
const window = await activeWindow("darwin");
184+
185+
assert.strictEqual(window?.id, 1);
186+
assert.deepEqual(window?.bounds, bounds);
187+
});
188+
189+
it("keeps an untitled content window ahead of a much taller background window", async () => {
190+
const bounds = { x: 0, y: 0, width: 800, height: 900 };
191+
stubMacLookup(
192+
macLookup([
193+
{ id: 1, title: "", bounds },
194+
{ id: 2, title: "Background document", bounds: { x: 0, y: 0, width: 800, height: 4_000 } },
195+
]),
196+
);
197+
198+
const window = await activeWindow("darwin");
199+
200+
assert.strictEqual(window?.id, 1);
201+
assert.deepEqual(window?.bounds, bounds);
202+
});

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

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,32 @@ export type ActiveWindow = {
2727

2828
const MAC_LOOKUP_TIMEOUT_MS = 5_000;
2929

30-
const MacActiveWindow = Schema.Struct({
31-
id: Schema.Number,
32-
title: Schema.String,
33-
bounds: Schema.Struct({
34-
x: Schema.Number,
35-
y: Schema.Number,
36-
width: Schema.Number,
37-
height: Schema.Number,
38-
}),
30+
const MAC_HELPER_STRIP_MAX_THICKNESS_RATIO = 0.25;
31+
32+
const MacActiveWindowLookup = Schema.Struct({
3933
owner: Schema.Struct({
4034
name: Schema.String,
4135
processId: Schema.Number,
4236
path: Schema.String,
4337
bundleId: Schema.String,
4438
}),
39+
windows: Schema.Array(
40+
Schema.Struct({
41+
id: Schema.Number,
42+
title: Schema.String,
43+
bounds: Schema.Struct({
44+
x: Schema.Number,
45+
y: Schema.Number,
46+
width: Schema.Number,
47+
height: Schema.Number,
48+
}),
49+
alpha: Schema.Number,
50+
}),
51+
),
4552
});
46-
const decodeMacActiveWindow = Schema.decodeUnknownSync(Schema.fromJsonString(MacActiveWindow));
53+
const decodeMacActiveWindowLookup = Schema.decodeUnknownSync(
54+
Schema.fromJsonString(MacActiveWindowLookup),
55+
);
4756

4857
const MAC_LOOKUP_SCRIPT = `
4958
ObjC.import("CoreGraphics");
@@ -52,38 +61,46 @@ function run() {
5261
const app = $.NSWorkspace.sharedWorkspace.frontmostApplication;
5362
if (app.isNil()) return "";
5463
const pid = app.processIdentifier;
64+
let ownerName = String(app.localizedName.js || "");
5565
const list = $.CGWindowListCopyWindowInfo(
5666
$.kCGWindowListOptionOnScreenOnly | $.kCGWindowListExcludeDesktopElements,
5767
$.kCGNullWindowID,
5868
);
5969
$.CFMakeCollectable(list);
6070
const count = $.CFArrayGetCount(list);
71+
const windows = [];
6172
for (let i = 0; i < count; i++) {
6273
const w = ObjC.castRefToObject($.CFArrayGetValueAtIndex(list, i));
6374
if (w.objectForKey("kCGWindowOwnerPID").js !== pid) continue;
6475
if (w.objectForKey("kCGWindowLayer").js !== 0) continue;
76+
if (!ownerName) {
77+
ownerName = String(ObjC.unwrap(w.objectForKey("kCGWindowOwnerName")) || "");
78+
}
6579
const b = ObjC.deepUnwrap(w.objectForKey("kCGWindowBounds"));
66-
return JSON.stringify({
80+
windows.push({
6781
id: w.objectForKey("kCGWindowNumber").js,
6882
title: String(ObjC.unwrap(w.objectForKey("kCGWindowName")) || ""),
6983
bounds: { x: b.X, y: b.Y, width: b.Width, height: b.Height },
70-
owner: {
71-
name: String(app.localizedName.js || ObjC.unwrap(w.objectForKey("kCGWindowOwnerName")) || ""),
72-
processId: pid,
73-
path: String(app.bundleURL.path.js || ""),
74-
bundleId: String(app.bundleIdentifier.js || ""),
75-
},
84+
alpha: w.objectForKey("kCGWindowAlpha").js,
7685
});
7786
}
78-
return "";
87+
return JSON.stringify({
88+
owner: {
89+
name: ownerName,
90+
processId: pid,
91+
path: String(app.bundleURL.path.js || ""),
92+
bundleId: String(app.bundleIdentifier.js || ""),
93+
},
94+
windows,
95+
});
7996
}`;
8097

8198
function runMacLookup(): Promise<string> {
8299
return new Promise((resolve, reject) => {
83100
NodeChildProcess.execFile(
84101
"/usr/bin/osascript",
85102
["-l", "JavaScript", "-e", MAC_LOOKUP_SCRIPT],
86-
{ timeout: MAC_LOOKUP_TIMEOUT_MS, maxBuffer: 64 * 1024 },
103+
{ timeout: MAC_LOOKUP_TIMEOUT_MS, maxBuffer: 1024 * 1024 },
87104
(error, stdout) => {
88105
if (error) reject(error);
89106
else resolve(stdout);
@@ -95,17 +112,45 @@ function runMacLookup(): Promise<string> {
95112
async function macActiveWindow(): Promise<ActiveWindow | undefined> {
96113
const output = (await runMacLookup()).trim();
97114
if (!output) return undefined;
98-
const window = decodeMacActiveWindow(output);
115+
const lookup = decodeMacActiveWindowLookup(output);
116+
const visible = lookup.windows.filter(
117+
(window) => window.alpha > 0 && window.bounds.width > 0 && window.bounds.height > 0,
118+
);
119+
// Helpers belong to the content behind them, which may not be the app's largest window.
120+
const window = visible.find((candidate, index) => {
121+
if (candidate.title.trim() !== "") return true;
122+
const bounds = candidate.bounds;
123+
return !visible.some((reference, referenceIndex) => {
124+
if (referenceIndex <= index) return false;
125+
return (
126+
(bounds.x === reference.bounds.x &&
127+
bounds.width === reference.bounds.width &&
128+
bounds.y <= reference.bounds.y &&
129+
reference.bounds.y - bounds.y <=
130+
reference.bounds.height * MAC_HELPER_STRIP_MAX_THICKNESS_RATIO &&
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.x <= reference.bounds.x &&
136+
reference.bounds.x - bounds.x <=
137+
reference.bounds.width * MAC_HELPER_STRIP_MAX_THICKNESS_RATIO &&
138+
bounds.width <= bounds.height * MAC_HELPER_STRIP_MAX_THICKNESS_RATIO &&
139+
bounds.width <= reference.bounds.width * MAC_HELPER_STRIP_MAX_THICKNESS_RATIO)
140+
);
141+
});
142+
});
143+
if (!window) return undefined;
99144
return {
100145
platform: "macos",
101146
id: window.id,
102147
title: window.title,
103148
bounds: window.bounds,
104149
owner: {
105-
name: window.owner.name,
106-
processId: window.owner.processId,
107-
path: window.owner.path,
108-
...(window.owner.bundleId ? { bundleId: window.owner.bundleId } : {}),
150+
name: lookup.owner.name,
151+
processId: lookup.owner.processId,
152+
path: lookup.owner.path,
153+
...(lookup.owner.bundleId ? { bundleId: lookup.owner.bundleId } : {}),
109154
},
110155
};
111156
}

0 commit comments

Comments
 (0)