-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtray.go
More file actions
78 lines (69 loc) · 1.81 KB
/
Copy pathtray.go
File metadata and controls
78 lines (69 loc) · 1.81 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
package main
import (
"context"
_ "embed"
"github.com/getlantern/systray"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
//go:embed build/windows/icon.ico
var trayIcon []byte
var (
trayCtx context.Context
trayQuitting bool
wailsReady bool // Wails 运行时已就绪(避免未启动时发事件)
)
// startTray 启动系统托盘(内部自带消息循环,goroutine 中运行)
func startTray(ctx context.Context) {
trayCtx = ctx
go systray.Run(func() {
systray.SetIcon(trayIcon)
systray.SetTitle("文件管理器")
systray.SetTooltip("文件管理器")
// 左键点击托盘图标:显示主窗口(右键仍弹出菜单)
systray.SetOnClick(func() {
showWindow()
})
mShow := systray.AddMenuItem("显示窗口", "显示主窗口")
mQuit := systray.AddMenuItem("退出", "退出文件管理器")
go func() {
for {
select {
case <-mShow.ClickedCh:
showWindow()
case <-mQuit.ClickedCh:
quitApp()
}
}
}()
}, func() {
// 托盘退出清理:卸载 Win+E 钩子
applyWinEHook(false)
})
}
// showWindow 显示并前置主窗口
func showWindow() {
runtime.WindowShow(trayCtx)
runtime.EventsEmit(trayCtx, "show-window") // 通知前端恢复标签(可能已被关闭)
}
// quitApp 真正退出应用
func quitApp() {
trayQuitting = true
// 先主动销毁托盘图标,避免退出后右下角残留图标
systray.Quit()
runtime.Quit(trayCtx)
}
// isQuitting 是否处于主动退出流程(区别于关闭窗口)
func isQuitting() bool {
return trayQuitting
}
// beforeClose 窗口关闭拦截:设置为托盘时隐藏窗口而不是退出
func (a *App) beforeClose(ctx context.Context) bool {
if isQuitting() {
return false
}
if s, err := LoadSettings(); err == nil && s.CloseToTray {
runtime.WindowHide(ctx)
return true
}
return false
}