ppforge/internal/ui/main.go

219 lines
5.3 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-04-12 15:14:19 +02:00
// A Representationer provides a representation as fyne.Container
type Representationer interface {
Representation() *fyne.Container
}
2023-04-12 15:14:19 +02:00
// A FileHandler handles files in the application
type FileHandler interface {
Name() string
Path() string
Open(path string) error
Save() error
SaveAs(path string) error
Close() error
2023-03-31 15:38:20 +02:00
SetChanged()
HasChanged() bool
2023-03-31 15:38:20 +02:00
GetReference() interface{}
}
2023-04-12 15:14:19 +02:00
// A TabProvider gives back a container.TabItem and allows to set it for internal storage
2023-04-11 11:32:22 +02:00
type TabProvider interface {
SetTab(tab *container.TabItem)
Tab() *container.TabItem
}
2023-04-12 15:14:19 +02:00
// ppfApp is the internal struct holding all the required main objects/elements for the application
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
// 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
2023-03-31 15:38:20 +02:00
OpenObjects []FileHandler // actual files
// Global important stuff
Settings map[string]interface{}
/*/ will be part of the Editor
2023-01-05 09:06:39 +01:00
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
// 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
}
2023-04-12 15:14:19 +02:00
// PPF is the global variable holding the application struct
2023-01-05 09:06:39 +01:00
var PPF ppfApp
2023-04-12 15:14:19 +02:00
// NewPPF is the entrypoint to create the application
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)
2023-03-31 15:38:20 +02:00
PPF.OpenTabs = CreateWorkarea()
2023-01-05 14:16:41 +01:00
return PPF
}
func (ppf *ppfApp) GetContainer() *fyne.Container {
return container.NewBorder(
2023-03-31 15:38:20 +02:00
ppf.Toolbar, //top
nil, //bottom
nil, //left // maybe Accordion with existing protocols, your protocols, your packets?
nil, //right
ppf.OpenTabs,
2023-01-05 14:16:41 +01:00
)
}
2023-03-18 23:07:14 +01:00
func (ppf *ppfApp) NewProtocolFile() {
//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
2023-01-05 14:16:41 +01:00
pfh := NewProtocolFileHandler()
2023-03-31 15:38:20 +02:00
ppf.OpenObjects = append(ppf.OpenObjects, pfh)
item := ppf.OpenTabs.Selected()
item.Text = pfh.Name()
item.Content = pfh.GetWorkarea()
2023-04-11 11:32:22 +02:00
pfh.SetTab(item)
2023-01-05 14:16:41 +01:00
}
2023-03-18 23:07:14 +01:00
func (ppf *ppfApp) NewPacketFile() {
}
2023-03-31 15:38:20 +02:00
func (ppf *ppfApp) OpenProtocolFile(path string) {
for _, v := range ppf.OpenTabs.Items {
if v.Text == path {
// do not open a file twice!
ppf.OpenTabs.Select(v)
return
}
}
pfh := NewProtocolFileHandler()
pfh.Open(path)
ppf.OpenObjects = append(ppf.OpenObjects, pfh)
item := ppf.OpenTabs.Selected()
item.Text = pfh.Path()
item.Content = pfh.GetWorkarea()
2023-04-11 11:32:22 +02:00
pfh.SetTab(item)
2023-03-31 15:38:20 +02:00
}
func (ppf *ppfApp) OpenPacketFile(path string) {
}
2023-01-05 14:16:41 +01:00
func (ppf *ppfApp) SaveFile(path string) {
2023-03-31 15:38:20 +02:00
item := ppf.OpenTabs.Selected()
for _, fh := range ppf.OpenObjects {
2023-04-11 11:32:22 +02:00
if fh.(TabProvider).Tab() == item {
2023-03-31 15:38:20 +02:00
fh.SaveAs(path)
2023-01-05 14:16:41 +01:00
}
}
2023-03-31 15:38:20 +02:00
// if there exists another one with the same path, (max one!!!)
2023-04-11 11:32:22 +02:00
/* // skip that check for now
2023-03-31 15:38:20 +02:00
existingTab := -1
for i, v := range ppf.OpenTabs.Items {
2023-04-11 11:32:22 +02:00
fmt.Printf("existing tab %d, path %s\n", i, v.Text)
2023-03-31 15:38:20 +02:00
if v.Text == path && i != ppf.OpenTabs.SelectedIndex() {
existingTab = i
}
}
if existingTab > 0 {
ppf.OpenTabs.Items = append(ppf.OpenTabs.Items[:existingTab], ppf.OpenTabs.Items[existingTab+1:]...)
fmt.Printf("removing %d", existingTab)
ppf.OpenTabs.Refresh()
2023-04-11 11:32:22 +02:00
} */
2023-01-05 14:16:41 +01:00
item.Text = path
2023-03-31 15:38:20 +02:00
ppf.OpenTabs.Refresh()
2023-01-05 14:16:41 +01:00
}
2023-03-31 15:38:20 +02:00
func (ppf *ppfApp) NewFile() {
for _, v := range ppf.OpenTabs.Items {
if v.Text == "[new item dialog]" {
ppf.OpenTabs.Select(v)
return
2023-01-05 14:16:41 +01:00
}
}
2023-03-31 15:38:20 +02:00
item := container.NewTabItem("[new item dialog]", CreateNewObjectDialog().Representation())
ppf.OpenTabs.Append(item)
ppf.OpenTabs.Select(item)
2023-01-05 14:16:41 +01:00
}
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.ContentAddIcon(), func() {
PPF.NewFile()
2023-01-05 09:06:39 +01:00
}))
toolbar.Append(widget.NewToolbarAction(theme.DocumentSaveIcon(), func() {
2023-03-31 15:38:20 +02:00
if strings.HasPrefix(PPF.OpenTabs.Selected().Text, "*new") {
2023-01-05 14:16:41 +01:00
dialog.NewFileSave(func(uri fyne.URIWriteCloser, err error) {
if uri == nil {
return
}
PPF.SaveFile(uri.URI().Path())
}, PPF.Window).Show()
} else {
2023-03-31 15:38:20 +02:00
PPF.SaveFile(PPF.OpenTabs.Selected().Text)
2023-01-05 14:16:41 +01:00
}
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) {
2023-03-31 15:38:20 +02:00
2023-01-05 14:16:41 +01:00
}
tabs.OnSelected = func(ti *container.TabItem) {
}
return tabs
2023-01-05 09:06:39 +01:00
}