372 lines
9.7 KiB
Go
372 lines
9.7 KiB
Go
package ui
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"go_dreamfactory/cmd/v2/lib/common"
|
|
"go_dreamfactory/cmd/v2/service"
|
|
"go_dreamfactory/cmd/v2/service/observer"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/modules/user"
|
|
"go_dreamfactory/pb"
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/container"
|
|
"fyne.io/fyne/v2/dialog"
|
|
"fyne.io/fyne/v2/theme"
|
|
"fyne.io/fyne/v2/widget"
|
|
"github.com/BabySid/gobase"
|
|
"github.com/Pallinder/go-randomdata"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
var (
|
|
globalWin *MainWindowImpl
|
|
toolWin *ToolWindowImpl
|
|
)
|
|
|
|
type MainWindow interface {
|
|
WindowInterface
|
|
}
|
|
|
|
type MainWindowImpl struct {
|
|
UIImpl
|
|
WindowDefaultOptions
|
|
w fyne.Window
|
|
tb *toolBar //工具条
|
|
toys *toys // side
|
|
sb *statusBar //状态栏
|
|
at *appContainer //tabs
|
|
}
|
|
|
|
func NewMainWindow(ui *UIImpl) MainWindow {
|
|
gobase.NewScheduler().Start()
|
|
gobase.RegisterAtExit(gobase.GlobalScheduler.Stop)
|
|
mw := &MainWindowImpl{
|
|
UIImpl: *ui,
|
|
}
|
|
|
|
globalWin = mw
|
|
|
|
ui.obs.AddListener(observer.EVENT_PING, observer.Listener{
|
|
OnNotify: func(data interface{}, args ...interface{}) {
|
|
logrus.Debug("即将于服务器断开链接")
|
|
conf := dialog.NewConfirm("链接中断", data.(error).Error(), func(
|
|
b bool) {
|
|
if b {
|
|
globalWin.w.Close()
|
|
} else {
|
|
dialog.NewInformation("提示", "重启登录", globalWin.w)
|
|
return
|
|
}
|
|
}, globalWin.w)
|
|
conf.SetDismissText("取消")
|
|
conf.SetConfirmText("退出")
|
|
conf.Show()
|
|
},
|
|
})
|
|
return mw
|
|
}
|
|
|
|
func GetGlobalWin() *MainWindowImpl {
|
|
return globalWin
|
|
}
|
|
|
|
// createWindowContainer create a container with the window content
|
|
func (ui *MainWindowImpl) createWindowContainer() {
|
|
// create main layout
|
|
|
|
// status bar
|
|
ui.sb = newStatusBar()
|
|
|
|
// tool bar
|
|
toolbar := widget.NewToolbar(
|
|
widget.NewToolbarAction(theme.ComputerIcon(), func() {
|
|
openApp2(common.TOOLBAR_MONITOR)
|
|
}),
|
|
widget.NewToolbarAction(theme.AccountIcon(), func() {
|
|
openApp2(common.TOOLBAR_TESTER)
|
|
}),
|
|
widget.NewToolbarSeparator(),
|
|
widget.NewToolbarAction(theme.ContentClearIcon(), func() {
|
|
newLogViewer().Win.Show()
|
|
}),
|
|
widget.NewToolbarSpacer(),
|
|
widget.NewToolbarAction(theme.HelpIcon(), func() {
|
|
showAbout()
|
|
}),
|
|
)
|
|
ui.tb = newToolBar(toolbar)
|
|
|
|
// Fun Toys
|
|
ui.toys = newToys(ui.obs)
|
|
|
|
// main app tabs
|
|
ui.at = newAppContainer(appRegister, ui.obs)
|
|
content := container.NewBorder(ui.tb.toolbar, ui.sb.widget, nil, ui.toys.widget, ui.at)
|
|
ui.w.SetContent(content)
|
|
ui.w.SetCloseIntercept(ui.quiteHandle)
|
|
}
|
|
|
|
func (ui *MainWindowImpl) SetStatusMsg(msg string) {
|
|
ui.sb.setMessage(fmt.Sprintf("err:%s", msg))
|
|
}
|
|
|
|
func (ui *MainWindowImpl) quiteHandle() {
|
|
ui.w.Close()
|
|
}
|
|
|
|
// CreateWindow ....
|
|
func (ui *MainWindowImpl) CreateWindow(_ string, width, height float32, _ bool) {
|
|
// init window
|
|
title := fmt.Sprintf(common.APP_WIN_TITLE, "登录", ui.app.Metadata().Version, ui.app.Metadata().Build, common.APP_NAME)
|
|
w := ui.app.NewWindow(title)
|
|
ui.AddWindow("main", w)
|
|
ui.w = w
|
|
|
|
if ui.windowAspect == common.WindowAspect_FullScreen {
|
|
w.SetFullScreen(true)
|
|
} else {
|
|
w.Resize(fyne.NewSize(width, height))
|
|
}
|
|
|
|
w.SetMaster()
|
|
w.CenterOnScreen()
|
|
_ = ui.createChooseServerPopUp(w)
|
|
}
|
|
|
|
// check version
|
|
func (ui *MainWindowImpl) checkVersion(parent fyne.Window) {
|
|
logrus.Debug("check version")
|
|
b, err := ui.connService.HttpConnect(ui.configService.GetConfig().UpgradeUrl + "version")
|
|
if err != nil {
|
|
dialog.ShowError(errors.New("版本检查:"+err.Error()), parent)
|
|
return
|
|
}
|
|
|
|
ver := gjson.Get(string(b), "version").String()
|
|
if strings.HasSuffix(ver, ".zip") {
|
|
ver = strings.TrimSuffix(ver, ".zip")
|
|
}
|
|
logrus.WithField("version", ver).Debug("server version")
|
|
if common.IsUpgrade(ver, ui.app.Metadata().Version) {
|
|
chkDialog := dialog.NewConfirm("版本检查", "检查到新版本:"+ver+" 请升级后使用", func(b bool) {
|
|
var commands = map[string]string{
|
|
"windows": "explorer",
|
|
"darwin": "open",
|
|
"linux": "xdg-open",
|
|
}
|
|
//open browser
|
|
open := func(uri string) error {
|
|
// runtime.GOOS获取当前平台
|
|
run, ok := commands[runtime.GOOS]
|
|
if !ok {
|
|
logrus.Errorf("don't know how to open things on %s platform", runtime.GOOS)
|
|
}
|
|
|
|
cmd := exec.Command(run, uri)
|
|
return cmd.Run()
|
|
}
|
|
_ = open(ui.configService.GetConfig().UpgradeUrl)
|
|
defer parent.Close()
|
|
}, parent)
|
|
chkDialog.SetConfirmText(common.BUTTON_OK)
|
|
chkDialog.Show()
|
|
}
|
|
|
|
}
|
|
|
|
// createChooseServerPopUp
|
|
func (ui *MainWindowImpl) createChooseServerPopUp(w fyne.Window) error {
|
|
ch := make(chan string)
|
|
|
|
title := fmt.Sprintf(common.APP_WIN_TITLE, "选服", ui.app.Metadata().Version, ui.app.Metadata().Build, common.APP_NAME)
|
|
selServerWin := ui.createChooseServerWindow(title, ch)
|
|
ui.checkVersion(selServerWin)
|
|
|
|
go func() {
|
|
data := <-ch
|
|
selServerWin.Hide()
|
|
ui.NewWelcomeLabel(data)
|
|
ui.w.Show()
|
|
}()
|
|
return nil
|
|
}
|
|
|
|
// createChooseServerWindow
|
|
func (ui *MainWindowImpl) createChooseServerWindow(
|
|
title string,
|
|
ch chan string) fyne.Window {
|
|
// choose server button func
|
|
makeButton := func(s *service.ServiceConf, parent fyne.Window) *widget.Button {
|
|
btn := widget.NewButton(s.Name, func() {
|
|
d := dialog.NewInformation("", common.INFO_WAIT, parent)
|
|
d.SetDismissText(common.BUTTON_CANCEL)
|
|
d.Show()
|
|
|
|
logrus.WithFields(logrus.Fields{"server": s.Name, "sid": s.SId}).Debug("choose server")
|
|
//conn server
|
|
if err := ui.connService.WsConnect(s.Url); err != nil {
|
|
d.Hide()
|
|
dialog.ShowError(err, parent)
|
|
} else {
|
|
ch <- fmt.Sprintf("%s:%s", s.SId, s.Name)
|
|
}
|
|
})
|
|
return btn
|
|
}
|
|
w := fyne.CurrentApp().NewWindow(title)
|
|
sGrid := container.NewGridWithColumns(2)
|
|
config := ui.configService.GetConfig()
|
|
if config != nil {
|
|
for _, s := range config.Services {
|
|
box := makeButton(s.Service, w)
|
|
sGrid.Add(box)
|
|
}
|
|
}
|
|
|
|
w.SetContent(sGrid)
|
|
w.SetFixedSize(true)
|
|
w.Resize(fyne.NewSize(500, 200))
|
|
w.Show()
|
|
w.CenterOnScreen()
|
|
return w
|
|
}
|
|
|
|
// createLoginWin
|
|
func (ui *MainWindowImpl) createLoginWin(sid, sname string) {
|
|
account := widget.NewEntry()
|
|
// account.Text = "user8080" //default account
|
|
|
|
items := []*widget.FormItem{
|
|
widget.NewFormItem(common.LABEL_ACCOUNT, account),
|
|
}
|
|
|
|
dialog.ShowForm(common.FORM_TITLE_LOGIN, common.BUTTON_LOGIN, common.BUTTON_CANCEL, items, func(b bool) {
|
|
if !b {
|
|
logrus.Debug("cancel login")
|
|
} else {
|
|
if account.Text != "" {
|
|
logrus.WithField("account", account.Text).Debug("submit login")
|
|
ui.createWindowContainer()
|
|
appName, err := ui.at.openDefaultApp()
|
|
if err != nil {
|
|
logrus.WithField("appName", appName).Error(err)
|
|
}
|
|
// hide toolbar
|
|
ui.tb.toolbar.Hide()
|
|
// call after ui.createWindowContainer
|
|
ui.connService.ListenerPush()
|
|
if code := ui.pttService.Login(sid, account.Text); code != pb.ErrorCode_Success {
|
|
err := fmt.Errorf("login err: %v[%d]", code, int32(code))
|
|
dialog.ShowError(err, ui.w)
|
|
} else {
|
|
ui.obs.AddListener(observer.EVENT_REQ_RSP, observer.Listener{
|
|
OnNotify: func(data interface{}, args ...interface{}) {
|
|
if data == nil {
|
|
return
|
|
}
|
|
msg := data.(*pb.UserMessage)
|
|
if msg.MainType == string(comm.ModuleUser) &&
|
|
msg.SubType == user.UserSubTypeLogin {
|
|
rsp := &pb.UserLoginResp{}
|
|
if !comm.ProtoUnmarshal(msg, rsp) {
|
|
logrus.Error("unmarshal err")
|
|
return
|
|
}
|
|
|
|
// reset main window title
|
|
subTitle := fmt.Sprintf("%s[%s]", sname, sid)
|
|
ui.w.SetTitle(fmt.Sprintf(common.APP_WIN_TITLE, subTitle, ui.app.Metadata().Version, ui.app.Metadata().Build, common.APP_NAME))
|
|
ui.pttService.SetUser(rsp.Data, rsp.Ex)
|
|
// isCreateRole
|
|
if rsp.Data.Created {
|
|
// set statusbar message
|
|
ui.sb.setMessage(common.LABEL_WELCOME + rsp.Data.Binduid)
|
|
// update userinfo
|
|
ui.obs.Notify(observer.EVENT_USERINFO, ui.pttService.GetUser())
|
|
// show toolbar
|
|
ui.tb.toolbar.Show()
|
|
} else {
|
|
// create role
|
|
ui.createRoleWindowPopUp()
|
|
}
|
|
}
|
|
},
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}, ui.w)
|
|
}
|
|
|
|
// createMainWindowMenu create the main window menu
|
|
func (ui *MainWindowImpl) createMainWindowMenu() *fyne.MainMenu {
|
|
|
|
menuItems := []*fyne.MenuItem{}
|
|
|
|
menu := fyne.Menu{
|
|
Label: common.MENU_FILE,
|
|
Items: menuItems,
|
|
}
|
|
|
|
return fyne.NewMainMenu(&menu)
|
|
}
|
|
|
|
// createRoleWindowPopUp
|
|
func (ui *MainWindowImpl) createRoleWindowPopUp() {
|
|
nickname := widget.NewEntry()
|
|
c := container.NewHBox(
|
|
nickname,
|
|
widget.NewButton(common.BUTTON_RANDOM, func() {
|
|
nickname.SetText(randomdata.SillyName())
|
|
}),
|
|
)
|
|
items := []*widget.FormItem{
|
|
widget.NewFormItem(common.LABEL_NICKNAME, c),
|
|
}
|
|
|
|
dialog.ShowForm(common.FORM_TITLE_CREATEROLE, common.BUTTON_OK, common.BUTTON_CANCEL, items, func(b bool) {
|
|
if !b {
|
|
return
|
|
} else {
|
|
if nickname.Text != "" {
|
|
logrus.WithField("nickname", nickname.Text).Debug("submit crete role")
|
|
if code := ui.pttService.CreateRole(nickname.Text); code != pb.ErrorCode_Success {
|
|
err := fmt.Errorf("login err: %v[%d]", code, int32(code))
|
|
dialog.ShowError(err, ui.w)
|
|
} else {
|
|
user := ui.pttService.GetUser()
|
|
logrus.WithField("uid", user.DbUser.Uid).Debug("create role succ")
|
|
ui.obs.Notify(observer.EVENT_USERINFO, ui.pttService.GetUser())
|
|
ui.tb.toolbar.Show()
|
|
}
|
|
}
|
|
}
|
|
}, ui.w)
|
|
}
|
|
|
|
// NewWelcomeLabel
|
|
func (ui *MainWindowImpl) NewWelcomeLabel(data string) {
|
|
p := strings.Split(data, ":")
|
|
if len(p) != 2 {
|
|
logrus.WithField("param", p).Error("choose service")
|
|
return
|
|
}
|
|
c := container.NewCenter(container.NewVBox(
|
|
widget.NewLabelWithStyle(fmt.Sprintf(common.LABEL_CHOOSE, p[0], p[1]),
|
|
fyne.TextAlignCenter,
|
|
fyne.TextStyle{Bold: true}),
|
|
container.NewCenter(container.NewHBox(
|
|
widget.NewButton(common.BUTTON_LOGIN, func() {
|
|
ui.createLoginWin(p[0], p[1])
|
|
}),
|
|
)),
|
|
))
|
|
ui.w.SetContent(c)
|
|
}
|