Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions apps/desktop/src/snapShot/DesktopSnapShot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1394,9 +1394,15 @@ it.effect("rejects X11 capture without registering shortcuts or loading capture
);
});

it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
"persists %s text without inventing AT-SPI screen coordinates",
(backend) => {
it.effect.each([
{ backend: "gnome", screenshot: "PNG" },
{ backend: "niri", screenshot: "PNG" },
{ backend: "kde", screenshot: "PNG" },
{ backend: "hyprland", screenshot: "PNG" },
{ backend: "gnome", screenshot: "invalid" },
] as const)(
"persists $backend accessibility text with a $screenshot screenshot",
({ backend, screenshot }) => {
vi.stubEnv("XDG_SESSION_TYPE", "wayland");
flashWindows.length = 0;
const window = {
Expand All @@ -1413,7 +1419,16 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
...(backend === "niri" ? { accessibilityBoundsReliable: false } : {}),
...(backend === "kde" ? { clientBounds: { x: 479, y: 371, width: 700, height: 520 } } : {}),
};
linuxCaptureMock.mockResolvedValueOnce({ png: Buffer.from([1, 2, 3]), window });
// The captured pixels differ from the compositor's logical window bounds.
const imageSize = screenshot === "PNG" ? { width: 1400, height: 1040 } : window.bounds;
const png =
screenshot === "PNG"
? Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAABXgAAAQQAQAAAABwoA4ZAAAAyElEQVR4nO3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHgYyx4AAepc/c0AAAAASUVORK5CYII=",
"base64",
)
: Buffer.from([1, 2, 3]);
linuxCaptureMock.mockResolvedValueOnce({ png, window });
activeWindowMock.mockClear();
getSourcesMock.mockClear();
accessibilityByPidMock.mockReset().mockResolvedValue({
Expand Down Expand Up @@ -1453,7 +1468,7 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
assert.deepInclude(saved.source.accessibility, {
format: "element-tree",
coordinateSpace: "captured-image",
imageSize: { width: 700, height: window.bounds.height },
imageSize: { width: imageSize.width, height: imageSize.height },
truncated: false,
});
assert.deepInclude(
Expand All @@ -1463,7 +1478,7 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)(
{
role: "window",
name: "Editor",
bounds: { x: 0, y: 0, width: 700, height: window.bounds.height },
bounds: { x: 0, y: 0, width: imageSize.width, height: imageSize.height },
state: { active: true },
},
);
Expand Down
9 changes: 5 additions & 4 deletions apps/desktop/src/snapShot/DesktopSnapShot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import {
snapShotShortcutSystemConflict,
} from "./snapShot.ts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { readImageDimensions } from "@t3tools/shared/imageDimensions";

const MAX_CAPTURE_WIDTH = 2_560;
const MAX_CAPTURE_HEIGHT = 1_600;
Expand Down Expand Up @@ -380,10 +381,10 @@ async function requestMacSnapShotPermissions(
}

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