Files
work_cation/main.go
T
2024-10-18 15:11:40 +08:00

141 lines
3.4 KiB
Go

package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"work_cation/assets"
"work_cation/global"
"work_cation/service"
"work_cation/version"
"work_cation/views"
)
// 本地缓存 上次打开目录
const preferenceCurrentTutorial = "currentTutorial"
func main() {
global.InitDB()
a := app.NewWithID("io.fyne.workCation")
a.Settings().SetTheme(&assets.MyTheme{})
a.SetIcon(assets.LogoDataSR)
w := a.NewWindow("开发小工具 " + version.NowVersion)
w.Resize(fyne.NewSize(800, 550))
w.SetContent(mainView(w))
w.CenterOnScreen()
w.SetMaster()
w.Show()
a.Run()
}
// 主界面
func mainView(w fyne.Window) fyne.CanvasObject {
service.Zeroconf.StartFindService()
views.ListenChat()
views.StartGameListen()
// 启动服务
if err := service.Server.StartListenServer(); err != nil {
return widget.NewLabel(err.Error())
}
var (
content = container.NewMax()
a = fyne.CurrentApp()
topWindow = w
//title = widget.NewLabel("Component name")
//intro = widget.NewLabel("An introduction would probably go\nhere, as well as a")
)
setTutorial := func(t views.Tutorial) {
if fyne.CurrentDevice().IsMobile() {
child := a.NewWindow(t.Title)
topWindow = child
child.SetContent(t.View(topWindow))
child.Show()
child.SetOnClosed(func() {
topWindow = w
})
return
}
//title.SetText(t.Title)
//intro.SetText(t.Intro)
content.Objects = []fyne.CanvasObject{t.View(w)}
content.Refresh()
}
tutorial := container.NewBorder(nil, nil, nil, nil, content)
//container.NewVBox(title, widget.NewSeparator(), intro)
split := container.NewHSplit(makeNav(setTutorial, true), tutorial)
split.Offset = 0.2
return container.NewBorder(nil, nil, nil, nil, split)
}
// 左侧菜单
func makeNav(setTutorial func(tutorial views.Tutorial), loadPrevious bool) fyne.CanvasObject {
a := fyne.CurrentApp()
tree := &widget.Tree{
ChildUIDs: func(uid string) []string {
return views.TutorialIndex[uid]
},
IsBranch: func(uid string) bool {
children, ok := views.TutorialIndex[uid]
return ok && len(children) > 0
},
CreateNode: func(branch bool) fyne.CanvasObject {
return widget.NewLabel("Collection Widgets")
},
UpdateNode: func(uid string, branch bool, obj fyne.CanvasObject) {
t, ok := views.Tutorials[uid]
if !ok {
fyne.LogError("Missing tutorial panel: "+uid, nil)
return
}
obj.(*widget.Label).SetText(t.Title)
if unsupportedTutorial(t) {
obj.(*widget.Label).TextStyle = fyne.TextStyle{Italic: true}
} else {
obj.(*widget.Label).TextStyle = fyne.TextStyle{}
}
},
OnSelected: func(uid string) {
if t, ok := views.Tutorials[uid]; ok {
if unsupportedTutorial(t) {
return
}
// 存储默认打开
a.Preferences().SetString(preferenceCurrentTutorial, uid)
setTutorial(t)
}
},
}
// 读取存储默认打开
if loadPrevious {
currentPref := a.Preferences().StringWithFallback(preferenceCurrentTutorial, "welcome")
tree.Select(currentPref)
}
//themes := container.NewGridWithColumns(2,
// widget.NewButton("Dark", func() {
// a.Settings().SetTheme(theme.DarkTheme())
// }),
// widget.NewButton("Light", func() {
// a.Settings().SetTheme(theme.LightTheme())
// }),
//)
return container.NewBorder(nil, nil, nil, nil, tree)
}
func unsupportedTutorial(t views.Tutorial) bool {
return !t.SupportWeb && fyne.CurrentDevice().IsBrowser()
}