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:
@@ -40,10 +40,23 @@ const sessionSaveInterval = 250 * time.Millisecond
|
||||
// SessionState is the relaunch snapshot (see the file doc above). All
|
||||
// positions are absolute file byte offsets; Scroll is in Dp. It is a plain
|
||||
// comparable value (used with == for change detection).
|
||||
//
|
||||
// Scroll is backed by ScrollLine: the pixel offset lives in VISUAL-line
|
||||
// space (a wrapped line occupies several visual lines, and the scroll-to-
|
||||
// line mapping runs through the WrapIndex), but on relaunch the counts of
|
||||
// every line above the restored viewport are still the estimate (1) until
|
||||
// the line is shaped, and lines above the visible window are never shaped.
|
||||
// Mapping the saved offset through that fresh index would land a logical
|
||||
// line DEEPER by all the wrapped continuations above the viewport, with no
|
||||
// self-correction. The logical line at the viewport top and the sub-line
|
||||
// remainder are wrap-independent, so they are the restorable unit; Scroll
|
||||
// is kept as the pre-line-coordinate value (legacy session files).
|
||||
type SessionState struct {
|
||||
File string // last opened file ("" = nothing to restore)
|
||||
Cursor int // cursor byte offset
|
||||
Scroll float64 // editor scroll offset, Dp
|
||||
ScrollLine int // logical line at the viewport top (-1 = unknown)
|
||||
ScrollSub float64 // Scroll's sub-line remainder, Dp (0 <= r < lineHeight)
|
||||
SelStart int // selection start byte (-1 = no selection)
|
||||
SelEnd int // selection end byte, exclusive
|
||||
FindQuery string // find bar query ("" = none)
|
||||
@@ -77,10 +90,30 @@ func (l *Logic) SnapshotSession() SessionState {
|
||||
if f.Cur >= 0 && f.Cur < len(f.Matches) {
|
||||
curByte = f.Matches[f.Cur][0]
|
||||
}
|
||||
// Persist the scroll in wrap-independent coordinates (see the
|
||||
// SessionState doc). The derivation mirrors visibleByteRangePrecise
|
||||
// (scrollDecompose, then LineForVisual against the CURRENT index), so
|
||||
// the line is exactly the one the layout is showing at this offset.
|
||||
scrollLine := -1
|
||||
var scrollSub float64
|
||||
if cb := e.ChunkedBuffer; cb != nil && !e.TooLarge {
|
||||
lh := EffectiveLineHeight()
|
||||
if gl := e.GlyphLayout; gl.LineHeight > 0 {
|
||||
lh = gl.LineHeight
|
||||
}
|
||||
v0, r0 := scrollDecompose(s.ScrollOffset, lh)
|
||||
scrollLine = int(v0)
|
||||
if w := cb.WrapIndex; w != nil {
|
||||
scrollLine = w.LineForVisual(int32(v0))
|
||||
}
|
||||
scrollSub = r0
|
||||
}
|
||||
return SessionState{
|
||||
File: e.Filename,
|
||||
Cursor: cur,
|
||||
Scroll: float64(s.ScrollOffset),
|
||||
ScrollLine: scrollLine,
|
||||
ScrollSub: scrollSub,
|
||||
SelStart: e.SelectionStart,
|
||||
SelEnd: e.SelectionEnd,
|
||||
FindQuery: f.Query,
|
||||
@@ -171,6 +204,14 @@ func (l *Logic) BeginRestore(s SessionState) {
|
||||
if s.File == "" {
|
||||
return
|
||||
}
|
||||
// The zero-value ScrollLine (0) is ambiguous: a genuine top-of-file
|
||||
// snapshot always has Scroll < lineHeight (no line above line 0
|
||||
// contributes visual lines), so line 0 with a deep offset is an unset
|
||||
// field (an in-process caller or test built the snapshot without it) —
|
||||
// fall back to the pixel offset rather than snapping to the top.
|
||||
if s.ScrollLine == 0 && s.Scroll >= 2*float64(EffectiveLineHeight()) {
|
||||
s.ScrollLine = -1
|
||||
}
|
||||
l.session = s
|
||||
e := &l.state.Editor
|
||||
e.Filename = s.File
|
||||
@@ -232,6 +273,21 @@ func (l *Logic) maybeApplyRestoreScroll() bool {
|
||||
if l.state.Page() != EditorPage || !l.restoreContentLanded {
|
||||
return false
|
||||
}
|
||||
// Re-derive the offset from the persisted logical line (see the
|
||||
// SessionState doc): the snapshot's pixel offset mapped to its position
|
||||
// only while the saving session's WrapIndex was valid. On relaunch the
|
||||
// counts above the restored viewport are all the estimate until shaped,
|
||||
// and those lines never are (shaping covers the visible window only),
|
||||
// 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 {
|
||||
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)
|
||||
}
|
||||
if l.restoreScroll > l.state.MaxScroll {
|
||||
l.restoreScroll = l.state.MaxScroll
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user