package ui import ( "strings" "fyne.io/fyne/v2" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/dialog" "fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/widget" "gitea.mmo.to/ProtocolPacketForger/ppf/protocol" "gitea.mmo.to/ProtocolPacketForger/ppf/protocolctl" ) type Representationer interface { Representation() *fyne.Container } type FileHandler interface { Name() string Path() string Open(path string) error Save() error SaveAs(path string) error Close() error Changed() HasChanged() bool } type ppfApp struct { App fyne.App Window fyne.Window // Container for the main layout, toolbar at top, in Center the doctabs. MainContainer *fyne.Container // toolbar Toolbar fyne.CanvasObject // left optional the local/remote library // tbd // center opened objects OpenTabs *container.DocTabs // DocTabs OpenObjects []*FileHandler // actual files // Global important stuff Settings map[string]interface{} /*/ will be part of the Editor Metadata *Metadata Extensions *fyne.Container ContextBar *fyne.Container ContentTabs *fyne.Container Workarea *container.DocTabs // Once initialized metadata containers for swapping ProtocolMeta *ProtocolMetadata PacketMeta *PacketMetadata // Once initialized instances of the editor views EditorPacket *PacketEditor EditorProtocol *ProtocolEditor // workspace handling OpenProtocolFiles []*ProtocolFileHandler//*/ } var PPF ppfApp func NewPPF(fyneApp fyne.App, w fyne.Window) ppfApp { PPF.App = fyneApp PPF.Window = w PPF.Toolbar = CreateToolbar(fyneApp) /*PPF.Workarea = CreateWorkarea() PPF.Extensions = container.NewCenter() PPF.ProtocolMeta = NewMetadataProtocol() PPF.Metadata = &PPF.ProtocolMeta.Metadata PPF.ContextBar = container.NewCenter() //*/ return PPF } func (ppf *ppfApp) GetContainer() *fyne.Container { return container.NewBorder( PPF.Toolbar, nil, //PPF.ContextBar, nil, //PPF.Metadata.Representation, nil, //PPF.Extensions, PPF.OpenTabs, ) } func (ppf *ppfApp) OpenFile(path string) { pfh := LoadProtocolFileHandler(path) ppf.OpenProtocolFiles = append(PPF.OpenProtocolFiles, pfh) ppf.Workarea.Append(container.NewTabItem(pfh.Filename, pfh.GetWorkarea())) } func (ppf *ppfApp) NewFile() { //TODO: show entry screen with choosable ways of: // PROTOCOL editor // create a new protocol // edit an existing protocol // PACKET editor // create a binary packet // edit an existing binary packet pfh := NewProtocolFileHandler() PPF.OpenProtocolFiles = append(PPF.OpenProtocolFiles, pfh) ppf.Workarea.Append(container.NewTabItem(pfh.Filename, pfh.GetWorkarea())) } func (ppf *ppfApp) SaveFile(path string) { item := ppf.Workarea.Selected() for _, pfh := range ppf.OpenProtocolFiles { if pfh.Filename == item.Text { protocolctl.Save(pfh.ProtocolEditor.Reference, path) } } item.Text = path ppf.Workarea.Refresh() } func (ppf *ppfApp) GetReferenceForFile(s string) *protocol.ProtocolStructure { for _, pfh := range ppf.OpenProtocolFiles { if pfh.Filename == s { return pfh.ProtocolEditor.Reference } } return nil } func CreateToolbar(fyneApp fyne.App) fyne.CanvasObject { toolbar := widget.NewToolbar() toolbar.Append(widget.NewToolbarAction(theme.ContentAddIcon(), func() { PPF.NewFile() })) toolbar.Append(widget.NewToolbarAction(theme.FolderOpenIcon(), func() { dialog.NewFileOpen(func(uri fyne.URIReadCloser, err error) { if uri == nil { return } PPF.OpenFile(uri.URI().Path()) }, PPF.Window).Show() })) toolbar.Append(widget.NewToolbarAction(theme.DocumentSaveIcon(), func() { if strings.HasPrefix(PPF.Workarea.Selected().Text, "*new") { dialog.NewFileSave(func(uri fyne.URIWriteCloser, err error) { if uri == nil { return } PPF.SaveFile(uri.URI().Path()) }, PPF.Window).Show() } else { PPF.SaveFile(PPF.Workarea.Selected().Text) } })) toolbar.Append(widget.NewToolbarAction(theme.StorageIcon(), func() { dialog.NewFileSave(func(uri fyne.URIWriteCloser, err error) { if uri == nil { return } PPF.SaveFile(uri.URI().Path()) }, PPF.Window).Show() })) toolbar.Append(widget.NewToolbarAction(theme.SettingsIcon(), func() { })) toolbar.Append(widget.NewToolbarSpacer()) toolbar.Append(widget.NewToolbarAction(theme.LogoutIcon(), func() { fyneApp.Quit() })) return toolbar } func CreateWorkarea() *container.DocTabs { tabs := container.NewDocTabs() tabs.OnClosed = func(ti *container.TabItem) { if len(tabs.Items) == 0 { PPF.Metadata.Representation.Hide() } } tabs.OnSelected = func(ti *container.TabItem) { PPF.ProtocolMeta.SetProtocol(PPF.GetReferenceForFile(ti.Text)) PPF.Metadata = &PPF.ProtocolMeta.Metadata PPF.Metadata.Representation.Refresh() PPF.Metadata.Representation.Show() } return tabs }