Skip to content

Commit 23dd7e7

Browse files
t3dotggclaude
andauthored
revert: "fix(web): preserve snapshot thumbnail context and detail" (pingdotgg#18440) (pingdotgg#22231)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 86729b4 commit 23dd7e7

5 files changed

Lines changed: 25 additions & 118 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),fit=Math.min(inner.width/imageWidth,inner.height/imageHeight);
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);
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*fit)/inner.width)+","+((imageHeight*fit)/inner.height)+")"}],options),
188+
snapshot.animate([{transform:"scale(1)"},{transform:"scale("+((imageWidth*cover)/inner.width)+","+((imageHeight*cover)/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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4923,9 +4923,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
49234923
{image.previewUrl ? (
49244924
<ComposerImageThumbnail
49254925
file={image.file}
4926-
mode={image.source?.kind === "snap-shot" ? "snapshot" : "square"}
49274926
alt=""
4928-
className="size-full"
4927+
className="size-full object-cover"
49294928
fallback={
49304929
isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
49314930
<AttachmentImage
@@ -6818,9 +6817,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
68186817
>
68196818
<ComposerImageThumbnail
68206819
file={image.file}
6821-
mode={image.source?.kind === "snap-shot" ? "snapshot" : "square"}
68226820
alt={image.name}
6823-
className="h-full w-full"
6821+
className="h-full w-full object-cover"
68246822
fallback={
68256823
isHeicImageFile({ name: image.name, type: image.mimeType }) ? (
68266824
<AttachmentImage
Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,29 @@
11
import { memo, useEffect, useState, type ReactNode } from "react";
22

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

95
/** Keep full-resolution image decoding out of composer rerenders. */
106
export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
117
file,
12-
mode = "square",
138
alt,
149
className,
1510
fallback,
1611
}: {
1712
file: File;
18-
mode?: ComposerImageThumbnailMode;
1913
alt: string;
2014
className: string;
2115
fallback: ReactNode;
2216
}) {
23-
const [preview, setPreview] = useState<{
24-
file: File;
25-
mode: ComposerImageThumbnailMode;
26-
src: string | null;
27-
} | null>(null);
17+
const [preview, setPreview] = useState<{ file: File; src: string | null } | null>(null);
2818
useEffect(() => {
2919
let active = true;
30-
void createComposerImageThumbnail(file, mode).then((src) => {
31-
if (active) setPreview({ file, mode, src });
20+
void createComposerImageThumbnail(file).then((src) => {
21+
if (active) setPreview({ file, src });
3222
});
3323
return () => {
3424
active = false;
3525
};
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-
);
26+
}, [file]);
27+
const src = preview?.file === file ? preview.src : null;
28+
return src ? <img src={src} alt={alt} className={className} /> : fallback;
4729
});

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

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -178,61 +178,6 @@ 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-
236181
it("releases the decoded image when thumbnail encoding fails", async () => {
237182
const close = vi.fn();
238183
vi.stubGlobal(

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

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

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];
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);
224211
if (cached) return cached;
225212
const thumbnail = (async () => {
226213
if (!canRecompress()) return null;
227214
let bitmap: ImageBitmap | undefined;
228215
try {
229216
bitmap = await createImageBitmap(file);
230-
if (bitmap.width <= 0 || bitmap.height <= 0) return null;
231217
const side = Math.min(bitmap.width, bitmap.height);
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);
218+
if (side <= 0) return null;
219+
const dimension = Math.min(256, side);
220+
const surface = createCanvas(dimension, dimension);
239221
if (!surface) return null;
240222
surface.context.drawImage(
241223
bitmap,
242-
(bitmap.width - sourceWidth) / 2,
243-
(bitmap.height - sourceHeight) / 2,
244-
sourceWidth,
245-
sourceHeight,
224+
(bitmap.width - side) / 2,
225+
(bitmap.height - side) / 2,
226+
side,
227+
side,
246228
0,
247229
0,
248-
width,
249-
height,
230+
dimension,
231+
dimension,
250232
);
251233
const blob = await encodeCanvas(surface.canvas, 1, "image/png");
252234
return blob ? await readFileAsDataUrl(blob) : null;
@@ -256,7 +238,7 @@ export function createComposerImageThumbnail(
256238
bitmap?.close();
257239
}
258240
})();
259-
composerThumbnails.set(file, { ...fileThumbnails, [mode]: thumbnail });
241+
composerThumbnails.set(file, thumbnail);
260242
return thumbnail;
261243
}
262244

0 commit comments

Comments
 (0)