Skip to content

Commit 6b43524

Browse files
shivamhwpPR Batch Testerjuliusmarminge
authored
fix(desktop): include Chrome app text in macOS SnapShots (pingdotgg#11325)
Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 1db05fa commit 6b43524

4 files changed

Lines changed: 153 additions & 1 deletion

File tree

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,6 +2725,69 @@ it.each([
27252725
}
27262726
});
27272727

2728+
it.each([
2729+
["com.google.Chrome", "Google Chrome – Profile"],
2730+
["org.chromium.Chromium", "Chromium"],
2731+
])("reads macOS %s page context with an accessibility title suffix", async (bundleId, suffix) => {
2732+
const title = "Issues · pingdotgg/t3code";
2733+
const bounds = { x: 0, y: 33, width: 1470, height: 857 };
2734+
const window = {
2735+
role: "window",
2736+
name: `${title} - ${suffix}`,
2737+
bounds,
2738+
tree: async () => ({ name: title, children: [{ name: "New issue", children: [] }] }),
2739+
children: async () => [{ role: "button", name: "New issue", bounds, children: async () => [] }],
2740+
};
2741+
accessibilityByPidMock.mockReset().mockImplementation(async (pid: number) => ({
2742+
children: async () => (pid === 42 ? [window] : []),
2743+
}));
2744+
2745+
const context = await readAccessibleWindowContext(
2746+
{ title, bounds, owner: { processId: 42, bundleId } },
2747+
"darwin",
2748+
title,
2749+
);
2750+
2751+
assert.equal(context?.accessibleText, `${title}\nNew issue`);
2752+
assert.equal(context?.accessibility?.format, "element-tree");
2753+
assert.equal(
2754+
context?.accessibility?.format === "element-tree"
2755+
? context.accessibility.root.children[0]?.name
2756+
: undefined,
2757+
"New issue",
2758+
);
2759+
});
2760+
2761+
it("does not borrow another Chrome process context when the captured PID has no windows", async () => {
2762+
const title = "Issues · pingdotgg/t3code";
2763+
const bounds = { x: 0, y: 33, width: 1470, height: 857 };
2764+
const otherWindow = {
2765+
role: "window",
2766+
name: `${title} - Google Chrome – Profile`,
2767+
bounds,
2768+
tree: async () => ({
2769+
name: title,
2770+
children: [{ name: "Other process private text", children: [] }],
2771+
}),
2772+
children: async () => [],
2773+
};
2774+
accessibilityByPidMock.mockReset().mockResolvedValue({
2775+
pid: 42,
2776+
children: async () => [],
2777+
});
2778+
accessibilityListMock.mockResolvedValue([
2779+
{ pid: 43, bundleId: "com.google.Chrome", children: async () => [otherWindow] },
2780+
]);
2781+
2782+
assert.isUndefined(
2783+
await readAccessibleWindowContext(
2784+
{ title, bounds, owner: { processId: 42, bundleId: "com.google.Chrome" } },
2785+
"darwin",
2786+
title,
2787+
),
2788+
);
2789+
});
2790+
27282791
it("reads Flatpak app text from the AT-SPI proxy when the compositor PID misses", async () => {
27292792
vi.stubEnv("XDG_SESSION_TYPE", "wayland");
27302793
const bounds = { x: 0, y: 0, width: 1_920, height: 1_048 };

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

Lines changed: 3 additions & 1 deletion
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

@@ -134,6 +134,8 @@ async function readCapturedWindowAccessibility(
134134
sourceTitle,
135135
bounds: active.bounds,
136136
clientBounds: active.clientBounds,
137+
platform,
138+
bundleId: pidWindows.length > 0 ? active.owner.bundleId : undefined,
137139
};
138140
const window = findAccessibleWindow(
139141
pidWindows.length > 0 || foreground !== undefined ? pidWindows : await windowsFromAppList(App),

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

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

335+
const chromeCapture = {
336+
title: "Issues · pingdotgg/t3code",
337+
bounds: captured.bounds,
338+
platform: "darwin" as const,
339+
bundleId: "com.google.Chrome",
340+
};
341+
const chromeWindow = {
342+
name: `${chromeCapture.title} - Google Chrome – Profile`,
343+
bounds: chromeCapture.bounds,
344+
active: true,
345+
};
346+
347+
it("uses a browser suffix only when no exact title matches", () => {
348+
const exact = { name: chromeCapture.title, bounds: chromeCapture.bounds };
349+
expect(findAccessibleWindow([chromeWindow, exact], chromeCapture)).toBe(exact);
350+
expect(findAccessibleWindow([chromeWindow], chromeCapture)).toBe(chromeWindow);
351+
});
352+
353+
it("rejects browser suffix matches with wrong bounds, page titles, or ambiguous windows", () => {
354+
expect(
355+
findAccessibleWindow(
356+
[{ ...chromeWindow, bounds: { ...chromeWindow.bounds, x: 0 } }],
357+
chromeCapture,
358+
),
359+
).toBeUndefined();
360+
expect(
361+
findAccessibleWindow([chromeWindow, { ...chromeWindow, active: false }], chromeCapture),
362+
).toBeUndefined();
363+
expect(
364+
findAccessibleWindow([chromeWindow], { ...chromeCapture, title: "Issues" }),
365+
).toBeUndefined();
366+
expect(
367+
findAccessibleWindow([chromeWindow], {
368+
...chromeCapture,
369+
title: `${chromeCapture.title} - Google Chrome`,
370+
}),
371+
).toBeUndefined();
372+
});
373+
374+
it("keeps strict titles outside macOS Chrome and Chromium", () => {
375+
for (const identity of [
376+
{ platform: "linux" as const, bundleId: "com.google.Chrome" },
377+
{ platform: "win32" as const, bundleId: "com.google.Chrome" },
378+
{ platform: "darwin" as const, bundleId: "com.example.Editor" },
379+
{ platform: "darwin" as const, bundleId: undefined },
380+
]) {
381+
expect(
382+
findAccessibleWindow([chromeWindow], { ...chromeCapture, ...identity }),
383+
).toBeUndefined();
384+
}
385+
});
386+
335387
it("matches one window by its captured bounds", () => {
336388
const windows = [
337389
{ name: "Private", bounds: { x: 0, y: 0, width: 400, height: 300 } },

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ export function findAccessibleWindow<
477477
readonly sourceTitle?: string;
478478
readonly bounds: WindowBounds;
479479
readonly clientBounds?: WindowBounds | undefined;
480+
readonly platform?: NodeJS.Platform;
481+
readonly bundleId?: string | undefined;
480482
},
481483
matchMode: "screen-bounds" | "wayland" = "screen-bounds",
482484
options: { readonly allowUntitledUniqueBounds?: boolean } = {},
@@ -515,6 +517,8 @@ export function findAccessibleWindow<
515517
const activeMatches = matches.filter((window) => safeProperty(() => window.active) === true);
516518
if (activeMatches.length === 1) return activeMatches[0];
517519
if (matches.length > 1) return undefined;
520+
const browserMatch = findBrowserTitleMatch(windows, titles, captured, matchMode, matchesBounds);
521+
if (browserMatch) return browserMatch;
518522
}
519523
// GTK4/libadwaita often exposes the frame as an unnamed group. A PID-scoped
520524
// lookup can accept the one window whose bounds match; size-only guesses cannot.
@@ -525,6 +529,37 @@ export function findAccessibleWindow<
525529
return boundsMatches.length === 1 ? boundsMatches[0] : undefined;
526530
}
527531

532+
// Chrome's macOS AX title can append the browser and profile to the captured page title.
533+
function findBrowserTitleMatch<T extends { readonly name?: string | null }>(
534+
windows: readonly T[],
535+
titles: ReadonlySet<string>,
536+
captured: { readonly platform?: NodeJS.Platform; readonly bundleId?: string | undefined },
537+
matchMode: "screen-bounds" | "wayland",
538+
matchesBounds: (window: T) => boolean,
539+
): T | undefined {
540+
if (captured.platform !== "darwin" || matchMode !== "screen-bounds") return undefined;
541+
const browserName = /^com\.google\.Chrome(?:\.(?:beta|dev|canary))?$/.test(
542+
captured.bundleId ?? "",
543+
)
544+
? "Google Chrome"
545+
: captured.bundleId === "org.chromium.Chromium"
546+
? "Chromium"
547+
: undefined;
548+
if (!browserName) return undefined;
549+
const browserMatches = windows.filter((window) => {
550+
if (!matchesBounds(window)) return false;
551+
const name = window.name?.trim() ?? "";
552+
return [...titles].some((title) => {
553+
const prefix = `${title} - ${browserName}`;
554+
return (
555+
name === prefix ||
556+
(name.startsWith(prefix) && /^ [–—-] \S.*$/u.test(name.slice(prefix.length)))
557+
);
558+
});
559+
});
560+
return browserMatches.length === 1 ? browserMatches[0] : undefined;
561+
}
562+
528563
const ELECTRON_KEY_NAMES: Readonly<Record<string, string>> = {
529564
" ": "Space",
530565
"+": "Plus",

0 commit comments

Comments
 (0)