314 lines
7.4 KiB
Go
314 lines
7.4 KiB
Go
package ui
|
|
|
|
import (
|
|
"fmt"
|
|
"go_dreamfactory/cmd/v2/lib/common"
|
|
"go_dreamfactory/cmd/v2/service"
|
|
"go_dreamfactory/cmd/v2/service/observer"
|
|
"go_dreamfactory/pb"
|
|
"os"
|
|
"strings"
|
|
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/container"
|
|
"fyne.io/fyne/v2/dialog"
|
|
"fyne.io/fyne/v2/widget"
|
|
"github.com/BabySid/gobase"
|
|
"github.com/Pallinder/go-randomdata"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
globalWin *MainWindowImpl
|
|
)
|
|
|
|
type MainWindow interface {
|
|
WindowInterface
|
|
}
|
|
|
|
type MainWindowImpl struct {
|
|
UIImpl
|
|
WindowDefaultOptions
|
|
w fyne.Window
|
|
mm *mainMenu
|
|
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,
|
|
}
|
|
|
|
// main menu
|
|
// mw.mm = newMainMenu()
|
|
// mw.w.SetMainMenu(mw.mm.MainMenu)
|
|
|
|
// status bar
|
|
mw.sb = newStatusBar()
|
|
|
|
// tool bar
|
|
mw.tb = newToolBar()
|
|
|
|
// main app tabs
|
|
mw.at = newAppContainer(ui.obs)
|
|
|
|
globalWin = mw
|
|
|
|
ui.obs.AddListener(observer.EVENT_PING, observer.Listener{
|
|
OnNotify: func(data interface{}, args ...interface{}) {
|
|
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("退出")
|
|
os.Exit(0)
|
|
},
|
|
})
|
|
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
|
|
ui.tb = newToolBar()
|
|
|
|
// Fun Toys
|
|
ui.toys = newToys(ui.obs)
|
|
|
|
// main app tabs
|
|
ui.at = newAppContainer(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(title string, width, height float32, _ bool) {
|
|
// init window
|
|
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))
|
|
}
|
|
|
|
// create main window menu
|
|
// ui.mm = newMainMenu()
|
|
// w.SetMainMenu(ui.mm.MainMenu)
|
|
|
|
// create window container
|
|
// mainLayout := ui.createWindowContainer()
|
|
|
|
w.SetMaster()
|
|
// w.Show()
|
|
// w.SetContent(mainLayout)
|
|
w.CenterOnScreen()
|
|
_ = ui.createChooseServerPopUp(w)
|
|
}
|
|
|
|
// createChooseServerPopUp
|
|
func (ui *MainWindowImpl) createChooseServerPopUp(w fyne.Window) error {
|
|
ch := make(chan string)
|
|
|
|
selServerWin := ui.createChooseServerWindow("选服", ch)
|
|
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 {
|
|
|
|
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.WithField("server", s.Name).Debug("choose server")
|
|
//conn server
|
|
if err := ui.connService.Connect(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) {
|
|
//form
|
|
account := widget.NewEntry()
|
|
account.Text = "user8080" //default account
|
|
// account.Validator = validation.NewRegexp(`^(\s*)$`, "account required")
|
|
// password := widget.NewPasswordEntry()
|
|
items := []*widget.FormItem{
|
|
widget.NewFormItem(common.LABEL_ACCOUNT, account),
|
|
// widget.NewFormItem("password", password),
|
|
}
|
|
|
|
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")
|
|
if code, rsp := 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.w.SetTitle(fmt.Sprintf("%s[%s]", sname, sid))
|
|
ui.connService.ListenerPush()
|
|
//show mainwindow
|
|
// logrus.Debug(rsp)
|
|
ui.pttService.SetUser(rsp.Data, rsp.Ex)
|
|
// isCreateRole
|
|
if rsp.Data.Created {
|
|
// ui.renderUserContainer()
|
|
ui.createWindowContainer()
|
|
ui.sb.setMessage(common.LABEL_WELCOME + rsp.Data.Binduid)
|
|
appName, err := ui.at.openDefaultApp()
|
|
if err != nil {
|
|
logrus.WithField("appName", appName).Error(err)
|
|
}
|
|
ui.obs.Notify(observer.EVENT_USERINFO, ui.pttService.GetUser())
|
|
} else {
|
|
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 {
|
|
// if rsp.IsSucc {
|
|
user := ui.pttService.GetUser()
|
|
logrus.WithField("uid", user.DbUser.Uid).Debug("create role succ")
|
|
ui.createWindowContainer()
|
|
appName, err := ui.at.openDefaultApp()
|
|
if err != nil {
|
|
logrus.WithField("appName", appName).Error(err)
|
|
}
|
|
ui.obs.Notify(observer.EVENT_USERINFO, ui.pttService.GetUser())
|
|
// }
|
|
}
|
|
}
|
|
}
|
|
}, 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)
|
|
}
|