feat(窗口管理): 添加系统托盘功能并优化窗口关闭行为

- 新增 TrayService 实现系统托盘功能
- 主窗口关闭时默认隐藏而非退出
- 添加 window_zoom 配置项支持窗口缩放
- 引入 TrayServiceToken 作为依赖注入标识
- 通过 isQuitting 状态控制应用退出行为
This commit is contained in:
Linkon
2026-01-18 22:06:28 +08:00
parent e9f994b511
commit f57597ca57
7 changed files with 83 additions and 2 deletions
+58
View File
@@ -0,0 +1,58 @@
import { Service } from '../../shared/kernel'
import { MainContext } from '../context'
import { Tray, Menu, app, nativeImage } from 'electron'
import type { windowManagerOptions } from './WindowManager'
export class TrayService extends Service {
private tray: Tray | null = null
constructor(
ctx: MainContext,
private readonly opts: windowManagerOptions
) {
super(ctx, 'tray')
}
private get mainCtx() {
return this.ctx as MainContext
}
public initialize() {
const icon = nativeImage.createFromPath(this.opts.icon)
this.tray = new Tray(icon)
const contextMenu = Menu.buildFromTemplate([
{
label: '显示主窗口',
click: () => {
this.showMainWindow()
}
},
{ type: 'separator' },
{
label: '退出 SecScore',
click: () => {
app.quit()
}
}
])
this.tray.setToolTip('SecScore 积分管理')
this.tray.setContextMenu(contextMenu)
this.tray.on('double-click', () => {
this.showMainWindow()
})
}
private showMainWindow() {
const mainWin = this.mainCtx.windows.get('main')
if (mainWin) {
if (mainWin.isMinimized()) mainWin.restore()
mainWin.show()
mainWin.focus()
} else {
this.mainCtx.windows.open({ key: 'main', title: 'SecScore', route: '/' })
}
}
}