🎨 Optimize code

This commit is contained in:
2025-10-20 22:59:17 +08:00
parent aa8139884b
commit b704dd2438
24 changed files with 274 additions and 602 deletions

View File

@@ -84,12 +84,12 @@ type HotkeyService struct {
logger *log.LogService
configService *ConfigService
windowService *WindowService
app *application.App
mainWindow *application.WebviewWindow
windowHelper *WindowHelper
mu sync.RWMutex
isRegistered atomic.Bool
currentHotkey *models.HotkeyCombo
cancelFunc atomic.Value // 使用atomic.Value存储cancel函数避免竞态条件
ctx context.Context
}
// HotkeyError 热键错误
@@ -138,6 +138,7 @@ func NewHotkeyService(configService *ConfigService, windowService *WindowService
logger: logger,
configService: configService,
windowService: windowService,
windowHelper: NewWindowHelper(),
}
// 设置全局实例
@@ -146,12 +147,14 @@ func NewHotkeyService(configService *ConfigService, windowService *WindowService
return service
}
// Initialize 初始化热键服务
func (hs *HotkeyService) Initialize(app *application.App, mainWindow *application.WebviewWindow) error {
hs.app = app
hs.mainWindow = mainWindow
// ServiceStartup initializes the service when the application starts
func (ds *HotkeyService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
ds.ctx = ctx
return ds.Initialize()
}
// 加载并应用当前配置
// Initialize 初始化热键服务
func (hs *HotkeyService) Initialize() error {
config, err := hs.configService.GetConfig()
if err != nil {
return &HotkeyError{"load_config", err}
@@ -311,13 +314,14 @@ func (hs *HotkeyService) IsRegistered() bool {
// ToggleWindow 切换窗口显示状态
func (hs *HotkeyService) ToggleWindow() {
if hs.mainWindow == nil {
hs.logger.Error("main window not set")
mainWindow := hs.windowHelper.MustGetMainWindow()
if mainWindow == nil {
hs.logger.Error("main window not found")
return
}
// 检查主窗口是否可见
if hs.isWindowVisible(hs.mainWindow) {
if mainWindow.IsVisible() {
// 如果主窗口可见,隐藏所有窗口
hs.hideAllWindows()
} else {
@@ -334,7 +338,7 @@ func (hs *HotkeyService) isWindowVisible(window *application.WebviewWindow) bool
// hideAllWindows 隐藏所有窗口
func (hs *HotkeyService) hideAllWindows() {
// 隐藏主窗口
hs.mainWindow.Hide()
hs.windowHelper.HideMainWindow()
// 隐藏所有子窗口
if hs.windowService != nil {
@@ -350,9 +354,7 @@ func (hs *HotkeyService) hideAllWindows() {
// showAllWindows 显示所有窗口
func (hs *HotkeyService) showAllWindows() {
// 显示主窗口
hs.mainWindow.Show()
hs.mainWindow.Restore()
hs.mainWindow.Focus()
hs.windowHelper.FocusMainWindow()
// 显示所有子窗口
if hs.windowService != nil {
@@ -374,7 +376,7 @@ func (hs *HotkeyService) ServiceShutdown() error {
//export hotkeyTriggered
func hotkeyTriggered() {
// 通过全局实例调用ToggleWindow
if globalHotkeyService != nil && globalHotkeyService.app != nil {
if globalHotkeyService != nil {
globalHotkeyService.ToggleWindow()
}
}