GeezerVison is showing github.com — perf(desktop): stop decoding snapshots twice for accessibility · pingdotgg/t3code@c0d49ff
File treeExpand file treeCollapse file tree Open diff view settings apps/desktop/src/snapShot
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/snapShot/DesktopSnapShot.test.ts +21-6Lines changed: 21 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1049,9 +1049,15 @@ it.effect("matches Windows accessibility windows on a scaled display", () => { | 1049 | 1049 | ); | 1050 | 1050 | }); | 1051 | 1051 |
| 1052 | | -it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)( | 1053 | | - "persists %s text without inventing AT-SPI screen coordinates", | 1054 | | - (backend) => { | | 1052 | +it.effect.each([ | | 1053 | + { backend: "gnome", screenshot: "PNG" }, | | 1054 | + { backend: "niri", screenshot: "PNG" }, | | 1055 | + { backend: "kde", screenshot: "PNG" }, | | 1056 | + { backend: "hyprland", screenshot: "PNG" }, | | 1057 | + { backend: "gnome", screenshot: "invalid" }, | | 1058 | +] as const)( | | 1059 | + "persists $backend accessibility text with a $screenshot screenshot", | | 1060 | + ({ backend, screenshot }) => { | 1055 | 1061 | vi.stubEnv("XDG_SESSION_TYPE", "wayland"); | 1056 | 1062 | flashWindows.length = 0; | 1057 | 1063 | const window = { | @@ -1068,7 +1074,16 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)( | 1068 | 1074 | ...(backend === "niri" ? { accessibilityBoundsReliable: false } : {}), | 1069 | 1075 | ...(backend === "kde" ? { clientBounds: { x: 479, y: 371, width: 700, height: 520 } } : {}), | 1070 | 1076 | }; | 1071 | | - linuxCaptureMock.mockResolvedValueOnce({ png: Buffer.from([1, 2, 3]), window }); | | 1077 | + // The captured pixels differ from the compositor's logical window bounds. | | 1078 | + const imageSize = screenshot === "PNG" ? { width: 1400, height: 1040 } : window.bounds; | | 1079 | + const png = | | 1080 | + screenshot === "PNG" | | 1081 | + ? Buffer.from( | | 1082 | + "iVBORw0KGgoAAAANSUhEUgAABXgAAAQQAQAAAABwoA4ZAAAAyElEQVR4nO3BMQEAAADCoPVPbQwfoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHgYyx4AAepc/c0AAAAASUVORK5CYII=", | | 1083 | + "base64", | | 1084 | + ) | | 1085 | + : Buffer.from([1, 2, 3]); | | 1086 | + linuxCaptureMock.mockResolvedValueOnce({ png, window }); | 1072 | 1087 | activeWindowMock.mockClear(); | 1073 | 1088 | getSourcesMock.mockClear(); | 1074 | 1089 | accessibilityByPidMock.mockReset().mockResolvedValue({ | @@ -1108,7 +1123,7 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)( | 1108 | 1123 | assert.deepInclude(saved.source.accessibility, { | 1109 | 1124 | format: "element-tree", | 1110 | 1125 | coordinateSpace: "captured-image", | 1111 | | - imageSize: { width: 700, height: window.bounds.height }, | | 1126 | + imageSize: { width: imageSize.width, height: imageSize.height }, | 1112 | 1127 | truncated: false, | 1113 | 1128 | }); | 1114 | 1129 | assert.deepInclude( | @@ -1118,7 +1133,7 @@ it.effect.each(["gnome", "niri", "kde", "hyprland"] as const)( | 1118 | 1133 | { | 1119 | 1134 | role: "window", | 1120 | 1135 | name: "Editor", | 1121 | | - bounds: { x: 0, y: 0, width: 700, height: window.bounds.height }, | | 1136 | + bounds: { x: 0, y: 0, width: imageSize.width, height: imageSize.height }, | 1122 | 1137 | state: { active: true }, | 1123 | 1138 | }, | 1124 | 1139 | ); | |
Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/snapShot/DesktopSnapShot.ts +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,6 +90,7 @@ import { | 90 | 90 | snapShotShortcutSystemConflict, | 91 | 91 | } from "./snapShot.ts"; | 92 | 92 | import * as HostProcess from "@t3tools/shared/HostProcess"; | | 93 | +import { readImageDimensions } from "@t3tools/shared/imageDimensions"; | 93 | 94 |
| 94 | 95 | const MAX_CAPTURE_WIDTH = 2_560; | 95 | 96 | const MAX_CAPTURE_HEIGHT = 1_600; | @@ -418,10 +419,10 @@ async function requestMacSnapShotPermissions( | 418 | 419 | } | 419 | 420 |
| 420 | 421 | function snapShotImageSize(png: Buffer, fallback: Electron.Rectangle): Electron.Size { | 421 | | - try { | 422 | | - const size = Electron.nativeImage.createFromBuffer(png).getSize(); | 423 | | - if (size.width > 0 && size.height > 0) return size; | 424 | | - } catch {} | | 422 | + // Capture backends already validate or encode the PNG. Reading its header | | 423 | + // avoids decoding every pixel again just to size accessibility coordinates. | | 424 | + const size = readImageDimensions(png); | | 425 | + if (size) return size; | 425 | 426 | return { | 426 | 427 | width: Math.max(1, Math.round(fallback.width)), | 427 | 428 | height: Math.max(1, Math.round(fallback.height)), | |
|