mirror of
https://github.com/mileswolfallen2/gelectron.git
synced 2026-12-03 23:53:26 +00:00
feat: implement application icon support for macOS
- Removed package.json and code signature files from the macOS app bundle. - Added support for setting application icons via base64 encoded PNG strings. - Enhanced the main application logic to detect and apply icons from various locations. - Updated the Electron app and browser window classes to handle icon setting. - Introduced new methods for decoding PNG icons and applying them to the application and window. - Updated Cargo.toml to include the cocoa dependency for macOS.
This commit is contained in:
1 parent
a7b6f9a072
commit
da521a33a0
37 files changed
+513
-5076
No files matched your search
+33
-2
@@ -12,6 +12,8 @@
|
||||
|
||||
const { EventEmitter } = require('events');
|
||||
const path = require('path');
|
||||
const { bridge } = require('./native-bridge');
|
||||
const NativeImage = require('./native-image');
|
||||
|
||||
class App extends EventEmitter {
|
||||
constructor() {
|
||||
@@ -24,7 +26,7 @@ class App extends EventEmitter {
|
||||
this._badgeCount = 0;
|
||||
this._secureKeyboardEntryEnabled = false;
|
||||
this._userAgent = null;
|
||||
this._name = 'Gelectron App';
|
||||
this._name = this._detectAppName();
|
||||
this._names = null;
|
||||
|
||||
const os = require('os');
|
||||
@@ -45,7 +47,10 @@ class App extends EventEmitter {
|
||||
|
||||
this._commandLine = new Map();
|
||||
this._dock = process.platform === 'darwin' ? {
|
||||
setIcon: (icon) => {},
|
||||
setIcon: (icon) => {
|
||||
const b64 = this._iconToBase64Png(icon);
|
||||
if (b64) bridge.setAppIcon(b64);
|
||||
},
|
||||
bounce: () => 0,
|
||||
cancelBounce: () => {},
|
||||
setBadge: () => {},
|
||||
@@ -72,6 +77,32 @@ class App extends EventEmitter {
|
||||
});
|
||||
}
|
||||
|
||||
_detectAppName() {
|
||||
try {
|
||||
const appDir = process.env.GELECTRON_APP_PATH || process.cwd();
|
||||
const pkg = require(path.join(appDir, 'package.json'));
|
||||
return pkg.productName || pkg.name || 'Gelectron App';
|
||||
} catch (e) {
|
||||
return 'Gelectron App';
|
||||
}
|
||||
}
|
||||
|
||||
_iconToBase64Png(icon) {
|
||||
try {
|
||||
let img = icon;
|
||||
if (typeof icon === 'string') {
|
||||
img = NativeImage.createFromPath(icon);
|
||||
}
|
||||
if (img && typeof img.toPNG === 'function' && !img.isEmpty()) {
|
||||
const png = img.toPNG();
|
||||
if (png && png.length > 0) return png.toString('base64');
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore invalid icon
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// ─── Properties ──────────────────────────────────────────────
|
||||
|
||||
get commandLine() {
|
||||
|
||||
@@ -8,6 +8,7 @@ const { EventEmitter } = require('events');
|
||||
const path = require('path');
|
||||
const { bridge, isNative } = require('./native-bridge');
|
||||
const { app } = require('./app');
|
||||
const NativeImage = require('./native-image');
|
||||
|
||||
class WebContents extends EventEmitter {
|
||||
constructor(id) {
|
||||
@@ -219,6 +220,7 @@ class BrowserWindow extends EventEmitter {
|
||||
resizable: this._options.resizable,
|
||||
alwaysOnTop: this._options.alwaysOnTop,
|
||||
fullscreen: this._options.fullscreen,
|
||||
icon: this._iconToBase64Png(this._options.icon),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -232,6 +234,23 @@ class BrowserWindow extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
_iconToBase64Png(icon) {
|
||||
if (!icon) return null;
|
||||
try {
|
||||
let img = icon;
|
||||
if (typeof icon === 'string') {
|
||||
img = NativeImage.createFromPath(icon);
|
||||
}
|
||||
if (img && typeof img.toPNG === 'function' && !img.isEmpty()) {
|
||||
const png = img.toPNG();
|
||||
if (png && png.length > 0) return png.toString('base64');
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore invalid icon
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static fromWebContents(webContents) {
|
||||
for (const win of BrowserWindow._windows.values()) {
|
||||
if (win.webContents && win.webContents.id === webContents.id) return win;
|
||||
@@ -270,7 +289,6 @@ class BrowserWindow extends EventEmitter {
|
||||
if (isNative) bridge.showWindow(this.id);
|
||||
this.emit('show');
|
||||
}
|
||||
|
||||
hide() {
|
||||
if (this._isDestroyed) return;
|
||||
this._isVisible = false;
|
||||
|
||||
@@ -152,6 +152,10 @@ class NativeBridge extends EventEmitter {
|
||||
quit() {
|
||||
this._send({ type: 'quit' });
|
||||
}
|
||||
|
||||
setAppIcon(base64Png) {
|
||||
this._send({ type: 'set-app-icon', icon: base64Png });
|
||||
}
|
||||
}
|
||||
|
||||
const bridge = new NativeBridge();
|
||||
|
||||
@@ -17,12 +17,22 @@ class NativeImage {
|
||||
try {
|
||||
const fs = require('fs');
|
||||
const data = fs.readFileSync(path);
|
||||
// Basic PNG header parsing for dimensions
|
||||
img._data = data;
|
||||
img._isEmpty = false;
|
||||
if (data.length > 24 && data[0] === 0x89 && data[1] === 0x50) {
|
||||
img._width = data.readUInt32BE(16);
|
||||
img._height = data.readUInt32BE(20);
|
||||
img._data = data;
|
||||
img._isEmpty = false;
|
||||
} else if (data.length > 22 && data[0] === 0x00 && data[1] === 0x00 && data[2] === 0x01 && data[3] === 0x00) {
|
||||
img._width = data[6] || 256;
|
||||
img._height = data[7] || 256;
|
||||
} else if (data.length > 12 && data[0] === 0x69 && data[1] === 0x63 && data[2] === 0x6e && data[3] === 0x73) {
|
||||
const type = data.toString('ascii', 8, 12);
|
||||
const c = type.charCodeAt(1);
|
||||
if (c >= 0x30 && c <= 0x39) {
|
||||
img._width = img._height = 1 << (c - 0x30);
|
||||
} else if (type[0] === 'p' && c >= 0x34 && c <= 0x38) {
|
||||
img._width = img._height = 16 << (c - 0x34);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[gelectron] Failed to load image: ${path}`, err.message);
|
||||
@@ -109,9 +119,6 @@ class NativeImage {
|
||||
isTemplateImage() {
|
||||
return this._isTemplate || false;
|
||||
}
|
||||
|
||||
toPNG(options) { return this.toPNG(options); }
|
||||
toJPEG(quality) { return this.toJPEG(quality); }
|
||||
}
|
||||
|
||||
module.exports = NativeImage;
|
||||
Reference in new issue
Block a user