Skip to content

Commit 9dcdccb

Browse files
committed
Improve icon loading for packaged app
1 parent cc20823 commit 9dcdccb

2 files changed

Lines changed: 49 additions & 27 deletions

File tree

electron/main.ts

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -691,13 +691,21 @@ function stopMousePositionCheck() {
691691
}
692692

693693
function createWindow() {
694+
// Windows 任务栏应用 ID
695+
if (process.platform === 'win32') {
696+
app.setAppUserModelId('com.delive.app')
697+
}
698+
699+
// 窗口/任务栏图标路径
700+
const windowIconPath = findIconPath()
701+
694702
mainWindow = new BrowserWindow({
695703
width: 1200,
696704
height: 800,
697705
minWidth: 800,
698706
minHeight: 600,
699707
title: 'DeLive - 桌面音频实时转录',
700-
icon: path.join(__dirname, '../build/icon.ico'),
708+
icon: windowIconPath || undefined,
701709
webPreferences: {
702710
preload: path.join(__dirname, 'preload.js'),
703711
contextIsolation: true,
@@ -793,34 +801,48 @@ function createWindow() {
793801
})
794802
}
795803

796-
// 读取托盘图标(兼容 asar 内路径和开发模式
797-
function loadTrayIcon(): NativeImage {
804+
// 构建图标候选路径(兼容开发模式和打包后的 asar/unpacked/resources
805+
function getIconCandidates(): string[] {
798806
const appPath = app.getAppPath() // dev: 项目根;prod: app.asar
799-
const isPackaged = app.isPackaged
800-
801-
// 构建候选路径列表
807+
const resourcesPath = process.resourcesPath
802808
const candidates: string[] = []
803-
804-
if (isPackaged) {
805-
// 打包后:图标在 resources 目录旁边的 build 文件夹
806-
// 或者在 asar 包内
807-
const resourcesPath = process.resourcesPath
808-
candidates.push(
809-
path.join(resourcesPath, 'build', 'icon.ico'),
810-
path.join(resourcesPath, 'build', 'icon.png'),
811-
path.join(appPath, 'build', 'icon.ico'),
812-
path.join(appPath, 'build', 'icon.png'),
813-
// 也尝试从 app.asar.unpacked 加载
814-
path.join(resourcesPath, 'app.asar.unpacked', 'build', 'icon.ico'),
815-
path.join(resourcesPath, 'app.asar.unpacked', 'build', 'icon.png'),
816-
)
817-
} else {
818-
// 开发模式:从项目根目录加载
819-
candidates.push(
820-
path.join(appPath, 'build', 'icon.ico'),
821-
path.join(appPath, 'build', 'icon.png'),
822-
)
809+
810+
// 优先 resources 根目录(extraResources 放置)
811+
candidates.push(
812+
path.join(resourcesPath, 'icon.ico'),
813+
path.join(resourcesPath, 'icon.png'),
814+
path.join(resourcesPath, 'build', 'icon.ico'),
815+
path.join(resourcesPath, 'build', 'icon.png'),
816+
)
817+
818+
// asar / asar.unpacked
819+
candidates.push(
820+
path.join(resourcesPath, 'app.asar.unpacked', 'build', 'icon.ico'),
821+
path.join(resourcesPath, 'app.asar.unpacked', 'build', 'icon.png'),
822+
path.join(appPath, 'build', 'icon.ico'),
823+
path.join(appPath, 'build', 'icon.png'),
824+
)
825+
826+
return candidates
827+
}
828+
829+
function findIconPath(): string | null {
830+
const candidates = getIconCandidates()
831+
for (const p of candidates) {
832+
try {
833+
if (fs.existsSync(p)) {
834+
return p
835+
}
836+
} catch (error) {
837+
console.warn('[Icon] 检查图标路径失败:', p, error)
838+
}
823839
}
840+
return null
841+
}
842+
843+
// 读取托盘图标(兼容 asar 内路径和开发模式)
844+
function loadTrayIcon(): NativeImage {
845+
const candidates = getIconCandidates()
824846

825847
console.log('[Tray] 尝试加载图标,候选路径:', candidates)
826848

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "delive",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"description": "DeLive",
55
"private": true,
66
"main": "dist-electron/main.js",

0 commit comments

Comments
 (0)