Skip to content

Count a scroll as the reader's when it ends, and keep lf-trace's tree places in continuity - #2560

Merged
max-sixty merged 9 commits into
mainfrom
fix-56-reader-scrolls
Oct 12, 2026
Merged

max-sixty merged 9 commits into
mainfrom
fix-56-reader-scrolls

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

The runtime now has one rule for whose a scroll position is, and one keeper for the reader's place in lf-trace's saved-element tree. This is item #56 of the Lit follow-up round (the round's own numbering, not a GitHub issue). It follows #2546.

One rule for the reader's scrolls

A box stands where the reader chose, or where layout put it. Continuity's shift restore (version.js, restoreShifted) should undo only layout's moves. #2546 counted Leaf's landings as the reader's choices. That missed the browser's own scrolls, such as find-in-page opening a closed tree onto its match, and counted a restore's focus reveal as a choice.

The platform already draws this line. A scroll sequence ends with scrollend whoever started it: wheel, keys, scrollTo/scrollBy/scrollTop, smooth motion, scrollIntoView, and fragment navigation, including one that opens a closed <details>. A clamp as content shrinks, a scroll-anchoring adjustment, and a <details> reopening at the offset it kept end none. I measured each case in headless Chromium; the reviewer confirmed the ordering in WebKit.

scrolling.js now owns the rule, and its header states it:

  • A scrollend on a box is the reader's choice (chosenSince).
  • A place keeper's write returns the reader to a place they already had, so it records where it left each box (placeKept). A sequence that ends there is the keeper's. This covers restorePlace, placeKeeper's correction, and continuity's page-band move.
  • A landing in smooth motion counts from its start, since layout it caused can be announced before the motion ends.
  • Continuity takes a reading at each scrollend as well as on scroll. WebKit, and Chromium for touch, end a scroll frames after its last scroll event, and without that reading every reader scroll would look newer than its own reading.

landing-scroll.js loses its own landing counter.

One keeper for the saved-element tree

lf-trace kept a place per checkpoint for its "Saved elements" tree and restored it on a summary press. Continuity restored the same box from its region reading. The two agreed only because focus on the summary made continuity's restore a no-op.

A region can now show one subject of several in turn. Its owner replaces the body's contents inside showReadingSubject(id, subject, render) (reading-regions.js). Continuity keeps a reading per subject and returns the reader to it, both when the subject shows and when the tree opens. A region whose body is a closed <details> is concealed, so continuity no longer records a closed tree's reading. That reading had only the enclosing trace as its landmark, and restoring it scrolled the tree to its top. lf-trace drops #treePlaces, #treeScope, its scroll listener and its summary press handler. readingRegion leaves widget-api.js, since nothing imports it now.

Behaviour changed

  • (c) When find-in-page opens the closed tree onto a match, the match stays shown; before, the shift restore put the tree back where the reader had left it.
  • (b) A restore's own scroll, its focus reveal included, is never the reader's choice, so the next frame's shift restore still corrects that box.
  • (a) Opening the tree, or returning to a checkpoint, puts the reader where they left that checkpoint's tree, through the runtime's keeper. A press now reopens the tree natively. For the same checkpoint, the browser keeps the offset. If the checkpoint changed while the tree was closed, the restore lands one frame after the opening rather than before its paint.
  • Focus on a closed tree's summary no longer makes the tree the region whose reading continuity records.
  • The per-checkpoint readings now live as long as the tree's reading region, so a trace that leaves the document and comes back starts without them; lf-trace's own map had kept them until the archive changed.

Tests

  • test_trace_tree_opened_by_the_browser_keeps_its_match (c) fails on main: the row ends 572px down in a tree showing 54–154px. Headless Chrome has no find bar, and Leaf intercepts fragment navigation, so the page makes the find's two moves itself: it opens the tree and scrolls the row into view natively.
  • test_trace_tree_opens_at_each_checkpoints_reading (a) checks that a checkpoint left scrolled in its tree reopens there after another checkpoint was scrolled elsewhere and the tree was closed between. It fails if the subject's reading is not adopted (488 vs 40) or if the closed tree is not concealed (482 vs 40).
  • test_a_split_drag_keeps_a_panes_reading, in Chromium and WebKit, has the reader wheel-scroll a pane and then drags the split frame by frame, with scroll anchoring off as in Safari. It fails if a keeper's writes count as choices (1487px instead of 102px), and in WebKit it fails if continuity takes no reading at the scroll's end (51px instead of 101px). WebKit leaves the restored passage about 5px off on main too, so the test allows 8px.
  • (b) did not fail on main in any run I made. There, restoreShifted re-records right after its restore, so the counted reveal never predates a reading. The split-drag test guards the general form under the new rule.
  • The Keep a Moment's landing on a saved element over the tree's older readings #2546 saved-element tests share a setup helper with the new ones and pass in both motions.

