@devries-cameron20 / t3code Mirror · read-only / commits / a8d379d65e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 01:45 UTC

fix(web): preserve snapshot thumbnail context and detail (#18440)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

maria committed Oct 19, 2026 at 17:48 UTC a8d379d65e6a87ddd543a54871d3db84e7d8702d
5 files changed +118 -25
apps/desktop/src/snapShot/SnapShotTransition.ts
+2 -2
@@ -175,7 +175,7 @@ window.prepareCaptureTransition=async destination=>{
175 const inner={width:Math.max(1,target.width-borderWidth*2),height:Math.max(1,target.height-borderWidth*2)};
176 const sourceCenter={x:source.x+source.width/2,y:source.y+source.height/2},targetCenter={x:target.x+target.width/2,y:target.y+target.height/2};
177 const initial="translate3d("+(sourceCenter.x-targetCenter.x)+"px,"+(sourceCenter.y-targetCenter.y)+"px,0) scale("+(source.width/target.width)+","+(source.height/target.height)+")";
178 - const imageWidth=Math.max(1,snapshot.naturalWidth||source.width),imageHeight=Math.max(1,snapshot.naturalHeight||source.height),cover=Math.max(inner.width/imageWidth,inner.height/imageHeight);
178 + const imageWidth=Math.max(1,snapshot.naturalWidth||source.width),imageHeight=Math.max(1,snapshot.naturalHeight||source.height),fit=Math.min(inner.width/imageWidth,inner.height/imageHeight);
179 Object.assign(card.style,{left:target.x+"px",top:target.y+"px",width:target.width+"px",height:target.height+"px",borderRadius:destination.cornerRadius+"px",backgroundColor:destination.backgroundColor,boxShadow:"none"});
180 Object.assign(content.style,{inset:borderWidth+"px",borderRadius:Math.max(0,destination.cornerRadius-borderWidth)+"px"});
181 Object.assign(border.style,{borderWidth:borderWidth+"px",borderColor:destination.borderColor});
@@ -185,7 +185,7 @@ window.prepareCaptureTransition=async destination=>{
185 const animations=[
186 card.animate([{transform:initial},{transform:"translate3d(0,0,0) scale(1)"}],options),
187 content.animate([{transform:"scale("+(target.width/inner.width)+","+(target.height/inner.height)+")"},{transform:"scale(1)"}],options),
188 - snapshot.animate([{transform:"scale(1)"},{transform:"scale("+((imageWidth*cover)/inner.width)+","+((imageHeight*cover)/inner.height)+")"}],options),
188 + snapshot.animate([{transform:"scale(1)"},{transform:"scale("+((imageWidth*fit)/inner.width)+","+((imageHeight*fit)/inner.height)+")"}],options),
189 border.animate([{opacity:0},{opacity:1}],options),
190 details.animate([{opacity:0},{opacity:1}],{delay:Math.max(0,destination.durationMs-125),duration:125,fill:"both",easing:"ease-in"})
191 ];
apps/web/src/components/chat/ChatComposer.tsx
+4 -2
@@ -4864,8 +4864,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
4864 {image.previewUrl ? (
4865 <ComposerImageThumbnail
4866 file={image.file}
4867 + mode={image.source?.kind === "snap-shot" ? "snapshot" : "square"}
4868 alt=""
4868 - className="size-full object-cover"
4869 + className="size-full"
4870 fallback={
4871 isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
4872 <AttachmentImage
@@ -6740,8 +6741,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
6741 >
6742 <ComposerImageThumbnail
6743 file={image.file}
6744 + mode={image.source?.kind === "snap-shot" ? "snapshot" : "square"}
6745 alt={image.name}
6744 - className="h-full w-full object-cover"
6746 + className="h-full w-full"
6747 fallback={
6748 isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
6749 <AttachmentImage
apps/web/src/components/chat/ComposerImageThumbnail.tsx
+25 -7
@@ -1,29 +1,47 @@
1 import { memo, useEffect, useState, type ReactNode } from "react";
2
3 -import { createComposerImageThumbnail } from "../../lib/imageCompression";
3 +import {
4 + createComposerImageThumbnail,
5 + type ComposerImageThumbnailMode,
6 +} from "../../lib/imageCompression";
7 +import { cn } from "../../lib/utils";
8
9 /** Keep full-resolution image decoding out of composer rerenders. */
10 export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
11 file,
12 + mode = "square",
13 alt,
14 className,
15 fallback,
16 }: {
17 file: File;
18 + mode?: ComposerImageThumbnailMode;
19 alt: string;
20 className: string;
21 fallback: ReactNode;
22 }) {
17 - const [preview, setPreview] = useState<{ file: File; src: string | null } | null>(null);
23 + const [preview, setPreview] = useState<{
24 + file: File;
25 + mode: ComposerImageThumbnailMode;
26 + src: string | null;
27 + } | null>(null);
28 useEffect(() => {
29 let active = true;
20 - void createComposerImageThumbnail(file).then((src) => {
21 - if (active) setPreview({ file, src });
30 + void createComposerImageThumbnail(file, mode).then((src) => {
31 + if (active) setPreview({ file, mode, src });
32 });
33 return () => {
34 active = false;
35 };
26 - }, [file]);
27 - const src = preview?.file === file ? preview.src : null;
28 - return src ? <img src={src} alt={alt} className={className} /> : fallback;
36 + }, [file, mode]);
37 + const src = preview?.file === file && preview.mode === mode ? preview.src : null;
38 + return src ? (
39 + <img
40 + src={src}
41 + alt={alt}
42 + className={cn(className, mode === "snapshot" ? "object-contain" : "object-cover")}
43 + />
44 + ) : (
45 + fallback
46 + );
47 });
apps/web/src/lib/imageCompression.test.ts
+55
@@ -178,6 +178,61 @@ describe("composer image thumbnails", () => {
178 expect(await original.text()).toBe("original bytes");
179 });
180
181 + it.each([
182 + [3840, 2160, 796, 448],
183 + [3840, 1600, 832, 347],
184 + ])(
185 + "preserves the full %ix%i snapshot within an 832x448 preview without replacing its square thumbnail",
186 + async (sourceWidth, sourceHeight, width, height) => {
187 + const bitmap = { width: sourceWidth, height: sourceHeight, close: vi.fn() };
188 + const decode = vi.fn(async () => bitmap);
189 + const drawImage = vi.fn();
190 + vi.stubGlobal("createImageBitmap", decode);
191 + vi.stubGlobal(
192 + "OffscreenCanvas",
193 + class {
194 + constructor(
195 + public width: number,
196 + public height: number,
197 + ) {}
198 + getContext() {
199 + return { drawImage };
200 + }
201 + async convertToBlob() {
202 + return new Blob([`${this.width}x${this.height}`], { type: "image/png" });
203 + }
204 + },
205 + );
206 + const original = new File(["original bytes"], "window.png", { type: "image/png" });
207 + const square = await createComposerImageThumbnail(original);
208 + const [snapshot, cachedSnapshot] = await Promise.all([
209 + createComposerImageThumbnail(original, "snapshot"),
210 + createComposerImageThumbnail(original, "snapshot"),
211 + ]);
212 +
213 + expect(snapshot).toBe(
214 + `data:image/png;base64,${Buffer.from(`${width}x${height}`).toString("base64")}`,
215 + );
216 + expect(drawImage).toHaveBeenLastCalledWith(
217 + bitmap,
218 + 0,
219 + 0,
220 + sourceWidth,
221 + sourceHeight,
222 + 0,
223 + 0,
224 + width,
225 + height,
226 + );
227 + expect(cachedSnapshot).toBe(snapshot);
228 + expect(await createComposerImageThumbnail(original, "snapshot")).toBe(snapshot);
229 + expect(square).toBe(`data:image/png;base64,${Buffer.from("256x256").toString("base64")}`);
230 + expect(await createComposerImageThumbnail(original)).toBe(square);
231 + expect(decode).toHaveBeenCalledTimes(2);
232 + expect(await original.text()).toBe("original bytes");
233 + },
234 + );
235 +
236 it("releases the decoded image when thumbnail encoding fails", async () => {
237 const close = vi.fn();
238 vi.stubGlobal(
apps/web/src/lib/imageCompression.ts
+32 -14
@@ -204,31 +204,49 @@ async function encodeCanvas(
204 return blob?.type === mimeType ? blob : null;
205 }
206
207 -const composerThumbnails = new WeakMap<File, Promise<string | null>>();
208 -
209 -export function createComposerImageThumbnail(file: File): Promise<string | null> {
210 - const cached = composerThumbnails.get(file);
207 +const COMPOSER_THUMBNAIL_SIZES = {
208 + square: { width: 256, height: 256 },
209 + // The 208x112 snapshot tile at retina density and 2x app zoom.
210 + snapshot: { width: 832, height: 448 },
211 +} as const;
212 +export type ComposerImageThumbnailMode = keyof typeof COMPOSER_THUMBNAIL_SIZES;
213 +const composerThumbnails = new WeakMap<
214 + File,
215 + Partial<Record<ComposerImageThumbnailMode, Promise<string | null>>>
216 +>();
217 +
218 +export function createComposerImageThumbnail(
219 + file: File,
220 + mode: ComposerImageThumbnailMode = "square",
221 +): Promise<string | null> {
222 + const fileThumbnails = composerThumbnails.get(file);
223 + const cached = fileThumbnails?.[mode];
224 if (cached) return cached;
225 const thumbnail = (async () => {
226 if (!canRecompress()) return null;
227 let bitmap: ImageBitmap | undefined;
228 try {
229 bitmap = await createImageBitmap(file);
230 + if (bitmap.width <= 0 || bitmap.height <= 0) return null;
231 const side = Math.min(bitmap.width, bitmap.height);
218 - if (side <= 0) return null;
219 - const dimension = Math.min(256, side);
220 - const surface = createCanvas(dimension, dimension);
232 + const sourceWidth = mode === "square" ? side : bitmap.width;
233 + const sourceHeight = mode === "square" ? side : bitmap.height;
234 + const bounds = COMPOSER_THUMBNAIL_SIZES[mode];
235 + const scale = Math.min(1, bounds.width / sourceWidth, bounds.height / sourceHeight);
236 + const width = Math.max(1, Math.round(sourceWidth * scale));
237 + const height = Math.max(1, Math.round(sourceHeight * scale));
238 + const surface = createCanvas(width, height);
239 if (!surface) return null;
240 surface.context.drawImage(
241 bitmap,
224 - (bitmap.width - side) / 2,
225 - (bitmap.height - side) / 2,
226 - side,
227 - side,
242 + (bitmap.width - sourceWidth) / 2,
243 + (bitmap.height - sourceHeight) / 2,
244 + sourceWidth,
245 + sourceHeight,
246 0,
247 0,
230 - dimension,
231 - dimension,
248 + width,
249 + height,
250 );
251 const blob = await encodeCanvas(surface.canvas, 1, "image/png");
252 return blob ? await readFileAsDataUrl(blob) : null;
@@ -238,7 +256,7 @@ export function createComposerImageThumbnail(file: File): Promise<string | null>
256 bitmap?.close();
257 }
258 })();
241 - composerThumbnails.set(file, thumbnail);
259 + composerThumbnails.set(file, { ...fileThumbnails, [mode]: thumbnail });
260 return thumbnail;
261 }
262