Skip to content

Commit a8d379d

Browse files
fix(web): preserve snapshot thumbnail context and detail (#18440)
Co-authored-by: Julius Marminge <[email protected]>
1 parent f298bd4 commit a8d379d

5 files changed

Lines changed: 118 additions & 25 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ window.prepareCaptureTransition=async destination=>{
175175
const inner={width:Math.max(1,target.width-borderWidth*2),height:Math.max(1,target.height-borderWidth*2)};
176176
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};
177177
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);
179179
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"});
180180
Object.assign(content.style,{inset:borderWidth+"px",borderRadius:Math.max(0,destination.cornerRadius-borderWidth)+"px"});
181181
Object.assign(border.style,{borderWidth:borderWidth+"px",borderColor:destination.borderColor});
@@ -185,7 +185,7 @@ window.prepareCaptureTransition=async destination=>{
185185
const animations=[
186186
card.animate([{transform:initial},{transform:"translate3d(0,0,0) scale(1)"}],options),
187187
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),
189189
border.animate([{opacity:0},{opacity:1}],options),
190190
details.animate([{opacity:0},{opacity:1}],{delay:Math.max(0,destination.durationMs-125),duration:125,fill:"both",easing:"ease-in"})
191191
];

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4864,8 +4864,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
48644864
{image.previewUrl ? (
48654865
<ComposerImageThumbnail
48664866
file={image.file}
4867+
mode={image.source?.kind === "snap-shot" ? "snapshot" : "square"}
48674868
alt=""
4868-
className="size-full object-cover"
4869+
className="size-full"
48694870
fallback={
48704871
isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
48714872
<AttachmentImage
@@ -6740,8 +6741,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
67406741
>
67416742
<ComposerImageThumbnail
67426743
file={image.file}
6744+
mode={image.source?.kind === "snap-shot" ? "snapshot" : "square"}
67436745
alt={image.name}
6744-
className="h-full w-full object-cover"
6746+
className="h-full w-full"
67456747
fallback={
67466748
isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
67476749
<AttachmentImage
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
11
import { memo, useEffect, useState, type ReactNode } from "react";
22

3-
import { createComposerImageThumbnail } from "../../lib/imageCompression";
3+
import {
4+
createComposerImageThumbnail,
5+
type ComposerImageThumbnailMode,
6+
} from "../../lib/imageCompression";
7+
import { cn } from "../../lib/utils";
48

59
/** Keep full-resolution image decoding out of composer rerenders. */
610
export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
711
file,
12+
mode = "square",
813
alt,
914
className,
1015
fallback,
1116
}: {
1217
file: File;
18+
mode?: ComposerImageThumbnailMode;
1319
alt: string;
1420
className: string;
1521
fallback: ReactNode;
1622
}) {
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);
1828
useEffect(() => {
1929
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 });
2232
});
2333
return () => {
2434
active = false;
2535
};
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+
);
2947
});

‎apps/web/src/lib/imageCompression.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,61 @@ describe("composer image thumbnails", () => {
178178
expect(await original.text()).toBe("original bytes");
179179
});
180180

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+
181236
it("releases the decoded image when thumbnail encoding fails", async () => {
182237
const close = vi.fn();
183238
vi.stubGlobal(

‎apps/web/src/lib/imageCompression.ts‎

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -204,31 +204,49 @@ async function encodeCanvas(
204204
return blob?.type === mimeType ? blob : null;
205205
}
206206

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];
211224
if (cached) return cached;
212225
const thumbnail = (async () => {
213226
if (!canRecompress()) return null;
214227
let bitmap: ImageBitmap | undefined;
215228
try {
216229
bitmap = await createImageBitmap(file);
230+
if (bitmap.width <= 0 || bitmap.height <= 0) return null;
217231
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);
221239
if (!surface) return null;
222240
surface.context.drawImage(
223241
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,
228246
0,
229247
0,
230-
dimension,
231-
dimension,
248+
width,
249+
height,
232250
);
233251
const blob = await encodeCanvas(surface.canvas, 1, "image/png");
234252
return blob ? await readFileAsDataUrl(blob) : null;
@@ -238,7 +256,7 @@ export function createComposerImageThumbnail(file: File): Promise<string | null>
238256
bitmap?.close();
239257
}
240258
})();
241-
composerThumbnails.set(file, thumbnail);
259+
composerThumbnails.set(file, { ...fileThumbnails, [mode]: thumbnail });
242260
return thumbnail;
243261
}
244262

0 commit comments

Comments
 (0)