gte/cmd/android-app/screen/screen.go

107 lines
1.9 KiB
Go
Raw Normal View History

2022-10-18 16:58:12 +02:00
package screen
2022-10-24 15:57:55 +02:00
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
)
2022-10-18 16:58:12 +02:00
2022-10-20 10:40:46 +02:00
type Task struct {
ID string
Action string
}
2022-10-18 16:58:12 +02:00
type State struct {
2022-10-24 15:57:55 +02:00
Status string
CurrentScreen string
Tasks []Task
Config map[string]string
Logs []string
2022-10-18 16:58:12 +02:00
}
type Screen interface {
2022-10-24 15:57:55 +02:00
Content() *fyne.Container
2022-10-18 16:58:12 +02:00
Refresh(state State)
2022-10-24 15:57:55 +02:00
Hide()
Show()
}
type ScreenSet struct {
current string
show chan string
status binding.String
menu *fyne.Container
screens map[string]Screen
root *fyne.Container
}
func NewScreenSet(requests chan interface{}) *ScreenSet {
status := binding.NewString()
show := make(chan string)
tasksButton := widget.NewButton("tasks", func() {
show <- "tasks"
})
configButton := widget.NewButton("config", func() {
show <- "config"
})
logsButton := widget.NewButton("logs", func() {
show <- "logs"
})
statusLabel := widget.NewLabel("> init...")
statusLabel.Bind(status)
statusLabel.TextStyle.Italic = true
menu := container.NewHBox(tasksButton, configButton, logsButton, statusLabel)
screens := map[string]Screen{
"tasks": NewTasks(requests, show),
"logs": NewLog(),
"config": NewConfig(requests, show),
}
cs := []fyne.CanvasObject{}
for _, s := range screens {
s.Hide()
cs = append(cs, s.Content())
}
screens["tasks"].Show()
root := container.NewBorder(menu, nil, nil, nil, cs...)
return &ScreenSet{
status: status,
current: "tasks",
show: show,
screens: screens,
root: root,
}
}
func (ss *ScreenSet) Run() {
for s := range ss.show {
if s != ss.current {
ss.screens[ss.current].Hide()
ss.screens[s].Show()
ss.current = s
ss.root.Refresh()
}
}
}
func (ss *ScreenSet) Refresh(state State) {
ss.status.Set(fmt.Sprintf("> %s", state.Status))
for _, s := range ss.screens {
s.Refresh(state)
}
ss.root.Refresh()
}
func (ss *ScreenSet) Content() *fyne.Container {
return ss.root
2022-10-18 16:58:12 +02:00
}