// Touch input over one DevTools page session, for phone/tablet review: // a touch-screen emulation switch, one-finger swipes and two-finger pinches. // The events are trusted browser input, so they reach whatever the page puts // under the finger (a Flutter canvas or an iframe platform view alike). import { setTimeout as delay } from 'node:timers/promises'; /** Emulates a touch screen (mobile metrics, 5 touch points). */ export async function enableTouch(send, { width, height }) { await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: true }); await send('Emulation.setTouchEmulationEnabled', { enabled: true, maxTouchPoints: 5 }); } /** The finger a held swipe left down, if any: the next swipe moves it on. */ let held = null; /** * One finger down at [at], held [press] ms (a long press), moved by dx/dy in * [steps] moves [gap] ms apart, lifted unless [hold] (to capture mid-drag; * the next swipe continues from where it stopped instead of touching down). */ export async function swipe(send, at, { dx = 0, dy = 0, steps = 12, gap = 16, press = 0, hold = false } = {}) { const touch = (type, x, y) => send('Input.dispatchTouchEvent', { type, touchPoints: type === 'touchEnd' ? [] : [{ x, y, id: 1 }], }); const from = held ?? at; if (!held) await touch('touchStart', from.x, from.y); if (press > 0) await delay(press); for (let i = 1; i <= steps; i++) { await delay(gap); await touch('touchMove', from.x + (dx * i) / steps, from.y + (dy * i) / steps); } const to = { x: from.x + dx, y: from.y + dy }; if (hold) { held = to; } else { held = null; await touch('touchEnd'); } return { from, to }; } /** Two fingers spread (scale > 1) or closed (< 1) around [at]. */ export async function pinch(send, at, scale) { await send('Input.synthesizePinchGesture', { x: at.x, y: at.y, scaleFactor: scale, relativeSpeed: 300, gestureSourceType: 'touch', }); return at; }