fix(editor): one press is either a pan or a click
useFraming takes an optional panning gate read on each one-pointer move; the editor passes the press machine's drag-with-pan phase, so a zoomed pen within its 6 px slop or a mouse wiggle no longer pans and clicks. Catalogue: no options, behaviour unchanged. Test added: « a press is either a pan or a click, never both (R-8) » (pen 5 px clicks without pan, pen 8 px pans without click, mouse wiggle clicks without pan). No assertion removed.
This commit is contained in:
1 parent
35e4a4c75a
commit
b2d78f9621
4 files changed
+83
-21
No files matched your search
@@ -25,6 +25,11 @@ type Point = { x: number; y: number }
|
||||
export interface FramingOptions {
|
||||
/** Asked on a one-pointer press; false leaves that drag to the caller (a drawing tool). */
|
||||
canPan?: (event: PointerEvent) => boolean
|
||||
/**
|
||||
* Asked on each one-pointer move: when given, it alone decides whether the move pans (the
|
||||
* caller tells a drag from a click), replacing the 4 px threshold and `canPan`.
|
||||
*/
|
||||
panning?: () => boolean
|
||||
}
|
||||
|
||||
function boxOf(
|
||||
@@ -43,6 +48,15 @@ function distance(a: Point, b: Point): number {
|
||||
return Math.hypot(a.x - b.x, a.y - b.y)
|
||||
}
|
||||
|
||||
/** One-pointer pan: the caller's gate when it gives one, else past the threshold and allowed. */
|
||||
function pansNow(
|
||||
gate: (() => boolean) | undefined,
|
||||
dragged: RefObject<boolean>,
|
||||
allowed: RefObject<boolean>,
|
||||
): boolean {
|
||||
return gate ? gate() : dragged.current && allowed.current
|
||||
}
|
||||
|
||||
/**
|
||||
* Zoom and pan of the canvas box: buttons, a drag with one pointer, a pinch
|
||||
* with two, the wheel (pans when zoomed; with Ctrl, as a trackpad pinch
|
||||
@@ -65,6 +79,8 @@ export function useFraming(
|
||||
const panAllowed = useRef(true)
|
||||
const canPan = useRef(options.canPan)
|
||||
canPan.current = options.canPan
|
||||
const panning = useRef(options.panning)
|
||||
panning.current = options.panning
|
||||
|
||||
useEffect(() => setFraming(FIT), [resetKey])
|
||||
useEffect(() => {
|
||||
@@ -135,7 +151,10 @@ export function useFraming(
|
||||
const factor =
|
||||
distance(now, other) / Math.max(distance(before, other), 1)
|
||||
zoom(factor, at)
|
||||
} else if (others.length === 0 && dragged.current && panAllowed.current) {
|
||||
} else if (
|
||||
others.length === 0 &&
|
||||
pansNow(panning.current, dragged, panAllowed)
|
||||
) {
|
||||
event.currentTarget.setPointerCapture?.(event.pointerId)
|
||||
pan(now.x - before.x, now.y - before.y)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { CSSProperties, KeyboardEvent, PointerEvent } from 'react'
|
||||
import { useFraming } from '@/catalogue/useFraming'
|
||||
import { dragAction, pointerKindOf } from './press'
|
||||
import type { ToolId } from './uiState'
|
||||
import { usePressGesture, type PressInput } from './usePressGesture'
|
||||
|
||||
@@ -60,15 +59,9 @@ function touchStyle(
|
||||
export function useCanvasGestures(input: GestureInput) {
|
||||
const press = usePressGesture(input)
|
||||
const framing = useFraming(input.viewport, input.viewId, {
|
||||
// A one-pointer pan only where the press machine would pan (a mouse or pen off the
|
||||
// movable selection), decided once at pointer-down.
|
||||
canPan: (event) =>
|
||||
input.tool === 'select' &&
|
||||
dragAction(press.target(), {
|
||||
kind: pointerKindOf(event.pointerType),
|
||||
movable: input.target !== null,
|
||||
moveMode: false,
|
||||
}).type === 'pan',
|
||||
// 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(),
|
||||
})
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
if (event.target !== event.currentTarget) return
|
||||
|
||||
@@ -120,7 +120,7 @@ function runClick(action: ClickAction, input: PressInput): void {
|
||||
|
||||
interface Machine {
|
||||
press: Press
|
||||
/** The press's target, read by the framing's pan decision at pointer-down. */
|
||||
/** The press's target, kept for the capture decision at pointer-down. */
|
||||
target: PressTarget
|
||||
/** The pointers pressed on the box; one pressed outside it is never counted (no release). */
|
||||
pointers: Set<number>
|
||||
@@ -285,7 +285,7 @@ const IDLE: Press = { phase: 'idle' }
|
||||
/**
|
||||
* The editor's one decider for the drawing (U2 R-8): every pointer event goes through the
|
||||
* press machine, whose effects select, deselect, place with a drawing tool, or move the
|
||||
* selection as a block. `target()` is the press's target, for the framing's pan decision.
|
||||
* selection as a block. `panning()` gates the framing's one-pointer pan.
|
||||
*/
|
||||
export function usePressGesture(input: PressInput) {
|
||||
const drag = useBlockDrag(input)
|
||||
@@ -306,7 +306,10 @@ export function usePressGesture(input: PressInput) {
|
||||
const cancel = () => feed(latest.current, { type: 'cancel' }, null)
|
||||
return {
|
||||
preview: drag.preview,
|
||||
target: () => machine.current.target,
|
||||
panning: () => {
|
||||
const press = machine.current.press
|
||||
return press.phase === 'drag' && press.action.type === 'pan'
|
||||
},
|
||||
handlers: {
|
||||
onPointerDown: (event: PointerEvent) => onPointerDown(latest, event),
|
||||
onPointerMove: (event: PointerEvent) =>
|
||||
|
||||
Reference in new issue
Block a user