Added system tray service

This commit is contained in:
2025-06-08 23:07:56 +08:00
parent d5a0b07f2a
commit 8522a47b5f
12 changed files with 136 additions and 646 deletions

View File

@@ -106,6 +106,7 @@ func setDefaults(v *viper.Viper) {
// 通用设置默认值
v.SetDefault("general.always_on_top", defaultConfig.General.AlwaysOnTop)
v.SetDefault("general.data_path", defaultConfig.General.DataPath)
v.SetDefault("general.enable_system_tray", defaultConfig.General.EnableSystemTray)
v.SetDefault("general.enable_global_hotkey", defaultConfig.General.EnableGlobalHotkey)
v.SetDefault("general.global_hotkey.ctrl", defaultConfig.General.GlobalHotkey.Ctrl)
v.SetDefault("general.global_hotkey.shift", defaultConfig.General.GlobalHotkey.Shift)
@@ -240,6 +241,7 @@ func (cs *ConfigService) ResetConfig() {
// 通用设置 - 批量设置到viper中
cs.viper.Set("general.always_on_top", defaultConfig.General.AlwaysOnTop)
cs.viper.Set("general.data_path", defaultConfig.General.DataPath)
cs.viper.Set("general.enable_system_tray", defaultConfig.General.EnableSystemTray)
cs.viper.Set("general.enable_global_hotkey", defaultConfig.General.EnableGlobalHotkey)
cs.viper.Set("general.global_hotkey.ctrl", defaultConfig.General.GlobalHotkey.Ctrl)
cs.viper.Set("general.global_hotkey.shift", defaultConfig.General.GlobalHotkey.Shift)

View File

@@ -120,3 +120,13 @@ func (sm *ServiceManager) GetHotkeyService() *HotkeyService {
func (sm *ServiceManager) GetDialogService() *DialogService {
return sm.dialogService
}
// GetLogger 获取日志服务实例
func (sm *ServiceManager) GetLogger() *log.LoggerService {
return sm.logger
}
// GetConfigService 获取配置服务实例
func (sm *ServiceManager) GetConfigService() *ConfigService {
return sm.configService
}

View File

@@ -0,0 +1,79 @@
package services
import (
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/services/log"
)
// TrayService 系统托盘服务
type TrayService struct {
logger *log.LoggerService
configService *ConfigService
app *application.App
mainWindow *application.WebviewWindow
}
// NewTrayService 创建新的系统托盘服务实例
func NewTrayService(logger *log.LoggerService, configService *ConfigService) *TrayService {
return &TrayService{
logger: logger,
configService: configService,
}
}
// SetAppReferences 设置应用引用
func (ts *TrayService) SetAppReferences(app *application.App, mainWindow *application.WebviewWindow) {
ts.app = app
ts.mainWindow = mainWindow
}
// ShouldMinimizeToTray 检查是否应该最小化到托盘
func (ts *TrayService) ShouldMinimizeToTray() bool {
config, err := ts.configService.GetConfig()
if err != nil {
ts.logger.Error("TrayService: Failed to get config", "error", err)
return true // 默认行为:隐藏到托盘
}
return config.General.EnableSystemTray
}
// HandleWindowClose 处理窗口关闭事件
func (ts *TrayService) HandleWindowClose() {
if ts.ShouldMinimizeToTray() {
// 隐藏到托盘
ts.mainWindow.Hide()
ts.app.EmitEvent("window:hidden", nil)
ts.logger.Info("TrayService: Window hidden to system tray")
} else {
// 直接退出应用
ts.app.Quit()
ts.logger.Info("TrayService: Application quit")
}
}
// HandleWindowMinimize 处理窗口最小化事件
func (ts *TrayService) HandleWindowMinimize() {
if ts.ShouldMinimizeToTray() {
// 隐藏到托盘
ts.mainWindow.Hide()
ts.app.EmitEvent("window:hidden", nil)
ts.logger.Info("TrayService: Window minimized to system tray")
} else {
// 允许正常最小化(不处理,让系统处理)
ts.logger.Info("TrayService: Window minimized normally")
}
}
// ShowWindow 显示主窗口
func (ts *TrayService) ShowWindow() {
if ts.mainWindow != nil {
ts.mainWindow.Show()
ts.mainWindow.Restore()
ts.mainWindow.Focus()
if ts.app != nil {
ts.app.EmitEvent("window:shown", nil)
}
ts.logger.Info("TrayService: Window shown from system tray")
}
}