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

@@ -2,13 +2,14 @@ package events
import (
"time"
"voidraft/internal/services"
"github.com/wailsapp/wails/v3/pkg/application"
wailsevents "github.com/wailsapp/wails/v3/pkg/events"
)
// RegisterTrayEvents 注册与系统托盘相关的所有事件
func RegisterTrayEvents(app *application.App, systray *application.SystemTray, mainWindow *application.WebviewWindow) {
func RegisterTrayEvents(app *application.App, systray *application.SystemTray, mainWindow *application.WebviewWindow, trayService *services.TrayService) {
// 不附加窗口到系统托盘,避免失去焦点自动缩小
// systray.AttachWindow(mainWindow)
@@ -17,31 +18,25 @@ func RegisterTrayEvents(app *application.App, systray *application.SystemTray, m
// 设置点击托盘图标显示主窗口
systray.OnClick(func() {
mainWindow.Show()
mainWindow.Restore()
mainWindow.Focus()
// 通知前端窗口已显示
app.EmitEvent("window:shown", nil)
trayService.ShowWindow()
})
// 处理窗口关闭事件 - 隐藏到托盘
// 处理窗口关闭事件 - 根据配置决定是隐藏到托盘还是直接退出
mainWindow.RegisterHook(wailsevents.Common.WindowClosing, func(event *application.WindowEvent) {
// 取消默认关闭行为
event.Cancel()
// 隐藏窗口到托盘
mainWindow.Hide()
// 通知前端窗口已隐藏
app.EmitEvent("window:hidden", nil)
// 使用托盘服务处理关闭事件
trayService.HandleWindowClose()
})
// 处理窗口最小化事件 - 也隐藏到托盘
// 处理窗口最小化事件 - 根据配置决定是隐藏到托盘还是正常最小化
mainWindow.RegisterHook(wailsevents.Common.WindowMinimise, func(event *application.WindowEvent) {
// 取消默认最小化行为
event.Cancel()
// 隐藏窗口到托盘
mainWindow.Hide()
// 通知前端窗口已隐藏
app.EmitEvent("window:hidden", nil)
if trayService.ShouldMinimizeToTray() {
// 取消默认最小化行为,隐藏到托盘
event.Cancel()
trayService.HandleWindowMinimize()
}
// 如果不启用托盘,允许正常最小化(不取消事件)
})
}

View File

@@ -58,8 +58,9 @@ const (
// GeneralConfig 通用设置配置
type GeneralConfig struct {
AlwaysOnTop bool `json:"alwaysOnTop" yaml:"always_on_top" mapstructure:"always_on_top"` // 窗口是否置顶
DataPath string `json:"dataPath" yaml:"data_path" mapstructure:"data_path"` // 数据存储路径
AlwaysOnTop bool `json:"alwaysOnTop" yaml:"always_on_top" mapstructure:"always_on_top"` // 窗口是否置顶
DataPath string `json:"dataPath" yaml:"data_path" mapstructure:"data_path"` // 数据存储路径
EnableSystemTray bool `json:"enableSystemTray" yaml:"enable_system_tray" mapstructure:"enable_system_tray"` // 是否启用系统托盘
// 全局热键设置
EnableGlobalHotkey bool `json:"enableGlobalHotkey" yaml:"enable_global_hotkey" mapstructure:"enable_global_hotkey"` // 是否启用全局热键
@@ -139,6 +140,7 @@ func NewDefaultAppConfig() *AppConfig {
General: GeneralConfig{
AlwaysOnTop: false,
DataPath: dataDir,
EnableSystemTray: true, // 默认启用系统托盘
EnableGlobalHotkey: false,
GlobalHotkey: HotkeyCombo{
Ctrl: false,

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")
}
}

View File

@@ -4,10 +4,11 @@ import (
"embed"
"github.com/wailsapp/wails/v3/pkg/application"
"voidraft/internal/events"
"voidraft/internal/services"
)
// SetupSystemTray 设置系统托盘及其功能
func SetupSystemTray(app *application.App, mainWindow *application.WebviewWindow, assets embed.FS) {
func SetupSystemTray(app *application.App, mainWindow *application.WebviewWindow, assets embed.FS, trayService *services.TrayService) {
// 创建系统托盘
systray := app.NewSystemTray()
@@ -27,5 +28,5 @@ func SetupSystemTray(app *application.App, mainWindow *application.WebviewWindow
systray.SetMenu(menu)
// 注册托盘相关事件
events.RegisterTrayEvents(app, systray, mainWindow)
events.RegisterTrayEvents(app, systray, mainWindow, trayService)
}