Line deltas

  • Hand-written source (JS, excluding Markdown): +177 / −98, net +79 (scrolling.js +43, version.js +61/−13, reading-regions.js +28/−4, lf-trace.js +20/−56, landing-scroll.js +6/−15, reading-place.js +11/−6, user-place.js +7/−3, widget-api.js +1/−1).
  • Tests: +160 / −23, net +137.

This was written by Claude Code on behalf of max-sixty

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reading-region export removal breaks an existing browser test and removes a query from the public widget API. The focused pane registration test fails at its first leaf.readingRegion(...) call.

@max-sixty
max-sixty merged commit 4186ca3 into main Oct 12, 2026
4 checks passed
@max-sixty
max-sixty deleted the fix-56-reader-scrolls branch October 12, 2026 09:26
max-sixty added a commit that referenced this pull request Oct 12, 2026
…third review's fixes

#2560 counts a scroll as the reader's when it ends (`chosenSince`), and has a
place keeper's write say where it left a box (`placeKept`). This branch now
answers "did the reader move this box" with that rule alone:

- A hold releases on the reader's next input or a scroll they chose there,
  not on an offset inferred from the resize observer. A script that grows the
  layout and scrolls in the same frame, or the frame after, keeps its scroll
  (gallery: 2450, was snapped back to 2050).
- A held place is applied again only while the shift's layout settles: until
  two frames pass with no layout delivery, at most 12 deliveries. That is what
  re-application is for (a widget drawing a frame late), and past it a place
  put back could undo someone's move.
- A shift leaves a box the reader chose a scroll in since its place was
  recorded, replacing main's `landedSince`; places record on `scrollend` too.
- Subject readings (`showReadingSubject`) are kept per subject in the
  per-scroller places. A region returning from concealment, as a closed tree
  opening, takes only its own place, not one from the box that carried it
  (`reading-regions.js` marks such a shift `returned`). The box a region left
  is no longer restored: its own words moved only as layout moves them.
- Focus on a container that holds the passage, as a section a fragment landed
  on, names that passage rather than itself, so PageDown from a landed section
  keeps the paged passage (gallery 5, 5, 5; was 5, 285, 542).
- A screen reading skips a block below or above the band only when its words
  are drawn inside its box: no positioned or transformed element between the
  words and the block, read once per page reading. capturePlace on 3,000
  paragraphs 86% down: 1.4ms warm, 2.0ms first; gallery 0.25ms.
- The drawers reading-keys test reads the page's position once rendering has
  rested after the drawer opens beside the page.

Tests: script scrolls in the same frame, the first frame and the next move a
held page; paging down from a landed section keeps the passage; a reading finds
words a positioned child draws outside its block's box. Each fails on the
previous commit.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Oct 12, 2026
… Leaf's settled reading

A place nobody chose could outrank the reader's. When wt-merge's panes flowed
back into the page, the page took the newest place by when it was read, and a
record taken after layout moved a box (a clamp, anchoring, the order places
were first read in) beat the passage the reader read at 390: 81 came back as
555. Recording now follows #2560's rule for whose a scroll is:

- A place is recorded only when the reader chose a scroll in its box since its
  last record (`chosenSince`), acted since (a wheel before its scroll ends), or
  stood there. Only those records rank; a box's first place, read as the page
  arrives, ranks below all of them. wt-merge: 81 -> 66 -> 81 from 390, and
  66 -> 81 -> 66 from 1200 (main 554 both ways).
- scrolling.js: a sequence a place keeper wrote in stays the keeper's when it
  ends elsewhere with no input since the write, since layout settling after a
  resize moves the box on before the sequence ends; that end otherwise read as
  the reader's choice and released the hold. A keep no scroll follows within
  two frames lapses, so a later script scroll is still the reader's.
- The re-application window closes when Leaf's rendering reads settled
  (`renderingState`), at most 12 deliveries.
- The scroll-rest and scrollend records no longer both run for one gesture:
  the second finds no choice since the first. 1.6ms per gesture on a 207,000px
  report page.
- A screen reading checks whether a block's words are drawn outside its box
  fresh each time rather than per page reading, so a breakpoint or stylesheet
  that moves a child is seen (1.56ms vs 1.43 on 3,000 paragraphs).

Tests: a passage read in a flowing wt-merge pane returns after the panes scroll
alone (fails on the previous head); the out-of-box reading test adds a
breakpoint-moved child (fails on the previous head).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Oct 12, 2026
The keeper's-sequence rule in scrolling.js gave a page script's scroll in the
frames after a resize to the keeper, so the hold stayed and re-application
undid it (gallery: 2050 where the script left 2450). scrolling.js is back to
#2560's rule: a sequence ending away from the kept offset is a choice. Whose a
move was is now read in version.js:

