Restore last file, cursor, scroll, selection and find state on relaunch (spec §7)

Persist a tiny JSON snapshot (SessionState) written by the cmd layer
($HOME/.pad/session.json off-Android, /storage/emulated/0/Pad/ on
Android) and call the logic-owned snapshot rate-limited (<=1/s, on
change) from emitFrame plus unconditionally at Shutdown. On launch the
cmd layer hands the snapshot to Logic.BeginRestore before Run; the
file re-opens through the normal openFile path and lands straight on
the editor page.

- Cursor/selection land with the content, clamped to a shrunk file
  (path-guarded so a late result for a replaced file cannot apply the
  snapshot to the wrong buffer); a missing file falls back to the
  browser.
- Scroll is applied only after the first ScaleEvent has been laid out:
  the size ConfigEvent precedes it and the one-way MaxScroll clamp in a
  wrong-unit layout would corrupt the offset (found by e2e).
- Find: query + open/closed + current match are persisted; results are
  regenerated by re-scanning and the saved current match is re-selected
  by byte offset (Find.Restoring/RestoreMatch) without re-scrolling the
  restored viewport. A closed-bar query re-scans on the next bar open
  instead (an eager scan would be dropped and leave Scanning stuck).
- The main-owned find_bar widget is seeded with the restored query so
  its first frame matches the logic-side query.

Docs: spec.md gains §2.4 and drops the §7 row; invariant 5 updated;
architecture.md gains §6.7. Tests: 7 e2e tests covering cursor/scroll/
selection restore, clamping, missing-file fallback, find-bar restore
(open/closed), and the saver/shutdown persist paths.
This commit is contained in:
gmp
2026-08-20 16:01:02 -04:00
parent f31f849665
commit e3690d6b98
10 changed files with 961 additions and 23 deletions
+7
View File
@@ -87,6 +87,13 @@ func SetGestureExclusions(rects [][4]int) {
})
}
// sessionFilePath is where the relaunch session file (spec §7) lives. The
// primary storage dir already carries the app's caches (.pad browser index
// dirs, PadPerf), and the app holds the storage permission.
func sessionFilePath() string {
return "/storage/emulated/0/Pad/session.json"
}
func OpenFile(path string) {
var env *C.JNIEnv
var detach bool
+13
View File
@@ -3,6 +3,9 @@
package main
import (
"os"
"path/filepath"
"gioui.org/io/event"
)
@@ -10,6 +13,16 @@ var (
startpath="."
)
// sessionFilePath is where the relaunch session file (spec §7) lives, off
// Android: $HOME/.pad/session.json, falling back to the temp dir when there
// is no home.
func sessionFilePath() string {
if d, err := os.UserHomeDir(); err == nil && d != "" {
return filepath.Join(d, ".pad", "session.json")
}
return filepath.Join(os.TempDir(), "pad", "session.json")
}
func handleEvent(e event.Event) { }
func OpenFile(path string) { }
+72
View File
@@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"flag"
"io"
"log"
@@ -106,6 +107,25 @@ func run(w *app.Window) error {
var findEditor widget.Editor
renderer.RegisterGioEditor("find_bar", &findEditor)
// Relaunch state restoration (spec §7): a tiny JSON file holds the last
// file, cursor, scroll, selection and find bar state. The cmd layer owns
// the file (sessionFilePath, per platform); the logic layer owns the
// snapshot (editor.SessionState) and calls the saver rate-limited and at
// Shutdown. Restoring re-opens the last file straight into the editor.
sessPath := sessionFilePath()
logic.SetSessionSaver(newSessionSaver(sessPath))
if sess, ok := loadSession(sessPath); ok {
log.Printf("restoring session: file=%s cursor=%d scroll=%v find=%q", sess.File, sess.Cursor, sess.Scroll, sess.FindQuery)
logic.BeginRestore(sess)
// The find bar's input is a main-owned widget and the input source of
// truth: seed it with the restored query so its first frame matches
// the logic-side query (an empty widget would forward "" and clear
// the restored query).
if sess.FindQuery != "" {
findEditor.SetText(sess.FindQuery)
}
}
// Clipboard plumbing (architecture.md §6.3): the logic goroutine only
// REQUESTS clipboard operations through channels (copy/cut write, paste
// read); the main goroutine executes the Gio ops during a frame and
@@ -360,6 +380,58 @@ func run(w *app.Window) error {
}
// exclEqual reports whether two exclusion-rect sets are identical.
// newSessionSaver builds the relaunch-session file writer registered with
// the logic goroutine (spec §7). The file is a tiny JSON snapshot; a torn
// write is rejected by loadSession's parse on the next launch, so a plain
// write is safe (no temp+rename needed).
func newSessionSaver(path string) func(editor.SessionState) {
return func(s editor.SessionState) {
b, err := json.Marshal(s)
if err != nil {
log.Printf("session: marshal: %v", err)
return
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
log.Printf("session: mkdir: %v", err)
return
}
if err := os.WriteFile(path, b, 0o600); err != nil {
log.Printf("session: write: %v", err)
}
}
}
// loadSession reads and sanity-checks the relaunch session file (spec §7).
// ok=false when there is no session, it is corrupt, or nothing is restorable
// — in which case the app starts in the browser as before.
func loadSession(path string) (editor.SessionState, bool) {
b, err := os.ReadFile(path)
if err != nil {
return editor.SessionState{}, false
}
var s editor.SessionState
if err := json.Unmarshal(b, &s); err != nil {
return editor.SessionState{}, false
}
// A partial/corrupt file must not restore garbage positions.
if s.Cursor < 0 {
s.Cursor = 0
}
if s.Scroll < 0 {
s.Scroll = 0
}
if s.SelStart < -1 || s.SelEnd < -1 || s.SelEnd <= s.SelStart {
s.SelStart, s.SelEnd = -1, -1
}
if s.FindCurByte < -1 {
s.FindCurByte = -1
}
if s.File == "" {
return editor.SessionState{}, false
}
return s, true
}
func exclEqual(a, b [][4]int) bool {
if len(a) != len(b) {
return false