Skip to content

Commit 3d00499

Browse files
authored
perf(desktop): stop decoding snapshots twice for accessibility (#19221)
1 parent 5f64c5e commit 3d00499

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,9 +1048,15 @@ it.effect("matches Windows accessibility windows on a scaled display", () => {
10481048
);
10491049
});
10501050

1051-
it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
1052-
"persists %s text without inventing AT-SPI screen coordinates",
1053-
(backend) => {
1051+
it.effect.each([
1052+
{ backend: "gnome", screenshot: "PNG" },
1053+
{ backend: "niri", screenshot: "PNG" },
1054+
{ backend: "kde", screenshot: "PNG" },
1055+
{ backend: "hyprland", screenshot: "PNG" },
1056+
{ backend: "gnome", screenshot: "invalid" },
1057+
] as const)(
1058+
"persists $backend accessibility text with a $screenshot screenshot",
1059+
({ backend, screenshot }) => {
10541060
vi.stubEnv("XDG_SESSION_TYPE", "wayland");
10551061
flashWindows.length = 0;
10561062
const window = {
@@ -1067,7 +1073,16 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
10671073
...(backend === "niri" ? { accessibilityBoundsReliable: false } : {}),
10681074
...(backend === "kde" ? { clientBounds: { x: 479, y: 371, width: 700, height: 520 } } : {}),
10691075
};
1070-
linuxCaptureMock.mockResolvedValueOnce({ png: Buffer.from([1, 2, 3]), window });
1076+
// The captured pixels differ from the compositor's logical window bounds.
1077+
const imageSize = screenshot === "PNG" ? { width: 1400, height: 1040 } : window.bounds;
1078+
const png =
1079+
screenshot === "PNG"
1080+
? Buffer.from(
1081+
"iVBORw0KGgoAAAANSUhEUgAABXgAAAQQAQAAAABwoA4ZAAAAyElEQVR4nO3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHgYyx4AAepc/c0AAAAASUVORK5CYII=",
1082+
"base64",
1083+
)
1084+
: Buffer.from([1, 2, 3]);
1085+
linuxCaptureMock.mockResolvedValueOnce({ png, window });
10711086
activeWindowMock.mockClear();
10721087
getSourcesMock.mockClear();
10731088
accessibilityByPidMock.mockReset().mockResolvedValue({
@@ -1107,7 +1122,7 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
11071122
assert.deepInclude(saved.source.accessibility, {
11081123
format: "element-tree",
11091124
coordinateSpace: "captured-image",
1110-
imageSize: { width: 700, height: window.bounds.height },
1125+
imageSize: { width: imageSize.width, height: imageSize.height },
11111126
truncated: false,
11121127
});
11131128
assert.deepInclude(
@@ -1117,7 +1132,7 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
11171132
{
11181133
role: "window",
11191134
name: "Editor",
1120-
bounds: { x: 0, y: 0, width: 700, height: window.bounds.height },
1135+
bounds: { x: 0, y: 0, width: imageSize.width, height: imageSize.height },
11211136
state: { active: true },
11221137
},
11231138
);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import {
9090
snapShotShortcutSystemConflict,
9191
} from "./snapShot.ts";
9292
import * as HostProcess from "@t3tools/shared/HostProcess";
93+
import { readImageDimensions } from "@t3tools/shared/imageDimensions";
9394

9495
const MAX_CAPTURE_WIDTH = 2_560;
9596
const MAX_CAPTURE_HEIGHT = 1_600;
@@ -413,10 +414,10 @@ async function requestMacSnapShotPermissions(
413414
}
414415

415416
function snapShotImageSize(png: Buffer, fallback: Electron.Rectangle): Electron.Size {
416-
try {
417-
const size = Electron.nativeImage.createFromBuffer(png).getSize();
418-
if (size.width > 0 && size.height > 0) return size;
419-
} catch {}
417+
// Capture backends already validate or encode the PNG. Reading its header
418+
// avoids decoding every pixel again just to size accessibility coordinates.
419+
const size = readImageDimensions(png);
420+
if (size) return size;
420421
return {
421422
width: Math.max(1, Math.round(fallback.width)),
422423
height: Math.max(1, Math.round(fallback.height)),

0 commit comments

Comments
 (0)