fix(app): D6b pen follows touch, stale press and hover guards (R1, D6b)

This commit is contained in:
bchanot committed 2026-11-12 13:42:01 +01:00
1 parent 132756eecd
commit 6ed90efb3a
2 files changed
+68 -10

No files matched your search

+17 -8
View File
@@ -7,7 +7,7 @@ const ANNOTATED = '[data-anno-id]'
type Point = { x: number; y: number }
/** What a pointer-up decides: act now (mouse, pen), at the click (touch), or not at all. */
/** What a pointer-up decides: act now (mouse), at the click (touch, pen), or not at all. */
export type TapVerdict = 'none' | 'now' | 'on_click'
/** What the click that follows must do: nothing, deselect, or its usual work. */
export type ClickAnswer = 'swallow' | 'deselect' | 'normal'
@@ -41,11 +41,18 @@ const pointOf = (e: PointerEvent): Point => ({ x: e.clientX, y: e.clientY })
type Ref = RefObject<Press>
/** A second finger can land outside the box: only the document sees it. */
/**
* A second finger can land outside the box: only the document sees it. The same pointer
* pressing again means the armed press lost its release (it ended outside the box): stale.
*/
function watchOtherPointers(p: Press): () => void {
const other = (ev: Event) => {
if ((ev as globalThis.PointerEvent).pointerId !== p.pointerId)
p.multi = true
if ((ev as globalThis.PointerEvent).pointerId !== p.pointerId) p.multi = true
else {
p.armed = false
p.stop?.()
p.stop = null
}
}
document.addEventListener('pointerdown', other, true)
return () => document.removeEventListener('pointerdown', other, true)
@@ -77,7 +84,8 @@ function press(ref: Ref, e: PointerEvent): void {
function move(ref: Ref, e: PointerEvent): void {
const p = ref.current
if (!e.isPrimary || !p.armed) return
// A hover (no button, no contact) is not part of the press.
if (!e.isPrimary || !p.armed || e.buttons === 0) return
const now = pointOf(e)
p.moved += Math.hypot(now.x - p.last.x, now.y - p.last.y)
p.last = now
@@ -89,7 +97,7 @@ function verdict(ref: Ref, e: PointerEvent, enabled: boolean): TapVerdict {
const tap = enabled && e.isPrimary && p.armed && still && !p.onAnnotation
if (e.isPrimary) disarm(ref)
if (!tap) return 'none'
if (e.pointerType === 'touch') {
if (e.pointerType !== 'mouse') {
p.touchPending = true
return 'on_click'
}
@@ -116,8 +124,9 @@ function takeClick(ref: Ref, target: EventTarget | null): ClickAnswer {
* from the click's target: the move gesture captures the pointer on the box, which
* retargets the click of a press on the selected marker; and on touch the browser builds
* the click after the lift, on the layout without the selection, and may adjust it toward
* a nearby marker. Mouse and pen act at the pointer-up (a blur commit at mousedown can
* rebuild the drawing and drop the click); touch acts in the tap's own click.
* a nearby marker; a stylus platform builds it the same way. Only the mouse acts at the
* pointer-up (a blur commit at mousedown can rebuild the drawing and drop the click);
* touch and pen act in the tap's own click.
* Refs only: no render follows a press.
*/
export function useEmptyTap() {