Skip to content

Commit fd8ccb2

Browse files
committed
egui: add auto-saving of answers based on version from leabra branch
1 parent 99981ff commit fd8ccb2

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

egui/gui.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"fmt"
1212
"io/fs"
1313
"net/http"
14+
"os"
15+
"path/filepath"
1416
"strings"
1517
"sync"
1618

@@ -199,14 +201,14 @@ func (gui *GUI) MakeBody(b tree.Node, sim any, fsroot fs.FS, appname, title, abo
199201
gui.Files.Tabber = tabs
200202

201203
if len(readme) > 0 {
202-
gui.addReadme(readme[0], split)
204+
gui.addReadme(readme[0], split, appname)
203205
} else {
204206
split.SetTiles(core.TileSplit, core.TileSpan)
205207
split.SetSplits(.2, .5, .8)
206208
}
207209
}
208210

209-
func (gui *GUI) addReadme(readmefs embed.FS, split *core.Splits) {
211+
func (gui *GUI) addReadme(readmefs embed.FS, split *core.Splits, appname string) {
210212
gui.Readme = core.NewFrame(split)
211213
gui.Readme.Name = "readme"
212214

@@ -231,6 +233,26 @@ func (gui *GUI) addReadme(readmefs embed.FS, split *core.Splits) {
231233
eds = append(eds, ed)
232234
id := htmlcore.GetAttr(ctx.Node, "id")
233235
ed.SetName(id)
236+
237+
// used with Embed in shared context so need appname in filename to avoid conflicts
238+
saveFile := filepath.Join(core.TheApp.AppDataDir(), appname+"-"+"q"+id+".md")
239+
err := ed.Lines.Open(saveFile)
240+
if err != nil {
241+
if errors.Is(err, fs.ErrNotExist) {
242+
err := os.WriteFile(saveFile, nil, 0666)
243+
core.ErrorSnackbar(ed, err, "Error creating answer file")
244+
if err == nil {
245+
err := ed.Lines.Open(saveFile)
246+
core.ErrorSnackbar(ed, err, "Error loading answer")
247+
}
248+
} else {
249+
core.ErrorSnackbar(ed, err, "Error loading answer")
250+
}
251+
}
252+
ed.OnChange(func(e events.Event) {
253+
core.ErrorSnackbar(ed, ed.SaveQuiet(), "Error saving answer")
254+
})
255+
234256
return true
235257
}
236258

0 commit comments

Comments
 (0)