248 lines
6.0 KiB
Go
248 lines
6.0 KiB
Go
package ui
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"go_dreamfactory/cmd/v2/lib/common"
|
|
"go_dreamfactory/cmd/v2/model"
|
|
"go_dreamfactory/cmd/v2/service"
|
|
"go_dreamfactory/cmd/v2/service/observer"
|
|
"go_dreamfactory/utils"
|
|
"io/ioutil"
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/container"
|
|
"fyne.io/fyne/v2/data/binding"
|
|
"fyne.io/fyne/v2/dialog"
|
|
"fyne.io/fyne/v2/layout"
|
|
"fyne.io/fyne/v2/storage"
|
|
"fyne.io/fyne/v2/theme"
|
|
"fyne.io/fyne/v2/widget"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type appPbGen struct {
|
|
appAdapter
|
|
folderList *folderList
|
|
folderChk *widget.List
|
|
}
|
|
|
|
func (this *appPbGen) LazyInit(obs observer.Observer) error {
|
|
this.tabItem = container.NewTabItemWithIcon(common.TOOLBAR_PB, theme.ContentAddIcon(), nil)
|
|
this.folderList = NewFolderList()
|
|
this.folderChk = this.folderList.createList()
|
|
|
|
countLabel := widget.NewLabel("")
|
|
|
|
//load
|
|
pcm := service.GetDbService().GetPbConf()
|
|
logrus.Debugf("%v", pcm)
|
|
|
|
// 打开目录
|
|
openFolder := func(entry *widget.Entry) {
|
|
dConf := dialog.NewFolderOpen(func(lu fyne.ListableURI, err error) {
|
|
if lu == nil {
|
|
return
|
|
}
|
|
entry.Text = lu.Path()
|
|
entry.Refresh()
|
|
}, toolWin.w)
|
|
luri, _ := storage.ListerForURI(storage.NewFileURI("."))
|
|
dConf.SetLocation(luri)
|
|
dConf.SetConfirmText("打开")
|
|
dConf.SetDismissText("取消")
|
|
dConf.Resize(fyne.NewSize(750, 500))
|
|
dConf.Show()
|
|
}
|
|
|
|
content := container.NewMax()
|
|
content.Objects = []fyne.CanvasObject{}
|
|
|
|
protoDir := widget.NewEntry()
|
|
protoDir.PlaceHolder = "proto目录"
|
|
protoDir.OnChanged = func(s string) {
|
|
this.folderList.initItem(protoDir.Text)
|
|
countLabel.SetText(fmt.Sprintf("总数:%v", this.folderList.fileTotal))
|
|
}
|
|
|
|
outDir := widget.NewEntry()
|
|
outDir.PlaceHolder = "go输出目录"
|
|
|
|
form := widget.NewForm(
|
|
widget.NewFormItem("proto目录", container.NewBorder(nil, nil, nil, widget.NewButtonWithIcon("", theme.FolderIcon(), func() {
|
|
openFolder(protoDir)
|
|
this.folderList.initItem(protoDir.Text)
|
|
}), protoDir)),
|
|
widget.NewFormItem("输出目录", container.NewBorder(nil, nil, nil, widget.NewButtonWithIcon("", theme.FolderIcon(), func() {
|
|
openFolder(outDir)
|
|
}), outDir)),
|
|
)
|
|
|
|
if pcm != nil {
|
|
protoDir.SetText(pcm.ProtoDir)
|
|
outDir.SetText(pcm.OutputDir)
|
|
}
|
|
|
|
genBtn := &widget.Button{Text: "生成", Icon: theme.ConfirmIcon()}
|
|
|
|
genBtn.OnTapped = func() {
|
|
genBtn.Disable()
|
|
defer func() {
|
|
genBtn.Enable()
|
|
}()
|
|
if runtime.GOOS != "windows" {
|
|
dialog.ShowError(errors.New("no support "+runtime.GOOS), toolWin.w)
|
|
return
|
|
}
|
|
dialog.ShowConfirm("确认",
|
|
strings.Join(this.folderList.selItemIds, ","), func(b bool) {
|
|
if !b {
|
|
return
|
|
}
|
|
|
|
if outDir.Text == "" {
|
|
dialog.ShowError(errors.New("请设置输出目录"), toolWin.w)
|
|
return
|
|
}
|
|
|
|
if protoDir.Text == "" {
|
|
dialog.ShowError(errors.New("请设置proto目录"), toolWin.w)
|
|
return
|
|
}
|
|
|
|
commandStr1 := `protoc --proto_path=%s --go_out=%s --go_opt=paths=import %s\*.proto`
|
|
commandStr2 := `protoc --proto_path=%s --go_out=%s --go_opt=paths=import %s\%s\*.proto`
|
|
var arg1, arg2 string
|
|
arg1 = fmt.Sprintf(commandStr1,
|
|
protoDir.Text,
|
|
outDir.Text,
|
|
protoDir.Text,
|
|
)
|
|
for _, name := range this.folderList.selItemIds {
|
|
arg2 = fmt.Sprintf(commandStr2,
|
|
protoDir.Text,
|
|
outDir.Text,
|
|
protoDir.Text,
|
|
name,
|
|
)
|
|
}
|
|
logrus.Debug(arg1)
|
|
logrus.Debug(arg2)
|
|
c1 := exec.Command("cmd.exe", "/c", arg1)
|
|
if err := c1.Run(); err != nil {
|
|
dialog.ShowError(err, toolWin.w)
|
|
return
|
|
}
|
|
c2 := exec.Command("cmd.exe", "/c", arg2)
|
|
if err := c2.Run(); err != nil {
|
|
dialog.ShowError(err, toolWin.w)
|
|
return
|
|
}
|
|
|
|
}, toolWin.w)
|
|
}
|
|
|
|
confBtn := &widget.Button{Text: "保存配置", Icon: theme.DocumentSaveIcon()}
|
|
confBtn.OnTapped = func() {
|
|
if err := service.GetDbService().SavePbConf(&model.PbConfModel{
|
|
ProtoDir: protoDir.Text,
|
|
OutputDir: outDir.Text,
|
|
}); err != nil {
|
|
logrus.WithField("err", err).Debug("保存配置")
|
|
}
|
|
logrus.Debug("save pb conf")
|
|
}
|
|
// layout
|
|
c := container.NewBorder(
|
|
form,
|
|
container.NewHBox(confBtn, genBtn, layout.NewSpacer(), countLabel), nil, nil,
|
|
container.NewMax(
|
|
container.NewVScroll(this.folderChk),
|
|
),
|
|
)
|
|
|
|
content.Objects = append(content.Objects, c)
|
|
this.tabItem.Content = content
|
|
return nil
|
|
}
|
|
|
|
func (a *appPbGen) GetAppName() string {
|
|
return common.TOOLBAR_PB
|
|
}
|
|
|
|
type folderList struct {
|
|
dataBinding binding.UntypedList
|
|
selItemIds []string //选择的ID
|
|
itemListData *model.ItemModelList
|
|
fileTotal int //文件总数
|
|
}
|
|
|
|
func NewFolderList() *folderList {
|
|
return &folderList{
|
|
dataBinding: binding.NewUntypedList(),
|
|
}
|
|
}
|
|
|
|
func (f *folderList) createList() *widget.List {
|
|
return widget.NewListWithData(f.dataBinding,
|
|
func() fyne.CanvasObject {
|
|
return container.NewHBox(
|
|
// &widget.Check{Checked: true},
|
|
widget.NewCheck("", func(b bool) {}),
|
|
widget.NewLabelWithStyle("", fyne.TextAlignCenter, fyne.TextStyle{}),
|
|
widget.NewLabel(""),
|
|
)
|
|
},
|
|
func(data binding.DataItem, item fyne.CanvasObject) {
|
|
o, _ := data.(binding.Untyped).Get()
|
|
pd := o.(*model.ItemModel)
|
|
item.(*fyne.Container).Objects[0].(*widget.Check).OnChanged = func(b bool) {
|
|
if b {
|
|
f.selItemIds = append(f.selItemIds, pd.Id)
|
|
} else {
|
|
f.selItemIds = utils.DeleteString(f.selItemIds, pd.Id)
|
|
}
|
|
}
|
|
item.(*fyne.Container).Objects[1].(*widget.Label).SetText(pd.Label)
|
|
},
|
|
)
|
|
}
|
|
|
|
func (f *folderList) initItem(dir string) {
|
|
f.itemListData = model.NewItemModelList()
|
|
|
|
files, err := ioutil.ReadDir(dir)
|
|
if err != nil {
|
|
logrus.Error(err)
|
|
return
|
|
}
|
|
|
|
for _, file := range files {
|
|
if file.IsDir() {
|
|
if file.Name() == ".vscode" {
|
|
continue
|
|
}
|
|
fm := &model.ItemModel{
|
|
Id: file.Name(),
|
|
Label: file.Name(),
|
|
}
|
|
f.itemListData.DataList = append(f.itemListData.DataList, fm)
|
|
// f.selItemIds = append(f.selItemIds, fm.Id)
|
|
f.fileTotal++
|
|
// logrus.Debugf("%v", fm.Id)
|
|
}
|
|
}
|
|
|
|
f.reloadListData()
|
|
}
|
|
|
|
func (f *folderList) reloadListData() {
|
|
if f.itemListData != nil {
|
|
d := f.itemListData.AsInterfaceArray()
|
|
f.dataBinding.Set(d)
|
|
}
|
|
}
|