Hold the restored scroll on its line while wrap counts settle (spec §2.4)

On device (Pixel 9 Pro) the relaunch landed further UP than the saved
position: while the restore scroll is still armed (scale + size + content
can take ~600 ms), the top-of-file window is what gets rendered, and its
shaping feedback — real wrap counts for lines ABOVE the restored line —
lands before or just after the offset is applied. The counts are correct
data, but they change V(line), so the line-derived offset (synthesized
for the all-estimate index) maps to a shallower line and the viewport
drifts up; the save then persists the drifted line and every subsequent
relaunch lands there.

Fix: the restore pins the logical line. Until the restored window's own
shaping arrives (or a 2 s timeout, or the user scrolls / a search jumps),
every accepted wrap correction re-derives the offset as
VisualsBefore(line)*lh + sub under the corrected index. The pin refresh
does not clamp to MaxScroll: the correction just grew the index, so the
pre-layout clamp value is stale and would under-clamp the re-derived
offset (the layout of the emitted frame clamps to the fresh value).

Also: the apply-time offset is mapped through VisualsBefore under the
current index (identical to line*lh while the index is all estimates),
so pre-apply corrections are absorbed instead of gated away.

Verified on device: saved line 420 -> restored 420 (was 333); saved 573
(near EOF, clamp territory) -> restored 573. New e2e regression
TestRestore_LinePinHoldsAcrossLateWrapFeedback replays the late
top-window feedback and fails pre-fix (window drifts 200 -> 66).
This commit is contained in:
gmp
2026-08-20 21:03:56 -04:00
parent 6968f6a284
commit 0f1b6e6290
7 changed files with 267 additions and 10 deletions
+29 -5
View File
@@ -76,8 +76,8 @@ type Logic struct {
// flushSession: one-shot request to persist the session snapshot now
// (the OS activity onStop hook, see FlushSession). Buffered 1 so the
// requester never blocks, even if an earlier flush is still queued.
flushSession chan struct{}
inspectChan chan *inspectReq
flushSession chan struct{}
inspectChan chan *inspectReq
// Per-file write protocol (see requestSave). Workers are a shared pool and
// the on-disk staging file is per-file, so two concurrent writes for the
@@ -112,8 +112,20 @@ type Logic struct {
// scaleSeen tracks the first ScaleEvent; restoreContentLanded marks the
// read result that filled the buffer (FileLen alone is set earlier, by
// the stat result, and is not a content-arrival signal).
restoreScroll ui.Dp
restoreScrollArmed bool
restoreScroll ui.Dp
restoreScrollArmed bool
// restoreScrollLine/restoreScrollSub/restorePinDeadline implement the
// restore line-pin (see refreshRestorePin in session.go): the
// line-derived scroll offset is only consistent with the all-estimate
// WrapIndex, so while relaunch restore is settling, every wrap-count
// correction landing below the pinned line shifts the offset-to-line
// mapping and would drag the viewport off the restored line. Until the
// restored window itself has shaped (or a timeout, or the user or a
// search takes over), the offset is re-derived from the pinned line
// after each correction.
restoreScrollLine int
restoreScrollSub float64
restorePinDeadline time.Time
scaleSeen bool
restoreContentLanded bool
sessionSaver func(SessionState)
@@ -260,7 +272,7 @@ func (l *Logic) Run() {
case <-l.flushSession:
// Persist now, bypassing the rate limit: the activity is going
// away (recents-wipe or app switch) and the process may die
// shortly after this returns.
// shortly after this returns.
l.flushSessionSave()
case update := <-l.configChan:
update.apply(l.state)
@@ -283,6 +295,18 @@ func (l *Logic) Run() {
// lines since shaping (fb.EditSeq correlates with the content).
if fb.EditSeq == l.state.Editor.EditSeq {
l.state.applyWrapCounts(fb)
// Restore line-pin (see refreshRestorePin): a correction
// landing below the pinned line shifted the offset-to-line
// mapping, so re-derive the offset from the pinned line under
// the corrected index. The restored window's own shaping means
// its neighborhood is real and the pin can stand down.
if l.restoreScrollLine >= 0 {
if fb.WindowStartLine == l.restoreScrollLine {
l.restoreScrollLine = -1
} else {
l.refreshRestorePin()
}
}
// Search settle (see EditorState.findSettle): the shaping above
// may have corrected the wrap counts around a find-jumped
// viewport; re-scroll while the correction still matters.
+3
View File
@@ -355,6 +355,9 @@ func scrollToFindMatch(absByte int) {
e.Find.SettleByte = -1
return
}
if TheLogic != nil {
TheLogic.releaseRestorePin() // search takes over the viewport
}
TheState.ScrollOffset = target
if e.Find.Visible {
e.Find.SettleByte = absByte
+68 -2
View File
@@ -243,6 +243,7 @@ func (l *Logic) abortRestore() {
l.restoreFile = ""
l.restoreScrollArmed = false
l.restoreContentLanded = false
l.restoreScrollLine = -1
l.session = SessionState{}
}
@@ -281,12 +282,30 @@ func (l *Logic) maybeApplyRestoreScroll() bool {
// so the raw offset would land a line deeper by every wrapped
// continuation above the viewport. The line-based offset lands on the
// saved line regardless of wrap state.
if s := l.session; s.ScrollLine >= 0 {
s := l.session
if s.ScrollLine >= 0 {
lh := EffectiveLineHeight()
if gl := l.state.Editor.GlyphLayout; gl.LineHeight > 0 {
lh = gl.LineHeight
}
l.restoreScroll = ui.Dp(float64(s.ScrollLine)*float64(lh) + s.ScrollSub)
// The offset for a logical line is V(L)*lh + sub under the CURRENT
// WrapIndex (V(L) = L while the index is all estimates). Wrapping
// below the line was already corrected before the offset could be
// applied (pre-apply frames shaped the top-of-file window), so
// mapping through V(L) lands on the line under either state.
base := float64(s.ScrollLine)
if cb := l.state.Editor.ChunkedBuffer; cb != nil && cb.WrapIndex != nil && s.ScrollLine < cb.WrapIndex.Len() {
base = float64(cb.WrapIndex.VisualsBefore(s.ScrollLine))
}
l.restoreScroll = ui.Dp(base*float64(lh) + s.ScrollSub)
// Arm the line-pin (see refreshRestorePin): wrap-count corrections
// landing below this line would shift the mapping and drag the
// viewport off the restored line while the index settles.
l.restoreScrollLine = s.ScrollLine
l.restoreScrollSub = float64(s.ScrollSub)
l.restorePinDeadline = time.Now().Add(restorePinTimeout)
} else {
l.restoreScrollLine = -1
}
if l.restoreScroll > l.state.MaxScroll {
l.restoreScroll = l.state.MaxScroll
@@ -299,6 +318,53 @@ func (l *Logic) maybeApplyRestoreScroll() bool {
return true
}
// restorePinTimeout bounds the line-pin: after this long the index around
// the restored window has either settled or the user has moved on.
const restorePinTimeout = 2 * time.Second
// releaseRestorePin clears the restore line-pin (user or search took over
// the viewport). Must be called on the logic goroutine.
func (l *Logic) releaseRestorePin() {
l.restoreScrollLine = -1
}
// refreshRestorePin re-derives the scroll offset from the pinned line under
// the CURRENT WrapIndex (V(L)*lh + sub, the same mapping the layout uses),
// so wrap-count corrections landing below the pinned line cannot drag the
// viewport off it. Releases the pin when the deadline passes. Must be
// called on the logic goroutine, after a wrap correction has been applied.
func (l *Logic) refreshRestorePin() {
if l.restoreScrollLine < 0 {
return
}
if time.Now().After(l.restorePinDeadline) {
l.restoreScrollLine = -1
return
}
cb := l.state.Editor.ChunkedBuffer
if cb == nil || cb.WrapIndex == nil {
return
}
w := cb.WrapIndex
if l.restoreScrollLine >= w.Len() {
return
}
lh := EffectiveLineHeight()
if gl := l.state.Editor.GlyphLayout; gl.LineHeight > 0 {
lh = gl.LineHeight
}
// No MaxScroll clamp here: the correction that triggered this refresh
// just grew the index, so the layout's MaxScroll (computed before it) is
// stale and would under-clamp the re-derived offset; the layout pass of
// the emitted frame clamps to the fresh value. An edit shrinking the
// file mid-pin is covered the same way.
off := ui.Dp(float64(w.VisualsBefore(l.restoreScrollLine))*float64(lh) + l.restoreScrollSub)
if off != l.state.ScrollOffset {
l.state.ScrollOffset = off
l.emitFrame()
}
}
// applyRestorePositions clamps the restored snapshot's cursor and selection
// to a file of n bytes and applies them. Must be called on the logic
// goroutine.
+3
View File
@@ -350,6 +350,9 @@ func HandleScroll(data any) {
return
}
delta := data.(int) // pixels
if TheLogic != nil {
TheLogic.releaseRestorePin() // the user takes over the viewport
}
TheState.ScrollOffset += ui.ToDp(ui.Px(delta), TheState.scale)
if TheState.ScrollOffset < 0 {
TheState.ScrollOffset = 0