fix(win): load window icon from ICO on disk + buffer PNG (1.0.6)
Release / release (push) Successful in 5m8s
Release / release (push) Successful in 5m8s
nativeImage from paths inside app.asar often yields empty images on Windows. Ship icon.ico via extraResources and read with createFromBuffer; setIcon on boot + app windows. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
import { app, BrowserWindow, nativeImage, screen } from 'electron';
|
||||
import { app, BrowserWindow, screen } from 'electron';
|
||||
|
||||
import { ipcChannels } from '../../shared/ipc/contracts';
|
||||
|
||||
import { getBootSplashWindow } from './bootWindow';
|
||||
import { loadBrandingWindowIcon } from './brandingIcon';
|
||||
|
||||
type WindowKind = 'editor' | 'presentation' | 'control';
|
||||
|
||||
@@ -69,91 +69,15 @@ function getPreloadPath(): string {
|
||||
return path.join(app.getAppPath(), 'dist', 'preload', 'index.cjs');
|
||||
}
|
||||
|
||||
/**
|
||||
* PNG для иконки окна / дока: копия `build/icon.png` в dist после сборки, затем окно 256px.
|
||||
* В упакованном приложении файлы под `dist/renderer/*.png` вынесены в `app.asar.unpacked`
|
||||
* — `nativeImage` на Windows с путём только внутри asar часто даёт пустую иконку.
|
||||
* SVG не используем для nativeImage на Windows — иначе пустая картинка и дефолт Electron.
|
||||
*/
|
||||
function resolveBrandingPngPaths(): string[] {
|
||||
const root = app.getAppPath();
|
||||
const relPack = path.join('dist', 'renderer', 'app-pack-icon.png');
|
||||
const relWindow = path.join('dist', 'renderer', 'app-window-icon.png');
|
||||
const paths: string[] = [];
|
||||
if (app.isPackaged) {
|
||||
const unpacked = path.join(process.resourcesPath, 'app.asar.unpacked');
|
||||
paths.push(path.join(unpacked, relPack), path.join(unpacked, relWindow));
|
||||
}
|
||||
paths.push(
|
||||
path.join(root, relPack),
|
||||
path.join(root, relWindow),
|
||||
path.join(root, 'build', 'icon.png'),
|
||||
path.join(root, 'app', 'renderer', 'public', 'app-window-icon.png'),
|
||||
);
|
||||
return paths;
|
||||
}
|
||||
|
||||
function resolveWindowIconPath(): string | undefined {
|
||||
for (const p of resolveBrandingPngPaths()) {
|
||||
try {
|
||||
if (fs.existsSync(p)) return p;
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
const root = app.getAppPath();
|
||||
const svgFallback = [
|
||||
path.join(root, 'dist', 'renderer', 'app-logo.svg'),
|
||||
path.join(root, 'app', 'renderer', 'public', 'app-logo.svg'),
|
||||
];
|
||||
for (const p of svgFallback) {
|
||||
try {
|
||||
if (fs.existsSync(p)) return p;
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function resolveWindowIcon(): Electron.NativeImage | undefined {
|
||||
const tryPath = (filePath: string): Electron.NativeImage | undefined => {
|
||||
try {
|
||||
const img = nativeImage.createFromPath(filePath);
|
||||
if (!img.isEmpty()) return img;
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
if (process.platform === 'win32' || process.platform === 'linux') {
|
||||
for (const p of resolveBrandingPngPaths()) {
|
||||
if (!fs.existsSync(p)) continue;
|
||||
const img = tryPath(p);
|
||||
if (img) return img;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const p = resolveWindowIconPath();
|
||||
if (!p) return undefined;
|
||||
return tryPath(p);
|
||||
}
|
||||
|
||||
/** macOS: в Dock показываем тот же PNG, что и у упакованного приложения на Windows (иконка exe). */
|
||||
/** macOS: в Dock — тот же растр, что и у окон (ICO/PNG из brandingIcon). */
|
||||
export function applyDockIconIfNeeded(): void {
|
||||
if (process.platform !== 'darwin' || !app.dock) return;
|
||||
for (const p of resolveBrandingPngPaths()) {
|
||||
if (!fs.existsSync(p)) continue;
|
||||
try {
|
||||
const img = nativeImage.createFromPath(p);
|
||||
if (img.isEmpty()) continue;
|
||||
app.dock.setIcon(img);
|
||||
return;
|
||||
} catch {
|
||||
/* try next */
|
||||
}
|
||||
const icon = loadBrandingWindowIcon();
|
||||
if (!icon || icon.isEmpty()) return;
|
||||
try {
|
||||
app.dock.setIcon(icon);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +113,7 @@ function ensureWindowBecomesVisible(win: BrowserWindow): void {
|
||||
|
||||
function createWindow(kind: WindowKind, opts?: CreateWindowOpts): BrowserWindow {
|
||||
const deferEditor = kind === 'editor' && opts?.deferVisibility === true;
|
||||
const icon = resolveWindowIcon();
|
||||
const icon = loadBrandingWindowIcon();
|
||||
const win = new BrowserWindow({
|
||||
width: kind === 'editor' ? 1280 : kind === 'control' ? 1200 : 1280,
|
||||
height: 800,
|
||||
@@ -208,6 +132,13 @@ function createWindow(kind: WindowKind, opts?: CreateWindowOpts): BrowserWindow
|
||||
webSecurity: Boolean(process.env.VITE_DEV_SERVER_URL),
|
||||
},
|
||||
});
|
||||
if (icon) {
|
||||
try {
|
||||
win.setIcon(icon);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
win.webContents.on('preload-error', (_event, preloadPath, error) => {
|
||||
console.error(`[preload-error] ${preloadPath}:`, error);
|
||||
|
||||
Reference in New Issue
Block a user