Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(image): pinch to zoom opened images on touch devices (#9346)

Peter Chinmanfdd7fd21fe07
diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md
index be29ed7612..1015c7750b 100644
--- a/apps/web/AGENTS.md
+++ b/apps/web/AGENTS.md
@@ -54,6 +54,12 @@ It uses the real records view with a local source and picker to exercise opening
replacement, cancellation, keyboard editing, and read-only references. Navigation
adapter tests separately check file types, access failures, and split selection.
+Pinch zoom on opened image files (Drive details and image splits) has a
+backend-free phone and tablet regression:
+`bunx playwright test --config src/features/block-image/browser-test/playwright.config.ts`.
+Keep the zoomed image laid out at its zoomed size, translating only: WebKit
+rasterizes an `<img>` at its layout size, so a `scale()` transform blurs it.
+
Discussion link scrolling has a backend-free browser regression:
`bunx playwright test --config src/lib/core/messages/browser-test/playwright.config.ts`.
diff --git a/apps/web/src/features/block-image/browser-test/index.html b/apps/web/src/features/block-image/browser-test/index.html
new file mode 100644
index 0000000000..6ff689ba6b
--- /dev/null
+++ b/apps/web/src/features/block-image/browser-test/index.html
@@ -0,0 +1 @@
+<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover, user-scalable=no"><title>Image pinch zoom regression</title></head><body><div id="root"></div><script type="module" src="/main.tsx"></script></body></html>
diff --git a/apps/web/src/features/block-image/browser-test/main.tsx b/apps/web/src/features/block-image/browser-test/main.tsx
new file mode 100644
index 0000000000..67aced8671
--- /dev/null
+++ b/apps/web/src/features/block-image/browser-test/main.tsx
@@ -0,0 +1,37 @@
+import { For } from 'solid-js';
+import { render } from 'solid-js/web';
+import { ImageContent } from '../component/ImageContent';
+import '../../../index.css';
+
+// A viewBox-only SVG, as most exported SVGs are: no intrinsic size, so
+// browsers report it as 300×150.
+const SVG = new Blob(
+ [
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 200"><rect width="400" height="200" fill="#3b82f6"/><circle cx="200" cy="100" r="60" fill="#f59e0b"/><text x="200" y="102" font-size="5" text-anchor="middle">fine detail</text></svg>',
+ ],
+ { type: 'image/svg+xml' }
+);
+
+const root = document.getElementById('root');
+if (!root) throw new Error('Missing fixture root');
+render(
+ () => (
+ <main class="flex h-dvh flex-col bg-page text-ink">
+ <ul
+ data-testid="scroll-list"
+ class="h-24 shrink-0 overflow-y-auto border-b border-edge"
+ >
+ <For each={Array.from({ length: 30 }, (_, i) => i)}>
+ {(i) => <li class="h-8 px-4">Row {i}</li>}
+ </For>
+ </ul>
+ <div
+ data-testid="image-pane"
+ class="relative size-full min-h-0 min-w-0 overflow-hidden"
+ >
+ <ImageContent file={SVG} alt="Diagram" />
+ </div>
+ </main>
+ ),
+ root
+);
diff --git a/apps/web/src/features/block-image/browser-test/pinch-zoom.browser.e2e.ts b/apps/web/src/features/block-image/browser-test/pinch-zoom.browser.e2e.ts
new file mode 100644
index 0000000000..40af803e10
--- /dev/null
+++ b/apps/web/src/features/block-image/browser-test/pinch-zoom.browser.e2e.ts
@@ -0,0 +1,167 @@
+import { type CDPSession, expect, type Page, test } from '@playwright/test';
+
+type Point = { x: number; y: number };
+
+async function touch(
+ cdp: CDPSession,
+ type: 'touchStart' | 'touchMove' | 'touchEnd' | 'touchCancel',
+ points: Point[]
+) {
+ await cdp.send('Input.dispatchTouchEvent', {
+ type,
+ touchPoints: points.map((point, id) => ({ ...point, id })),
+ });
+}
+
+/** Moves each finger from its start to its end point in even steps. */
+async function drag(cdp: CDPSession, from: Point[], to: Point[]) {
+ await touch(cdp, 'touchStart', from);
+ const steps = 10;
+ for (let step = 1; step <= steps; step++) {
+ await touch(
+ cdp,
+ 'touchMove',
+ from.map((start, i) => ({
+ x: start.x + ((to[i].x - start.x) * step) / steps,
+ y: start.y + ((to[i].y - start.y) * step) / steps,
+ }))
+ );
+ }
+ await touch(cdp, 'touchEnd', []);
+}
+
+/** Two fingers spreading or closing from `from` to `to` px off the center. */
+async function pinch(
+ cdp: CDPSession,
+ center: Point,
+ from: number,
+ to: number,
+ axis: 'x' | 'y' = 'x'
+) {
+ const at = (offset: number): Point =>
+ axis === 'x'
+ ? { x: center.x + offset, y: center.y }
+ : { x: center.x, y: center.y + offset };
+ await drag(cdp, [at(-from), at(from)], [at(-to), at(to)]);
+}
+
+async function imageBox(page: Page) {
+ const box = await page.getByAltText('Diagram').boundingBox();
+ if (!box) throw new Error('Image is not rendered');
+ return box;
+}
+
+async function paneCenter(page: Page): Promise<Point> {
+ const box = await page.getByTestId('image-pane').boundingBox();
+ if (!box) throw new Error('Image pane is not rendered');
+ return { x: box.x + box.width / 2, y: box.y + box.height / 2 };
+}
+
+test.beforeEach(async ({ page }) => {
+ await page.goto('/');
+ // Unzoomed, the viewBox-only SVG fills the pane's width, as the browser lays
+ // it out, rather than its reported 300×150 natural size.
+ const pane = await page.getByTestId('image-pane').boundingBox();
+ if (!pane) throw new Error('Image pane is not rendered');
+ await expect
+ .poll(async () => Math.round((await imageBox(page)).width))
+ .toBe(Math.round(pane.width));
+});
+
+test('pinching zooms an opened image in and back out', async ({ page }) => {
+ const cdp = await page.context().newCDPSession(page);
+ const center = await paneCenter(page);
+ const fitted = await imageBox(page);
+
+ await pinch(cdp, center, 30, 90);
+ const zoomed = await imageBox(page);
+ expect(zoomed.width).toBeGreaterThan(fitted.width * 2.5);
+ expect(zoomed.height / zoomed.width).toBeCloseTo(
+ fitted.height / fitted.width,
+ 2
+ );
+ // Laid out at its zoomed size rather than scaled up by a transform, which
+ // WebKit renders blurry.
+ const layoutWidth = await page
+ .getByAltText('Diagram')
+ .evaluate((image: HTMLImageElement) => image.offsetWidth);
+ expect(layoutWidth).toBeCloseTo(zoomed.width, 0);
+
+ await pinch(cdp, center, 90, 10);
+ const restored = await imageBox(page);
+ expect(restored.width).toBeCloseTo(fitted.width, 0);
+ expect(restored.height).toBeCloseTo(fitted.height, 0);
+});
+
+test('pinching beside a small image zooms it', async ({ page }) => {
+ const cdp = await page.context().newCDPSession(page);
+ const center = await paneCenter(page);
+ const pane = await page.getByTestId('image-pane').boundingBox();
+ if (!pane) throw new Error('Image pane is not rendered');
+ const fitted = await imageBox(page);
+
+ // Both fingers land in the empty space above and below the wide image.
+ await pinch(cdp, center, fitted.height / 2 + 15, pane.height / 2 - 10, 'y');
+
+ expect((await imageBox(page)).width).toBeGreaterThan(fitted.width * 1.5);
+});
+
+test('one finger pans only a zoomed image', async ({ page }) => {
+ const cdp = await page.context().newCDPSession(page);
+ const center = await paneCenter(page);
+ const left = { x: center.x - 40, y: center.y };
+ const right = { x: center.x + 40, y: center.y };
+
+ const fitted = await imageBox(page);
+ await drag(cdp, [left], [right]);
+ expect((await imageBox(page)).x).toBeCloseTo(fitted.x, 0);
+
+ await pinch(cdp, center, 20, 120);
+ const zoomed = await imageBox(page);
+ await drag(cdp, [left], [right]);
+ await expect
+ .poll(async () => (await imageBox(page)).x)
+ .toBeGreaterThan(zoomed.x + 30);
+});
+
+test('touches outside the image still scroll', async ({ page }) => {
+ const cdp = await page.context().newCDPSession(page);
+ const list = await page.getByTestId('scroll-list').boundingBox();
+ if (!list) throw new Error('Scroll list is not rendered');
+ const x = list.x + list.width / 2;
+
+ await drag(cdp, [{ x, y: list.y + 80 }], [{ x, y: list.y + 10 }]);
+
+ await expect
+ .poll(() =>
+ page.getByTestId('scroll-list').evaluate((element) => element.scrollTop)
+ )
+ .toBeGreaterThan(0);
+});
+
+test('a cancelled pinch leaves later touches alone', async ({ page }) => {
+ const cdp = await page.context().newCDPSession(page);
+ const center = await paneCenter(page);
+ const list = await page.getByTestId('scroll-list').boundingBox();
+ if (!list) throw new Error('Scroll list is not rendered');
+ const x = list.x + list.width / 2;
+
+ // The system takes over mid-pinch (an iOS edge gesture, say): no touchend.
+ await touch(cdp, 'touchStart', [
+ { x: center.x - 30, y: center.y },
+ { x: center.x + 30, y: center.y },
+ ]);
+ await touch(cdp, 'touchMove', [
+ { x: center.x - 60, y: center.y },
+ { x: center.x + 60, y: center.y },
+ ]);
+ await touch(cdp, 'touchCancel', []);
+
+ await drag(cdp, [{ x, y: list.y + 80 }], [{ x, y: list.y + 10 }]);
+
+ await expect
+ .poll(() =>
+ page.getByTestId('scroll-list').evaluate((element) => element.scrollTop)
+ )
+ .toBeGreaterThan(0);
+});
diff --git a/apps/web/src/features/block-image/browser-test/playwright.config.ts b/apps/web/src/features/block-image/browser-test/playwright.config.ts
new file mode 100644
index 0000000000..74dd15f833
--- /dev/null
+++ b/apps/web/src/features/block-image/browser-test/playwright.config.ts
@@ -0,0 +1,38 @@
+import { fileURLToPath } from 'node:url';
+import { defineConfig, devices } from '@playwright/test';
+
+const directory = fileURLToPath(new URL('.', import.meta.url));
+const webDirectory = fileURLToPath(new URL('../../../../', import.meta.url));
+const port = Number(process.env.IMAGE_BROWSER_PORT ?? 3139);
+export default defineConfig({
+ testDir: directory,
+ testMatch: '*.browser.e2e.ts',
+ outputDir: `${directory}/test-results`,
+ timeout: 30_000,
+ workers: 1,
+ reporter: 'line',
+ use: {
+ launchOptions: {
+ executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,
+ },
+ baseURL: `http://127.0.0.1:${port}`,
+ trace: 'retain-on-failure',
+ screenshot: 'only-on-failure',
+ },
+ webServer: {
+ command: `bunx vite --config src/features/block-image/browser-test/vite.config.ts --port ${port}`,
+ cwd: webDirectory,
+ url: `http://127.0.0.1:${port}`,
+ reuseExistingServer: false,
+ },
+ projects: [
+ {
+ name: 'tablet',
+ use: { ...devices['iPad Mini'], browserName: 'chromium' },
+ },
+ {
+ name: 'mobile',
+ use: { ...devices['iPhone 13'], browserName: 'chromium' },
+ },
+ ],
+});
diff --git a/apps/web/src/features/block-image/browser-test/vite.config.ts b/apps/web/src/features/block-image/browser-test/vite.config.ts
new file mode 100644
index 0000000000..a6d5457a31
--- /dev/null
+++ b/apps/web/src/features/block-image/browser-test/vite.config.ts
@@ -0,0 +1,25 @@
+import { fileURLToPath } from 'node:url';
+import tailwindcss from '@tailwindcss/vite';
+import { defineConfig } from 'vite';
+import solid from 'vite-plugin-solid';
+import solidSvg from 'vite-plugin-solid-svg';
+import tsconfigPaths from 'vite-tsconfig-paths';
+
+const directory = fileURLToPath(new URL('.', import.meta.url));
+const webDirectory = fileURLToPath(new URL('../../../../', import.meta.url));
+export default defineConfig({
+ root: directory,
+ cacheDir: `${directory}/.vite`,
+ plugins: [
+ tailwindcss(),
+ solid(),
+ solidSvg({ defaultAsComponent: true }),
+ tsconfigPaths({ projects: [`${webDirectory}/tsconfig.json`] }),
+ ],
+ resolve: {
+ dedupe: ['solid-js'],
+ alias: { 'loro-crdt': 'loro-crdt/base64' },
+ },
+ optimizeDeps: { esbuildOptions: { target: 'esnext' } },
+ server: { host: '127.0.0.1', strictPort: true },
+});
diff --git a/apps/web/src/features/block-image/component/ImageContent.tsx b/apps/web/src/features/block-image/component/ImageContent.tsx
index f9262ab045..de551b0e39 100644
--- a/apps/web/src/features/block-image/component/ImageContent.tsx
+++ b/apps/web/src/features/block-image/component/ImageContent.tsx
@@ -1,3 +1,10 @@
+import { SWIPE_EDGE_THRESHOLD } from '@components/app/split-layout/mobile/createMobileSplitMotion';
+import { createMomentum } from '@core/component/Lightbox/createMomentum';
+import { createZoomModel } from '@core/component/Lightbox/createZoomModel';
+import { Zoompinch, type ZoompinchHandle } from '@core/component/Zoompinch';
+import { isTouchDevice } from '@core/mobile/isTouchDevice';
+import { createResizeObserver } from '@solid-primitives/resize-observer';
+import { cn } from '@ui';
import { createEffect, createSignal, on, onCleanup, Show } from 'solid-js';
export function ImageContent(props: { file: Blob; alt: string }) {
@@ -20,14 +27,172 @@ export function ImageContent(props: { file: Blob; alt: string }) {
fallback={<div class="size-full" aria-label="Loading image" />}
>
{(url) => (
- <div class="flex size-full items-center justify-center">
- <img
- src={url()}
- alt={props.alt}
- class="max-h-full max-w-full object-contain"
- />
- </div>
+ <Show
+ when={isTouchDevice()}
+ fallback={
+ <div class="flex size-full items-center justify-center">
+ <img
+ src={url()}
+ alt={props.alt}
+ class="max-h-full max-w-full object-contain"
+ />
+ </div>
+ }
+ >
+ <PinchZoomImage src={url()} alt={props.alt} />
+ </Show>
)}
</Show>
);
}
+
+/**
+ * Touch image view with pinch-to-zoom and panning. The viewport disables
+ * native page zoom, so the zoom comes from the lightbox's zoom model: a pinch
+ * grows the image until it fills the pane, then crops and pans.
+ */
+function PinchZoomImage(props: { src: string; alt: string }) {
+ const [zoompinchHandle, setZoompinchHandle] = createSignal<
+ ZoompinchHandle | undefined
+ >();
+ const [restingImage, setRestingImage] = createSignal<HTMLImageElement>();
+ const zoom = createZoomModel(zoompinchHandle);
+ const momentum = createMomentum(zoompinchHandle);
+
+ // The unzoomed size is the browser's own layout of the image in the pane,
+ // not its natural size: an SVG with only a viewBox reports 300×150 but
+ // stretches to fill. Following that layout also re-fits the image, and resets
+ // the zoom, when the pane resizes.
+ createResizeObserver(restingImage, ({ width, height }) => {
+ if (width && height) zoom.setBase({ w: width, h: height });
+ });
+
+ // WebKit rasterizes an <img> at its layout size, so the engine's scale()
+ // transform blurs a zoomed image, an SVG most of all. Keep the engine's
+ // gesture math, but lay the image out at its zoomed size and only translate.
+ let zoomedImage: HTMLImageElement | undefined;
+ const onEngineUpdate = (engine: ZoompinchHandle['engine']) => {
+ zoom.onEngineUpdate(engine);
+ if (!zoomedImage) return;
+ const x = engine.offset.left + engine.translateX;
+ const y = engine.offset.top + engine.translateY;
+ const scale = engine.naturalScale * engine.scale;
+ engine.canvasElement.style.transform = `translate(${x}px, ${y}px)`;
+ zoomedImage.style.width = `${engine.canvasBounds.width * scale}px`;
+ zoomedImage.style.height = `${engine.canvasBounds.height * scale}px`;
+ };
+
+ // Zoompinch's touchmove/touchend listeners are on the window. Only touches
+ // in the pane are handled, and a single finger only once zoomed in and away
+ // from the left edge, so taps and the pane's swipe-back keep working.
+ let containerElement: HTMLDivElement | undefined;
+ let handlingTouch = false;
+ const startHandling = (e: TouchEvent, engine: ZoompinchHandle['engine']) => {
+ handlingTouch = true;
+ momentum.cancelMomentum();
+ engine.handleTouchstart(e);
+ momentum.resetMomentumTracking(e, engine);
+ };
+ // Fingers around a small image land beside it, outside Zoompinch's own
+ // touchstart listener: take over once two fingers are down in the pane.
+ const isPinchInPane = (e: TouchEvent) =>
+ e.touches.length > 1 &&
+ Array.from(e.touches).every(
+ (t) => t.target instanceof Node && containerElement?.contains(t.target)
+ );
+ // Zoompinch doesn't listen for touchcancel, which the system sends when it
+ // takes over a gesture. Without a reset, the next touch anywhere, even one
+ // scrolling outside the pane, would go to the engine.
+ const cancelTouch = (e: TouchEvent) => {
+ if (!handlingTouch) return;
+ handlingTouch = false;
+ zoompinchHandle()?.engine.handleTouchend(e);
+ };
+ window.addEventListener('touchcancel', cancelTouch);
+ onCleanup(() => window.removeEventListener('touchcancel', cancelTouch));
+ const touch = {
+ onStart: (e: TouchEvent, engine: ZoompinchHandle['engine']) => {
+ momentum.cancelMomentum();
+ handlingTouch = false;
+ const canPan =
+ zoom.totalZoom() > 1.01 && e.touches[0].clientX >= SWIPE_EDGE_THRESHOLD;
+ if (e.touches.length > 1 || canPan) startHandling(e, engine);
+ },
+ onWindowMove: (e: TouchEvent, engine: ZoompinchHandle['engine']) => {
+ if (!handlingTouch) {
+ if (isPinchInPane(e)) startHandling(e, engine);
+ return;
+ }
+ engine.handleTouchmove(e);
+ // A single-finger pan can fling; a pinch shouldn't.
+ if (e.touches.length === 1) momentum.sampleMomentum(e);
+ else momentum.resetMomentumTracking(e, engine);
+ },
+ onWindowEnd: (e: TouchEvent, engine: ZoompinchHandle['engine']) => {
+ if (!handlingTouch) return;
+ engine.handleTouchend(e);
+ if (e.touches.length > 0) {
+ momentum.resetMomentumTracking(e, engine);
+ return;
+ }
+ handlingTouch = false;
+ if (zoom.totalZoom() > 1.01) momentum.startMomentum(e, engine);
+ },
+ };
+
+ return (
+ <div
+ ref={(element) => {
+ containerElement = element;
+ zoom.setContainer(element);
+ }}
+ class="relative flex size-full touch-none items-center justify-center overflow-hidden"
+ >
+ <div
+ aria-hidden="true"
+ class="pointer-events-none invisible absolute inset-0 flex items-center justify-center"
+ >
+ <img
+ ref={setRestingImage}
+ src={props.src}
+ alt=""
+ class="max-h-full max-w-full object-contain"
+ />
+ </div>
+ <Zoompinch
+ handleRef={setZoompinchHandle}
+ clampBounds
+ // Below 1 so a pinch can zoom out past the engine's floor; the zoom
+ // model moves that into the image size and keeps the real floor at 1.
+ minScale={0.1}
+ onUpdate={onEngineUpdate}
+ onWheel={zoom.handleWheel}
+ touch={touch}
+ class={cn('relative overflow-hidden', !zoom.cardSize() && 'size-full')}
+ style={
+ zoom.cardSize() && {
+ width: `${zoom.cardSize()!.w}px`,
+ height: `${zoom.cardSize()!.h}px`,
+ }
+ }
+ canvasStyle={
+ zoom.baseSize() && {
+ width: `${zoom.baseSize()!.w}px`,
+ height: `${zoom.baseSize()!.h}px`,
+ }
+ }
+ >
+ <img
+ ref={zoomedImage}
+ src={props.src}
+ alt={props.alt}
+ class={cn(
+ 'size-full max-w-none object-contain select-none',
+ !zoom.baseSize() && 'invisible'
+ )}
+ style={{ '-webkit-touch-callout': 'none' }}
+ />
+ </Zoompinch>
+ </div>
+ );
+}
diff --git a/apps/web/src/lib/core/component/Lightbox/createZoomModel.ts b/apps/web/src/lib/core/component/Lightbox/createZoomModel.ts
index 17104f1211..9de18744e0 100644
--- a/apps/web/src/lib/core/component/Lightbox/createZoomModel.ts
+++ b/apps/web/src/lib/core/component/Lightbox/createZoomModel.ts
@@ -201,12 +201,16 @@ export function createZoomModel(
else setZoom(1);
};
+ // Adopt a new unzoomed size and snap to the zoom floor.
+ const setBase = (base: Size) => {
+ setBaseSize(base);
+ resetZoom();
+ };
+
// Measure the freshly-loaded image and snap to the zoom floor.
const handleImageLoad = (img: HTMLImageElement) => {
const base = measureBase(img);
- if (!base) return;
- setBaseSize(base);
- resetZoom();
+ if (base) setBase(base);
};
return {
@@ -218,6 +222,7 @@ export function createZoomModel(
applyZoom,
handleWheel,
onEngineUpdate,
+ setBase,
handleImageLoad,
resetZoom,
};
diff --git a/docs/agent-guide/files.md b/docs/agent-guide/files.md
index d99b3bc501..2bc896bc4b 100644
--- a/docs/agent-guide/files.md
+++ b/docs/agent-guide/files.md
@@ -201,6 +201,19 @@ title and navigation goes through the hamburger overlay. Location, search,
filters, expanded folders, list focus, and scroll position are restored when
returning from an opened file.
+## Zooming opened images
+
+On a touch device, open an image file (an SVG, PNG, or JPEG) in Drive or as its
+own split. It rests at the size it always had: no larger than its own pixels,
+and an SVG with only a viewBox fills the pane. Pinch anywhere in the pane, on
+the image or beside it: the image grows until it fills the pane, then crops and
+pans with one finger, with a glide on release. A zoomed SVG stays sharp, because
+the image is laid out at its zoomed size rather than scaled up. One finger on an
+unzoomed image does nothing, so taps and the left-edge swipe back still work.
+Desktop keeps the plain image. The backend-free phone and tablet regression is
+`bunx playwright test --config src/features/block-image/browser-test/playwright.config.ts`
+from `apps/web`.
+
## Reading unrecognized files
A file Macro has no viewer for reads in place when its content is text: one whose