bedrocktool/ui/gui.go

138 lines
2.6 KiB
Go
Raw Normal View History

2023-03-08 11:46:38 +00:00
//go:build gui || android
2023-03-06 14:50:36 +00:00
package ui
2023-03-05 21:50:58 +00:00
import (
"context"
2023-03-15 01:36:55 +00:00
"image/color"
2023-03-14 01:07:39 +00:00
"gioui.org/app"
"gioui.org/font/gofont"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/widget/material"
2023-03-15 01:36:55 +00:00
"gioui.org/x/pref/theme"
2023-03-14 01:07:39 +00:00
"github.com/bedrock-tool/bedrocktool/ui/gui/pages"
"github.com/bedrock-tool/bedrocktool/ui/gui/pages/settings"
"github.com/bedrock-tool/bedrocktool/ui/gui/pages/worlds"
2023-03-05 21:50:58 +00:00
"github.com/bedrock-tool/bedrocktool/utils"
2023-03-14 01:07:39 +00:00
"github.com/sirupsen/logrus"
2023-03-05 21:50:58 +00:00
)
type GUI struct {
utils.BaseUI
2023-03-05 21:50:58 +00:00
2023-03-14 01:07:39 +00:00
router pages.Router
cancel context.CancelFunc
2023-03-05 21:50:58 +00:00
}
func (g *GUI) Init() bool {
2023-03-14 01:07:39 +00:00
utils.SetCurrentUI(g)
return true
}
2023-03-15 01:36:55 +00:00
var paletteLight = material.Palette{
Bg: color.NRGBA{0xff, 0xff, 0xff, 0xff},
Fg: color.NRGBA{0x12, 0x12, 0x12, 0xff},
ContrastBg: color.NRGBA{0x7c, 0x00, 0xf8, 0xff},
ContrastFg: color.NRGBA{0x00, 0x00, 0x00, 0xff},
}
var paletteDark = material.Palette{
Bg: color.NRGBA{0x12, 0x12, 0x12, 0xff},
Fg: color.NRGBA{0xff, 0xff, 0xff, 0xff},
ContrastBg: color.NRGBA{0x7c, 0x00, 0xf8, 0xff},
ContrastFg: color.NRGBA{0xff, 0xff, 0xff, 0xff},
}
2023-03-15 13:51:31 +00:00
func (g *GUI) Start(ctx context.Context, cancel context.CancelFunc) (err error) {
g.cancel = cancel
w := app.NewWindow(
app.Title("Bedrocktool"),
)
2023-03-14 01:07:39 +00:00
th := material.NewTheme(gofont.Collection())
2023-03-15 01:36:55 +00:00
dark, err := theme.IsDarkMode()
if err != nil {
logrus.Warn(err)
}
if dark {
_th := th.WithPalette(paletteDark)
th = &_th
} else {
_th := th.WithPalette(paletteLight)
th = &_th
}
2023-03-15 13:51:31 +00:00
g.router = pages.NewRouter(ctx, w.Invalidate, th)
g.router.Register("Settings", settings.New(&g.router))
g.router.Register("worlds", worlds.New(&g.router))
g.router.SwitchTo("Settings")
go func() {
err = g.run(w)
}()
go func() {
app.Main()
}()
<-ctx.Done()
return err
}
func (g *GUI) run(w *app.Window) error {
2023-03-14 01:07:39 +00:00
var ops op.Ops
for {
select {
case e := <-w.Events():
switch e := e.(type) {
case system.DestroyEvent:
2023-03-15 14:01:34 +00:00
logrus.Info("Closing")
g.cancel()
g.router.Wg.Wait()
2023-03-14 01:07:39 +00:00
return e.Err
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)
2023-03-15 13:51:31 +00:00
g.router.Layout(gtx, g.router.Theme)
2023-03-14 01:07:39 +00:00
e.Frame(gtx.Ops)
}
case <-g.router.Ctx.Done():
logrus.Info("Closing")
g.cancel()
g.router.Wg.Wait()
return nil
}
}
2023-03-14 01:07:39 +00:00
}
func (g *GUI) Message(name string, data interface{}) utils.MessageResponse {
r := g.router.Handler(name, data)
if r.Ok || r.Data != nil {
return r
}
2023-03-05 21:50:58 +00:00
2023-03-14 01:07:39 +00:00
r = utils.MessageResponse{
Ok: false,
Data: nil,
2023-03-05 21:50:58 +00:00
}
switch name {
case "can_show_images":
r.Ok = true
}
return r
2023-03-05 21:50:58 +00:00
}
2023-03-06 14:50:36 +00:00
func init() {
utils.MakeGui = func() utils.UI {
return &GUI{}
}
}