♻️ Refactor code

This commit is contained in:
2025-06-02 13:34:54 +08:00
parent 44f7baad10
commit a516b8973e
53 changed files with 1513 additions and 1094 deletions

View File

@@ -8,7 +8,7 @@ import (
)
// RegisterTrayEvents 注册与系统托盘相关的所有事件
func RegisterTrayEvents(app *application.App, systray *application.SystemTray, mainWindow *application.WebviewWindow, settingsWindow *application.WebviewWindow) {
func RegisterTrayEvents(app *application.App, systray *application.SystemTray, mainWindow *application.WebviewWindow) {
// 不附加窗口到系统托盘,避免失去焦点自动缩小
// systray.AttachWindow(mainWindow)
@@ -28,30 +28,14 @@ func RegisterTrayEvents(app *application.App, systray *application.SystemTray, m
mainWindow.Hide()
})
// 设置窗口关闭事件处理
settingsWindow.RegisterHook(wailsevents.Common.WindowClosing, func(event *application.WindowEvent) {
// 取消默认关闭行为
event.Cancel()
// 隐藏窗口
settingsWindow.Hide()
})
// 注册事件监听器,用于处理前端发送的显示设置窗口事件
app.OnEvent("show_settings_window", func(event *application.CustomEvent) {
settingsWindow.Show()
})
}
// RegisterTrayMenuEvents 注册系统托盘菜单事件
func RegisterTrayMenuEvents(app *application.App, menu *application.Menu, mainWindow *application.WebviewWindow, settingsWindow *application.WebviewWindow) {
func RegisterTrayMenuEvents(app *application.App, menu *application.Menu, mainWindow *application.WebviewWindow) {
menu.Add("主窗口").OnClick(func(data *application.Context) {
mainWindow.Show()
})
menu.Add("设置").OnClick(func(data *application.Context) {
settingsWindow.Show()
})
menu.AddSeparator()
menu.Add("退出").OnClick(func(data *application.Context) {

View File

@@ -9,6 +9,7 @@ import (
type ServiceManager struct {
configService *ConfigService
documentService *DocumentService
systemService *SystemService
logger *log.LoggerService
}
@@ -23,6 +24,9 @@ func NewServiceManager() *ServiceManager {
// 初始化文档服务
documentService := NewDocumentService(configService, logger)
// 初始化系统服务
systemService := NewSystemService(logger)
// 初始化文档服务
err := documentService.Initialize()
if err != nil {
@@ -33,6 +37,7 @@ func NewServiceManager() *ServiceManager {
return &ServiceManager{
configService: configService,
documentService: documentService,
systemService: systemService,
logger: logger,
}
}
@@ -42,5 +47,6 @@ func (sm *ServiceManager) GetServices() []application.Service {
return []application.Service{
application.NewService(sm.configService),
application.NewService(sm.documentService),
application.NewService(sm.systemService),
}
}

View File

@@ -0,0 +1,83 @@
package services
import (
"fmt"
"runtime"
"github.com/wailsapp/wails/v3/pkg/services/log"
)
// SystemService 系统监控服务
type SystemService struct {
logger *log.LoggerService
}
// MemoryStats 内存统计信息
type MemoryStats struct {
// 当前堆内存使用量(字节)
HeapInUse uint64 `json:"heapInUse"`
// 堆内存分配总量(字节)
HeapAlloc uint64 `json:"heapAlloc"`
// 系统内存使用量(字节)
Sys uint64 `json:"sys"`
// GC 次数
NumGC uint32 `json:"numGC"`
// GC 暂停时间(纳秒)
PauseTotalNs uint64 `json:"pauseTotalNs"`
// Goroutine 数量
NumGoroutine int `json:"numGoroutine"`
}
// NewSystemService 创建新的系统服务实例
func NewSystemService(logger *log.LoggerService) *SystemService {
return &SystemService{
logger: logger,
}
}
// GetMemoryStats 获取当前内存统计信息
func (ss *SystemService) GetMemoryStats() MemoryStats {
var m runtime.MemStats
runtime.ReadMemStats(&m)
return MemoryStats{
HeapInUse: m.HeapInuse,
HeapAlloc: m.HeapAlloc,
Sys: m.Sys,
NumGC: m.NumGC,
PauseTotalNs: m.PauseTotalNs,
NumGoroutine: runtime.NumGoroutine(),
}
}
// FormatBytes 格式化字节数为人类可读的格式
func (ss *SystemService) FormatBytes(bytes uint64) string {
const unit = 1024
if bytes < unit {
return "< 1 KB"
}
div, exp := uint64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
switch exp {
case 0:
return "< 1 KB"
case 1:
return fmt.Sprintf("%.1f KB", float64(bytes)/float64(div))
case 2:
return fmt.Sprintf("%.1f MB", float64(bytes)/float64(div))
case 3:
return fmt.Sprintf("%.1f GB", float64(bytes)/float64(div))
default:
return fmt.Sprintf("%.1f TB", float64(bytes)/float64(div))
}
}
// TriggerGC 手动触发垃圾回收
func (ss *SystemService) TriggerGC() {
runtime.GC()
ss.logger.Info("Manual GC triggered")
}

View File

@@ -7,7 +7,7 @@ import (
)
// SetupSystemTray 设置系统托盘及其功能
func SetupSystemTray(app *application.App, mainWindow *application.WebviewWindow, settingsWindow *application.WebviewWindow, assets embed.FS) {
func SetupSystemTray(app *application.App, mainWindow *application.WebviewWindow, assets embed.FS) {
// 创建系统托盘
systray := app.NewSystemTray()
@@ -22,10 +22,10 @@ func SetupSystemTray(app *application.App, mainWindow *application.WebviewWindow
menu := app.NewMenu()
// 注册托盘菜单事件
events.RegisterTrayMenuEvents(app, menu, mainWindow, settingsWindow)
events.RegisterTrayMenuEvents(app, menu, mainWindow)
systray.SetMenu(menu)
// 注册托盘相关事件
events.RegisterTrayEvents(app, systray, mainWindow, settingsWindow)
events.RegisterTrayEvents(app, systray, mainWindow)
}