Skip to content

Commit 74896dd

Browse files
committed
fix(desktop): include Chrome app text in macOS SnapShots
1 parent 20363c3 commit 74896dd

4 files changed

Lines changed: 172 additions & 6 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { expect, it } from "vite-plus/test";
2+
3+
import { readAccessibleWindowContextWithApp } from "./SnapShotAccessibility.ts";
4+
5+
const bounds = { x: 0, y: 33, width: 1470, height: 857 };
6+
const window = {
7+
name: "Issues · pingdotgg/t3code - Google Chrome – Profile",
8+
role: "window",
9+
bounds,
10+
children: async () => [],
11+
tree: async () => ({
12+
role: "window",
13+
name: "Issues · pingdotgg/t3code",
14+
children: [{ role: "link", name: "Issues" }],
15+
}),
16+
};
17+
const App = {
18+
byPid: async (pid: number) => ({ children: async () => (pid === 42 ? [window] : []) }),
19+
} as unknown as Parameters<typeof readAccessibleWindowContextWithApp>[0];
20+
21+
const request = {
22+
active: {
23+
title: "Issues · pingdotgg/t3code",
24+
bounds,
25+
owner: { processId: 42, bundleId: "com.google.Chrome" },
26+
},
27+
platform: "darwin" as const,
28+
sourceTitle: "Issues · pingdotgg/t3code",
29+
imageSize: { width: 1470, height: 857 },
30+
};
31+
32+
it("includes Chrome accessibility context when macOS adds a profile to its title", async () => {
33+
const context = await readAccessibleWindowContextWithApp(App, request);
34+
expect(context?.accessibleText).toContain("Issues");
35+
expect(context?.accessibility).toBeDefined();
36+
});
37+
38+
it("does not borrow browser accessibility from another process or application", async () => {
39+
for (const owner of [
40+
{ processId: 43, bundleId: "com.google.Chrome" },
41+
{ processId: 42, bundleId: "com.example.Editor" },
42+
]) {
43+
expect(
44+
await readAccessibleWindowContextWithApp(App, {
45+
...request,
46+
active: { ...request.active, owner },
47+
}),
48+
).toBeUndefined();
49+
}
50+
});

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type AccessibleWindowIdentity = {
2020
readonly title: string;
2121
readonly bounds: Electron.Rectangle;
2222
readonly clientBounds?: Electron.Rectangle;
23-
readonly owner: { readonly processId: number };
23+
readonly owner: { readonly processId: number; readonly bundleId?: string };
2424
readonly accessibilityBoundsReliable?: boolean;
2525
};
2626

