Skip to content

Commit bb51604

Browse files
committed
Centralize browser pointer authority
Move pointer position tracking from anchors.js to a new dedicated pointer.js module. The shared unrounded pointer record is now created and owned in one place, with event listeners installed at capture phase to ensure all consumers read the same true coordinates before any handler modifies them. This fixes aim's ability to claim a press that is the first pointer event, by guaranteeing the position recorder runs before aim's capture listener.
1 parent ca25e69 commit bb51604

7 files changed

Lines changed: 74 additions & 43 deletions

File tree

‎plugins/leaf/skills/leaf/CLAUDE.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ highlight rules;
8383
`runtime/storage.js` owns page addressing and browser-backed stores;
8484
`runtime/syntax.js` owns code tokenization and highlighting;
8585
`runtime/passages.js` owns the DOM reading and quote resolver;
86+
`runtime/pointer.js` owns the shared unrounded pointer position;
8687
`runtime/geometry.js` owns the shared readings of visible boxes and clipping;
8788
`runtime/navigation.js` owns reader travel and scroller selection;
8889
`runtime/anchors.js` owns anchor resolution, paint, and anchor-specific travel;

‎plugins/leaf/skills/leaf/assets/leaf.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ import {
274274
observeServerNow,
275275
quietSince,
276276
} from "./runtime/presence.js";
277+
import { createPointer } from "./runtime/pointer.js";
277278
import { createReactions } from "./runtime/reactions.js";
278279
import { createStateApplication } from "./runtime/state-application.js";
279280
import { createStateFeed } from "./runtime/state-feed.js";
@@ -355,6 +356,7 @@ const { postEvent, reportPageError, revealLayer, sameLayer } = createLayerClient
355356
currentRevision: () => runtime.currentRevision,
356357
layerGeneration: vendoredLayerGeneration,
357358
});
359+
const { pointerAt } = createPointer();
358360

359361
createMeasurements({ shownBox });
360362

