ppforge/internal/ui/main.go

159 lines
4.1 KiB
Go
Raw Normal View History

2023-01-05 09:06:39 +01:00
package ui
import (
2023-01-05 14:16:41 +01:00
"strings"
2023-01-05 09:06:39 +01:00
"fyne.io/fyne/v2"
2023-01-05 14:16:41 +01:00
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
2023-01-05 09:06:39 +01:00
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
2023-01-05 14:16:41 +01:00
"gitea.mmo.to/ProtocolPacketForger/ppf/protocol"
"gitea.mmo.to/ProtocolPacketForger/ppf/protocolctl"
2023-01-05 09:06:39 +01:00
)
type ppfApp struct {
2023-01-05 14:16:41 +01:00
App fyne.App
Window fyne.Window
2023-01-05 09:06:39 +01:00
// Container for the borderlayout
Toolbar fyne.CanvasObject
Metadata *Metadata
Extensions *fyne.Container
ContextBar *fyne.Container
ContentTabs *fyne.Container
2023-01-05 14:16:41 +01:00
Workarea *container.DocTabs
2023-01-05 09:06:39 +01:00
// Once initialized metadata containers for swapping
ProtocolMeta *ProtocolMetadata
PacketMeta *PacketMetadata
// Global important stuff
Settings map[string]string
ProtocolForging bool
// Once initialized instances of the editor views
EditorPacket *PacketEditor
EditorProtocol *ProtocolEditor
2023-01-05 14:16:41 +01:00
// workspace handling
OpenProtocolFiles []*ProtocolFileHandler
2023-01-05 09:06:39 +01:00
}
var PPF ppfApp
2023-01-05 14:16:41 +01:00
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,
PPF.ContextBar,
PPF.Metadata.Representation,
PPF.Extensions,
PPF.Workarea,
)
}
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() {
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
}
2023-01-05 09:06:39 +01:00
func CreateToolbar(fyneApp fyne.App) fyne.CanvasObject {
toolbar := widget.NewToolbar()
2023-01-05 14:16:41 +01:00
toolbar.Append(widget.NewToolbarAction(theme.MenuIcon(), func() {
2023-01-05 09:06:39 +01:00
PPF.ProtocolForging = !PPF.ProtocolForging
//TODO: implement the switch
}))
toolbar.Append(widget.NewToolbarSeparator())
2023-01-05 14:16:41 +01:00
toolbar.Append(widget.NewToolbarAction(theme.ContentAddIcon(), func() {
PPF.NewFile()
2023-01-05 09:06:39 +01:00
}))
toolbar.Append(widget.NewToolbarAction(theme.FolderOpenIcon(), func() {
2023-01-05 14:16:41 +01:00
dialog.NewFileOpen(func(uri fyne.URIReadCloser, err error) {
if uri == nil {
return
}
PPF.OpenFile(uri.URI().Path())
}, PPF.Window).Show()
2023-01-05 09:06:39 +01:00
}))
toolbar.Append(widget.NewToolbarAction(theme.DocumentSaveIcon(), func() {
2023-01-05 14:16:41 +01:00
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)
}
2023-01-05 09:06:39 +01:00
}))
2023-01-05 14:16:41 +01:00
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()
}))
2023-01-05 09:06:39 +01:00
toolbar.Append(widget.NewToolbarAction(theme.SettingsIcon(), func() {
}))
toolbar.Append(widget.NewToolbarSpacer())
toolbar.Append(widget.NewToolbarAction(theme.LogoutIcon(), func() {
fyneApp.Quit()
}))
return toolbar
}
2023-01-05 14:16:41 +01:00
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
2023-01-05 09:06:39 +01:00
}