From ea6f9e138ce68834ffefa1391cae4e3d2686ccfd Mon Sep 17 00:00:00 2001 From: bincxz <16399091+binaricat@users.noreply.github.com> Date: Thu, 19 Mar 2026 19:33:51 +0800 Subject: [PATCH] feat: support mouse wheel zoom in image preview Scroll up to zoom in, scroll down to zoom out (10% per tick, range 25%-200%). Uses zoomRef to avoid stale closures so wheel + drag always read the latest zoom level. Co-Authored-By: Claude Opus 4.6 (1M context) --- components/ai/ChatMessageList.tsx | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/components/ai/ChatMessageList.tsx b/components/ai/ChatMessageList.tsx index 3c1bf56f1..ca34ae54b 100644 --- a/components/ai/ChatMessageList.tsx +++ b/components/ai/ChatMessageList.tsx @@ -42,17 +42,31 @@ const ChatMessageList: React.FC = ({ messages, isStreaming imgRef.current.style.transform = `scale(${z / 100}) translate(${x / (z / 100)}px, ${y / (z / 100)}px)`; }, []); - const zoomIn = useCallback(() => setZoom(z => { const nz = Math.min(z + 25, 200); applyTransform(nz, dragPos.current.x, dragPos.current.y, true); return nz; }), [applyTransform]); - const zoomOut = useCallback(() => setZoom(z => { const nz = Math.max(z - 25, 25); applyTransform(nz, dragPos.current.x, dragPos.current.y, true); return nz; }), [applyTransform]); + const zoomRef = useRef(100); + const setZoomAndRef = useCallback((fn: (z: number) => number) => { + setZoom(z => { const nz = fn(z); zoomRef.current = nz; return nz; }); + }, []); + const zoomIn = useCallback(() => setZoomAndRef(z => { const nz = Math.min(z + 25, 200); applyTransform(nz, dragPos.current.x, dragPos.current.y, true); return nz; }), [applyTransform, setZoomAndRef]); + const zoomOut = useCallback(() => setZoomAndRef(z => { const nz = Math.max(z - 25, 25); applyTransform(nz, dragPos.current.x, dragPos.current.y, true); return nz; }), [applyTransform, setZoomAndRef]); + + const onWheel = useCallback((e: React.WheelEvent) => { + e.preventDefault(); + const delta = e.deltaY > 0 ? -10 : 10; + setZoomAndRef(z => { + const nz = Math.max(25, Math.min(200, z + delta)); + applyTransform(nz, dragPos.current.x, dragPos.current.y, false); + return nz; + }); + }, [applyTransform, setZoomAndRef]); const openPreview = useCallback((src: string, name: string) => { - setZoom(100); + setZoom(100); zoomRef.current = 100; setDragged(false); dragPos.current = { x: 0, y: 0 }; setPreview({ src, name }); }, []); const resetPreview = useCallback(() => { - setZoom(100); + setZoom(100); zoomRef.current = 100; setDragged(false); dragPos.current = { x: 0, y: 0 }; applyTransform(100, 0, 0, true); @@ -66,13 +80,12 @@ const ChatMessageList: React.FC = ({ messages, isStreaming const onPointerMove = useCallback((e: React.PointerEvent) => { if (!dragStart.current) return; - // Bail out if primary button is no longer pressed (e.g. released outside window) if ((e.buttons & 1) === 0) { dragStart.current = null; return; } const x = dragStart.current.origX + (e.clientX - dragStart.current.startX); const y = dragStart.current.origY + (e.clientY - dragStart.current.startY); dragPos.current = { x, y }; - applyTransform(zoom, x, y, false); - }, [zoom, applyTransform]); + applyTransform(zoomRef.current, x, y, false); + }, [applyTransform]); const endDrag = useCallback(() => { if (dragStart.current && (dragPos.current.x !== 0 || dragPos.current.y !== 0)) { @@ -299,6 +312,7 @@ const ChatMessageList: React.FC = ({ messages, isStreaming onPointerMove={onPointerMove} onPointerUp={endDrag} onPointerCancel={endDrag} + onWheel={onWheel} onLostPointerCapture={endDrag} >