Files
metona-ai-desktop/electron/services/tray-manager.service.ts
T
thzxx 4cd6e997b5
CI / 类型检查 + Lint + 单元测试 (push) Failing after 9m45s
CI / 全量测试 (Electron ABI) (push) Failing after 6m28s
CI / 产物编译验证 (push) Successful in 11m18s
feat: v0.8.2 安全纵深补全 · 协议保真 · 断链修复 — 图片SSRF/根MEMORY.md保护根治 · Anthropic thinking回传+pause_turn续传 · 2523 用例全量回归 + E2E 扩充
2026-09-08 14:30:27 +08:00

246 lines
6.3 KiB
TypeScript

/**
* Tray Manager — 系统托盘管理
*
* 职责:
* 1. 创建系统托盘图标
* 2. 托盘右键菜单(新建会话、显示窗口、退出)
* 3. 托盘图标状态指示(空闲/思考中/执行中)
* 4. 点击托盘图标显示/隐藏窗口
*
* @see docs/MetonaAI-Desktop UI UX 设计集成方案.html — 窗口管理
*/
import { Tray, Menu, BrowserWindow, app, nativeImage, Notification } from 'electron';
import { join } from 'path';
import { existsSync } from 'fs';
import log from 'electron-log';
// v0.8.2 P2-5: 托盘菜单文案双语(ui.locale 驱动)
import { mt } from '../utils/main-locale';
export type TrayStatus = 'idle' | 'thinking' | 'executing' | 'error';
export class TrayManager {
private tray: Tray | null = null;
private mainWindow: BrowserWindow | null = null;
private currentStatus: TrayStatus = 'idle';
private static isQuitting = false;
constructor(private resourcesPath: string) {}
/**
* 初始化系统托盘
*/
initialize(mainWindow: BrowserWindow): void {
this.mainWindow = mainWindow;
// 创建托盘图标
const iconPath = this.getTrayIconPath();
if (!iconPath) {
log.warn('Tray icon not found, skipping tray initialization');
return;
}
this.tray = new Tray(iconPath);
this.tray.setToolTip('MetonaAI Desktop');
// 构建右键菜单
this.updateContextMenu();
// 点击托盘图标:显示/隐藏窗口
this.tray.on('click', () => {
if (this.mainWindow) {
if (this.mainWindow.isVisible()) {
this.mainWindow.hide();
} else {
this.mainWindow.show();
this.mainWindow.focus();
}
}
});
// 窗口关闭时隐藏到托盘(不退出)
mainWindow.on('close', (event) => {
if (!TrayManager.isQuitting) {
event.preventDefault();
mainWindow.hide();
}
});
log.info('System tray initialized');
}
/**
* 标记应用正在退出(窗口关闭时不阻止)
*/
static markQuitting(): void {
TrayManager.isQuitting = true;
}
/**
* F-4 修复: 重新绑定主窗口引用(macOS activate / 托盘重建窗口后调用)
*
* 旧窗口销毁后 this.mainWindow 指向已 destroyed 实例,托盘点击(显示/隐藏)
* 与"关闭到托盘"行为全部失效。此方法更新引用并为新窗口重新挂载 close 拦截
* (原监听随旧窗口销毁自动释放,新窗口需要同样的 hide-to-tray 行为)。
*/
rebindWindow(mainWindow: BrowserWindow): void {
this.mainWindow = mainWindow;
mainWindow.on('close', (event) => {
if (!TrayManager.isQuitting) {
event.preventDefault();
mainWindow.hide();
}
});
}
/**
* 更新托盘状态
*/
setStatus(status: TrayStatus): void {
if (!this.tray) return;
this.currentStatus = status;
const statusLabels: Record<TrayStatus, string> = {
idle: 'MetonaAI — 空闲',
thinking: 'MetonaAI — 思考中...',
executing: 'MetonaAI — 执行中...',
error: 'MetonaAI — 错误',
};
this.tray.setToolTip(statusLabels[status]);
this.updateContextMenu();
// 更新图标(如果有多状态图标)
this.updateTrayIcon(status);
}
/**
* 发送系统通知
*/
sendNotification(title: string, body: string, onClick?: () => void): void {
if (!Notification.isSupported()) return;
const notification = new Notification({
title,
body,
icon: this.getTrayIconPath() ?? undefined,
});
if (onClick) {
notification.on('click', onClick);
}
notification.show();
}
/**
* 销毁托盘
*/
destroy(): void {
if (this.tray) {
this.tray.destroy();
this.tray = null;
}
}
// ===== 私有方法 =====
/**
* 获取托盘图标路径
*
* 优先级:process.resourcesPath(打包后)> resourcesPath 参数(开发模式) > __dirname fallback
*/
private getTrayIconPath(): string | null {
const searchDirs: string[] = [
join(process.resourcesPath || '', 'app.asar.unpacked', 'assets'),
join(process.resourcesPath || '', 'assets'),
this.resourcesPath,
join(__dirname, '../../assets'),
];
const candidates = ['logo.ico', 'logo.png', 'icon.ico', 'icon.png'];
for (const dir of searchDirs) {
for (const name of candidates) {
const fullPath = join(dir, name);
if (existsSync(fullPath)) return fullPath;
}
}
return null;
}
/**
* 更新托盘图标
*
* 始终使用原始 logo 图标(状态已在右键菜单中显示,图标保持品牌识别)。
*/
private updateTrayIcon(_status: TrayStatus): void {
if (!this.tray) return;
const basePath = this.getTrayIconPath();
if (!basePath) return;
const image = nativeImage.createFromPath(basePath);
this.tray.setImage(image.resize({ width: 16, height: 16 }));
}
/**
* 更新右键菜单
*/
private updateContextMenu(): void {
if (!this.tray) return;
const statusIcons: Record<TrayStatus, string> = {
idle: '⚪',
thinking: '🔵',
executing: '🟢',
error: '🔴',
};
const contextMenu = Menu.buildFromTemplate([
{
label: `MetonaAI Desktop`,
enabled: false,
},
{
// v0.8.2 P2-5: 托盘菜单出层(ui.locale 驱动 mt(),语言切换即热生效)
label: mt('tray.menu.status', {
status: `${statusIcons[this.currentStatus]} ${mt(`tray.status.${this.currentStatus}`)}`,
}),
enabled: false,
},
{ type: 'separator' },
{
label: mt('tray.menu.showWindow'),
click: () => {
if (this.mainWindow) {
this.mainWindow.show();
this.mainWindow.focus();
}
},
},
{
label: mt('tray.menu.newSession'),
click: () => {
if (this.mainWindow) {
this.mainWindow.show();
this.mainWindow.focus();
this.mainWindow.webContents.send('tray:newSession');
}
},
},
{ type: 'separator' },
{
label: mt('tray.menu.quit'),
click: () => {
TrayManager.isQuitting = true;
app.quit();
},
},
]);
this.tray.setContextMenu(contextMenu);
}
}