Skip to content

Commit 061681a

Browse files
committed
perf(desktop): stop decoding snapshots twice for accessibility
1 parent dff750b commit 061681a

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
@@ -1394,9 +1394,15 @@ it.effect("rejects X11 capture without registering shortcuts or loading capture
13941394
);
13951395
});
13961396

1397-
it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
1398-
"persists %s text without inventing AT-SPI screen coordinates",
1399-
(backend) => {
1397+
it.effect.each([
1398+
{ backend: "gnome", screenshot: "PNG" },
1399+
{ backend: "niri", screenshot: "PNG" },
1400+
{ backend: "kde", screenshot: "PNG" },
1401+
{ backend: "hyprland", screenshot: "PNG" },
1402+
{ backend: "gnome", screenshot: "invalid" },
1403+
] as const)(
1404+
"persists $backend accessibility text with a $screenshot screenshot",
1405+
({ backend, screenshot }) => {
14001406
vi.stubEnv("XDG_SESSION_TYPE", "wayland");
14011407
flashWindows.length = 0;
14021408
const window = {
@@ -1413,7 +1419,16 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
14131419
...(backend === "niri" ? { accessibilityBoundsReliable: false } : {}),
14141420
...(backend === "kde" ? { clientBounds: { x: 479, y: 371, width: 700, height: 520 } } : {}),
14151421
};
1416-
linuxCaptureMock.mockResolvedValueOnce({ png: Buffer.from([1, 2, 3]), window });
1422+
// The captured pixels differ from the compositor's logical window bounds.
1423+
const imageSize = screenshot === "PNG" ? { width: 1400, height: 1040 } : window.bounds;
1424+
const png =
1425+
screenshot === "PNG"
1426+
? Buffer.from(
1427+
"iVBORw0KGgoAAAANSUhEUgAABXgAAAQQAQAAAABwoA4ZAAAAyElEQVR4nO3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHgYyx4AAepc/c0AAAAASUVORK5CYII=",
1428+
"base64",
1429+
)
1430+
: Buffer.from([1, 2, 3]);
1431+
linuxCaptureMock.mockResolvedValueOnce({ png, window });
14171432
activeWindowMock.mockClear();
14181433
getSourcesMock.mockClear();
14191434
accessibilityByPidMock.mockReset().mockResolvedValue({
@@ -1453,7 +1468,7 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
14531468
assert.deepInclude(saved.source.accessibility, {
14541469
format: "element-tree",
14551470
coordinateSpace: "captured-image",
1456-
imageSize: { width: 700, height: window.bounds.height },
1471+
imageSize: { width: imageSize.width, height: imageSize.height },
14571472
truncated: false,
14581473
});
14591474
assert.deepInclude(
@@ -1463,7 +1478,7 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
14631478
{
14641479
role: "window",
14651480
name: "Editor",
1466-
bounds: { x: 0, y: 0, width: 700, height: window.bounds.height },
1481+
bounds: { x: 0, y: 0, width: imageSize.width, height: imageSize.height },
14671482
state: { active: true },
14681483
},
14691484
);

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

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

9394
const MAX_CAPTURE_WIDTH = 2_560;
9495
const MAX_CAPTURE_HEIGHT = 1_600;
@@ -380,10 +381,10 @@ async function requestMacSnapShotPermissions(
380381
}
381382

382383
function snapShotImageSize(png: Buffer, fallback: Electron.Rectangle): Electron.Size {
383-
try {
384-
const size = Electron.nativeImage.createFromBuffer(png).getSize();
385-
if (size.width > 0 && size.height > 0) return size;
386-
} catch {}
384+
// Capture backends already validate or encode the PNG. Reading its header
385+
// avoids decoding every pixel again just to size accessibility coordinates.
386+
const size = readImageDimensions(png);
387+
if (size) return size;
387388
return {
388389
width: Math.max(1, Math.round(fallback.width)),
389390
height: Math.max(1, Math.round(fallback.height)),

0 commit comments

Comments
 (0)