/* 生成应用图标:渲染 SVG 风格图形 → PNG 多尺寸 → 打包为 .ico */
const { app, BrowserWindow, nativeImage } = require('electron');
const path = require('node:path');
const fs = require('node:fs');
const HTML = `
`;
function buildIco(pngs) {
const count = pngs.length;
const header = Buffer.alloc(6);
header.writeUInt16LE(0, 0);
header.writeUInt16LE(1, 2);
header.writeUInt16LE(count, 4);
let offset = 6 + count * 16;
const entries = [];
const datas = [];
for (const p of pngs) {
const e = Buffer.alloc(16);
e[0] = p.size >= 256 ? 0 : p.size;
e[1] = p.size >= 256 ? 0 : p.size;
e[2] = 0;
e[3] = 0;
e.writeUInt16LE(1, 4);
e.writeUInt16LE(32, 6);
e.writeUInt32LE(p.buf.length, 8);
e.writeUInt32LE(offset, 12);
offset += p.buf.length;
entries.push(e);
datas.push(p.buf);
}
return Buffer.concat([header, ...entries, ...datas]);
}
app.whenReady().then(async () => {
const win = new BrowserWindow({
width: 512, height: 512, show: false, frame: false, transparent: true,
backgroundColor: '#00000000',
webPreferences: { offscreen: false }
});
await win.loadURL('data:text/html;charset=utf-8,' + encodeURIComponent(HTML));
await new Promise((r) => setTimeout(r, 900));
const img = await win.webContents.capturePage();
const sizes = [16, 24, 32, 48, 64, 128, 256];
const pngs = sizes.map((size) => ({
size,
buf: img.resize({ width: size, height: size, quality: 'best' }).toPNG()
}));
const outDir = path.join(__dirname, '..', 'build');
fs.mkdirSync(outDir, { recursive: true });
fs.writeFileSync(path.join(outDir, 'icon.ico'), buildIco(pngs));
fs.writeFileSync(path.join(outDir, 'icon.png'), img.resize({ width: 512, height: 512, quality: 'best' }).toPNG());
console.log('icon written:', path.join(outDir, 'icon.ico'), fs.statSync(path.join(outDir, 'icon.ico')).size, 'bytes');
app.exit(0);
});