go_dreamfactory/cmd/v2/ui/ui.go
2022-09-21 18:01:59 +08:00

66 lines
1.2 KiB
Go

package ui
import (
"go_dreamfactory/cmd/v2/service"
"go_dreamfactory/cmd/v2/service/observer"
"go_dreamfactory/cmd/v2/theme"
"os"
"sync"
"fyne.io/fyne/v2"
"github.com/BabySid/gobase"
)
type UI interface {
AddWindow(name string, w fyne.Window)
Run()
Stop()
}
type UIImpl struct {
app fyne.App
windows map[string]fyne.Window
winMux *sync.Mutex
connService service.ConnService
pttService service.PttService
configService service.ConfigService
obs observer.Observer
}
func NewUI(app fyne.App,
configService service.ConfigService,
connService service.ConnService,
pttService service.PttService,
obs observer.Observer,
) *UIImpl {
app.Settings().SetTheme(&theme.MyTheme{})
return &UIImpl{
app: app,
windows: make(map[string]fyne.Window),
winMux: &sync.Mutex{},
configService: configService,
connService: connService,
pttService: pttService,
obs: obs,
}
}
func (ui *UIImpl) AddWindow(name string, w fyne.Window) {
ui.winMux.Lock()
defer ui.winMux.Unlock()
ui.windows[name] = w
}
func (ui *UIImpl) Run() {
defer func() {
_ = os.Unsetenv("FYNE_SCALE")
gobase.Exit()
}()
ui.app.Run()
}
func (ui *UIImpl) Stop() {
ui.app.Quit()
}