Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(chat): trackpad pinch zooms the chat in the Mac app (WebKit gestures)

purple39cc75eafdb2
diff --git a/e2e/chat-look.spec.ts b/e2e/chat-look.spec.ts
index 8a629a8b..5fdeb155 100644
--- a/e2e/chat-look.spec.ts
+++ b/e2e/chat-look.spec.ts
@@ -90,6 +90,25 @@ test('Ctrl + wheel zooms the messages only, and the zoom stays', async ({ page }
await expect.poll(() => zoomOf(page)).toBe(1)
})
+test('a trackpad pinch zooms in WebKit, where it arrives as gesture events', async ({ page, browserName }) => {
+ test.skip(browserName !== 'webkit', 'gesture events are WebKit only (the Mac app)')
+ await boot(page)
+ const scope = page.getByTestId('chat-look-chat')
+ const prevented = await scope.evaluate((el) => {
+ const fire = (type: string, scale: number) => {
+ const e = new Event(type, { bubbles: true, cancelable: true })
+ Object.defineProperty(e, 'scale', { value: scale })
+ el.querySelector('.lu-chat-zoom')!.dispatchEvent(e)
+ return e.defaultPrevented
+ }
+ return [fire('gesturestart', 1), fire('gesturechange', 1.25), fire('gesturechange', 1.5), fire('gestureend', 1.5)]
+ })
+ // Prevented: the WebView must not magnify the whole page at the same time.
+ expect(prevented).toEqual([true, true, true, true])
+ await expect.poll(() => zoomOf(page)).toBeCloseTo(1.5, 2)
+ await expect(page.getByTestId('chat-zoom-badge')).toContainText('150%')
+})
+
test('a right click in a zoomed transcript opens the menu at the pointer', async ({ page }) => {
await boot(page)
await page.evaluate(() => localStorage.setItem('lu-chat-look', JSON.stringify({ state: { look: {}, zoom: 1.5, backgrounds: {} }, version: 0 })))
diff --git a/playwright.config.ts b/playwright.config.ts
index 343058d2..4701d752 100644
--- a/playwright.config.ts
+++ b/playwright.config.ts
@@ -80,10 +80,11 @@ export default defineConfig({
isMobile: true,
},
},
- // WebKit is the engine of the macOS app (WKWebView). Only the voice
- // session runs on it: its audio path is the part of the app where the two
- // engines really differ. `npx playwright test --project=webkit`.
- { name: 'webkit', testMatch: /voice-(session|cold-start|thinking)-live\.spec\.ts/, use: { ...devices['Desktop Safari'] } },
+ // WebKit is the engine of the macOS app (WKWebView). Two parts of the app
+ // run on it, where the engines really differ: the voice session (audio
+ // path) and the chat look (3.1.0: a pinch arrives as gesture events, not
+ // as ctrl + wheel; CSS zoom). `npx playwright test --project=webkit`.
+ { name: 'webkit', testMatch: /(voice-(session|cold-start|thinking)-live|chat-look)\.spec\.ts/, use: { ...devices['Desktop Safari'] } },
],
webServer: [
{
diff --git a/src/components/chat/ChatLookScope.tsx b/src/components/chat/ChatLookScope.tsx
index 540d5ca2..7559c36e 100644
--- a/src/components/chat/ChatLookScope.tsx
+++ b/src/components/chat/ChatLookScope.tsx
@@ -3,7 +3,7 @@ import { useChatLookStore } from '../../stores/chatLookStore'
import { useSettingsStore } from '../../stores/settingsStore'
import { useChatStore } from '../../stores/chatStore'
import { highlightPlain } from '../../lib/chat-highlight'
-import { keyZoom, lookVars, pickBackground, wheelZoom, widthFactor, DEFAULT_LOOK } from '../../lib/chat-look'
+import { gestureZoom, keyZoom, lookVars, pickBackground, wheelZoom, widthFactor, DEFAULT_LOOK } from '../../lib/chat-look'
import { sanitizeChatCss } from '../../lib/chat-css-sanitize'
import { readAttachment } from '../../lib/chat-attachments'
import { log } from '../../lib/logger'
@@ -105,8 +105,31 @@ export function ChatLookScope({ variant, className, children }: ScopeProps) {
if (next !== now) setZoom(next)
setBadge(Date.now())
}
+ // WebKit (the Mac app, Safari) sends a pinch as gesture events, not as
+ // ctrl + wheel; without these the pinch did nothing in the Mac app.
+ let atStart = 1
+ const onGestureStart = (e: Event) => {
+ e.preventDefault()
+ atStart = useChatLookStore.getState().zoom
+ }
+ const onGestureChange = (e: Event) => {
+ e.preventDefault()
+ const next = gestureZoom(atStart, (e as Event & { scale?: unknown }).scale)
+ if (next === null) return
+ if (next !== useChatLookStore.getState().zoom) setZoom(next)
+ setBadge(Date.now())
+ }
+ const stop = (e: Event) => e.preventDefault()
el.addEventListener('wheel', onWheel, { passive: false })
- return () => el.removeEventListener('wheel', onWheel)
+ el.addEventListener('gesturestart', onGestureStart, { passive: false })
+ el.addEventListener('gesturechange', onGestureChange, { passive: false })
+ el.addEventListener('gestureend', stop, { passive: false })
+ return () => {
+ el.removeEventListener('wheel', onWheel)
+ el.removeEventListener('gesturestart', onGestureStart)
+ el.removeEventListener('gesturechange', onGestureChange)
+ el.removeEventListener('gestureend', stop)
+ }
}, [setZoom])
// Ctrl/Cmd with + - 0, while this transcript is on screen.
diff --git a/src/lib/__tests__/chat-look.test.ts b/src/lib/__tests__/chat-look.test.ts
index 782663d1..27c2f9ec 100644
--- a/src/lib/__tests__/chat-look.test.ts
+++ b/src/lib/__tests__/chat-look.test.ts
@@ -1,7 +1,7 @@
import { describe, it, expect } from 'vitest'
import {
DEFAULT_LOOK, LOOK_PRESETS, MIN_CONTRAST, PANE, clampZoom, cleanFontName, contrast, keyZoom, lookVars,
- normalizeBackground, normalizeLook, parseHex, pickBackground, readableOn, scenePrompt, wheelZoom, widthFactor,
+ gestureZoom, normalizeBackground, normalizeLook, parseHex, pickBackground, readableOn, scenePrompt, wheelZoom, widthFactor,
} from '../chat-look'
const ref = 'lu-attachment:v1:' + 'a'.repeat(64)
@@ -97,6 +97,13 @@ describe('zoom (C42)', () => {
expect(wheelZoom(0.7, 100)).toBe(0.7)
expect(clampZoom(Number.NaN)).toBe(1)
})
+ it('a WebKit pinch follows the fingers from where it began', () => {
+ expect(gestureZoom(1, 1.5)).toBe(1.5)
+ expect(gestureZoom(1.2, 0.5)).toBe(0.7)
+ expect(gestureZoom(1, 9)).toBe(2)
+ expect(gestureZoom(1, Number.NaN)).toBeNull()
+ expect(gestureZoom(1, undefined)).toBeNull()
+ })
it('keys', () => {
expect(keyZoom(1, '=')).toBe(1.1)
expect(keyZoom(1, '+')).toBe(1.1)
diff --git a/src/lib/chat-look.ts b/src/lib/chat-look.ts
index 7b52c866..7ec727c6 100644
--- a/src/lib/chat-look.ts
+++ b/src/lib/chat-look.ts
@@ -317,6 +317,17 @@ export function wheelZoom(current: number, deltaY: number, deltaMode = 0): numbe
return clampZoom(current * Math.exp(-deltaY * 0.01))
}
+/**
+ * A trackpad pinch in WebKit (the macOS app's WKWebView, Safari). WebKit does
+ * not turn a pinch into ctrl + wheel the way Chromium does; it sends
+ * `gesturestart` / `gesturechange` with `scale` relative to the start of the
+ * gesture. The zoom follows the fingers from where the gesture began.
+ */
+export function gestureZoom(atStart: number, scale: unknown): number | null {
+ if (typeof scale !== 'number' || !Number.isFinite(scale) || scale <= 0) return null
+ return clampZoom(atStart * scale)
+}
+
/** Ctrl/Cmd with + / = / - / 0, as in a browser. */
export function keyZoom(current: number, key: string): number | null {
if (key === '+' || key === '=') return clampZoom(Math.round((current + ZOOM_STEP) * 10) / 10)