From 25ca4faf6fbed555b270fc5435ad0ff7feca299a Mon Sep 17 00:00:00 2001 From: Prithvi Vishak Date: Fri, 16 Jun 2023 12:35:35 +0530 Subject: [PATCH] Attempt to load map from last used file on startup --- ui/mapeditor/mapeditor.go | 66 ++++++++++++++++++++++----------------- ui/ui.go | 11 ++++++- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/ui/mapeditor/mapeditor.go b/ui/mapeditor/mapeditor.go index f7d7eea..84957ea 100644 --- a/ui/mapeditor/mapeditor.go +++ b/ui/mapeditor/mapeditor.go @@ -16,7 +16,6 @@ import ( ) type MapEditor struct { - parentApp fyne.App parentWindow fyne.Window mainContainer fyne.CanvasObject @@ -29,11 +28,12 @@ type MapEditor struct { StickConfigs map[wiimote.Stick]StickConfig `json:"StickConfig"` stickConfigurators map[wiimote.Stick]*StickConfigurator + + mapFile string } -func New(a fyne.App, w fyne.Window) *MapEditor { +func New(w fyne.Window) *MapEditor { m := new(MapEditor) - m.parentApp = a m.parentWindow = w m.buttons = make(map[wiimote.Keycode]*widget.Button) m.selectors = make(map[wiimote.Keycode]*KeyChooser) @@ -110,37 +110,43 @@ func (m *MapEditor) chooserChangeHandler(c wiimote.Keycode) func(fyne.KeyName) { } func (m *MapEditor) loadMap() { - dialog.ShowFileOpen(m.loadMapFromFile, m.parentWindow) -} - -func (m *MapEditor) loadMapFromFile(file fyne.URIReadCloser, err error) { - if err != nil { - loggo.Error(err) - dialog.ShowError(err, m.parentWindow) - return - } - if file == nil { - return - } - defer file.Close() + dialog.ShowFileOpen(func(file fyne.URIReadCloser, err error) { + if err != nil { + loggo.Error(err) + dialog.ShowError(err, m.parentWindow) + return + } + if file == nil { + return + } + defer file.Close() + + jsonBytes := make([]byte, 2048) + n, err := file.Read(jsonBytes) + jsonBytes = jsonBytes[:n] + if err != nil { + loggo.Error(err) + dialog.ShowError(errors.New("Unable to read file"), m.parentWindow) + } - jsonBytes := make([]byte, 2048) - n, err := file.Read(jsonBytes) - if err != nil { - loggo.Error(err) - dialog.ShowError(errors.New("Unable to read file"), m.parentWindow) - return - } + err = m.LoadMapFromBytes(jsonBytes) + if err != nil { + loggo.Error(err) + dialog.ShowError(errors.New("Unable to parse file"), m.parentWindow) + } + m.mapFile = file.URI().Path() + }, m.parentWindow, + ) +} - jsonBytes = jsonBytes[:n] - err = json.Unmarshal(jsonBytes, m) +func (m *MapEditor) LoadMapFromBytes(jsonBytes []byte) error { + err := json.Unmarshal(jsonBytes, m) if err != nil { - loggo.Error(err) - dialog.ShowError(errors.New("Unable to parse file"), m.parentWindow) - return + return err } m.updateButtonsFromMap() + return nil } func (m *MapEditor) saveMap() { @@ -170,6 +176,8 @@ func (m *MapEditor) saveMapToFile(file fyne.URIWriteCloser, err error) { loggo.Error(err) dialog.ShowError(errors.New("Unable to write to file"), m.parentWindow) } + + m.mapFile = file.URI().Path() } func (m *MapEditor) remapBtnFor(c wiimote.Keycode) *widget.Button { @@ -214,3 +222,5 @@ func (m *MapEditor) StickConfigFor(stick wiimote.Stick) StickConfig { } return cfg } + +func (m *MapEditor) MapFile() string { return m.mapFile } diff --git a/ui/ui.go b/ui/ui.go index 22dfdf1..4325b45 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -1,6 +1,7 @@ package ui import ( + "io/ioutil" "math" "time" @@ -46,12 +47,20 @@ func (u *UI) Initialize() (err error) { u.wwApp = app.NewWithID("com.github.Pippadi.WiiWill") u.wwApp.Lifecycle().SetOnStopped(func() { actor.SendStopMsg(u.Inbox()) + u.wwApp.Preferences().SetString("MapFile", u.mapEditor.MapFile()) + time.Sleep(500 * time.Millisecond) // Wait for preferences to persist }) u.mainWindow = u.wwApp.NewWindow("WiiWill") u.activityBar = widget.NewProgressBarInfinite() u.statusLbl = widget.NewLabel("") - u.mapEditor = mapeditor.New(u.wwApp, u.mainWindow) + u.mapEditor = mapeditor.New(u.mainWindow) + if mf := u.wwApp.Preferences().String("MapFile"); mf != "" { + bytes, err := ioutil.ReadFile(mf) + if err == nil { + u.mapEditor.LoadMapFromBytes(bytes) + } + } box := container.NewVBox( u.mapEditor.UI(),