From 262bef4e3b84fc3c65ad43a40add027111fa635c Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Tue, 11 Apr 2023 11:32:22 +0200 Subject: [PATCH] Fix empty file on save --- internal/ui/main.go | 15 +++++++++++---- internal/ui/protocolfilehandler.go | 23 +++++++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/internal/ui/main.go b/internal/ui/main.go index 69e3bb6..b101120 100644 --- a/internal/ui/main.go +++ b/internal/ui/main.go @@ -1,7 +1,6 @@ package ui import ( - "fmt" "strings" "fyne.io/fyne/v2" @@ -29,6 +28,11 @@ type FileHandler interface { GetReference() interface{} } +type TabProvider interface { + SetTab(tab *container.TabItem) + Tab() *container.TabItem +} + type ppfApp struct { App fyne.App Window fyne.Window @@ -94,6 +98,7 @@ func (ppf *ppfApp) NewProtocolFile() { item := ppf.OpenTabs.Selected() item.Text = pfh.Name() item.Content = pfh.GetWorkarea() + pfh.SetTab(item) } func (ppf *ppfApp) NewPacketFile() { @@ -114,6 +119,7 @@ func (ppf *ppfApp) OpenProtocolFile(path string) { item := ppf.OpenTabs.Selected() item.Text = pfh.Path() item.Content = pfh.GetWorkarea() + pfh.SetTab(item) } func (ppf *ppfApp) OpenPacketFile(path string) { @@ -123,14 +129,15 @@ func (ppf *ppfApp) OpenPacketFile(path string) { func (ppf *ppfApp) SaveFile(path string) { item := ppf.OpenTabs.Selected() for _, fh := range ppf.OpenObjects { - if fh.Path() == item.Text { + if fh.(TabProvider).Tab() == item { fh.SaveAs(path) } } // if there exists another one with the same path, (max one!!!) + /* // skip that check for now existingTab := -1 for i, v := range ppf.OpenTabs.Items { - fmt.Printf("tab %d, path %s", i, v.Text) + fmt.Printf("existing tab %d, path %s\n", i, v.Text) if v.Text == path && i != ppf.OpenTabs.SelectedIndex() { existingTab = i @@ -140,7 +147,7 @@ func (ppf *ppfApp) SaveFile(path string) { ppf.OpenTabs.Items = append(ppf.OpenTabs.Items[:existingTab], ppf.OpenTabs.Items[existingTab+1:]...) fmt.Printf("removing %d", existingTab) ppf.OpenTabs.Refresh() - } + } */ item.Text = path ppf.OpenTabs.Refresh() } diff --git a/internal/ui/protocolfilehandler.go b/internal/ui/protocolfilehandler.go index 0a6aa30..13c9262 100644 --- a/internal/ui/protocolfilehandler.go +++ b/internal/ui/protocolfilehandler.go @@ -1,7 +1,10 @@ package ui import ( + "fmt" + "fyne.io/fyne/v2" + "fyne.io/fyne/v2/container" "gitea.mmo.to/ProtocolPacketForger/ppf/protocol" "gitea.mmo.to/ProtocolPacketForger/ppf/protocolctl" ) @@ -12,6 +15,7 @@ type ProtocolFileHandler struct { name string path string changed bool + tab *container.TabItem Reference *protocol.ProtocolStructure } @@ -21,6 +25,7 @@ func NewProtocolFileHandler() *ProtocolFileHandler { pfh.name = "*new" pfh.changed = true pfh.ProtocolEditor = NewProtocolEditor(protocolctl.NewProtocolStructure()) + pfh.Reference = pfh.ProtocolEditor.Reference return &pfh } @@ -50,14 +55,16 @@ func (pfh *ProtocolFileHandler) Open(path string) error { return pfh.Load() } func (pfh *ProtocolFileHandler) Save() error { - protocolctl.Save(pfh.Reference, pfh.path) + err := protocolctl.Save(pfh.Reference, pfh.path) + if err != nil { + fmt.Printf("%v\n", err) + } pfh.changed = false - return nil + return err } func (pfh *ProtocolFileHandler) SaveAs(path string) error { pfh.path = path - pfh.Save() - return nil + return pfh.Save() } func (pfh *ProtocolFileHandler) Close() error { return pfh.Save() @@ -73,3 +80,11 @@ func (pfh *ProtocolFileHandler) HasChanged() bool { func (pfh *ProtocolFileHandler) GetReference() interface{} { return pfh.Reference } + +func (pfh *ProtocolFileHandler) Tab() *container.TabItem { + return pfh.tab +} + +func (pfh *ProtocolFileHandler) SetTab(tab *container.TabItem) { + pfh.tab = tab +}