🎨 Optimize code
This commit is contained in:
10
internal/common/constant/constant.go
Normal file
10
internal/common/constant/constant.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package constant
|
||||
|
||||
// VOIDRAFT_MAIN_WINDOW_NAME is the name of the main window of the Voidcraft client.
|
||||
const VOIDRAFT_MAIN_WINDOW_NAME = "voidraft-main-window"
|
||||
|
||||
const VOIDRAFT_WINDOW_TITLE = "voidraft"
|
||||
|
||||
const VOIDRAFT_WINDOW_WIDTH = 700
|
||||
|
||||
const VOIDRAFT_WINDOW_HEIGHT = 800
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
|
||||
// DialogService 对话框服务,处理文件选择等对话框操作
|
||||
type DialogService struct {
|
||||
logger *log.LogService
|
||||
window *application.WebviewWindow // 绑定的窗口
|
||||
logger *log.LogService
|
||||
windowHelper *WindowHelper
|
||||
}
|
||||
|
||||
// NewDialogService 创建新的对话框服务实例
|
||||
@@ -18,16 +18,11 @@ func NewDialogService(logger *log.LogService) *DialogService {
|
||||
}
|
||||
|
||||
return &DialogService{
|
||||
logger: logger,
|
||||
window: nil, // 初始为空,后续通过 SetWindow 设置
|
||||
logger: logger,
|
||||
windowHelper: NewWindowHelper(),
|
||||
}
|
||||
}
|
||||
|
||||
// SetWindow 设置绑定的窗口
|
||||
func (ds *DialogService) SetWindow(window *application.WebviewWindow) {
|
||||
ds.window = window
|
||||
}
|
||||
|
||||
// SelectDirectory 打开目录选择对话框
|
||||
func (ds *DialogService) SelectDirectory() (string, error) {
|
||||
dialog := application.OpenFileDialog()
|
||||
@@ -59,7 +54,7 @@ func (ds *DialogService) SelectDirectory() (string, error) {
|
||||
Directory: "",
|
||||
|
||||
// 绑定到主窗口
|
||||
Window: ds.window,
|
||||
Window: ds.windowHelper.MustGetMainWindow(),
|
||||
})
|
||||
|
||||
path, err := dialog.PromptForSingleSelection()
|
||||
@@ -100,7 +95,7 @@ func (ds *DialogService) SelectFile() (string, error) {
|
||||
Directory: "",
|
||||
|
||||
// 绑定到主窗口
|
||||
Window: ds.window,
|
||||
Window: ds.windowHelper.MustGetMainWindow(),
|
||||
})
|
||||
|
||||
path, err := dialog.PromptForSingleSelection()
|
||||
|
||||
@@ -27,8 +27,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
|
||||
@@ -84,6 +83,7 @@ func NewHotkeyService(configService *ConfigService, windowService *WindowService
|
||||
logger: logger,
|
||||
configService: configService,
|
||||
windowService: windowService,
|
||||
windowHelper: NewWindowHelper(),
|
||||
ctx: ctx,
|
||||
}
|
||||
// 初始化时设置cancel函数
|
||||
@@ -91,11 +91,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}
|
||||
@@ -229,13 +232,14 @@ func cBool(b bool) C.int {
|
||||
|
||||
// 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 {
|
||||
@@ -252,7 +256,7 @@ func (hs *HotkeyService) isWindowVisible(window *application.WebviewWindow) bool
|
||||
// hideAllWindows 隐藏所有窗口
|
||||
func (hs *HotkeyService) hideAllWindows() {
|
||||
// 隐藏主窗口
|
||||
hs.mainWindow.Hide()
|
||||
hs.windowHelper.HideMainWindow()
|
||||
|
||||
// 隐藏所有子窗口
|
||||
if hs.windowService != nil {
|
||||
@@ -268,9 +272,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 {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -142,6 +142,7 @@ func NewServiceManager() *ServiceManager {
|
||||
databaseService: databaseService,
|
||||
documentService: documentService,
|
||||
windowService: windowService,
|
||||
windowSnapService: windowSnapService,
|
||||
migrationService: migrationService,
|
||||
systemService: systemService,
|
||||
hotkeyService: hotkeyService,
|
||||
@@ -168,6 +169,7 @@ func (sm *ServiceManager) GetServices() []application.Service {
|
||||
application.NewService(sm.databaseService),
|
||||
application.NewService(sm.documentService),
|
||||
application.NewService(sm.windowService),
|
||||
application.NewService(sm.windowSnapService),
|
||||
application.NewService(sm.keyBindingService),
|
||||
application.NewService(sm.extensionService),
|
||||
application.NewService(sm.migrationService),
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
// SystemService 系统监控服务
|
||||
type SystemService struct {
|
||||
logger *log.LogService
|
||||
app *application.App
|
||||
}
|
||||
|
||||
// MemoryStats 内存统计信息
|
||||
@@ -54,11 +53,6 @@ func NewSystemService(logger *log.LogService) *SystemService {
|
||||
}
|
||||
}
|
||||
|
||||
// SetAppReferences 设置应用引用
|
||||
func (ss *SystemService) SetAppReferences(app *application.App) {
|
||||
ss.app = app
|
||||
}
|
||||
|
||||
// GetMemoryStats 获取当前内存统计信息
|
||||
func (ss *SystemService) GetMemoryStats() MemoryStats {
|
||||
var m runtime.MemStats
|
||||
@@ -76,11 +70,8 @@ func (ss *SystemService) GetMemoryStats() MemoryStats {
|
||||
|
||||
// GetSystemInfo 获取系统环境信息
|
||||
func (ss *SystemService) GetSystemInfo() (*SystemInfo, error) {
|
||||
if ss.app == nil {
|
||||
return nil, fmt.Errorf("app reference not set")
|
||||
}
|
||||
|
||||
envInfo := ss.app.Env.Info()
|
||||
app := application.Get()
|
||||
envInfo := app.Env.Info()
|
||||
|
||||
systemInfo := &SystemInfo{
|
||||
OS: envInfo.OS,
|
||||
|
||||
@@ -9,8 +9,7 @@ import (
|
||||
type TrayService struct {
|
||||
logger *log.LogService
|
||||
configService *ConfigService
|
||||
app *application.App
|
||||
mainWindow *application.WebviewWindow
|
||||
windowHelper *WindowHelper
|
||||
}
|
||||
|
||||
// NewTrayService 创建新的系统托盘服务实例
|
||||
@@ -18,15 +17,10 @@ func NewTrayService(logger *log.LogService, configService *ConfigService) *TrayS
|
||||
return &TrayService{
|
||||
logger: logger,
|
||||
configService: configService,
|
||||
windowHelper: NewWindowHelper(),
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
@@ -41,10 +35,10 @@ func (ts *TrayService) ShouldMinimizeToTray() bool {
|
||||
func (ts *TrayService) HandleWindowClose() {
|
||||
if ts.ShouldMinimizeToTray() {
|
||||
// 隐藏到托盘
|
||||
ts.mainWindow.Hide()
|
||||
ts.windowHelper.HideMainWindow()
|
||||
} else {
|
||||
// 直接退出应用
|
||||
ts.app.Quit()
|
||||
application.Get().Quit()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,21 +46,16 @@ func (ts *TrayService) HandleWindowClose() {
|
||||
func (ts *TrayService) HandleWindowMinimize() {
|
||||
if ts.ShouldMinimizeToTray() {
|
||||
// 隐藏到托盘
|
||||
ts.mainWindow.Hide()
|
||||
ts.windowHelper.HideMainWindow()
|
||||
}
|
||||
}
|
||||
|
||||
// ShowWindow 显示主窗口
|
||||
func (ts *TrayService) ShowWindow() {
|
||||
if ts.mainWindow != nil {
|
||||
ts.mainWindow.Show()
|
||||
ts.mainWindow.Restore()
|
||||
ts.mainWindow.Focus()
|
||||
}
|
||||
ts.windowHelper.FocusMainWindow()
|
||||
}
|
||||
|
||||
// MinimizeButtonClicked 处理标题栏最小化按钮点击
|
||||
func (ts *TrayService) MinimizeButtonClicked() {
|
||||
// 最小化按钮总是执行正常最小化到任务栏,不隐藏到托盘
|
||||
ts.mainWindow.Minimise()
|
||||
ts.windowHelper.MinimiseMainWindow()
|
||||
}
|
||||
|
||||
69
internal/services/window_helper.go
Normal file
69
internal/services/window_helper.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
"voidraft/internal/common/constant"
|
||||
)
|
||||
|
||||
// WindowHelper 窗口辅助工具
|
||||
type WindowHelper struct{}
|
||||
|
||||
// NewWindowHelper 创建窗口辅助工具实例
|
||||
func NewWindowHelper() *WindowHelper {
|
||||
return &WindowHelper{}
|
||||
}
|
||||
|
||||
// GetMainWindow 获取主窗口实例
|
||||
// 返回窗口对象和是否找到的标志
|
||||
func (wh *WindowHelper) GetMainWindow() (application.Window, bool) {
|
||||
app := application.Get()
|
||||
return app.Window.GetByName(constant.VOIDRAFT_MAIN_WINDOW_NAME)
|
||||
}
|
||||
|
||||
// MustGetMainWindow 获取主窗口实例
|
||||
// 如果窗口不存在则返回 nil
|
||||
func (wh *WindowHelper) MustGetMainWindow() application.Window {
|
||||
window, ok := wh.GetMainWindow()
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return window
|
||||
}
|
||||
|
||||
// ShowMainWindow 显示主窗口
|
||||
func (wh *WindowHelper) ShowMainWindow() bool {
|
||||
if window := wh.MustGetMainWindow(); window != nil {
|
||||
window.Show()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// HideMainWindow 隐藏主窗口
|
||||
func (wh *WindowHelper) HideMainWindow() bool {
|
||||
if window := wh.MustGetMainWindow(); window != nil {
|
||||
window.Hide()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// MinimiseMainWindow 最小化主窗口
|
||||
func (wh *WindowHelper) MinimiseMainWindow() bool {
|
||||
if window := wh.MustGetMainWindow(); window != nil {
|
||||
window.Minimise()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// FocusMainWindow 聚焦主窗口
|
||||
func (wh *WindowHelper) FocusMainWindow() bool {
|
||||
if window := wh.MustGetMainWindow(); window != nil {
|
||||
window.Show()
|
||||
window.Restore()
|
||||
window.Focus()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -2,14 +2,16 @@ package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
"voidraft/internal/common/constant"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
"github.com/wailsapp/wails/v3/pkg/events"
|
||||
"github.com/wailsapp/wails/v3/pkg/services/log"
|
||||
)
|
||||
|
||||
// WindowInfo 窗口信息(简化版)
|
||||
// WindowInfo 窗口信息
|
||||
type WindowInfo struct {
|
||||
Window *application.WebviewWindow
|
||||
DocumentID int64
|
||||
@@ -20,8 +22,6 @@ type WindowInfo struct {
|
||||
type WindowService struct {
|
||||
logger *log.LogService
|
||||
documentService *DocumentService
|
||||
app *application.App
|
||||
mainWindow *application.WebviewWindow
|
||||
windows map[int64]*WindowInfo // documentID -> WindowInfo
|
||||
mu sync.RWMutex
|
||||
|
||||
@@ -47,17 +47,6 @@ func (ws *WindowService) SetWindowSnapService(snapService *WindowSnapService) {
|
||||
ws.windowSnapService = snapService
|
||||
}
|
||||
|
||||
// SetAppReferences 设置应用和主窗口引用
|
||||
func (ws *WindowService) SetAppReferences(app *application.App, mainWindow *application.WebviewWindow) {
|
||||
ws.app = app
|
||||
ws.mainWindow = mainWindow
|
||||
|
||||
// 如果吸附服务已设置,也为它设置引用
|
||||
if ws.windowSnapService != nil {
|
||||
ws.windowSnapService.SetAppReferences(app, mainWindow)
|
||||
}
|
||||
}
|
||||
|
||||
// OpenDocumentWindow 为指定文档ID打开新窗口
|
||||
func (ws *WindowService) OpenDocumentWindow(documentID int64) error {
|
||||
ws.mu.Lock()
|
||||
@@ -81,11 +70,13 @@ func (ws *WindowService) OpenDocumentWindow(documentID int64) error {
|
||||
return fmt.Errorf("document not found: %d", documentID)
|
||||
}
|
||||
|
||||
app := application.Get()
|
||||
// 创建新窗口
|
||||
newWindow := ws.app.Window.NewWithOptions(application.WebviewWindowOptions{
|
||||
newWindow := app.Window.NewWithOptions(application.WebviewWindowOptions{
|
||||
Name: strconv.FormatInt(doc.ID, 10),
|
||||
Title: fmt.Sprintf("voidraft - %s", doc.Title),
|
||||
Width: 700,
|
||||
Height: 800,
|
||||
Width: constant.VOIDRAFT_WINDOW_WIDTH,
|
||||
Height: constant.VOIDRAFT_WINDOW_HEIGHT,
|
||||
Hidden: false,
|
||||
Frameless: true,
|
||||
DevToolsEnabled: false,
|
||||
@@ -104,7 +95,7 @@ func (ws *WindowService) OpenDocumentWindow(documentID int64) error {
|
||||
|
||||
newWindow.Center()
|
||||
|
||||
ws.app.Window.Add(newWindow)
|
||||
app.Window.Add(newWindow)
|
||||
|
||||
// 保存窗口信息
|
||||
windowInfo := &WindowInfo{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -15,8 +16,7 @@ import (
|
||||
type WindowSnapService struct {
|
||||
logger *log.LogService
|
||||
configService *ConfigService
|
||||
app *application.App
|
||||
mainWindow *application.WebviewWindow
|
||||
windowHelper *WindowHelper
|
||||
mu sync.RWMutex
|
||||
|
||||
// 吸附配置
|
||||
@@ -53,6 +53,7 @@ func NewWindowSnapService(logger *log.LogService, configService *ConfigService)
|
||||
return &WindowSnapService{
|
||||
logger: logger,
|
||||
configService: configService,
|
||||
windowHelper: NewWindowHelper(),
|
||||
snapEnabled: snapEnabled,
|
||||
baseThresholdRatio: 0.025, // 2.5%的主窗口宽度作为基础阈值
|
||||
minThreshold: 8, // 最小8像素(小屏幕保底)
|
||||
@@ -62,18 +63,14 @@ func NewWindowSnapService(logger *log.LogService, configService *ConfigService)
|
||||
}
|
||||
}
|
||||
|
||||
// SetAppReferences 设置应用和主窗口引用
|
||||
func (wss *WindowSnapService) SetAppReferences(app *application.App, mainWindow *application.WebviewWindow) {
|
||||
wss.app = app
|
||||
wss.mainWindow = mainWindow
|
||||
|
||||
// ServiceStartup 服务启动时初始化
|
||||
func (wss *WindowSnapService) ServiceStartup(ctx context.Context, options application.ServiceOptions) error {
|
||||
// 初始化主窗口位置缓存
|
||||
wss.updateMainWindowCache()
|
||||
|
||||
// 设置主窗口移动事件监听
|
||||
if mainWindow != nil {
|
||||
wss.setupMainWindowEvents()
|
||||
}
|
||||
wss.setupMainWindowEvents()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterWindow 注册需要吸附管理的窗口
|
||||
@@ -171,8 +168,14 @@ func (wss *WindowSnapService) OnWindowSnapConfigChanged(enabled bool) error {
|
||||
|
||||
// setupMainWindowEvents 设置主窗口事件监听
|
||||
func (wss *WindowSnapService) setupMainWindowEvents() {
|
||||
// 获取主窗口
|
||||
mainWindow, ok := wss.windowHelper.GetMainWindow()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// 监听主窗口移动事件
|
||||
wss.mainWindow.RegisterHook(events.Common.WindowDidMove, func(event *application.WindowEvent) {
|
||||
mainWindow.RegisterHook(events.Common.WindowDidMove, func(event *application.WindowEvent) {
|
||||
wss.onMainWindowMoved()
|
||||
})
|
||||
}
|
||||
@@ -187,12 +190,13 @@ func (wss *WindowSnapService) setupWindowEvents(window *application.WebviewWindo
|
||||
|
||||
// updateMainWindowCache 更新主窗口缓存
|
||||
func (wss *WindowSnapService) updateMainWindowCache() {
|
||||
if wss.mainWindow == nil {
|
||||
mainWindow := wss.windowHelper.MustGetMainWindow()
|
||||
if mainWindow == nil {
|
||||
return
|
||||
}
|
||||
|
||||
x, y := wss.mainWindow.Position()
|
||||
w, h := wss.mainWindow.Size()
|
||||
x, y := mainWindow.Position()
|
||||
w, h := mainWindow.Size()
|
||||
|
||||
wss.lastMainWindowPos = models.WindowPosition{X: x, Y: y}
|
||||
wss.lastMainWindowSize = [2]int{w, h}
|
||||
|
||||
@@ -9,7 +9,8 @@ import (
|
||||
)
|
||||
|
||||
// SetupSystemTray 设置系统托盘及其功能
|
||||
func SetupSystemTray(app *application.App, mainWindow *application.WebviewWindow, assets embed.FS, trayService *services.TrayService) {
|
||||
func SetupSystemTray(mainWindow *application.WebviewWindow, assets embed.FS, trayService *services.TrayService) {
|
||||
app := application.Get()
|
||||
// 创建系统托盘
|
||||
systray := app.SystemTray.New()
|
||||
// 设置提示
|
||||
|
||||
Reference in New Issue
Block a user