Skip to content

Commit c7c0908

Browse files
committed
fix: transition systray to external loop and simplify exit logic
1 parent 9ca7189 commit c7c0908

7 files changed

Lines changed: 51 additions & 54 deletions

File tree

bridge/bridge.go

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
sysruntime "runtime"
1515

16-
"github.com/energye/systray"
1716
"github.com/wailsapp/wails/v2/pkg/menu"
1817
"github.com/wailsapp/wails/v2/pkg/menu/keys"
1918
"github.com/wailsapp/wails/v2/pkg/options"
@@ -25,10 +24,11 @@ var Config = &AppConfig{}
2524

2625
var Env = &EnvResult{
2726
IsStartup: true,
27+
PreventExit: true,
2828
FromTaskSch: false,
2929
WebviewPath: "",
3030
AppName: "",
31-
AppVersion: "v1.20.0-dev",
31+
AppVersion: "v1.20.0-dev.2",
3232
BasePath: "",
3333
OS: sysruntime.GOOS,
3434
ARCH: sysruntime.GOARCH,
@@ -42,7 +42,7 @@ func NewApp() *App {
4242
}
4343
}
4444

45-
func CreateApp(fs embed.FS, icon []byte) *App {
45+
func CreateApp(fs embed.FS) *App {
4646
exePath, err := os.Executable()
4747
if err != nil {
4848
panic(err)
@@ -74,8 +74,6 @@ func CreateApp(fs embed.FS, icon []byte) *App {
7474

7575
loadConfig()
7676

77-
go createTray(app, icon)
78-
7977
return app
8078
}
8179

@@ -89,8 +87,7 @@ func (a *App) IsStartup() bool {
8987

9088
func (a *App) ExitApp() {
9189
log.Printf("ExitApp")
92-
Env.PreventExit.Store(true)
93-
systray.Quit()
90+
Env.PreventExit = false
9491
runtime.Quit(a.Ctx)
9592
}
9693

@@ -111,7 +108,7 @@ func (a *App) RestartApp() FlagResult {
111108
}
112109

113110
func (a *App) GetEnv() EnvResult {
114-
log.Printf("EnvResult")
111+
log.Printf("GetEnv")
115112
return EnvResult{
116113
AppName: Env.AppName,
117114
AppVersion: Env.AppVersion,
@@ -144,35 +141,6 @@ func (a *App) ShowMainWindow() {
144141
runtime.WindowShow(a.Ctx)
145142
}
146143

147-
func createTray(app *App, icon []byte) {
148-
sysruntime.LockOSThread()
149-
defer sysruntime.UnlockOSThread()
150-
151-
systray.Run(func() {
152-
systray.SetIcon(icon)
153-
systray.SetTooltip("GUI.for.Cores")
154-
155-
systray.SetOnRClick(func(menu systray.IMenu) { menu.ShowMenu() })
156-
systray.SetOnClick(func(menu systray.IMenu) {
157-
if Env.OS == "darwin" {
158-
menu.ShowMenu()
159-
} else {
160-
app.ShowMainWindow()
161-
}
162-
})
163-
164-
addClickMenuItem := func(title, tooltip string, action func()) {
165-
m := systray.AddMenuItem(title, tooltip)
166-
m.Click(action)
167-
}
168-
169-
// Ensure the tray is still available if rolling-release fails
170-
addClickMenuItem("Show", "Show", func() { app.ShowMainWindow() })
171-
addClickMenuItem("Restart", "Restart", func() { app.RestartApp() })
172-
addClickMenuItem("Exit", "Exit", func() { app.ExitApp() })
173-
}, nil)
174-
}
175-
176144
func createMacOSSymlink() {
177145
user, _ := user.Current()
178146
linkPath := Env.BasePath + "/data"

bridge/tray.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ import (
88
"github.com/wailsapp/wails/v2/pkg/runtime"
99
)
1010

11+
func CreateTray(a *App, icon []byte) (trayStart, trayEnd func()) {
12+
return systray.RunWithExternalLoop(func() {
13+
systray.SetIcon(icon)
14+
systray.SetTooltip("GUI.for.Cores")
15+
16+
systray.SetOnRClick(func(menu systray.IMenu) { menu.ShowMenu() })
17+
systray.SetOnClick(func(menu systray.IMenu) {
18+
if Env.OS == "darwin" {
19+
menu.ShowMenu()
20+
} else {
21+
a.ShowMainWindow()
22+
}
23+
})
24+
25+
addClickMenuItem := func(title, tooltip string, action func()) {
26+
m := systray.AddMenuItem(title, tooltip)
27+
m.Click(action)
28+
}
29+
30+
// Ensure the tray is still available if rolling-release fails
31+
addClickMenuItem("Show", "Show", func() { a.ShowMainWindow() })
32+
addClickMenuItem("Restart", "Restart", func() { a.RestartApp() })
33+
addClickMenuItem("Exit", "Exit", func() { a.ExitApp() })
34+
}, nil)
35+
}
36+
1137
func (a *App) UpdateTray(tray TrayContent) {
1238
log.Printf("UpdateTray")
1339
updateTray(a, tray)

bridge/types.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package bridge
33
import (
44
"context"
55
"net/http"
6-
"sync/atomic"
76

87
"github.com/wailsapp/wails/v2/pkg/menu"
98
)
@@ -15,16 +14,16 @@ type App struct {
1514
}
1615

1716
type EnvResult struct {
18-
IsStartup bool `json:"-"`
19-
PreventExit atomic.Bool `json:"-"`
20-
FromTaskSch bool `json:"-"`
21-
WebviewPath string `json:"-"`
22-
AppName string `json:"appName"`
23-
AppVersion string `json:"appVersion"`
24-
BasePath string `json:"basePath"`
25-
OS string `json:"os"`
26-
ARCH string `json:"arch"`
27-
IsPrivileged bool `json:"isPrivileged"`
17+
IsStartup bool `json:"-"`
18+
PreventExit bool `json:"-"`
19+
FromTaskSch bool `json:"-"`
20+
WebviewPath string `json:"-"`
21+
AppName string `json:"appName"`
22+
AppVersion string `json:"appVersion"`
23+
BasePath string `json:"basePath"`
24+
OS string `json:"os"`
25+
ARCH string `json:"arch"`
26+
IsPrivileged bool `json:"isPrivileged"`
2827
}
2928

3029
type RequestOptions struct {

frontend/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
VITE_APP_TITLE = GUI.for.SingBox
2-
VITE_APP_VERSION = v1.20.0-dev
2+
VITE_APP_VERSION = v1.20.0-dev.2
33
VITE_APP_PROJECT_URL = https://github.com/GUI-for-Cores/GUI.for.SingBox
44
VITE_APP_VERSION_API = https://api.github.com/repos/GUI-for-Cores/GUI.for.SingBox/releases/latest
55
VITE_APP_LOCALES_URL = https://github.com/GUI-for-Cores/GUI-for-Cores.github.io/tree/main/app-resources/locales/gfs

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ require (
5858
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
5959
)
6060

61-
replace github.com/energye/systray => github.com/GUI-for-Cores/systray v1.0.0
61+
replace github.com/energye/systray => github.com/GUI-for-Cores/systray v1.0.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
git.sr.ht/~jackmordaunt/go-toast v1.1.2 h1:/yrfI55LRt1M7H1vkaw+NaH1+L1CDxrqDltwm5euVuE=
22
git.sr.ht/~jackmordaunt/go-toast v1.1.2/go.mod h1:jA4OqHKTQ4AFBdwrSnwnskUIIS3HYzlJSgdzCKqfavo=
3-
github.com/GUI-for-Cores/systray v1.0.0 h1:B9FduSyTVQuYtvFBRpbEbhFBGZ0HkCbH3L74a5SuyO4=
4-
github.com/GUI-for-Cores/systray v1.0.0/go.mod h1:HelKhC3PXwv3ryDxbuQqV+7kAxAYNzE5cfdrerGOZTc=
3+
github.com/GUI-for-Cores/systray v1.0.1 h1:nJEIsm3yHoYhQD7PnUxCD2bOP/+axLt/bWLWHB8OWT8=
4+
github.com/GUI-for-Cores/systray v1.0.1/go.mod h1:HelKhC3PXwv3ryDxbuQqV+7kAxAYNzE5cfdrerGOZTc=
55
github.com/bep/debounce v1.2.1 h1:v67fRdBA9UQu2NhLFXrSg0Brw7CexQekrBwDMM8bzeY=
66
github.com/bep/debounce v1.2.1/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0=
77
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=

main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ var assets embed.FS
2323
var icon []byte
2424

2525
func main() {
26-
app := bridge.CreateApp(assets, icon)
26+
app := bridge.CreateApp(assets)
27+
28+
trayStart, trayEnd := bridge.CreateTray(app, icon)
2729

2830
// Create application with options
2931
err := wails.Run(&options.App{
@@ -79,9 +81,11 @@ func main() {
7981
},
8082
OnStartup: func(ctx context.Context) {
8183
app.Ctx = ctx
84+
trayStart()
8285
},
8386
OnBeforeClose: func(ctx context.Context) (prevent bool) {
84-
if bridge.Env.PreventExit.Load() {
87+
if !bridge.Env.PreventExit {
88+
trayEnd()
8589
return false
8690
}
8791
runtime.EventsEmit(ctx, "onBeforeExitApp")

0 commit comments

Comments
 (0)