|
| 1 | +import type { Page } from "@playwright/test" |
| 2 | + |
| 3 | +/** A divider to drag: its centre, how far it can travel each way (px), and the display's frame period (ms). */ |
| 4 | +export type Divider = { x: number; y: number; left: number; right: number; period: number } |
| 5 | + |
| 6 | +type Leg = { seconds: number; at: (progress: number) => number } |
| 7 | + |
| 8 | +const ease = (from: number, to: number, seconds: number): Leg => ({ |
| 9 | + seconds, |
| 10 | + at: (progress) => from + ((to - from) * (1 - Math.cos(Math.PI * progress))) / 2, |
| 11 | +}) |
| 12 | + |
| 13 | +// Whole cycles, so a jiggle ends where it began. |
| 14 | +const jiggle = (around: number, amplitude: number, cycles: number, seconds: number): Leg => ({ |
| 15 | + seconds, |
| 16 | + at: (progress) => around + amplitude * Math.sin(2 * Math.PI * cycles * progress), |
| 17 | +}) |
| 18 | + |
| 19 | +/** |
| 20 | + * How a hand drags a divider, as offsets from where the drag starts against time, scaled by the room each way: a slow |
| 21 | + * stretch one way, a jiggle there, a fast throw the other way, a slow pull back, a small jiggle and home. 4.95 s. |
| 22 | + */ |
| 23 | +function legs(divider: Divider) { |
| 24 | + const l = -divider.left |
| 25 | + const r = divider.right |
| 26 | + |
| 27 | + return [ |
| 28 | + ease(0, 0.8 * l, 1.2), |
| 29 | + jiggle(0.8 * l, 0.15 * divider.left, 3, 0.75), |
| 30 | + ease(0.8 * l, r, 0.6), |
| 31 | + ease(r, l, 1.5), |
| 32 | + ease(l, 0.4 * l, 0.4), |
| 33 | + jiggle(0.4 * l, 0.1 * divider.left, 3, 0.5), |
| 34 | + ease(0.4 * l, 0, 0.5), |
| 35 | + ] |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Presses the divider, moves the pointer once per display frame along the drag above without letting go, and releases |
| 40 | + * it where it started. strobe's driver has no pointer drag (`page.move` does not hold a button, and its `until: |
| 41 | + * "frame"` waits two animation frames and a round trip per move), so Playwright's mouse drives the drag, from the |
| 42 | + * runner, paced by the display's frame period on the wall clock. A frame the page misses is skipped, as a hand keeps |
| 43 | + * moving while the screen lags. |
| 44 | + */ |
| 45 | +export async function dragDivider(mouse: Page["mouse"], divider: Divider) { |
| 46 | + const path = legs(divider) |
| 47 | + const total = path.reduce((sum, leg) => sum + leg.seconds, 0) |
| 48 | + |
| 49 | + const offset = (seconds: number) => { |
| 50 | + let left = seconds |
| 51 | + |
| 52 | + for (const leg of path) { |
| 53 | + if (left <= leg.seconds) return leg.at(left / leg.seconds) |
| 54 | + left -= leg.seconds |
| 55 | + } |
| 56 | + |
| 57 | + return 0 |
| 58 | + } |
| 59 | + |
| 60 | + await mouse.move(divider.x, divider.y) |
| 61 | + await mouse.down() |
| 62 | + const began = performance.now() |
| 63 | + |
| 64 | + for (let frame = 1; ; frame++) { |
| 65 | + const wait = began + frame * divider.period - performance.now() |
| 66 | + |
| 67 | + if (wait > 0) await new Promise((resolve) => setTimeout(resolve, wait)) |
| 68 | + frame = Math.max(frame, Math.floor((performance.now() - began) / divider.period)) |
| 69 | + const seconds = (performance.now() - began) / 1000 |
| 70 | + |
| 71 | + if (seconds >= total) break |
| 72 | + await mouse.move(divider.x + offset(seconds), divider.y) |
| 73 | + } |
| 74 | + |
| 75 | + await mouse.move(divider.x, divider.y) |
| 76 | + await mouse.up() |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Reads, in the page, where the divider `selector` sits, the room its panel `panel` has to shrink to `min` and grow to |
| 81 | + * the row `row` less `rest` (px), and the display's frame period over 30 frames. Only in setup: it reads layout. |
| 82 | + */ |
| 83 | +export function measureDivider(input: { selector: string; panel: string; row: string; min: number; rest: number }) { |
| 84 | + return new Promise<Divider>((resolve) => { |
| 85 | + const box = document.querySelector(input.selector)!.getBoundingClientRect() |
| 86 | + const width = document.querySelector(input.panel)!.getBoundingClientRect().width |
| 87 | + const row = document.querySelector(input.row)!.getBoundingClientRect().width |
| 88 | + const times: number[] = [] |
| 89 | + |
| 90 | + const tick = (time: number) => { |
| 91 | + times.push(time) |
| 92 | + |
| 93 | + if (times.length < 31) return requestAnimationFrame(tick) |
| 94 | + resolve({ |
| 95 | + x: box.left + box.width / 2, |
| 96 | + y: box.top + box.height / 2, |
| 97 | + left: Math.max(0, Math.min(360, width - input.min - 10)), |
| 98 | + right: Math.max(0, Math.min(160, row - input.rest - width - 10)), |
| 99 | + period: (times[30]! - times[0]!) / 30, |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + requestAnimationFrame(tick) |
| 104 | + }) |
| 105 | +} |
0 commit comments