Files
ChatLab/apps/desktop/main/window/windows-tray.ts
T
digua 3db40375b6 fix(electron): default Windows close to background
Remove the one-time close prompt and remembered-choice path so Windows close behavior is always explicit and predictable. New installations now minimize to the system tray by default, while users can still choose to quit from settings.
2026-08-07 20:29:37 +08:00

45 lines
1.2 KiB
TypeScript

import { app, BrowserWindow, Menu, Tray } from 'electron'
import { join } from 'path'
import { t } from '../i18n'
let tray: Tray | null = null
let targetWindow: BrowserWindow | null = null
let quitHandler: (() => void) | null = null
export function ensureWindowsTray(win: BrowserWindow, onQuit: () => void): void {
targetWindow = win
quitHandler = onQuit
if (!tray) {
tray = new Tray(resolveTrayIconPath())
tray.setToolTip('ChatLab')
tray.on('click', restoreMainWindow)
}
tray.setContextMenu(
Menu.buildFromTemplate([
{ label: t('windowsTray.showApp'), click: restoreMainWindow },
{ type: 'separator' },
{ label: t('windowsTray.quitApp'), click: () => quitHandler?.() },
])
)
}
export function destroyWindowsTray(): void {
tray?.destroy()
tray = null
targetWindow = null
quitHandler = null
}
function restoreMainWindow(): void {
if (!targetWindow || targetWindow.isDestroyed()) return
if (targetWindow.isMinimized()) targetWindow.restore()
targetWindow.show()
targetWindow.focus()
}
function resolveTrayIconPath(): string {
return app.isPackaged ? join(process.resourcesPath, 'tray-icon.ico') : join(__dirname, '../../build/icon.ico')
}