Restore scroll by logical line, not pixel offset (wrap-aware)
On device: scroll far down a wrapped file, relaunch, and the app lands further DOWN than where the user left off — the deeper the scroll, the further off. Root cause: the persisted Scroll is a pixel offset in VISUAL-line space. Restoring maps it through the WrapIndex (scrollDecompose -> LineForVisual), but on relaunch every count is the estimate (1) until the line is shaped, and shaping covers the visible window only — the lines ABOVE the restored viewport are never shaped. With all-ones counts LineForVisual maps the offset 1:1, landing a logical line deeper by every wrapped continuation above the viewport, and the state is stable (the under-counted lines never re-enter the window), so it never self-corrects. The snapshot now persists wrap-independent coordinates: the logical line at the viewport top (derived with the same mapping the layout uses, against the current index, so it is exactly the shown line) plus the sub-line remainder. The restore re-derives the offset as line*lh + sub, which maps to the saved line under any wrap state (all-ones or populated). The raw Dp offset is kept for pre-line-coordinate session files (loadSession defaults the missing key to -1; BeginRestore rejects the ambiguous zero value: a genuine line-0 snapshot always has Scroll < lh). TestRestore_ScrollSurvivesWrapState reproduces it: 150 lines recorded as wrapped x3, viewport at logical line 200 (visual 500); a relaunch with a fresh WrapIndex must land the window on line 200. Fails pre-fix (window at line 254, i.e. deeper) and passes with the fix. Docs: spec §2.4 (line-based scroll persist + current save policy), architecture §6.7 (why the offset is unrestorable by re-mapping).
This commit is contained in:
@@ -292,6 +292,18 @@ Only the visible byte range is shaped and drawn each frame:
|
||||
- Max scroll is `TotalVisuals()·lh − regionH + lh/2` with the
|
||||
font-scale-effective line height; it grows incrementally as shaped
|
||||
counts arrive (pre-shaping it equals the no-wrap estimate).
|
||||
- **Relaunch snapshot (spec §2.4):** the counts are process-local (rebuilt
|
||||
from the all-ones estimate on relaunch; only the visible window is ever
|
||||
shaped), so a persisted pixel scroll offset cannot be restored by
|
||||
re-mapping — with every count above the restored viewport at the
|
||||
estimate, `LineForVisual` maps the offset 1:1 to a logical line deeper
|
||||
by all the wrapped continuations above it, with no self-correction
|
||||
(those lines never enter the window). The snapshot therefore persists
|
||||
the wrap-independent coordinates: the logical line at the viewport top
|
||||
(derived with the same scrollDecompose + LineForVisual mapping the
|
||||
layout uses, against the CURRENT index) plus the sub-line remainder;
|
||||
the restore re-derives the offset as `line·lh + sub`. The raw offset is
|
||||
kept as the fallback for pre-line-coordinate session files.
|
||||
- **Font-scale axis.** The shaper draws baselines in sp, so on Android the
|
||||
rendered line pitch in density-dp is `EditorLineHeight()*fontScale`
|
||||
(`fontScale = Metric.PxPerSp/PxPerDp`, the user font-size setting).
|
||||
|
||||
+14
-4
@@ -114,18 +114,28 @@ elsewhere.
|
||||
### 2.4 State restoration on relaunch
|
||||
|
||||
- On launch, Pad re-opens the file from the last session and lands straight
|
||||
on the editor page, restoring the cursor, scroll offset, live selection,
|
||||
on the editor page, restoring the cursor, scroll position, live selection,
|
||||
and the find bar (query, open/closed, current match). A relaunch therefore
|
||||
never requires re-browsing to the last file.
|
||||
- The scroll position is persisted as the LOGICAL line at the viewport top
|
||||
plus the sub-line remainder (the raw pixel offset is kept too, for
|
||||
pre-line-coordinate session files). The pixel offset alone is not
|
||||
restorable: it lives in visual-line space, and the wrap counts that map
|
||||
it to a line are rebuilt from estimates on relaunch — lines above the
|
||||
restored viewport are never shaped, so the offset would land a line
|
||||
deeper by every wrapped continuation above it.
|
||||
- The snapshot is a tiny JSON file (a few hundred bytes) written by the
|
||||
cmd layer: `$HOME/.pad/session.json` off-Android, and
|
||||
`/storage/emulated/0/Pad/session.json` on Android (the dir that already
|
||||
carries the browser's `.pad` index caches). Search results are NOT stored:
|
||||
they are regenerated by re-scanning the restored query, and the current
|
||||
match is re-selected by byte offset.
|
||||
- The snapshot is written rate-limited (≤ 1/s, only on change) while the app
|
||||
runs and unconditionally at shutdown, so a kill shortly after a change
|
||||
loses at most ~1 s of state.
|
||||
- The snapshot is written rate-limited (250 ms, only on change) while the
|
||||
app runs; a file change or a selection appearing/vanishing saves
|
||||
immediately; on Android the activity's onStop (recents-wipe, app switch)
|
||||
flushes it one last time; and it is written unconditionally at shutdown.
|
||||
A kill shortly after a change therefore loses at most a fraction of a
|
||||
second of state.
|
||||
- If the restored file no longer exists (deleted/moved, e.g. by Syncthing),
|
||||
the app falls back to the browser page; positions that exceed a shrunk
|
||||
file are clamped to its new end.
|
||||
|
||||
Reference in New Issue
Block a user