Fix empty file on save

This commit is contained in:
Marcel M. Otte 2023-04-11 11:32:22 +02:00
parent 52bff71153
commit 262bef4e3b
2 changed files with 30 additions and 8 deletions

View File

@ -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()
}

View File

@ -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
}