- A hold ends when the reader aims input at its box, or when a scroll carries
  the held landmark with the box: the box and the landmark moved by the same
  distance. Layout moves the landmark under a box that stays or that anchoring
  moves to keep something else still, and re-application puts it back. Such a
  scroll records the place it left as the reader's; input in a box the restore
  put the reader in makes the held place theirs, as if recorded before the act.
  A landmark another box now carries ends the hold.
- The end of a scroll a restore started is the runtime's, wherever layout
  carried the box before it ended (`writing`), unless the reader aimed input at
  the box meanwhile; continuity reads choices through that (`readerChose`).
- Input counts for the box it scrolls: a wheel for the first box in its chain
  that can still move its way, a wheel nothing can follow and every other input
  for the innermost box. A wheel in a pane no longer counts as a choice on the
  page around it.
- Re-application skips a place whose landmark still stands, and
  `moveScrollerBy` writes no move under half a pixel: a zero write stopped a
  smooth scroll under way (gallery: 2050 -> 2850, as main).
- reading-place.js gives `placeNode` and the new `placeOffset` one landmark
  reading (`landmarkOf`).

Journeys vs main: wt-merge 81/66 both ways (main 554); gallery fragment,
wheel and panel cases hold 25 or 33; alert-review returns to 202; a script
scroll 1 or 2 frames after a resize, smooth with growth, or a frame after the
first scroll stands. A script scroll in the same task as the resize's first
scroll is undone here as on main: it lands in the frame layout settles in.

Tests: script-scroll variants added (1 and 2 frames after the resize, smooth
with growth, a frame after the first scroll); a wheel in a box is no choice of
the page around it. Each new case fails on the previous head.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Oct 14, 2026
…m, not where rounding left the box (#2877)

A reader who doesn't scroll now stays within one pixel of their line
across any number of revisions, and a card stays on its pixel while a
Threads-list fold corrects for it frame after frame. Before this, each
correction added its rounding to the last one's, so what the reader
looked at crept across the screen. It settles item 65 of the Lit
follow-up review, "WebKit place restores land about 5px off".

## Cause

A scroll offset is a whole pixel. Chromium rounds a fractional
`scrollBy`/`scrollTo` to the nearest pixel and WebKit rounds it down. I
measured both for a box and for the page, at device pixel ratios 1 and
2. Words stand at fractions of a pixel, so a restore lands up to a pixel
off its aim. A place captured afterwards took that error as part of the
reader's place, and the next restore added its own on top.

- **The reported 5px.** At #2560's commit I logged each frame of the
split drag. WebKit's landmark crept down by about half a pixel to a
pixel per frame, from 101.75px to 105.875px, because each frame's
restore re-recorded the place it had just left and WebKit always rounds
the same way. Chromium's errors went both ways and stayed under a pixel.
#2562 stopped this for layout shifts: its holds keep the reader's own
reading and don't re-record a box the runtime restored. On `main` the
drag lands 0.75px off in WebKit.
- **What #2562 left: revisions.** Revision continuity (`captureView`)
reads a fresh place at every revision, from wherever the last restore
left the box. On a page whose paragraphs stand 36.28px apart, both
engines scroll 36px for each paragraph added above, and the reader's
line drifts 0.28px per revision.
- **The same in `placeKeeper`.** `correct()` set `scrollTop += delta`
and then took its new baseline in content coordinates, so it never
recovered what the rounding lost. A fold above the reader corrects once
per frame. When I stepped a Threads-list fold through 30 frames, the
card below drifted 2px in Chromium and 10px in WebKit.

## Change

`moveScrollerBy` (`scrolling.js`) remembers the fractional offset it
aimed at for as long as the box stays at the offset the move left it
(`aimedTop`). `capturePlace` measures from that aim. The aim isn't kept
when the move was smooth, or when the box landed a pixel or more off,
since a box clamped at its range doesn't stand at its aim. Every restore
that moves through `moveScrollerBy` gets this, as does every place
capture: revisions, region shifts, and the subjects and tabs a region
shows. `placeKeeper.correct()` now moves through `moveScrollerBy` and
aims from where its last correction aimed.

## Tests

- `test_revision_after_revision_keeps_the_reader_on_the_same_line` (new,
Chromium and WebKit): six revisions each add a paragraph above the
reader. On `main` it fails in both engines (259.66px → 261.34px). With
the fix it lands at −0.31px in Chromium and +0.69px in WebKit.
- `test_a_fold_frame_by_frame_keeps_the_card_below_on_its_pixel` (new,
Chromium and WebKit): a held fold steps through 30 frames above the card
the reader looks at. On `main` the card drifts 2px in Chromium and 10px
in WebKit. With the fix it doesn't move.
- `test_a_split_drag_keeps_a_panes_reading`: the tolerance goes from 8px
to 1px, the most one rounding can leave.

## Line deltas (item 65)

- Hand-written source: +36 / −5 (`scrolling.js`, `reading-place.js`,
`user-place.js`).
- Tests: +89 / −3.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants