From b1cadbdd76b2aa735701039d8c56d43c869cfb4d Mon Sep 17 00:00:00 2001 From: Eugene Date: Sun, 17 May 2026 18:38:20 +0300 Subject: [PATCH] io/input: do not track scroll events as pointers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Scroll events arrived at pointerQueue.Push and went through pointerOf + deliverEnterLeaveEvents + deliverEvent like Move/Press/Release. The side effect: every scroll created or updated a state.pointers entry, populated p.entered with whatever handlers sat under the wheel position, and overwrote state.cursor based on hit-test at the scroll position. When the platform layer reports scroll with a different PointerID than mouse-move events for the same physical mouse — which the Windows backend does (scrollEvent omits PointerID, defaulting to 0, while pointerUpdate forwards Windows' assigned ID) — the scroll spawns a phantom state.pointers entry. Subsequent moves go to the mouse's "real" entry, so the phantom never receives Leave events, its entered set never empties, and the cleanup at the end of Push keeps it alive. pointerQueue.Frame then runs hit-test for it every frame at the user's last scroll position, threading state.cursor through it after the live pointer's resolution and clobbering it with whatever's under the scroll position. The wheel is positional but isn't a pointer. Treating it as one is the bug. Hit-test inline at the scroll position to find delivery targets, dispatch via deliverEvent (which already handles filter matching, scroll axis clamping, and area-local position), and return without creating or updating a state.pointers entry. Add a router-level test that fails without the fix: a Move sets the cursor over a CursorPointer region, a subsequent Scroll over a CursorText region, and the test asserts the cursor is still CursorPointer. Pre-fix the scroll's deliverEnterLeaveEvents overwrites state.cursor with CursorText. Signed-off-by: Eugene --- io/input/pointer.go | 19 ++++++++++++++++--- io/input/pointer_test.go | 37 +++++++++++++++++++++++++++++++++++++ io/pointer/pointer.go | 4 +++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/io/input/pointer.go b/io/input/pointer.go index c3e399f8..7ba9aa36 100644 --- a/io/input/pointer.go +++ b/io/input/pointer.go @@ -739,6 +739,10 @@ func (q *pointerQueue) Push(handlers map[event.Tag]*handler, state pointerState, state.pointers = nil return state, evts } + if e.Kind == pointer.Scroll { + // Scroll events are not bound to a pointer; see pointer.Event.PointerID. + return state, q.deliverScrollEvent(handlers, evts, e) + } state, pidx := state.pointerOf(e) p := state.pointers[pidx] @@ -761,9 +765,6 @@ func (q *pointerQueue) Push(handlers map[event.Tag]*handler, state pointerState, p.pressed = false p, evts, state.cursor, _ = q.deliverEnterLeaveEvents(handlers, state.cursor, p, evts, e) p, evts = q.deliverDropEvent(handlers, p, evts) - case pointer.Scroll: - p, evts, state.cursor, _ = q.deliverEnterLeaveEvents(handlers, state.cursor, p, evts, e) - evts = q.deliverEvent(handlers, p, evts, e) default: panic("unsupported pointer event type") } @@ -780,6 +781,18 @@ func (q *pointerQueue) Push(handlers map[event.Tag]*handler, state pointerState, return state, evts } +// deliverScrollEvent delivers scroll events to the handlers hit by the event coordinate. +func (q *pointerQueue) deliverScrollEvent(handlers map[event.Tag]*handler, evts []taggedEvent, e pointer.Event) []taggedEvent { + var hits []event.Tag + q.hitTest(e.Position, func(n *hitNode) bool { + if _, ok := handlers[n.tag]; ok { + hits = addHandler(hits, n.tag) + } + return true + }) + return q.deliverEvent(handlers, pointerInfo{handlers: hits}, evts, e) +} + func (q *pointerQueue) deliverEvent(handlers map[event.Tag]*handler, p pointerInfo, evts []taggedEvent, e pointer.Event) []taggedEvent { if p.pressed && len(p.handlers) == 1 { e.Priority = pointer.Grabbed diff --git a/io/input/pointer_test.go b/io/input/pointer_test.go index fbe92a8a..a5e3b4a9 100644 --- a/io/input/pointer_test.go +++ b/io/input/pointer_test.go @@ -1345,3 +1345,40 @@ func events(r *Router, n int, filters ...event.Filter) []event.Event { } return events } + +// TestPointerScrollDoesNotTrackPointer queues two events over two cursor +// regions. The Move puts the live pointer over the button (CursorPointer); +// the Scroll happens over the cell (CursorText) and must not update the +// cursor. +func TestPointerScrollDoesNotTrackPointer(t *testing.T) { + var ops op.Ops + + button := clip.Rect(image.Rect(0, 0, 50, 50)).Push(&ops) + pointer.CursorPointer.Add(&ops) + button.Pop() + + cell := clip.Rect(image.Rect(100, 0, 200, 50)).Push(&ops) + pointer.CursorText.Add(&ops) + cell.Pop() + + var r Router + r.Frame(&ops) + r.Queue( + pointer.Event{ + Kind: pointer.Move, + Source: pointer.Mouse, + Position: f32.Pt(25, 25), + }, + pointer.Event{ + Kind: pointer.Scroll, + Source: pointer.Mouse, + Position: f32.Pt(150, 25), + Scroll: f32.Pt(0, 1), + }, + ) + + if got, want := r.Cursor(), pointer.CursorPointer; got != want { + t.Errorf("got %q, want %q (scroll position must not update the cursor; "+ + "the live pointer's last position is what determines it)", got, want) + } +} diff --git a/io/pointer/pointer.go b/io/pointer/pointer.go index a65f840c..489a8909 100644 --- a/io/pointer/pointer.go +++ b/io/pointer/pointer.go @@ -19,7 +19,9 @@ type Event struct { Source Source // PointerID is the id for the pointer and can be used // to track a particular pointer from Press to - // Release. + // Release. Populated for Press, Release, Move, Drag, + // Enter, Leave, and Cancel; Scroll events are not + // bound to a tracked pointer and leave it zero. PointerID ID // Priority is the priority of the receiving handler // for this event.