style: 修复图标 样式 #46

Fixes https://github.com/Wxw-Gu/TraceMemo/issues/46
This commit is contained in:
电摇小子 committed 2026-10-01 17:35:47 +07:00
1 parent 194dd0035b
commit e5a1656644
10 files changed
+241 -19

No files matched your search

+21 -7
View File
@@ -21,7 +21,10 @@ import { dirname, extname, join } from 'path'
import { existsSync, promises as fsPromises } from 'fs'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import trayTemplateIcon from '../../resources/trayTemplate.png?asset'
import trayTemplateRetinaIcon from '../../resources/trayTemplate@2x.png?asset'
import { WechatDb } from './wechat-db'
import { createTrayImage } from './tray-icon'
import { bootstrapWcdbNativeAsync, Wcdb4Client } from './wcdb4-client'
import { VoiceService } from './voice-service'
import { StickerService } from './sticker-service'
@@ -370,6 +373,18 @@ function configureRecallProtection(
const packagedIconPath = join(process.resourcesPath, 'resources', 'icon.png')
const appIconPath = existsSync(packagedIconPath) ? packagedIconPath : icon
const packagedTrayTemplateIconPath = join(process.resourcesPath, 'resources', 'trayTemplate.png')
const packagedTrayTemplateRetinaIconPath = join(
process.resourcesPath,
'resources',
'trayTemplate@2x.png'
)
const trayTemplateIconPath = existsSync(packagedTrayTemplateIconPath)
? packagedTrayTemplateIconPath
: trayTemplateIcon
const trayTemplateRetinaIconPath = existsSync(packagedTrayTemplateRetinaIconPath)
? packagedTrayTemplateRetinaIconPath
: trayTemplateRetinaIcon
protocol.registerSchemesAsPrivileged([
{
@@ -921,8 +936,6 @@ app.whenReady().then(async () => {
// 设置应用程序用户模型 ID
electronApp.setAppUserModelId('com.tracememo.app')
if (process.platform === 'darwin') app.dock?.setIcon(appIconPath)
// 开发环境中默认使用 F12 打开或关闭 DevTools
// 生产环境中忽略 CommandOrControl + R
// 参见 https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
@@ -2875,11 +2888,12 @@ function buildTrayMenu(): Menu {
function setupTray(): void {
if (tray) return
try {
const image = nativeImage.createFromPath(appIconPath)
const traySize = process.platform === 'darwin' ? 20 : 24
const trayImage = image.isEmpty()
? nativeImage.createEmpty()
: image.resize({ width: traySize, height: traySize, quality: 'best' })
const trayImage = createTrayImage(
process.platform,
appIconPath,
{ oneX: trayTemplateIconPath, twoX: trayTemplateRetinaIconPath },
nativeImage
)
tray = new Tray(trayImage)
tray.setToolTip('TraceMemo')
// macOS may show a Tray context menu on a primary click when it is set
+51
View File
@@ -0,0 +1,51 @@
import { readFileSync } from 'fs'
export interface TrayNativeImage {
addRepresentation(options: { scaleFactor: number; dataURL: string }): void
isEmpty(): boolean
resize(options: { width: number; height: number; quality: 'best' }): TrayNativeImage
setTemplateImage(option: boolean): void
}
export interface TrayNativeImageFactory<T extends TrayNativeImage = TrayNativeImage> {
createEmpty(): T
createFromPath(path: string): T
}
export interface TrayTemplateIconPaths {
oneX: string
twoX: string
}
type ReadImageFile = (path: string) => Buffer
function toPngDataUrl(buffer: Buffer): string {
return `data:image/png;base64,${buffer.toString('base64')}`
}
export function createTrayImage<T extends TrayNativeImage>(
platform: NodeJS.Platform,
appIconPath: string,
templateIconPaths: TrayTemplateIconPaths,
nativeImage: TrayNativeImageFactory<T>,
readImageFile: ReadImageFile = readFileSync
): T {
if (platform === 'darwin') {
const image = nativeImage.createEmpty()
image.addRepresentation({
scaleFactor: 1,
dataURL: toPngDataUrl(readImageFile(templateIconPaths.oneX))
})
image.addRepresentation({
scaleFactor: 2,
dataURL: toPngDataUrl(readImageFile(templateIconPaths.twoX))
})
image.setTemplateImage(true)
return image
}
const image = nativeImage.createFromPath(appIconPath)
return image.isEmpty()
? nativeImage.createEmpty()
: (image.resize({ width: 24, height: 24, quality: 'best' }) as T)
}