emdb/desktop-client/gui/gui.go

103 lines
1.9 KiB
Go
Raw Normal View History

2024-04-25 15:28:52 +02:00
package gui
import (
2024-04-26 15:10:38 +02:00
"strings"
2024-04-26 10:01:46 +02:00
"code.ewintr.nl/emdb/desktop-client/backend"
2024-04-25 15:28:52 +02:00
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
2024-04-26 10:01:46 +02:00
"fyne.io/fyne/v2/data/binding"
2024-04-25 15:28:52 +02:00
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
type GUI struct {
2024-04-26 15:10:38 +02:00
a fyne.App
w fyne.Window
in chan backend.State
out chan backend.Command
s *State
2024-04-25 15:28:52 +02:00
}
2024-04-26 15:10:38 +02:00
func New(out chan backend.Command, in chan backend.State) *GUI {
2024-04-25 15:28:52 +02:00
a := app.New()
w := a.NewWindow("EMDB")
w.Resize(fyne.NewSize(800, 600))
2024-04-26 10:01:46 +02:00
g := &GUI{
2024-04-26 15:10:38 +02:00
a: a,
w: w,
in: in,
out: out,
s: NewState(),
2024-04-25 15:28:52 +02:00
}
2024-04-26 10:01:46 +02:00
g.SetContent()
return g
2024-04-25 15:28:52 +02:00
}
func (g *GUI) Run() {
2024-04-26 15:10:38 +02:00
go func() {
for s := range g.in {
g.Update(s)
}
}()
2024-04-25 15:28:52 +02:00
g.w.ShowAndRun()
}
2024-04-26 15:10:38 +02:00
func (g *GUI) Update(bs backend.State) {
// watched
watched := make([]string, 0, len(bs.Watched))
for _, m := range bs.Watched {
2024-04-26 15:22:35 +02:00
watched = append(watched, m.EnglishTitle)
2024-04-26 15:10:38 +02:00
}
g.s.Watched.Set(watched)
// log
g.s.Log.Set(strings.Join(bs.Log, "\n"))
}
2024-04-26 10:01:46 +02:00
func (g *GUI) SetContent() {
2024-04-25 15:28:52 +02:00
label1 := widget.NewLabel("Label 1")
label2 := widget.NewLabel("Label 2")
value2 := widget.NewLabel("Something")
input := widget.NewEntry()
input.SetPlaceHolder("Enter text...")
form := container.New(layout.NewFormLayout(), label1, input, label2, value2)
button := widget.NewButton("Save", func() {
2024-04-26 15:10:38 +02:00
g.out <- backend.Command{
2024-04-26 10:01:46 +02:00
Name: backend.CommandAdd,
Args: map[string]any{
backend.ArgName: input.Text,
},
}
2024-04-25 15:28:52 +02:00
})
grid := container.NewBorder(nil, button, nil, nil, form)
2024-04-26 10:01:46 +02:00
logLines := container.NewVScroll(widget.NewLabelWithData(g.s.Log))
2024-04-25 15:28:52 +02:00
//logLines.ScrollToBottom()
2024-04-26 10:01:46 +02:00
list := widget.NewListWithData(g.s.Watched,
func() fyne.CanvasObject {
return widget.NewLabel("template")
},
func(i binding.DataItem, o fyne.CanvasObject) {
o.(*widget.Label).Bind(i.(binding.String))
})
2024-04-25 15:28:52 +02:00
tabs := container.NewAppTabs(
2024-04-26 10:01:46 +02:00
container.NewTabItem("Watched", list),
2024-04-25 15:28:52 +02:00
container.NewTabItem("TheMovieDB", grid),
container.NewTabItem("Log", logLines),
)
2024-04-26 10:01:46 +02:00
g.w.SetContent(tabs)
2024-04-25 15:28:52 +02:00
}