@@ -114,7 +114,14 @@ async function readCapturedWindowAccessibility(
114114
const matchMode = isWaylandSession(platform, process.env) ? "wayland" : "screen-bounds";
115115
const window = findAccessibleWindow(
116116
windows,
117-
{ title: active.title, sourceTitle, bounds: active.bounds, clientBounds: active.clientBounds },
117+
{
118+
title: active.title,
119+
sourceTitle,
120+
bounds: active.bounds,
121+
clientBounds: active.clientBounds,
122+
platform,
123+
bundleId: active.owner.bundleId,
124+
},
118125
matchMode,
119126
);
120127
if (!window) {

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,89 @@ describe("findAccessibleWindow", () => {
332332
bounds: { x: 100, y: 200, width: 800, height: 600 },
333333
};
334334

335+
const chromeCapture = {
336+
title: "Issues · pingdotgg/t3code",
337+
bounds: { x: 0, y: 33, width: 1470, height: 857 },
338+
platform: "darwin" as const,
339+
bundleId: "com.google.Chrome",
340+
};
341+
342+
it.each([
343+
["com.google.Chrome", "Google Chrome"],
344+
["com.google.Chrome.beta", "Google Chrome"],
345+
["com.google.Chrome.dev", "Google Chrome"],
346+
["com.google.Chrome.canary", "Google Chrome"],
347+
["org.chromium.Chromium", "Chromium"],
348+
])("matches a macOS browser profile suffix for %s", (bundleId, browserName) => {
349+
const window = {
350+
name: `${chromeCapture.title} - ${browserName} – Profile`,
351+
bounds: chromeCapture.bounds,
352+
};
353+
expect(findAccessibleWindow([window], { ...chromeCapture, bundleId })).toBe(window);
354+
});
355+
356+
it("matches a Chrome title without a profile suffix", () => {
357+
const window = {
358+
name: `${chromeCapture.title} - Google Chrome`,
359+
bounds: chromeCapture.bounds,
360+
};
361+
expect(findAccessibleWindow([window], chromeCapture)).toBe(window);
362+
});
363+
364+
it.each([
365+
{ platform: "linux" as const, bundleId: "com.google.Chrome" },
366+
{ platform: "win32" as const, bundleId: "com.google.Chrome" },
367+
{ platform: "darwin" as const, bundleId: "com.example.Editor" },
368+
{ platform: "darwin" as const, bundleId: undefined },
369+
])("keeps strict titles outside macOS Chrome: %j", (identity) => {
370+
const window = {
371+
name: `${chromeCapture.title} - Google Chrome – Profile`,
372+
bounds: chromeCapture.bounds,
373+
};
374+
expect(findAccessibleWindow([window], { ...chromeCapture, ...identity })).toBeUndefined();
375+
});
376+
377+
it.each([
378+
"Different page - Google Chrome – Profile",
379+
"Issues · pingdotgg/t3code copied - Google Chrome – Profile",
380+
"Issues · pingdotgg/t3code - Google ChromeSomething",
381+
])("rejects unrelated browser titles: %s", (name) => {
382+
expect(
383+
findAccessibleWindow([{ name, bounds: chromeCapture.bounds }], chromeCapture),
384+
).toBeUndefined();
385+
});
386+
387+
it("keeps bounds checks and rejects ambiguous browser suffix matches", () => {
388+
const window = {
389+
name: `${chromeCapture.title} - Google Chrome – Profile`,
390+
bounds: chromeCapture.bounds,
391+
};
392+
expect(
393+
findAccessibleWindow([{ ...window, bounds: { ...window.bounds, x: 3 } }], chromeCapture),
394+
).toBeUndefined();
395+
expect(
396+
findAccessibleWindow([window, { ...window, active: true }], chromeCapture),
397+
).toBeUndefined();
398+
expect(findAccessibleWindow([window], chromeCapture, "wayland")).toBeUndefined();
399+
});
400+
401+
it("prefers an exact title over a browser suffix match", () => {
402+
const exact = { name: chromeCapture.title, bounds: chromeCapture.bounds };
403+
const browser = {
404+
name: `${chromeCapture.title} - Google Chrome – Profile`,
405+
bounds: chromeCapture.bounds,
406+
active: true,
407+
};
408+
expect(findAccessibleWindow([browser, exact], chromeCapture)).toBe(exact);
409+
});
410+
411+
it("does not strip browser suffixes from captured page titles", () => {
412+
const window = { name: "Page - Google Chrome – Profile", bounds: chromeCapture.bounds };
413+
expect(
414+
findAccessibleWindow([window], { ...chromeCapture, title: "Page - Google Chrome" }),
415+
).toBeUndefined();
416+
});
417+
335418
it("matches one window by its captured bounds", () => {
336419
const windows = [
337420
{ name: "Private", bounds: { x: 0, y: 0, width: 400, height: 300 } },

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ export function findAccessibleWindow<
478478
readonly sourceTitle?: string;
479479
readonly bounds: WindowBounds;
480480
readonly clientBounds?: WindowBounds | undefined;
481+
readonly platform?: NodeJS.Platform;
482+
readonly bundleId?: string | undefined;
481483
},
482484
matchMode: "screen-bounds" | "wayland" = "screen-bounds",
483485
): T | undefined {
@@ -499,19 +501,43 @@ export function findAccessibleWindow<
499501
matchMode === "wayland" && captured.clientBounds
500502
? [captured.bounds, captured.clientBounds]
501503
: [captured.bounds];
502-
const matches = windows.filter((window) => {
504+
const candidates = windows.filter((window) => {
503505
const bounds = window.bounds;
504506
return (
505-
titles.has(normalizeTitle(window.name ?? "")) &&
506507
bounds !== null &&
507508
candidateBounds.some((candidate) =>
508509
boundsKeys.every((key) => Math.abs(bounds[key] - candidate[key]) <= 2),
509510
)
510511
);
511512
});
513+
const matches = candidates.filter((window) => titles.has(normalizeTitle(window.name ?? "")));
512514
if (matches.length === 1) return matches[0];
513-
const activeMatches = matches.filter((window) => safeProperty(() => window.active) === true);
514-
return activeMatches.length === 1 ? activeMatches[0] : undefined;
515+
if (matches.length > 1) {
516+
const activeMatches = matches.filter((window) => safeProperty(() => window.active) === true);
517+
return activeMatches.length === 1 ? activeMatches[0] : undefined;
518+
}
519+
520+
// Chrome's macOS AX title can append the browser and profile to the captured page title.
521+
if (captured.platform !== "darwin" || matchMode !== "screen-bounds") return undefined;
522+
const browserName = /^com\.google\.Chrome(?:\.(?:beta|dev|canary))?$/.test(
523+
captured.bundleId ?? "",
524+
)
525+
? "Google Chrome"
526+
: captured.bundleId === "org.chromium.Chromium"
527+
? "Chromium"
528+
: undefined;
529+
if (!browserName) return undefined;
530+
const browserMatches = candidates.filter((window) => {
531+
const name = window.name?.trim() ?? "";
532+
return [...titles].some((title) => {
533+
const prefix = `${title} - ${browserName}`;
534+
return (
535+
name === prefix ||
536+
(name.startsWith(prefix) && /^ [–—-] \S.*$/u.test(name.slice(prefix.length)))
537+
);
538+
});
539+
});
540+
return browserMatches.length === 1 ? browserMatches[0] : undefined;
515541
}
516542

517543
const ELECTRON_KEY_NAMES: Readonly<Record<string, string>> = {

0 commit comments

Comments
 (0)