Skip to content

Commit 5fd1455

Browse files
author
user123456
committed
修复一个系统托盘BUG
1 parent 71b72b7 commit 5fd1455

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

backend/services/tray_service.go

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"runtime"
1010
"sync"
11+
"time"
1112

1213
"github.com/getlantern/systray"
1314
"github.com/skratchdot/open-golang/open"
@@ -159,7 +160,7 @@ func (s *TrayService) onReady() {
159160
// 设置托盘图标
160161
systray.SetIcon(iconData)
161162
systray.SetTitle("host 管理工具")
162-
systray.SetTooltip("host 管理工具 - 双击显示主窗口")
163+
systray.SetTooltip("host 管理工具")
163164

164165
// 创建托盘菜单
165166
mShow := systray.AddMenuItem("显示主界面", "显示主应用界面")
@@ -174,63 +175,73 @@ func (s *TrayService) onReady() {
174175
go s.handleMenuEvents(mShow, mRefreshRemote, mExit)
175176
}
176177

177-
// handleMenuEvents 处理菜单事件
178+
// handleMenuEvents 处理菜单事件 - 优雅的统一事件处理器
178179
func (s *TrayService) handleMenuEvents(mShow, mRefreshRemote, mExit *systray.MenuItem) {
179180
defer func() {
180181
if r := recover(); r != nil {
181182
if s.ctx != nil {
182183
wailsRuntime.LogError(s.ctx, fmt.Sprintf("托盘菜单处理发生错误: %v", r))
183184
}
185+
// 出错后重新启动事件处理
186+
time.Sleep(1 * time.Second)
187+
if !s.isRunning {
188+
return
189+
}
190+
go s.handleMenuEvents(mShow, mRefreshRemote, mExit)
184191
}
185192
}()
186193

187-
// 启动独立的goroutine处理每个菜单项
188-
go func() {
189-
for {
190-
select {
191-
case <-s.stopChan:
192-
return
193-
case <-mShow.ClickedCh:
194-
// 显示主窗口
194+
// 简单防抖:记录上次处理时间
195+
var lastProcessTime time.Time
196+
debounceInterval := 500 * time.Millisecond
197+
198+
// 统一事件循环 - 只用一个goroutine处理所有事件
199+
for {
200+
select {
201+
case <-s.stopChan:
202+
return
203+
204+
case <-mShow.ClickedCh:
205+
// 防抖检查
206+
if time.Since(lastProcessTime) < debounceInterval {
207+
continue
208+
}
209+
lastProcessTime = time.Now()
210+
211+
// 异步处理,避免阻塞事件循环
212+
go func() {
195213
if s.ctx != nil {
196214
wailsRuntime.WindowShow(s.ctx)
197215
wailsRuntime.WindowUnminimise(s.ctx)
198216
wailsRuntime.WindowSetAlwaysOnTop(s.ctx, false)
199217
wailsRuntime.WindowCenter(s.ctx)
200218
}
219+
}()
220+
221+
case <-mRefreshRemote.ClickedCh:
222+
if time.Since(lastProcessTime) < debounceInterval {
223+
continue
201224
}
202-
}
203-
}()
204-
205-
go func() {
206-
for {
207-
select {
208-
case <-s.stopChan:
209-
return
210-
case <-mRefreshRemote.ClickedCh:
211-
// 更新远程源
225+
lastProcessTime = time.Now()
226+
227+
go func() {
212228
if s.ctx != nil {
213229
wailsRuntime.EventsEmit(s.ctx, "tray-refresh-remote")
214230
}
215-
}
216-
}
217-
}()
218-
219-
go func() {
220-
for {
221-
select {
222-
case <-s.stopChan:
223-
return
224-
case <-mExit.ClickedCh:
225-
// 退出应用
231+
}()
232+
233+
case <-mExit.ClickedCh:
234+
// 退出无需防抖,立即处理
235+
go func() {
226236
if s.ctx != nil {
227237
wailsRuntime.LogInfo(s.ctx, "用户从托盘退出应用")
228238
wailsRuntime.Quit(s.ctx)
229239
}
230240
systray.Quit()
231-
}
241+
}()
242+
return
232243
}
233-
}()
244+
}
234245
}
235246

236247
// OpenSystemHostsFile 打开系统 hosts 文件

0 commit comments

Comments
 (0)