Files
geo/apps/desktop-client/src/main/tray.ts
T
root 6359a2ddd3
Deployment Config CI / Deployment Config (push) Successful in 24s
Desktop Client Build / Resolve Build Metadata (push) Successful in 39s
Frontend CI / Frontend (push) Successful in 3m3s
Backend CI / Backend (push) Successful in 16m18s
Desktop Client Build / Build Desktop Client (push) Successful in 24m1s
Desktop Client Build / Publish Client Artifacts to NAS (push) Successful in 35s
feat(workbench): implement workbench window management and navigation state
2026-05-20 15:44:26 +08:00

102 lines
3.8 KiB
TypeScript

import { nativeImage } from 'electron/common'
import type { Tray as ElectronTray } from 'electron/main'
import { Menu, Tray, app } from 'electron/main'
type TrayIconImage = ReturnType<typeof nativeImage.createFromBuffer>
let tray: ElectronTray | null = null
let normalTrayIcon: TrayIconImage | null = null
let dangerTrayIcon: TrayIconImage | null = null
const TRAY_ICON_BASE64 =
'iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAAnUlEQVR4nN2UwQmAMAxF360Dee8I' +
'LuNIxT2cpIv0oJcIEqJGMSIG/qX8fn5/msDfqgcKUIEmqHLW3xHsgAmYTzAJ1+2yOURXNI/77qLo' +
'VvzQufX8EchAEmQ5s2LZjUCThwMTg8E3IymG07PSzotFqoqUHcJZ3akWSTctOYST0cT3hMOiCGte' +
'2HcjakCIHGmiltDW+eNrU7t/dNF/txaahq+VoZ+VygAAAABJRU5ErkJggg=='
const TRAY_DANGER_ICON_BASE64 =
'iVBORw0KGgoAAAANSUhEUgAAABYAAAAWCAYAAADEtGw7AAAABmJLR0QA/wD/AP+gvaeTAAACSUlEQVQ4jd2Uz04TURTGf2dmkG5spwU3mOgKXoCFGlwM0qZpDYEFvoLEPyuN6F4TTYxLWPAKaqILxKZtmMSQ6CvAygUuhNIpLpowt3NcaMt0GkASN/qtbs6533e+e++5B/41yEnJRqmUlsPDMiLTAmMACt9Q3dBz5z6MrK8fnElYJyeHmrncI1FdAjLHcAMReZEJw1fi++ZU4abnudj2W0RunHSaGOoYs5D1/SAetJJOcZw3ZxAFmMFxXqvnOccKN7PZh8BMgriLyFNVnVXVWeAZsJvYk2/Z9oN4oHcVjVIpbYXhV8CN5T9Ztj2fqVT246RWsZiLOp33wPVYODDt9qULm5s/+hyLMTcTot9leHiuK9oqFMZbhcI4QKZS2bdV54G92H53KJUqD16Fqpc43qq7ttYECPL5lUh1K1LdCvL5ZYB0vd4Q1dU+hkhP48jx7z49qqNfuk4V7vTicLfrPLLtzwnOxUHHfxk9YRXZiSdE5ApAplrdFliJ2VrOVKvbAFanczXB6WkcOY4iP1F0sVUs5gDcWu2eJTJhiUxk6/X7AAeeN6oiiwnORq9Id3FMu23aqnPper0RZx943mjHcd4BU7Fw07Tbl7vt1vel9/P5JwLPEy72RHW1+1CW6jVVvQ2M9F0DLLm12ssBxwDqeU7gOB8Z/H2noeYaU4oPo76uEN83GLMA1P5YUqSKMbeSE26g3bK+H7jGlET1MRAk8zE0BZbcMCwnJxucMuh3p6bOD6VS5ciypiWKxn4ZlB1gI2y317sP9X/gJ6o+6LM9+rGLAAAAAElFTkSuQmCC'
function createTrayIcon(tone: 'normal' | 'danger'): TrayIconImage {
if (tone === 'danger') {
if (dangerTrayIcon) {
return dangerTrayIcon
}
const icon = nativeImage.createFromBuffer(Buffer.from(TRAY_DANGER_ICON_BASE64, 'base64'))
icon.setTemplateImage(false)
dangerTrayIcon = icon.isEmpty() ? createTrayIcon('normal') : icon
return dangerTrayIcon
}
if (normalTrayIcon) {
return normalTrayIcon
}
const buffer = Buffer.from(TRAY_ICON_BASE64, 'base64')
const icon = nativeImage.createFromBuffer(buffer)
if (icon.isEmpty()) {
return nativeImage.createEmpty()
}
icon.setTemplateImage(true)
normalTrayIcon = icon
return icon
}
function formatIssueCount(count: number): string {
return count > 99 ? '99+' : String(count)
}
// Windows-only: tray.displayBalloon shows a system toast next to the tray
// icon. macOS and Linux ignore this call (Electron returns silently), so the
// guard here is just to avoid the no-op log noise on non-Windows.
export function showTrayBalloon(title: string, content: string): void {
if (process.platform !== 'win32') {
return
}
if (!tray || tray.isDestroyed()) {
return
}
try {
tray.displayBalloon({
title,
content,
icon: createTrayIcon('normal'),
iconType: 'custom',
})
} catch (error) {
console.warn('[desktop-tray] displayBalloon failed', error)
}
}
export function updateTrayIssueIndicator(issueCount: number): void {
if (!tray || tray.isDestroyed()) {
return
}
const safeCount = Math.max(0, Math.floor(issueCount))
const hasIssues = safeCount > 0
tray.setImage(createTrayIcon(hasIssues ? 'danger' : 'normal'))
tray.setTitle(hasIssues ? formatIssueCount(safeCount) : '', { fontType: 'monospacedDigit' })
tray.setToolTip(hasIssues ? `省心推 · ${safeCount} 个账号健康问题` : '省心推')
}
export function initTray(onOpen: () => void): ElectronTray {
if (tray) {
return tray
}
tray = new Tray(createTrayIcon('normal'))
updateTrayIssueIndicator(0)
tray.setContextMenu(
Menu.buildFromTemplate([
{ label: '打开省心推', click: () => onOpen() },
{ type: 'separator' },
{ label: '退出省心推', click: () => app.quit() },
]),
)
tray.on('click', () => onOpen())
return tray
}