const { app, nativeImage } = require('electron') const { writeFileSync } = require('node:fs') const path = require('node:path') const projectRoot = path.join(__dirname, '..') const sizes = [16, 24, 32, 48, 64, 128, 256] function insideRoundedRect(x, y, left, top, right, bottom, radius) { const nearestX = Math.max(left + radius, Math.min(x, right - radius)) const nearestY = Math.max(top + radius, Math.min(y, bottom - radius)) return Math.hypot(x - nearestX, y - nearestY) <= radius } function createSourceImage() { const size = 512 const bitmap = Buffer.alloc(size * size * 4) const bars = [ [120, 124, 196, 388, 38, 255], [218, 188, 294, 388, 38, 224], [316, 252, 392, 388, 38, 173], ] for (let y = 0; y < size; y += 1) { for (let x = 0; x < size; x += 1) { const offset = (y * size + x) * 4 if (insideRoundedRect(x + 0.5, y + 0.5, 32, 32, 480, 480, 120)) { bitmap[offset] = 246 bitmap[offset + 1] = 87 bitmap[offset + 2] = 49 bitmap[offset + 3] = 255 } const bar = bars.find(([left, top, right, bottom, radius]) => ( insideRoundedRect(x + 0.5, y + 0.5, left, top, right, bottom, radius) )) if (bar) { const opacity = bar[5] / 255 bitmap[offset] = Math.round(246 + (255 - 246) * opacity) bitmap[offset + 1] = Math.round(87 + (255 - 87) * opacity) bitmap[offset + 2] = Math.round(49 + (255 - 49) * opacity) bitmap[offset + 3] = 255 } } } return nativeImage.createFromBitmap(bitmap, { width: size, height: size, scaleFactor: 1 }) } function generateIcon() { const source = createSourceImage() const images = sizes.map((size) => source.resize({ width: size, height: size, quality: 'best' }).toPNG()) const headerSize = 6 + images.length * 16 const header = Buffer.alloc(headerSize) header.writeUInt16LE(0, 0) header.writeUInt16LE(1, 2) header.writeUInt16LE(images.length, 4) let imageOffset = headerSize images.forEach((image, index) => { const entryOffset = 6 + index * 16 const size = sizes[index] header.writeUInt8(size === 256 ? 0 : size, entryOffset) header.writeUInt8(size === 256 ? 0 : size, entryOffset + 1) header.writeUInt8(0, entryOffset + 2) header.writeUInt8(0, entryOffset + 3) header.writeUInt16LE(1, entryOffset + 4) header.writeUInt16LE(32, entryOffset + 6) header.writeUInt32LE(image.length, entryOffset + 8) header.writeUInt32LE(imageOffset, entryOffset + 12) imageOffset += image.length }) writeFileSync(path.join(projectRoot, 'build', 'icon-preview.png'), images.at(-1)) writeFileSync(path.join(projectRoot, 'build', 'icon.ico'), Buffer.concat([header, ...images])) } app.whenReady() .then(generateIcon) .then(() => app.quit()) .catch((error) => { console.error(error) app.exit(1) })