-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtray.go
More file actions
142 lines (123 loc) · 3.27 KB
/
tray.go
File metadata and controls
142 lines (123 loc) · 3.27 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 (
"os"
goruntime "runtime"
"strings"
"fyne.io/systray"
"github.com/wailsapp/wails/v2/pkg/runtime"
"skillui/internal/platform"
)
// TrayManager manages the system tray icon and menu
type TrayManager struct {
app *App
mShow *systray.MenuItem
mQuit *systray.MenuItem
}
// NewTrayManager creates a new TrayManager
func NewTrayManager(app *App) *TrayManager {
return &TrayManager{
app: app,
}
}
// getLocalizedLabels returns menu labels based on app locale setting
func (t *TrayManager) getLocalizedLabels() (showLabel, quitLabel string) {
showLabel = "Show Window"
quitLabel = "Quit"
// Check app configuration first
if t.app != nil {
if t.app.config.Locale == "zh" {
showLabel = "显示界面"
quitLabel = "退出"
}
} else if isChineseLocale() {
// Fallback to system locale
showLabel = "显示界面"
quitLabel = "退出"
}
return showLabel, quitLabel
}
// UpdateLanguage updates the menu labels based on current locale
func (t *TrayManager) UpdateLanguage() {
if t.mShow == nil || t.mQuit == nil {
return
}
showLabel, quitLabel := t.getLocalizedLabels()
t.mShow.SetTitle(showLabel)
t.mQuit.SetTitle(quitLabel)
}
// isChineseLocale checks if the system is using Chinese locale
func isChineseLocale() bool {
// Check common environment variables for locale
for _, envVar := range []string{"LANG", "LC_ALL", "LC_MESSAGES", "LANGUAGE"} {
if lang := os.Getenv(envVar); lang != "" {
lang = strings.ToLower(lang)
if strings.HasPrefix(lang, "zh") {
return true
}
}
}
return false
}
// onReady is called when systray is ready
func (t *TrayManager) onReady() {
// Use different tray icons for different platforms
// macOS: Black template icon (transparent background)
// Windows: Colored ICO format icon
if goruntime.GOOS == "windows" {
systray.SetIcon(trayIconWindows)
} else {
systray.SetIcon(trayIcon)
}
systray.SetTooltip("SkillUI - AI Skills Manager")
// Get labels based on app configuration
showLabel, quitLabel := t.getLocalizedLabels()
// Create menu items with localized labels
t.mShow = systray.AddMenuItem(showLabel, "Show the main window")
systray.AddSeparator()
t.mQuit = systray.AddMenuItem(quitLabel, "Quit the application")
// Handle menu clicks in a goroutine
go func() {
for {
select {
case <-t.mShow.ClickedCh:
t.showWindow()
case <-t.mQuit.ClickedCh:
t.quitApp()
return
}
}
}()
}
// onExit is called when systray is about to exit
func (t *TrayManager) onExit() {
// Cleanup if needed
}
// showWindow shows the main application window and Dock icon
func (t *TrayManager) showWindow() {
if t.app != nil && t.app.ctx != nil {
// Show Dock icon first (macOS)
platform.ShowDockIcon()
// Then show the window
runtime.WindowShow(t.app.ctx)
// On macOS, bring window to front
if goruntime.GOOS == "darwin" {
runtime.WindowSetAlwaysOnTop(t.app.ctx, true)
runtime.WindowSetAlwaysOnTop(t.app.ctx, false)
}
}
}
// quitApp properly quits the application
func (t *TrayManager) quitApp() {
if t.app != nil && t.app.ctx != nil {
runtime.Quit(t.app.ctx)
}
systray.Quit()
}
// Run starts the systray - should be called in a goroutine
func (t *TrayManager) Run() {
systray.Run(t.onReady, t.onExit)
}
// Quit stops the systray
func (t *TrayManager) Quit() {
systray.Quit()
}