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
+137
View File
@@ -616,6 +616,143 @@ got:
}
}
// TestRestore_LinePinHoldsAcrossLateWrapFeedback reproduces the on-device
// report "relaunch lands further UP than where I left off" (Pixel 9 Pro):
// while the restore scroll is still armed, the top-of-file window is what's
// rendered, and its shaping feedback (real wrap counts for the lines ABOVE
// the restored line) can land before or just after the offset is applied.
// Those counts are correct data, but they change the offset-to-line mapping:
// the line-derived offset was synthesized for the all-estimate index, so
// once counts land below the pinned line the same offset maps to a shallower
// line and the viewport drifts up. The restore therefore pins the logical
// line: every wrap correction re-derives the offset as V(L)*lh + sub until
// the restored window itself has shaped (or a timeout, or the user scrolls).
func TestRestore_LinePinHoldsAcrossLateWrapFeedback(t *testing.T) {
const lines = 300
const lineLen = 17 // "line %03d content\n"
var b strings.Builder
for i := 0; i < lines; i++ {
fmt.Fprintf(&b, "line %03d content\n", i)
}
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "wrap.txt"), []byte(b.String()), 0644); err != nil {
t.Fatal(err)
}
// The harness runs at the default font scale, so the stateless variant
// matches what the owner will compute.
lh := float64(editor.EffectiveLineHeightAt(1))
snap := editor.SessionState{
File: "/wrap.txt",
Cursor: 0,
Scroll: float64(ui.Dp(500 * lh)), // the saving session's visual-space offset
ScrollLine: 200, // the logical line at the viewport top
ScrollSub: 0,
}
h := e2e.NewHarness(e2e.WithFileSystem(real.NewRealFileSystem(dir), "/"))
h.Logic().BeginRestore(snap)
h.Run()
h.SendConfig(780, 1688)
h.SendScale(2.0)
defer h.Cleanup()
// Wait for the restore to land: with the fresh all-estimate index the
// offset maps to exactly line 200.
var winLine int
for i := 0; i < 100; i++ {
v, err := h.Inspect(func(st *editor.State) any {
cb := st.Editor.ChunkedBuffer
if cb == nil || cb.FileLen() == 0 || cb.LineIndex == nil {
return -1
}
return st.WindowStartLine
})
if err == nil {
winLine = v.(int)
if winLine == 200 {
break
}
}
time.Sleep(20 * time.Millisecond)
}
if winLine != 200 {
t.Fatalf("restore did not land on line 200 (window at %d)", winLine)
}
// Replay the on-device interleaving: the top-of-file window (lines
// 0-49) was shaped while the scroll was still armed, and its feedback —
// real wrap counts (3 visual lines each for those lines) — arrives now,
// after the offset was applied.
editSeq, err := h.Inspect(func(st *editor.State) any {
return st.Editor.EditSeq
})
if err != nil {
t.Fatalf("Inspect: %v", err)
}
var wt strings.Builder
for i := 0; i < 50; i++ {
fmt.Fprintf(&wt, "line %03d content\n", i)
}
windowText := wt.String()
var starts []int
for i := 0; i < 50; i++ {
base := i * lineLen
starts = append(starts, base, base+5, base+10) // 3 visual lines per line
}
h.Logic().LayoutChan() <- ui.LayoutFeedback{
GlyphLayout: ui.GlyphLayout{
VisualLineStarts: starts,
},
WindowText: windowText,
WindowStartByte: 0,
WindowStartLine: 0,
EditSeq: editSeq.(uint64),
}
// The correction must NOT drag the viewport: the pin re-derives the
// offset as V(200)*lh, where V(200) = 50*3 + 150 = 300 under the
// corrected index, keeping line 200 at the top. Pre-fix, the applied
// offset (200*lh) mapped to line ~66 under the corrected index.
for i := 0; i < 100; i++ {
v, err := h.Inspect(func(st *editor.State) any {
return struct {
Scroll float64
WinLine int
}{float64(st.ScrollOffset), st.WindowStartLine}
})
if err == nil {
got := v.(struct {
Scroll float64
WinLine int
})
if got.WinLine == 200 && got.Scroll > 299*lh && got.Scroll < 301*lh {
return // held
}
}
time.Sleep(20 * time.Millisecond)
}
v, err := h.Inspect(func(st *editor.State) any {
return struct {
Scroll float64
WinLine int
}{float64(st.ScrollOffset), st.WindowStartLine}
})
if err != nil {
t.Fatalf("Inspect: %v", err)
}
got := v.(struct {
Scroll float64
WinLine int
})
if got.WinLine != 200 {
t.Errorf("window at line %d after late wrap feedback, want 200: the line-pin must hold the viewport on the restored line (a shallower line is the pre-fix drift)", got.WinLine)
}
if got.Scroll < 499*lh || got.Scroll > 501*lh {
t.Errorf("scroll = %v after late wrap feedback, want ~300*lh (%v): V(200) under the corrected index", got.Scroll, 300*lh)
}
}
// TestRestore_UrgentSaveOnSelection verifies the kill-race fix: a selection
// appearing (the user just highlighted text) saves immediately, even inside
// the rate-limit window that the file-open save opened. Without the urgent