package main import ( "context" "embed" "log" "sync/atomic" "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options/assetserver" "github.com/wailsapp/wails/v2/pkg/options/windows" "github.com/wailsapp/wails/v2/pkg/runtime" "go_win_template/internal/plugins" "go_win_template/internal/tray" ) //go:embed all:frontend/dist //go:embed assets/icon.png //go:embed build/windows/icon.ico var assets embed.FS var ( trayMgr *tray.Manager forceQuit atomic.Bool ) func main() { wailsApp := NewApp() cfg := &options.App{ Title: "青梧映星", Width: 1280, Height: 800, MinWidth: 1024, MinHeight: 600, WindowStartState: options.Normal, AssetServer: &assetserver.Options{ Assets: assets, }, BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1}, Windows: &windows.Options{ // 窗口标题栏跟随系统白昼/黑夜主题 Theme: windows.SystemDefault, }, OnStartup: func(ctx context.Context) { log.Println("[main] startup complete") // 启动插件 if err := plugins.Start(ctx); err != nil { log.Printf("[main] plugin start error: %v", err) } plugins.PrintStatus() // 启动系统托盘 startTray(ctx) }, OnBeforeClose: func(ctx context.Context) bool { // 真正退出时不再拦截 if forceQuit.Load() { return false } // 托盘可用时,关闭窗口改为隐藏到托盘 if trayMgr != nil && trayMgr.Active() { runtime.WindowHide(ctx) return true } return false }, OnShutdown: func(context.Context) { if trayMgr != nil { trayMgr.Quit() } }, Bind: []interface{}{ wailsApp, }, } err := wails.Run(cfg) if err != nil { log.Fatal(err) } } // startTray 初始化系统托盘。Windows 下托盘图标需为 .ico 格式, // 使用 build/windows/icon.ico;macOS/Linux 可改用 .png。 func startTray(ctx context.Context) { icon, err := assets.ReadFile("build/windows/icon.ico") if err != nil { log.Printf("[tray] load icon failed: %v", err) return } trayMgr = tray.New(tray.Options{ Icon: icon, Tooltip: "青梧映星", OnQuit: func() { forceQuit.Store(true) runtime.Quit(ctx) }, }) trayMgr.Run(ctx) }