-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.go
More file actions
142 lines (128 loc) · 3.53 KB
/
Copy pathmain.go
File metadata and controls
142 lines (128 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"embed"
"os"
"path/filepath"
"runtime"
"github.com/energye/systray"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
wailsruntime "github.com/wailsapp/wails/v2/pkg/runtime"
"loris-tunnel/internal/traytext"
"loris-tunnel/internal/uilocale"
)
//go:embed all:frontend/dist
var assets embed.FS
//go:embed build/windows/icon.ico
var trayIconWindows []byte
//go:embed build/macos-systray.png
var trayIconMacOS []byte
//go:embed build/appicon.png
var trayIconFallback []byte
func main() {
// Create an instance of the app structure
app := NewApp()
configDir := "."
if app.storage != nil {
configDir = filepath.Dir(app.storage.Path())
}
localeTag := uilocale.Resolve(configDir)
trayLabels := traytext.ForLocale(localeTag)
showMainWindow := func() {
if app == nil || app.ctx == nil {
return
}
wailsruntime.Show(app.ctx)
wailsruntime.WindowShow(app.ctx)
wailsruntime.WindowUnminimise(app.ctx)
}
startTray, endTray := systray.RunWithExternalLoop(func() {
iconBytes := trayIconFallback
switch runtime.GOOS {
case "windows":
if len(trayIconWindows) > 0 {
iconBytes = trayIconWindows
}
if len(iconBytes) > 0 {
systray.SetIcon(iconBytes)
}
case "darwin":
if len(trayIconMacOS) > 0 {
iconBytes = trayIconMacOS
}
// macOS menu bar icon prefers template icons.
if len(iconBytes) > 0 {
systray.SetTemplateIcon(iconBytes, iconBytes)
}
default:
if len(iconBytes) > 0 {
systray.SetIcon(iconBytes)
}
}
if runtime.GOOS != "darwin" {
systray.SetTitle(trayLabels.AppTitle)
}
systray.SetTooltip(trayLabels.IconTooltip)
showWinItem := systray.AddMenuItem(trayLabels.ShowMainTitle, trayLabels.ShowMainTooltip)
showWinItem.Click(showMainWindow)
quitMenu := systray.AddMenuItem(trayLabels.QuitTitle, trayLabels.QuitTooltip)
quitMenu.Click(func() {
if app != nil && app.ctx != nil {
app.PrepareForQuit()
wailsruntime.Quit(app.ctx)
return
}
// Fallback for edge cases where Wails context isn't ready yet.
os.Exit(0)
})
// 点击图标弹出菜单(与 energye/systray 示例一致)。
// macOS:CreateMenu 把菜单挂到 NSStatusItem,系统负责左键出菜单(在 Wails 下比 SetOnClick 可靠)。
// Windows / Linux:用 ShowMenu 弹出同一套菜单项。
popupTrayMenu := func(menu systray.IMenu) {
if menu != nil {
_ = menu.ShowMenu()
}
}
switch runtime.GOOS {
case "darwin":
systray.CreateMenu()
default:
systray.SetOnClick(popupTrayMenu)
systray.SetOnRClick(popupTrayMenu)
}
app.SetTrayMenuItems(showWinItem, quitMenu)
}, func() {})
startTray()
defer endTray()
// Create application with options
err := wails.Run(&options.App{
Title: "Loris Tunnel",
Width: 1024,
Height: 768,
DisableResize: true,
AssetServer: &assetserver.Options{
Assets: assets,
},
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
OnStartup: app.startup,
OnBeforeClose: app.beforeClose,
OnShutdown: app.shutdown,
HideWindowOnClose: runtime.GOOS == "darwin",
SingleInstanceLock: &options.SingleInstanceLock{
UniqueId: "loris-tunnel-single-instance",
OnSecondInstanceLaunch: func(secondInstanceData options.SecondInstanceData) {
_ = secondInstanceData
showMainWindow()
},
},
Bind: []interface{}{
app,
},
})
if err != nil {
println("Error:", err.Error())
}
// Ensure tray resources are released when the window exits directly.
systray.Quit()
}