295 lines
6.8 KiB
Go
295 lines
6.8 KiB
Go
package ui
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"go_dreamfactory/cmd/ptt/lib/common"
|
|
"go_dreamfactory/pb"
|
|
|
|
"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"
|
|
"github.com/spf13/cast"
|
|
)
|
|
|
|
var (
|
|
globalWin *MainWindowImpl
|
|
)
|
|
|
|
type MainWindow interface {
|
|
WindowInterface
|
|
}
|
|
|
|
type MainWindowImpl struct {
|
|
UIImpl
|
|
WindowDefaultOptions
|
|
w fyne.Window
|
|
tb *toolBar //工具条
|
|
toys *toys //
|
|
sb *statusBar //状态栏
|
|
at *appContainer //tabs
|
|
}
|
|
|
|
func NewMainWindow(ui *UIImpl) MainWindow {
|
|
gobase.NewScheduler().Start()
|
|
gobase.RegisterAtExit(gobase.GlobalScheduler.Stop)
|
|
mw := &MainWindowImpl{
|
|
UIImpl: *ui,
|
|
}
|
|
|
|
// status bar
|
|
mw.sb = newStatusBar()
|
|
|
|
// tool bar
|
|
mw.tb = newToolBar()
|
|
|
|
// Fun Toys
|
|
mw.toys = newToys()
|
|
|
|
// main app tabs
|
|
mw.at = newAppContainer()
|
|
|
|
globalWin = mw
|
|
return mw
|
|
}
|
|
|
|
// 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()
|
|
|
|
// main app tabs
|
|
ui.at = newAppContainer()
|
|
content := container.NewBorder(ui.tb.toolbar, ui.sb.widget, nil, ui.toys.widget, ui.at)
|
|
ui.w.SetContent(content)
|
|
}
|
|
|
|
// 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
|
|
w.SetMainMenu(ui.createMainWindowMenu())
|
|
|
|
// 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 int32)
|
|
|
|
selServerWin := ui.createChooseServerWindow("选服", ch)
|
|
go func() {
|
|
sid := <-ch
|
|
selServerWin.Hide()
|
|
ui.NewWelcomeLabel(sid)
|
|
ui.w.Show()
|
|
}()
|
|
return nil
|
|
}
|
|
|
|
// createChooseServerWindow
|
|
func (ui *MainWindowImpl) createChooseServerWindow(
|
|
title string,
|
|
ch chan int32) fyne.Window {
|
|
|
|
makeButton := func(sid int32, w fyne.Window) *widget.Button {
|
|
serverName := "service " + cast.ToString(sid)
|
|
btn := widget.NewButton(serverName, func() {
|
|
d := dialog.NewInformation("req", common.INFO_WAIT, w)
|
|
d.SetDismissText(common.BUTTON_CANCEL)
|
|
d.Show()
|
|
defer d.Hide()
|
|
|
|
logrus.WithField("server", serverName).Debug("choose server")
|
|
ui.sb.setMessage(serverName)
|
|
//conn server
|
|
if err := ui.connService.Connect("ws://localhost:7891/gateway"); err != nil {
|
|
logrus.Error(err)
|
|
derr := errors.New("conn err")
|
|
dialog.ShowError(derr, w)
|
|
} else {
|
|
ch <- sid
|
|
}
|
|
|
|
})
|
|
//other btn setting
|
|
return btn
|
|
}
|
|
|
|
w := ui.app.NewWindow(title)
|
|
box1 := makeButton(1, w)
|
|
box2 := makeButton(2, w)
|
|
box3 := makeButton(3, w)
|
|
box4 := makeButton(4, w)
|
|
|
|
w.SetContent(container.NewGridWithColumns(2, box1, box2, box3, box4))
|
|
w.SetFixedSize(true)
|
|
w.Resize(fyne.NewSize(500, 200))
|
|
w.Show()
|
|
w.CenterOnScreen()
|
|
return w
|
|
}
|
|
|
|
// createLoginWin
|
|
func (ui *MainWindowImpl) createLoginWin(sid int32) {
|
|
//form
|
|
account := widget.NewEntry()
|
|
// 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 {
|
|
//show mainwindow
|
|
logrus.Debug(rsp)
|
|
ui.pttService.SetUser(rsp.Data, rsp.Ex)
|
|
// TODO 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)
|
|
}
|
|
} 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, rsp := 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()
|
|
} else {
|
|
logrus.Error("create role failure")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}, ui.w)
|
|
}
|
|
|
|
// renderUserContainer fill userinfo in container
|
|
func (ui *MainWindowImpl) renderUserContainer() {
|
|
userInfo := ui.pttService.GetUser()
|
|
var (
|
|
nickname string
|
|
)
|
|
|
|
if userInfo != nil && userInfo.DbUser != nil {
|
|
nickname = userInfo.DbUser.Name
|
|
}
|
|
c := container.NewCenter(container.NewHBox(
|
|
widget.NewLabelWithStyle(common.LABEL_NICKNAME+nickname,
|
|
fyne.TextAlignCenter,
|
|
fyne.TextStyle{Bold: true}),
|
|
))
|
|
ui.w.SetContent(c)
|
|
}
|
|
|
|
// NewWelcomeLabel
|
|
func (ui *MainWindowImpl) NewWelcomeLabel(sid int32) {
|
|
c := container.NewCenter(container.NewVBox(
|
|
widget.NewLabelWithStyle(common.LABEL_CHOOSE+cast.ToString(sid),
|
|
fyne.TextAlignCenter,
|
|
fyne.TextStyle{Bold: true}),
|
|
container.NewCenter(container.NewHBox(
|
|
widget.NewButton(common.BUTTON_LOGIN, func() {
|
|
ui.createLoginWin(sid)
|
|
}),
|
|
// widget.NewButton(common.BUTTON_REGISTE, func() {
|
|
// logrus.Debug("registe")
|
|
// }),
|
|
)),
|
|
))
|
|
ui.w.SetContent(c)
|
|
}
|
|
|
|
// show userInfo
|
|
func (ui *MainWindowImpl) showUserInfoWin() {
|
|
|
|
}
|