@@ -1441,7 +1443,7 @@ const {
14411443
panel,
14421444
panelCovers,
14431445
pendingMarks: () => anchorRuntime.pendingMarks,
1444-
pointerAt: () => pointer,
1446+
pointerAt,
14451447
reactionTokens: () => reactionTokens(),
14461448
reactionsOn: (anchor) => conversationRuntime.reactionsOn(anchor),
14471449
referenceIsOpen: () => reference.open,
@@ -1466,7 +1468,7 @@ const { AIM, aimIsOn, aimedItem } = createAim({
14661468
itemAt,
14671469
openOnDesign,
14681470
openOnVisual,
1469-
pointerAt: () => pointer,
1471+
pointerAt,
14701472
raiseOnItem,
14711473
refreshAim,
14721474
spell,
@@ -3590,6 +3592,7 @@ anchorRuntime = createAnchors({
35903592
pageWords,
35913593
paintThreadQuotes,
35923594
panel,
3595+
pointerAt,
35933596
quoteFrom,
35943597
queueLegend,
35953598
rangeOf,
@@ -3608,7 +3611,7 @@ anchorRuntime = createAnchors({
36083611
withdraw,
36093612
worksSelector: WORKS,
36103613
});
3611-
const { VIEW_KEY, ITEM, NOTE, marked, placed, pointer } = anchorRuntime;
3614+
const { VIEW_KEY, ITEM, NOTE, marked, placed } = anchorRuntime;
36123615

36133616
createConversationLanding({ scrollToThread });
36143617
createConversationBox({ post, renderPanel, showToast, wireInput });

‎plugins/leaf/skills/leaf/assets/runtime/anchors.js‎

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export function createAnchors(dependencies) {
5959
pageWords,
6060
paintThreadQuotes,
6161
panel,
62+
pointerAt,
6263
quoteFrom,
6364
queueLegend,
6465
rangeOf,
@@ -600,6 +601,7 @@ export function createAnchors(dependencies) {
600601
const item = aimedItem();
601602
return item ? { el: item, part: "" } : null;
602603
}
604+
const pointer = pointerAt();
603605
if (designIsOn() && pointer.x >= 0)
604606
return designTarget(document.elementFromPoint(pointer.x, pointer.y));
605607
return null;
@@ -655,7 +657,6 @@ export function createAnchors(dependencies) {
655657
inspectEl.style.left = `${Math.max(2, corner.left)}px`;
656658
inspectEl.style.top = `${(above >= 0 ? above : corner.top + 2) + pageScroller.scrollTop}px`;
657659
}
658-
const pointer = { x: -1, y: -1 }; // last seen, so a repaint can re-answer the hover
659660
let hovering = null;
660661
let hoverQueued = false;
661662
const marksOf = (id) => marked.get(id) ?? [];
@@ -1261,6 +1262,7 @@ export function createAnchors(dependencies) {
12611262
// panel already says for itself, on the quote that makes it. Unconditional because
12621263
// toggle runs no update step when the answer has not changed, unlike the add that
12631264
// noteMarks and the standing paint have to guard.
1265+
const pointer = pointerAt();
12641266
const onMark = markAt(pointer.x, pointer.y);
12651267
document.body.classList.toggle("lf-over-mark", Boolean(onMark));
12661268
const id = hoveredThreadOf()?.dataset.id ?? onMark;
@@ -1270,32 +1272,9 @@ export function createAnchors(dependencies) {
12701272
if (id !== hovering || hoverCardOf(id) !== hoverThread) paintHover(id);
12711273
});
12721274
}
1273-
// The pointer's place is read off a pointer event and not off `mousemove`, because the
1274-
// legacy mouse events round clientX/clientY to whole pixels and the browser hit-tests the
1275-
// position they were rounded from. Every consumer of this record asks a hit-test question
1276-
// — what a press would take, whether a mark is under the hand — so a rounded point is an
1277-
// answer about somewhere the pointer is not, and within a pixel of a boundary it is a
1278-
// different element: an ⌥ aim outlined the option above the one the press then took,
1279-
// because the outline asked elementFromPoint at the rounded point while the press read
1280-
// the target the browser resolved at the true one.
1281-
document.addEventListener("pointermove", (ev) => {
1282-
pointer.x = ev.clientX;
1283-
pointer.y = ev.clientY;
1284-
refreshHover();
1285-
});
1286-
// A finger arrives already down. A tap dispatches `pointerdown` and then the
1287-
// compatibility mouse events — no `pointermove` anywhere in it — so a record kept from
1288-
// movement alone is still its start value when the click asks, and a consumer with no
1289-
// guard asks elementFromPoint about a point off the page: the quote under the finger
1290-
// opened nothing. The press is the pointer's place too, and for a tap it is the only
1291-
// statement of it: a drag still dispatches `pointermove` until the browser cancels it.
1292-
// No hover refresh with it — a mouse has already moved to this point and refreshed
1293-
// there — which is not a claim that the record paints no hover on touch. pageShifted
1294-
// reads it too, and a drag reached that paint before this recorder existed.
1295-
document.addEventListener("pointerdown", (ev) => {
1296-
pointer.x = ev.clientX;
1297-
pointer.y = ev.clientY;
1298-
});
1275+
// The shared pointer recorder is installed before this listener, so the hover reads the
1276+
// same unrounded point the browser used for the event's hit test.
1277+
document.addEventListener("pointermove", refreshHover);
12991278
// The page moving under a parked pointer is the pointer moving over the page: what a
13001279
// press would take, whether a mark is under the hand, and where every legend box
13011280
// stands can all change with no mouse event to say so, and a box left over the old
@@ -1347,7 +1326,6 @@ export function createAnchors(dependencies) {
13471326
refreshAim,
13481327
dockSeats,
13491328
paintAnchors,
1350-
pointer,
13511329
fragmentId,
13521330
markAt,
13531331
scrollToElement,

‎plugins/leaf/skills/leaf/assets/runtime/composing/aim.js‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ export function createAim({
4040
// is no reason to say nothing: the press still acts (it re-anchors the box), so the
4141
// promise still paints — what stood down here left that one press made blind.
4242
function aimedTarget() {
43-
if (pointerAt().x < 0) return null;
44-
const at = document.elementFromPoint(pointerAt().x, pointerAt().y);
43+
const pointer = pointerAt();
44+
if (pointer.x < 0) return null;
45+
const at = document.elementFromPoint(pointer.x, pointer.y);
4546
if (!at || inChrome(at)) return null;
4647
// The explicit Alt-click aim claims a declared part even inside a native control;
4748
// plain activation and keyboard proxies use visualAt's unclaimed-only default.
@@ -68,12 +69,8 @@ export function createAim({
6869
// carry that same live state, so the move re-derives the arm from the freshest carrier,
6970
// through the one setter, rather than trusting the latch.
7071
document.addEventListener("pointermove", (ev) => {
71-
// This listener used to follow the pointer recorder in the monolith. Keep that
72-
// ordering explicit now that the recorder is installed by the anchor module. On the
73-
// pointer event for the reason the recorder is (anchors.js): `mousemove` carries the
74-
// pointer's place rounded to a whole pixel, and this record answers hit tests.
75-
pointerAt().x = ev.clientX;
76-
pointerAt().y = ev.clientY;
72+
// The shared recorder was installed before this listener, so aimedTarget reads this
73+
// event's unrounded point when setAiming or refreshAim asks for it below.
7774
const held = ev.getModifierState(AIM.modifier);
7875
if (held !== aiming) setAiming(held);
7976
else refreshAim();

‎plugins/leaf/skills/leaf/assets/runtime/composing/surface.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ export function createSelectionSurface({
516516
return;
517517
}
518518
// The record rather than this event's own coordinates, for the reason the record is
519-
// kept from a pointer event at all (anchors.js): `click` is a legacy mouse event and
519+
// kept from a pointer event at all (pointer.js): `click` is a legacy mouse event and
520520
// carries the pointer's place rounded to a whole pixel, while markAt measures against
521521
// getClientRects, whose edges are floats. Asked at the rounded point this answered a
522522
// different thread than refreshHover had just promised at the true one — a quote lit
@@ -525,9 +525,8 @@ export function createSelectionSurface({
525525
// A click with no press behind it carries 0,0 rather than a position — `offer` calls
526526
// click() to supply the keys a span doesn't come with — and the record would answer
527527
// for wherever the pointer is parked, so that one keeps reading the event.
528-
const threadId = ev.detail
529-
? markAt(pointerAt().x, pointerAt().y)
530-
: markAt(ev.clientX, ev.clientY);
528+
const point = ev.detail ? pointerAt() : { x: ev.clientX, y: ev.clientY };
529+
const threadId = markAt(point.x, point.y);
531530
if (threadId) return showThread(threadId);
532531
// Native controls, including links, keep their ordinary activation. visualAt applies
533532
// the same unclaimed-gesture rule used when keyboard proxies are discovered.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* The one unrounded pointer position shared by hit-test consumers. */
2+
export function createPointer() {
3+
let x = -1;
4+
let y = -1;
5+
6+
// Read pointer events rather than legacy mouse events, whose client coordinates are
7+
// rounded away from the point the browser hit-tested. A snapshot keeps consumers from
8+
// becoming another writer of the position they read.
9+
const remember = (ev) => {
10+
x = ev.clientX;
11+
y = ev.clientY;
12+
};
13+
document.addEventListener("pointermove", remember, { capture: true });
14+
// A finger can arrive already down, with no preceding pointermove. The press is its
15+
// position too, and for a tap it is the only statement of it.
16+
document.addEventListener("pointerdown", remember, { capture: true });
17+
18+
return { pointerAt: () => ({ x, y }) };
19+
}

‎tests/test_render_aim.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,40 @@ def test_the_aim_reads_the_pointer_where_the_press_is_dispatched_from(browser, s
144144
page.close()
145145

146146

147+
def test_an_aimed_first_press_records_its_pointer_before_claiming_it(browser, serve):
148+
"""A press can be the first pointer event, and capture still reads its position.
149+
150+
Aim claims pointerdown during capture and stops the gesture before it reaches the
151+
page. The shared position recorder therefore has to run earlier in that same phase:
152+
a bubble listener never sees this event, and aim would ask about the stale initial
153+
point instead of the paragraph the browser dispatched the press to.
154+
"""
155+
page, errors = open_page(browser, serve(LONG_PAGE))
156+
page.locator("#p2").evaluate(
157+
"""target => {
158+
const box = target.getBoundingClientRect();
159+
const init = {
160+
bubbles: true,
161+
clientX: box.left + box.width / 2,
162+
clientY: box.top + box.height / 2,
163+
altKey: true,
164+
};
165+
target.dispatchEvent(new PointerEvent("pointerdown", init));
166+
target.dispatchEvent(new MouseEvent("mousedown", init));
167+
target.dispatchEvent(new PointerEvent("pointerup", init));
168+
target.dispatchEvent(new MouseEvent("mouseup", init));
169+
target.dispatchEvent(new MouseEvent("click", {...init, detail: 1}));
170+
}"""
171+
)
172+
173+
expect(page.locator(".lf-fab-bar")).to_be_visible()
174+
page.locator(".lf-fab").click()
175+
expect(page.locator(".lf-composer")).to_be_visible()
176+
assert page.evaluate(DRAFT_MARK) == "p2"
177+
assert errors == []
178+
page.close()
179+
180+
147181
@pytest.mark.parametrize(
148182
("example", "required_paths"),
149183
AIM_PRESS_CASES,

0 commit comments

Comments
 (0)