feat(file): add trackpad zoom and pan to image previews (#54590)

This commit is contained in:
Kit Langton
2026-10-13 14:19:45 -07:00
committed by GitHub
Unverified
parent 80d43eb707
commit 7497b89b6d
3 changed files with 297 additions and 42 deletions
+124 -1
View File
@@ -86,7 +86,9 @@ test("Git initialization belongs to its session and refreshes after a session sw
await expect(pending).toHaveCount(0)
})
test("a rejected Git init releases its session's button without showing an error in another session", async ({ page }) => {
test("a rejected Git init releases its session's button without showing an error in another session", async ({
page,
}) => {
await openSession(page, {
name: "ReviewInitFailure",
project: { vcs: undefined },
@@ -498,6 +500,127 @@ test("rereads a file each time an artifact link opens it", async ({ page }) => {
/** What the image case's frame probe records on the window. */
type ImageProbe = { problems: string[]; frames: number; running: boolean }
test("image gestures anchor zoom, pan, and reset independently of the pane", async ({ page }) => {
// Some browsers expose no natural dimensions for a viewBox-only SVG.
await page.addInitScript(() => {
for (const name of ["naturalWidth", "naturalHeight"]) {
const original = Object.getOwnPropertyDescriptor(HTMLImageElement.prototype, name)!
Object.defineProperty(HTMLImageElement.prototype, name, {
...original,
get() {
return this.alt === "second.svg" ? 0 : original.get?.call(this)
},
})
}
})
await openSession(page, {
name: "ImageGestures",
fileList: () => [],
fileContent: (path) => ({
content:
path === "second.svg"
? '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1600 1600"><rect width="1600" height="1600" fill="#6699aa"/></svg>'
: '<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="1600"><rect width="1600" height="1600" fill="#6699aa"/></svg>',
}),
pageMessages: () => ({
items: [
{
id: "msg_images",
type: "assistant",
agent: "build",
model: { id: "test", providerID: "opencode" },
content: [{ type: "text", text: "[First](first.svg) [Second](second.svg)" }],
time: { created: 1, completed: 2 },
},
],
}),
})
await page.getByRole("link", { name: "First", exact: true }).click()
const panel = page.locator("#review-panel")
const image = panel.getByRole("img", { name: "first.svg", exact: true })
const stage = panel.locator('[data-slot="artifact-stage"]')
const fit = panel.getByRole("button", { name: "Fit", exact: true })
await expect(image).toBeVisible()
await expect(fit).toBeDisabled()
// Wait for the side pane's opening motion before choosing a gesture anchor.
await image.hover()
const initial = (await image.boundingBox())!
const pane = (await panel.boundingBox())!
const anchor = { x: Math.round(initial.x + initial.width * 0.55), y: Math.round(initial.y + initial.height * 0.55) }
await page.mouse.move(anchor.x, anchor.y)
// A continuous pinch catches anchor drift while the image still fits one axis.
const cdp = await page.context().newCDPSession(page)
await cdp.send("Input.synthesizePinchGesture", {
...anchor,
scaleFactor: 2,
relativeSpeed: 500,
gestureSourceType: "mouse",
})
await cdp.detach()
await expect(fit).toBeEnabled()
const zoomed = (await image.boundingBox())!
expect(zoomed.width).toBeCloseTo(initial.width * 2, 1)
expect((anchor.x - zoomed.x) / zoomed.width).toBeCloseTo((anchor.x - initial.x) / initial.width, 4)
expect((anchor.y - zoomed.y) / zoomed.height).toBeCloseTo((anchor.y - initial.y) / initial.height, 4)
expect(await panel.boundingBox()).toEqual(pane)
expect(await page.evaluate(() => window.visualViewport?.scale)).toBe(1)
await page.mouse.wheel(40, 60)
await expect.poll(async () => (await image.boundingBox())!.x).toBeCloseTo(zoomed.x - 40, 1)
expect((await image.boundingBox())!.y).toBeCloseTo(zoomed.y - 60, 1)
const scrolled = (await image.boundingBox())!
await page.mouse.down()
await page.mouse.move(anchor.x + 60, anchor.y + 40, { steps: 5 })
await page.mouse.up()
await expect(fit).toBeEnabled()
expect((await image.boundingBox())!.x).toBeCloseTo(scrolled.x + 60, 1)
expect((await image.boundingBox())!.y).toBeCloseTo(scrolled.y + 40, 1)
// Touch uses the same captured-pointer path and must not generate a click-to-fit.
const touch = await page.context().newCDPSession(page)
await touch.send("Input.dispatchTouchEvent", { type: "touchStart", touchPoints: [{ x: anchor.x, y: anchor.y }] })
await touch.send("Input.dispatchTouchEvent", {
type: "touchMove",
touchPoints: [{ x: anchor.x - 30, y: anchor.y - 20 }],
})
await touch.send("Input.dispatchTouchEvent", { type: "touchEnd", touchPoints: [] })
await touch.detach()
await expect(fit).toBeEnabled()
expect((await image.boundingBox())!.x).toBeCloseTo(scrolled.x + 30, 1)
expect((await image.boundingBox())!.y).toBeCloseTo(scrolled.y + 20, 1)
// Keyboard-accessible fit restores the exact original geometry.
await fit.focus()
await page.keyboard.press("Enter")
await expect(fit).toBeDisabled()
expect(await image.boundingBox()).toEqual(initial)
expect(
await stage.evaluate((element) =>
element.dispatchEvent(new WheelEvent("wheel", { deltaY: 40, bubbles: true, cancelable: true })),
),
).toBe(true)
await image.click()
await expect(fit).toBeEnabled()
await page.getByRole("link", { name: "Second", exact: true }).click()
await expect(panel.getByRole("img", { name: "second.svg", exact: true })).toBeVisible()
await expect(fit).toBeDisabled()
await panel.getByRole("img", { name: "second.svg", exact: true }).hover()
const second = (await panel.getByRole("img", { name: "second.svg", exact: true }).boundingBox())!
expect(second.width).toBeGreaterThan(0)
expect(second.height).toBeGreaterThan(0)
await page.keyboard.down("Control")
await page.mouse.wheel(0, -50)
await expect(fit).toBeEnabled()
await page.keyboard.up("Control")
await fit.click()
await page.setViewportSize({ width: 1200, height: 700 })
await expect
.poll(async () => {
const frame = (await stage.boundingBox())!
const media = (await panel.getByRole("img", { name: "second.svg", exact: true }).boundingBox())!
return media.width <= frame.width - 47 && media.height <= frame.height - 47
})
.toBe(true)
})
test("image files load into the review panel and keep it painted while loading", async ({ page }) => {
await page.setViewportSize({ width: 960, height: 900 })
const directory = "C:/OpenCode/ReviewImage"
@@ -1,4 +1,15 @@
import { createMemo, createSignal, ErrorBoundary, For, Match, onCleanup, Show, Switch, untrack, type JSX } from "solid-js"
import {
createMemo,
createSignal,
ErrorBoundary,
For,
Match,
onCleanup,
Show,
Switch,
untrack,
type JSX,
} from "solid-js"
import { createStore } from "solid-js/store"
import { Dynamic } from "solid-js/web"
import { makeEventListener } from "@solid-primitives/event-listener"
@@ -38,8 +49,6 @@ type ViewerState = {
readonly reason?: string
}
type ImageZoom = { readonly url: string; readonly zoom: "fit" | "actual"; readonly overflow: boolean }
type MediaProps = {
path: string
content: FileContent
@@ -295,31 +304,92 @@ function createBlobUrl(content: () => FileContent) {
})
}
/** Images and SVG previews: fit the pane, click to inspect at 1:1 when the image is larger. */
/** Images and SVG previews: each decoded image owns its viewport. */
function ArtifactImage(props: MediaProps) {
const url = createBlobUrl(() => props.content)
const [size, setSize] = createStore({ width: 0, height: 0 })
// Zoom belongs to one image: a new one starts fitted.
const fitted = (): ImageZoom => ({ url: url(), zoom: "fit", overflow: false })
const [saved, setSaved] = createSignal(fitted())
const state = () => {
const value = saved()
return (
<Show when={url()} keyed>
{(src) => <ImageStage {...props} src={src} />}
</Show>
)
}
return value.url === url() ? value : fitted()
function ImageStage(props: MediaProps & { src: string }) {
const ctx = useExtension()
const [view, setView] = createStore({
width: 0,
height: 0,
imageWidth: 0,
imageHeight: 0,
zoom: 1,
x: 0,
y: 0,
dragging: false,
})
const fit = () =>
Math.min(
1,
Math.max(1, view.width - 48) / (view.imageWidth || 1),
Math.max(1, view.height - 48) / (view.imageHeight || 1),
)
const bound = (offset: number, image: number, zoom: number) => {
// Keep the fitted footprint covered, including its letterboxing. Clamping to
// the stage edges instead would move the anchor during small pinch increments.
const limit = (image * fit() * (zoom - 1)) / 2
return Math.max(-limit, Math.min(limit, offset))
}
let stage: HTMLDivElement | undefined
const x = () => bound(view.x, view.imageWidth, view.zoom)
const y = () => bound(view.y, view.imageHeight, view.zoom)
const reset = () => setView({ zoom: 1, x: 0, y: 0 })
const measure = () => {
const pan = (left: number, top: number) =>
setView({ x: bound(left, view.imageWidth, view.zoom), y: bound(top, view.imageHeight, view.zoom) })
let stage: HTMLDivElement | undefined
let drag: { id: number; clientX: number; clientY: number; x: number; y: number } | undefined
let dragged = false
const cursor = () => {
if (view.zoom === 1) return undefined
return view.dragging ? "grabbing" : "grab"
}
const endDrag = (event: PointerEvent) => {
if (event.pointerId !== drag?.id) return
drag = undefined
setView("dragging", false)
}
const zoomAt = (zoom: number, clientX: number, clientY: number) => {
if (!stage) return
setSaved({ ...state(), overflow: size.width > stage.clientWidth - 48 || size.height > stage.clientHeight - 48 })
const rect = stage.getBoundingClientRect()
const next = Math.max(1, Math.min(Math.max(8, 1 / fit()), zoom))
const ratio = next / view.zoom
const anchor = { x: clientX - rect.left - rect.width / 2, y: clientY - rect.top - rect.height / 2 }
setView({
zoom: next,
x: bound(anchor.x - (anchor.x - x()) * ratio, view.imageWidth, next),
y: bound(anchor.y - (anchor.y - y()) * ratio, view.imageHeight, next),
})
}
createResizeObserver(
() => stage,
() => measure(),
() => {
if (!stage) return
setView({ width: stage.clientWidth, height: stage.clientHeight })
setView({ x: x(), y: y() })
},
)
return (
@@ -327,37 +397,98 @@ function ArtifactImage(props: MediaProps) {
ref={stage}
data-slot="artifact-stage"
data-checker
data-zoom={state().zoom}
data-overflow={state().overflow || undefined}
class="relative min-h-0 flex-1 overflow-auto"
data-zoom={view.zoom === 1 ? "fit" : "actual"}
data-overflow={fit() < 1 || undefined}
class="relative min-h-0 flex-1 overflow-hidden"
on:wheel={(event) => {
if (!view.imageWidth || (!event.ctrlKey && view.zoom === 1)) return
// Chromium delivers trackpad pinch as Ctrl-wheel. Consume it here before
// Desktop's window-level zoom handler; ordinary fitted scrolling stays native.
event.preventDefault()
event.stopPropagation()
const unit = [1, 16, view.height][event.deltaMode] ?? 1
if (event.ctrlKey) {
zoomAt(view.zoom * Math.exp((-event.deltaY * unit) / 100), event.clientX, event.clientY)
return
}
// Use the OS's trackpad momentum directly, with no second animation loop.
pan(x() - event.deltaX * unit, y() - event.deltaY * unit)
}}
>
<div
classList={{
"absolute inset-0 flex items-center justify-center p-6": state().zoom === "fit",
"flex min-h-full min-w-full w-max items-center justify-center p-6": state().zoom === "actual",
<img
data-slot="artifact-media"
src={props.src}
alt={getFilename(props.path)}
draggable={false}
class="absolute left-1/2 top-1/2 select-none"
style={{
width: view.imageWidth ? `${view.imageWidth * fit()}px` : undefined,
height: view.imageHeight ? `${view.imageHeight * fit()}px` : undefined,
"max-width": "none",
"max-height": "none",
transform: `translate(-50%, -50%) translate(${x()}px, ${y()}px) scale(${view.zoom})`,
"touch-action": view.zoom > 1 ? "none" : "auto",
cursor: cursor(),
}}
>
<img
data-slot="artifact-media"
src={url()}
alt={getFilename(props.path)}
draggable={false}
onError={() => props.onError()}
onLoad={(event) => {
const image = event.currentTarget
onPointerDown={(event) => {
if (!event.isPrimary || event.button !== 0) return
setSize({ width: image.naturalWidth, height: image.naturalHeight })
props.onInfo({ width: image.naturalWidth, height: image.naturalHeight })
measure()
}}
onClick={() => {
const value = state()
dragged = false
if (!value.overflow && value.zoom === "fit") return
if (view.zoom === 1) return
setSaved({ ...value, zoom: value.zoom === "fit" ? "actual" : "fit" })
}}
/>
drag = { id: event.pointerId, clientX: event.clientX, clientY: event.clientY, x: x(), y: y() }
event.currentTarget.setPointerCapture(event.pointerId)
setView("dragging", true)
}}
onPointerMove={(event) => {
if (!drag || drag.id !== event.pointerId) return
const dx = event.clientX - drag.clientX
const dy = event.clientY - drag.clientY
if (!dragged && Math.hypot(dx, dy) < 3) return
dragged = true
pan(drag.x + dx, drag.y + dy)
}}
onPointerUp={endDrag}
onPointerCancel={endDrag}
onLostPointerCapture={endDrag}
onError={() => props.onError()}
onLoad={(event) => {
const image = event.currentTarget
const width = image.naturalWidth || image.width || image.clientWidth || 300
const height = image.naturalHeight || image.height || image.clientHeight || 150
setView({
imageWidth: width,
imageHeight: height,
width: stage?.clientWidth ?? 0,
height: stage?.clientHeight ?? 0,
})
props.onInfo({ width, height })
}}
onClick={(event) => {
if (dragged) {
dragged = false
return
}
if (view.zoom > 1) return reset()
zoomAt(1 / fit(), event.clientX, event.clientY)
}}
/>
<div class="absolute end-3 top-3">
<Button size="small" variant="neutral" disabled={view.zoom === 1} onClick={reset}>
{ctx.t("view.fit")}
</Button>
</div>
</div>
)
@@ -7,6 +7,7 @@ export default {
"mobile.title": "Files",
"mobile.openTabs": "Open files",
"view.preview": "Preview",
"view.fit": "Fit",
"view.source": "Source",
"view.openInBrowser": "Open in browser",
"view.fullscreen": "Enter full screen",