Skip to content

Commit 6ed6a6a

Browse files
committed
fix(desktop): skip auxiliary macOS snapshot windows
Re-lands #12978 (b4cebe8), reverted in #22222. ActiveWindow.test.ts was deleted by #22557 after the revert, so only the macOS window selection cases this fix adds are restored.
1 parent e64766c commit 6ed6a6a

2 files changed

Lines changed: 223 additions & 24 deletions

File tree

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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: "a smaller front window's strips with a larger background window",
106+
helpers: [
107+
{ id: 75, title: "", bounds: { x: 100, y: 139, width: 1_200, height: 41 } },
108+
{ id: 74, title: "", bounds: { x: 100, y: 180, width: 1_200, height: 81 } },
109+
],
110+
content: {
111+
id: 71,
112+
title: "Video - YouTube",
113+
bounds: { x: 100, y: 261, width: 1_200, height: 700 },
114+
},
115+
background: [
116+
{ id: 80, title: "Other document", bounds: { x: 0, y: 39, width: 1_800, height: 1_130 } },
117+
],
118+
},
119+
])("selects the content window behind $name", async ({ helpers, content, background = [] }) => {
120+
stubMacLookup(macLookup([...helpers, content, ...background]));
121+
122+
const window = await activeWindow("darwin");
123+
124+
assert.strictEqual(window?.id, content.id);
125+
assert.deepEqual(window?.bounds, content.bounds);
126+
});
127+
128+
it.each([
129+
{ name: "untitled dialog", title: "", bounds: { x: 300, y: 200, width: 400, height: 240 } },
130+
{ name: "titled palette", title: "Palette", bounds: { x: 0, y: 0, width: 800, height: 50 } },
131+
{ name: "offset untitled strip", title: "", bounds: { x: 100, y: 100, width: 800, height: 50 } },
132+
{ name: "tall untitled window", title: "", bounds: { x: 0, y: 0, width: 800, height: 900 } },
133+
])("keeps a frontmost $name ahead of a larger document", async ({ title, bounds }) => {
134+
stubMacLookup(
135+
macLookup([
136+
{ id: 1, title, bounds },
137+
{ id: 2, title: "Document", bounds: { x: 0, y: 0, width: 800, height: 1_000 } },
138+
]),
139+
);
140+
141+
const window = await activeWindow("darwin");
142+
143+
assert.strictEqual(window?.id, 1);
144+
assert.deepEqual(window?.bounds, bounds);
145+
});
146+
147+
it("keeps an untitled content window ahead of a much taller background window", async () => {
148+
const bounds = { x: 0, y: 0, width: 800, height: 900 };
149+
stubMacLookup(
150+
macLookup([
151+
{ id: 1, title: "", bounds },
152+
{ id: 2, title: "Background document", bounds: { x: 0, y: 0, width: 800, height: 4_000 } },
153+
]),
154+
);
155+
156+
const window = await activeWindow("darwin");
157+
158+
assert.strictEqual(window?.id, 1);
159+
assert.deepEqual(window?.bounds, bounds);
160+
});

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

Lines changed: 63 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,39 @@ 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 <= 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;
102141
return {
103142
platform: "macos",
104143
id: window.id,
105144
title: window.title,
106145
bounds: window.bounds,
107146
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 } : {}),
112151
},
113152
};
114153
}

0 commit comments

Comments
 (0)