Restore the browser page on relaunch, not the last edited file

A relaunch session always re-opened the last edited file, even when the
app was closed sitting on the browser page. The snapshot now records
which page the app was on (SessionState.InBrowser/BrowserPath); the cmd
layer lands back in the browser at the saved directory on relaunch
(falling back to the startup directory if it has vanished) instead of
calling BeginRestore. Browser-page snapshots clear File, which also
makes the editor->browser transition an urgent save, so the
back-in-browser state hits disk immediately. Legacy session files (no
InBrowser key) unmarshal to false and restore the editor exactly as
before.

Verified on emulator and phone: editor sessions still restore the file
(cursor/scroll/font), browser sessions land in the browser at the saved
dir; legacy files keep the old behavior. New e2e regression test
TestRestore_BrowserPageSession.
This commit is contained in:
gmp
2026-09-03 12:11:44 -04:00
parent 4a3ba37075
commit e96464ec79
4 changed files with 119 additions and 17 deletions
+25 -11
View File
@@ -112,18 +112,30 @@ func run(w *app.Window) error {
// 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.
// Shutdown. A session taken on the editor page re-opens the last file
// straight into the editor; one taken on the browser page lands back in
// the browser at the saved directory.
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)
if sess.InBrowser {
log.Printf("restoring session: browser path=%q", sess.BrowserPath)
// The saved directory may have vanished (deleted, synced away):
// fall back to the startup directory rather than pointing the
// browser at a dead path.
if fi, err := os.Stat(sess.BrowserPath); err == nil && fi.IsDir() {
logic.BeginBrowserRestore(sess.BrowserPath)
}
} else {
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)
}
}
}
@@ -435,7 +447,9 @@ func newSessionSaver(path string) func(editor.SessionState) {
// 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.
// — in which case the app starts in the browser at the startup directory
// as before. Browser-page sessions carry no file; an editor-page session
// (the pre-InBrowser legacy shape included) needs one.
func loadSession(path string) (editor.SessionState, bool) {
b, err := os.ReadFile(path)
if err != nil {
@@ -465,7 +479,7 @@ func loadSession(path string) (editor.SessionState, bool) {
if s.ScrollSub < 0 {
s.ScrollSub = 0
}
if s.File == "" {
if !s.InBrowser && s.File == "" {
return editor.SessionState{}, false
}
return s, true