go_dreamfactory/cmd/v2/ui/perfwindow.go
2022-12-07 19:12:48 +08:00

108 lines
2.4 KiB
Go

package ui
import (
"fmt"
"go_dreamfactory/cmd/v2/lib/common"
"go_dreamfactory/pb"
"reflect"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
)
var perfWin *PerfWindowImpl
var pbMap map[string]proto.Message //pb
type PerfWindow interface {
WindowInterface
}
type PerfWindowImpl struct {
UIImpl
parent fyne.Window
w fyne.Window
statusbar *statusBar //状态栏
tabs *appContainer //tabs
toolbar *toolBar //工具条
}
func NewPerfWindow(ui *UIImpl, parent fyne.Window) PerfWindow {
pw := &PerfWindowImpl{
UIImpl: *ui,
parent: parent,
}
perfWin = pw
pbMap = make(map[string]proto.Message)
pw.initPb()
return pw
}
func (ui *PerfWindowImpl) CreateWindow(_ string, width, height float32, _ bool) {
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
w.Resize(fyne.NewSize(width, height))
w.CenterOnScreen()
w.SetCloseIntercept(func() {
ui.parent.Show()
})
ui.statusbar = newStatusBar()
toolbar := widget.NewToolbar(
// widget.NewToolbarAction(theme.MediaVideoIcon(), func() {
// openApp(ui.tabs, common.TOOLBAR_PERF_TIP)
// }),
// widget.NewToolbarAction(theme.MediaVideoIcon(), func() {
// openApp(ui.tabs, common.TOOLBAR_PERF_CONF)
// }),
)
ui.toolbar = newToolBar(toolbar)
ui.tabs = newAppContainer(perfRegister, ui.pttService, ui.obs)
content := container.NewBorder(ui.toolbar.toolbar, ui.statusbar.widget, nil, nil, ui.tabs)
ui.w.SetContent(content)
defer func() {
appName, err := ui.tabs.openDefaultApp(common.TOOLBAR_PERF_TIP)
if err != nil {
logrus.WithField("appName", appName).Error(err)
}
}()
w.SetCloseIntercept(func() {
ui.parent.Show()
w.Close()
})
w.Show()
}
func (ui *PerfWindowImpl) registerPb(route string, pb proto.Message) {
typeOf := reflect.TypeOf(pb)
pbName := typeOf.Elem().Name()
if strings.HasSuffix(pbName, "Req") {
s := strings.SplitN(route, ".", 2)
low := strings.ToLower(pbName)
if strings.Contains(low, s[0]) &&
strings.Contains(low, s[1]) {
pbMap[route] = pb
}
}
}
func (ui *PerfWindowImpl) initPb() {
ui.registerPb("user.login", &pb.UserLoginReq{})
ui.registerPb("user.create", &pb.UserCreateReq{})
ui.registerPb("sys.funclist", &pb.SysFuncListReq{})
}