🎨 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

@@ -144,8 +144,7 @@ type HotkeyService struct {
logger *log.LogService
configService *ConfigService
windowService *WindowService
app *application.App
mainWindow *application.WebviewWindow
windowHelper *WindowHelper
mu sync.RWMutex
currentHotkey *models.HotkeyCombo
@@ -202,6 +201,7 @@ func NewHotkeyService(configService *ConfigService, windowService *WindowService
logger: logger,
configService: configService,
windowService: windowService,
windowHelper: NewWindowHelper(),
ctx: ctx,
}
// 初始化时设置cancel函数
@@ -209,11 +209,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 {
if int(C.initX11Display()) == 0 {
return &HotkeyError{"init_x11", fmt.Errorf("failed to initialize X11 display")}
}
@@ -346,13 +349,14 @@ func (hs *HotkeyService) hotkeyListener(ctx context.Context, ready chan<- error)
// 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 {
@@ -369,7 +373,7 @@ func (hs *HotkeyService) isWindowVisible(window *application.WebviewWindow) bool
// hideAllWindows 隐藏所有窗口
func (hs *HotkeyService) hideAllWindows() {
// 隐藏主窗口
hs.mainWindow.Hide()
hs.windowHelper.HideMainWindow()
// 隐藏所有子窗口
if hs.windowService != nil {
@@ -385,9 +389,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 {