feat(editor): two-finger pan, touch hint, loupe, touch-sized handles

useFraming gains twoPointerPan (the editor's pinch also pans by the
midpoint and scales from the spread since the pinch began; the
catalogue passes nothing). A futile one-finger drag shows the R-1
hint unless two fingers already worked in this editor visit (work
store flag). A finger drag of a handle shows the loupe (technique A:
the drawing copied once, data-anno-id stripped, outline on top).
editor-touch.spec covers 1 024, 1 180 and 1 280 px with CDP touch.
measure-drag.ts is the hand-run smoothness measure (MEASURE_DRAG=1).
equineShape moved from editor-zones.spec to support/editor.ts, no
assertion changed.
This commit is contained in:
bchanot committed 2026-11-18 10:54:46 +01:00
1 parent 3a7715486b
commit 85231a2680
18 files changed
+1338 -53

No files matched your search

@@ -135,6 +135,49 @@ function dragActions(
return { start, follow, drop, cancel }
}
type Actions = ReturnType<typeof dragActions>
/**
* The client point of a finger's drag, for the loupe (board 13): set when the drag really
* starts, followed, cleared by the drop and by every cancel. A mouse or pen sets nothing.
*/
function useFinger(drag: RefObject<Drag | null>, actions: Actions) {
const [finger, setFinger] = useState<XY | null>(null)
const touch = useRef(false)
// A mouse drag never set a finger: no state update (and no render) at its end.
const end = () => {
if (!touch.current) return
touch.current = false
setFinger(null)
}
return {
finger,
start: (
pressedAt: XY,
client: XY,
mode: boolean,
vertex: number | null,
byFinger: boolean,
) => {
actions.start(pressedAt, client, mode, vertex)
touch.current = byFinger && drag.current !== null
if (touch.current) setFinger(client)
},
follow: (client: XY) => {
actions.follow(client)
if (touch.current && drag.current) setFinger(client)
},
drop: (client: XY) => {
end()
actions.drop(client)
},
cancel: () => {
end()
actions.cancel()
},
}
}
/**
* The selection moved as a block (`drag_shape`, `drag_point`) or one of its vertices
* (`drag_vertex`): the overlay previews the outline, kept local to the canvas for speed;
@@ -144,7 +187,7 @@ function dragActions(
export function useBlockDrag(input: BlockDragInput) {
const drag = useRef<Drag | null>(null)
const [preview, setPreview] = useState<Geometry | null>(null)
const actions = dragActions(input, drag, setPreview)
const actions = useFinger(drag, dragActions(input, drag, setPreview))
useDropOnChange(input.target, actions.cancel)
return { preview, ...actions }
}
@@ -1,7 +1,15 @@
import type { CSSProperties, KeyboardEvent, PointerEvent } from 'react'
import {
useEffect,
useRef,
type CSSProperties,
type KeyboardEvent,
type PointerEvent,
} from 'react'
import type { Framing as Frame } from '@/catalogue/framing'
import { useFraming } from '@/catalogue/useFraming'
import type { ToolId } from './uiState'
import { usePressGesture, type PressInput } from './usePressGesture'
import { markTwoFingers } from './workStore'
export interface GestureInput extends PressInput {
viewId: string
@@ -50,6 +58,24 @@ function touchStyle(
return { touchAction: none ? 'none' : 'pan-x pan-y' }
}
const sameFrame = (a: Frame, b: Frame) =>
a.scale === b.scale && a.x === b.x && a.y === b.y
/**
* R-1: a framing that changed while two pointers or more were down is a two-finger pan or
* zoom that worked; the editor visit then never shows the two-finger hint again.
*/
function useTwoFingersWorked(framing: Frame, multi: () => boolean): void {
const last = useRef(framing)
const isMulti = useRef(multi)
isMulti.current = multi
useEffect(() => {
const changed = !sameFrame(last.current, framing)
last.current = framing
if (changed && isMulti.current()) markTwoFingers()
}, [framing])
}
/**
* Everything the canvas box listens to: zoom and pan, the press machine (click, drag,
* tap), and the keys. Keys only count when the box itself is the target: Enter on
@@ -62,7 +88,9 @@ export function useCanvasGestures(input: GestureInput) {
// The press machine decides: the drawing pans only while the press is a pan, so one
// press is either a pan or a click, never both (R-8).
panning: () => input.tool === 'select' && press.panning(),
twoPointerPan: true,
})
useTwoFingersWorked(framing.framing, press.multi)
const onKeyDown = (event: KeyboardEvent) => {
if (event.target !== event.currentTarget) return
framing.handlers.onKeyDown(event)
@@ -71,6 +99,7 @@ export function useCanvasGestures(input: GestureInput) {
return {
framing,
drag: press.preview,
finger: press.finger,
boxProps: {
...composedPointer(framing, press),
onKeyDown,
@@ -40,6 +40,8 @@ export interface PressInput extends BlockDragInput {
onHandle: (action: HandleAction) => void
/** A tap on the empty drawing with the select tool. */
onDeselect: () => void
/** A one-finger drag that moved nothing ended (R-1: the two-finger hint). */
onFutileDrag: () => void
}
const clientOf = (event: { clientX: number; clientY: number }): XY => ({
@@ -206,7 +208,9 @@ function applyEffect(f: Feed, effect: PressEffect, event: PointerEvent | null) {
const { action } = effect
const vertex = action.type === 'drag_vertex' ? action.index : null
const mode = action.type === 'drag_mode'
f.drag.start(f.machine.current.pressedAt, clientOf(event), mode, vertex)
const finger = pointerKindOf(event.pointerType) === 'touch'
const { pressedAt } = f.machine.current
f.drag.start(pressedAt, clientOf(event), mode, vertex, finger)
}
}
@@ -222,13 +226,19 @@ const contextOf = (input: PressInput) => ({
moveMode: inMoveMode(),
})
/** A drag that ends having moved nothing (`none` with `futile`): a finger off the handles. */
const isFutile = (press: Press) =>
press.phase === 'drag' && press.action.type === 'none' && press.action.futile
/** One event through the machine and its effect; at idle the press is over. */
function feed(f: Feed, event: PressEvent, pointer: PointerEvent | null) {
const m = f.machine.current
const context = contextOf(f.input)
const step = stepPress(m.press, event, context)
const futile = step.effect?.type === 'drag_end' && isFutile(m.press)
m.press = step.press
applyEffect(f, step.effect, pointer)
if (futile) f.input.onFutileDrag()
if (m.press.phase !== 'idle') return
m.pointers.clear()
m.stop?.()
@@ -358,6 +368,9 @@ export function usePressGesture(input: PressInput) {
const cancel = () => feed(latest.current, { type: 'cancel' }, null)
return {
preview: drag.preview,
finger: drag.finger,
/** Two pointers or more are down: a pan or zoom of the fingers, never a click. */
multi: () => machine.current.press.phase === 'multi',
panning: () => {
const press = machine.current.press
return press.phase === 'drag' && press.action.type === 'pan'
@@ -74,6 +74,8 @@ interface Work {
*/
dropped: ModeWork['kind'] | 'creation' | null
question: DrawingQuestion | null
/** A two-finger pan or zoom worked in this editor visit: the hint never shows again (R-1). */
twoFingers: boolean
}
const NO_WORK: Work = {
creation: null,
@@ -84,6 +86,7 @@ const NO_WORK: Work = {
chosen: null,
dropped: null,
question: null,
twoFingers: false,
}
export const useAnnotationWork = create<Work>(() => NO_WORK)
@@ -172,6 +175,12 @@ export function dropStale(text: string): void {
})
}
export function markTwoFingers(): void {
if (!useAnnotationWork.getState().twoFingers) {
useAnnotationWork.setState({ twoFingers: true })
}
}
/** Another sheet opened, or the editor closed: nothing of the previous one survives. */
export function resetWork(): void {
useAnnotationWork.setState(NO_WORK)