-
Notifications
You must be signed in to change notification settings - Fork 0
/
tray.go
202 lines (180 loc) · 5.27 KB
/
tray.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"embed"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"runtime"
"github.com/energye/systray"
"golang.org/x/sys/windows"
)
//go:embed static/*
var staticFiles embed.FS // 嵌入静态文件
// 初始化系统托盘
func initSystray() {
systray.Run(onReady, onExit)
}
func onReady() {
bytes, err := staticFiles.ReadFile("static/icon.ico")
if err == nil {
systray.SetIcon(bytes)
}
systray.SetTitle(AppName)
systray.SetTooltip(AppName)
systray.AddMenuItem(fmt.Sprintf("%s %s", AppName, build), AppName).Click(func() {
// 点击打开主页
_ = openBrowser(AppGitHubRepo)
})
// 分割线
systray.AddSeparator()
coreItem := systray.AddMenuItem(CoreShowName, CoreShowName)
coreItem.Click(func() {
// 点击打开主页
_ = openBrowser("https://github.com/MetaCubeX/mihomo")
})
sysProxyItem := systray.AddMenuItemCheckbox("System Proxy", "Set or Unset", getProxyEnable())
sysProxyItem.Click(func() {
if sysProxyItem.Checked() {
if unsetProxy() {
sysProxyItem.Uncheck()
}
} else {
if setCoreProxy() {
sysProxyItem.Check()
}
}
})
restartCoreItem := systray.AddMenuItem("Restart Core", "Restart Core")
restartCoreItem.Click(func() {
// 重新加载核心配置
if err := loadCoreConfig(); err != nil {
messageBoxAlert(AppName, fmt.Sprint(err))
return
}
if restartCore() {
if sysProxyItem != nil && sysProxyItem.Checked() {
// 重新设置代理
setCoreProxy()
}
} else {
messageBoxAlert(AppName, "Failed to restart core")
}
})
systray.AddMenuItem("Edit Config", "Edit Config").Click(func() {
// 打开配置文件
_ = openBrowser(coreConfigPath)
})
dashboardItem := systray.AddMenuItem("Core Dashboard", "Core Dashboard")
dashboardItem.AddSubMenuItem("External UI", "External UI").Click(func() {
_ = openBrowser(coreConfig.ExternalUiAddr)
})
dashboardItem.AddSubMenuItem("Official UI", "Official UI").Click(func() {
_ = openBrowser(coreConfig.OfficialUiAddr)
})
dashboardItem.AddSubMenuItem("YACD UI", "YACD UI").Click(func() {
_ = openBrowser(coreConfig.YACDUiAddr)
})
// 分割线
systray.AddSeparator()
openItem := systray.AddMenuItem("Open", "Open")
// 打开本地工作目录
openItem.AddSubMenuItem("Work Directory", "Open Work Directory").Click(func() {
_ = openDirectory(workDir)
})
var openShellFn = func(shell string) {
cmd := exec.Command(shell)
cmd.Dir = workDir
// 设置代理环境变量
cmd.Env = append(os.Environ(),
fmt.Sprintf("HTTP_PROXY=http://127.0.0.1:%d", coreConfig.HttpProxyPort),
fmt.Sprintf("HTTPS_PROXY=http://127.0.0.1:%d", coreConfig.HttpProxyPort))
cmd.SysProcAttr = &windows.SysProcAttr{
CreationFlags: windows.CREATE_NEW_CONSOLE | windows.CREATE_UNICODE_ENVIRONMENT | windows.CREATE_NEW_PROCESS_GROUP,
}
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
messageBoxAlert(AppName, fmt.Sprintf("Failed to start %s: %v", shell, err))
}
}
// 打开powershell
openItem.AddSubMenuItem("PowerShell", "Open PowerShell").Click(func() {
ps := "pwsh.exe"
// 先判断 pwsh.exe 是否在环境变量内存在
if _, err := exec.LookPath(ps); err != nil {
// 不存在使用系统默认的 PowerShell
ps = "powershell.exe"
}
openShellFn(ps)
})
// 打开命令行
openItem.AddSubMenuItem("Command Prompt", "Open Command Prompt").Click(func() {
openShellFn("cmd.exe")
})
// 分割线
systray.AddSeparator()
systray.AddMenuItem("Check Update", "Check Update").Click(func() {
go func() {
resp, err := http.Get(fmt.Sprintf("https://ghp.ci/%s/releases/latest/download/version.txt", AppGitHubRepo))
if err != nil {
messageBoxAlert(AppName, fmt.Sprintf("Failed to check update: %v", err))
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
messageBoxAlert(AppName, fmt.Sprintf("Failed to read response: %v", err))
return
}
latestVersion := string(body)
if latestVersion != "" && latestVersion != build {
if messageBoxConfirm(AppName, fmt.Sprintf("New version available: %s\nDo you want to download it?", latestVersion)) {
_ = openBrowser(fmt.Sprintf("%s/releases/latest", AppGitHubRepo))
}
} else {
messageBoxAlert(AppName, "You are using the latest version.")
}
}()
})
systray.AddMenuItem("About", "About").Click(func() {
about := fmt.Sprintf(`Name: %s
Description: %s
Build Hash: %s
Go Version: %s
---
Work Directory: %s
Log Directory: %s
Core Directory: %s
Core Path: %s
Core Version: %s
Config Path: %s`,
AppName, "Wrapper for Mihomo written in Golang.", build, runtime.Version(), workDir, logDir, coreDir, corePath, getCoreVersion(), coreConfigPath)
messageBoxAlert(AppName, about)
})
exitItem := systray.AddMenuItem("Exit", "Exit")
exitItem.Click(func() { systray.Quit() })
// 托盘点击事件处理函数
var clickFn = func(menu systray.IMenu) {
if menu != nil {
go func() {
coreItem.SetTitle(fmt.Sprintf("%s %s", CoreShowName, getCoreVersion()))
}()
_ = menu.ShowMenu()
}
}
// 左键点击托盘时显示菜单
systray.SetOnClick(clickFn)
// 右键点击托盘
systray.SetOnRClick(clickFn)
}
func onExit() {
// 退出程序后的处理操作
// 清理pid文件,写入-1
_ = os.WriteFile(getPidFilePath(), []byte("-1"), 0644)
unsetProxy()
stopCore()
os.Exit(0)
}