feat: enhance native binary resolution and add bundled Node.js runtime

- Refactored gelectron.js to improve native binary discovery with additional paths and environment variable support.
- Implemented a function to find the Node.js runtime, allowing bundled Node.js to be used when available.
- Updated package.json to include gelectron-packager as a CLI command.
- Enhanced gelectron-packager.js to locate installed gelectron runtime and added compatibility for bundled Node.js.
- Modified install-release.sh to download and install a private Node.js runtime alongside the gelectron binary.
- Updated NSIS installer script to include bundled Node.js runtime.
- Enhanced AppImage and DMG packaging scripts to include Node.js runtime.
- Added clipboard, nativeTheme, and screen modules for Electron compatibility.
- Implemented native bridge request handling for clipboard operations.
This commit is contained in:
miles committed 2026-09-05 22:23:37 -05:00
1 parent 247daa6460
commit 1689adffc6
16 files changed
+816 -79

No files matched your search

+195
View File
@@ -0,0 +1,195 @@
'use strict';
/**
* Gelectron - clipboard module (Electron compatible)
*
* Implements text / HTML / rich-text clipboard operations using the OS-native
* tooling so it works both in native Node mode and in the pure-Node fallback.
*/
const { execSync } = require('child_process');
const os = require('os');
const { isNative } = require('./native-bridge');
function platform() {
return os.platform();
}
// ─── Low-level platform pipes ───────────────────────────────────────────────
function readCommand(cmd) {
try {
const out = execSync(cmd, { stdio: ['pipe', 'pipe', 'pipe'], encoding: 'utf8' });
return out == null ? '' : out.toString();
} catch (e) {
return '';
}
}
function writePipe(cmd, text) {
try {
execSync(`${cmd} '${String(text).replace(/'/g, "'\\''")}'`, { stdio: 'pipe' });
return true;
} catch (e) {
try {
const { spawnSync } = require('child_process');
if (platform() === 'win32') {
// Powershell stdin pipe is the most reliable on Windows
const ps = spawnSync('powershell', ['-NoProfile', '-Command', 'Set-Clipboard'], {
input: String(text),
encoding: 'utf8',
});
return ps.status === 0;
}
return false;
} catch (e2) {
return false;
}
}
}
function readTextFromSystem() {
const p = platform();
try {
if (p === 'darwin') return execSync('pbpaste', { encoding: 'utf8' }).toString();
if (p === 'win32') {
const out = execSync('powershell -NoProfile -Command "Get-Clipboard -Raw"', { encoding: 'utf8' });
return out.replace(/\r?\n$/, '');
}
for (const tool of ['xclip -selection clipboard -o', 'xsel --clipboard --output', 'wl-paste']) {
try {
return execSync(tool, { encoding: 'utf8' }).toString();
} catch (e) { /* try next */ }
}
return '';
} catch (e) {
return '';
}
}
function writeTextToSystem(text) {
const p = platform();
const content = String(text);
try {
if (p === 'darwin') return writePipe('pbcopy', content);
if (p === 'win32') {
const { spawnSync } = require('child_process');
const ps = spawnSync('powershell', ['-NoProfile', '-Command', 'Set-Clipboard'], {
input: content,
encoding: 'utf8',
});
return ps.status === 0;
}
for (const cmd of [
`xclip -selection clipboard -in <<'GELECTRON_EOF'\n${content}\nGELECTRON_EOF`,
`xsel --clipboard --input <<'GELECTRON_EOF'\n${content}\nGELECTRON_EOF`,
`wl-copy <<'GELECTRON_EOF'\n${content}\nGELECTRON_EOF`,
]) {
try {
execSync(cmd, { encoding: 'utf8' });
return true;
} catch (e) { /* try next */ }
}
return false;
} catch (e) {
return false;
}
}
// ─── Public API ─────────────────────────────────────────────────────────────
const clipboard = {
readText() {
return readTextFromSystem();
},
writeText(text) {
return writeTextToSystem(text);
},
readHTML() {
// Native bridge request falls back to plain text when unavailable
if (isNative && typeof globalThis.__gelectron_clipboard_read_html === 'function') {
return globalThis.__gelectron_clipboard_read_html();
}
return readTextFromSystem();
},
writeHTML(markup, type = 'text/html') {
try {
if (platform() === 'win32') {
const html = String(markup);
const { spawnSync } = require('child_process');
const ps = spawnSync('powershell', ['-NoProfile', '-Command',
`Set-Clipboard -Value @'${html.replace(/'/g, "''")}'@`], { encoding: 'utf8' });
return ps.status === 0;
}
return writeTextToSystem(markup);
} catch (e) {
return false;
}
},
// Best-effort plain text read (Electron returns an image object for rich
// clipboard types on some platforms; we degrade to text where possible).
readTextOnly() {
return readTextFromSystem();
},
readImage() {
return NativeImageStub.empty();
},
writeImage(image) {
return !!image;
},
availableFormats() {
return ['text/plain'];
},
read(format = 'text/plain') {
return readTextFromSystem();
},
write(data, type = 'text/plain') {
return writeTextToSystem(data);
},
clear() {},
readBookmark() {
return { title: '', url: '' };
},
writeBookmark() {},
readFindText() {
try {
return process.env.GELECTRON_FIND_TEXT || '';
} catch (e) {
return '';
}
},
writeFindText(text) {
try {
process.env.GELECTRON_FIND_TEXT = String(text);
return true;
} catch (e) {
return false;
}
},
};
// Avoid a hard dependency on native-image for the stub paths.
const NativeImageStub = {
empty() {
return {
isEmpty: () => true,
toPNG: () => Buffer.alloc(0),
};
},
};
module.exports = clipboard;
+8
View File
@@ -70,6 +70,14 @@ class NativeBridge extends EventEmitter {
});
}
// Generic request to the native process: request(type, payload) where
// `type` matches a ToRust variant. Resolves with the JSON response from the
// native side (or null when running without the native binary).
request(type, payload = {}) {
if (!isNative) return Promise.resolve(null);
return this._request({ type, ...payload });
}
_resolveRequest(requestId, result, error) {
const pending = this._pendingRequests[requestId];
if (pending) {
+72
View File
@@ -0,0 +1,72 @@
'use strict';
/**
* Gelectron - nativeTheme module (Electron compatible)
*
* Reports dark mode / accent colors. When running with the native binary it
* asks the OS for the current theme; otherwise it falls back to an env hint
* (or 'light').
*/
const { EventEmitter } = require('events');
const { bridge, isNative } = require('./native-bridge');
function osDarkHint() {
const env = process.env;
if (env.COLORFGBG && /^0;/.test(env.COLORFGBG)) return true;
return false;
}
class NativeTheme extends EventEmitter {
constructor() {
super();
this._dark = osDarkHint();
this._source = 'system';
this._accent = '#007AFF';
this._loaded = false;
if (isNative) {
bridge.request('native-theme-query', {}).then((res) => {
if (res) {
if (typeof res.shouldUseDarkColors === 'boolean') this._dark = res.shouldUseDarkColors;
if (res.accentColor) this._accent = res.accentColor;
if (res.themeSource) this._source = res.themeSource;
this._loaded = true;
}
}).catch(() => {});
}
}
get shouldUseDarkColors() {
return this._dark;
}
get shouldUseInvertedColorScheme() {
return false;
}
get themeSource() {
return this._source;
}
set themeSource(value) {
if (value === 'system' || value === 'light' || value === 'dark') {
this._source = value;
if (value === 'dark') this._dark = true;
else if (value === 'light') this._dark = false;
}
}
get themes() {
return {
initial: this._dark ? ['dark'] : ['light'],
current: this._dark ? ['dark'] : ['light'],
};
}
on(eventName, listener) { super.on(eventName, listener); return this; }
once(eventName, listener) { super.once(eventName, listener); return this; }
}
module.exports = new NativeTheme();
module.exports.NativeTheme = NativeTheme;
+151
View File
@@ -0,0 +1,151 @@
'use strict';
/**
* Gelectron - screen module (Electron compatible)
*
* Queries display information from the native process when available and
* otherwise returns sensible single-display defaults so apps never hang.
*/
const os = require('os');
const { EventEmitter } = require('events');
const { bridge, isNative } = require('./native-bridge');
function defaultDisplay() {
const width = 1920;
const height = 1080;
return {
id: 1,
label: 'Display 1',
bounds: { x: 0, y: 0, width, height },
workArea: { x: 0, y: 0, width, height },
size: { width, height },
workAreaSize: { width, height },
scaleFactor: 1,
rotation: 0,
internal: true,
touchSupport: 'unknown',
displayFrequency: 60,
colorSpace: '',
colorDepth: 24,
monitors: [],
};
}
function normalize(raw) {
const d = raw || {};
return {
id: d.id != null ? d.id : 1,
label: d.label || 'Display 1',
bounds: d.bounds || { x: 0, y: 0, width: 1920, height: 1080 },
workArea: d.workArea || d.bounds || { x: 0, y: 0, width: 1920, height: 1080 },
size: d.size || { width: 1920, height: 1080 },
workAreaSize: d.workAreaSize || d.size || { width: 1920, height: 1080 },
scaleFactor: d.scaleFactor || 1,
rotation: d.rotation || 0,
internal: !!d.internal,
touchSupport: d.touchSupport || 'unknown',
displayFrequency: d.displayFrequency || 60,
colorSpace: d.colorSpace || '',
colorDepth: d.colorDepth || 24,
monitors: d.monitors || [],
};
}
function rectsIntersect(a, b) {
return (
a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y
);
}
class Screen extends EventEmitter {
constructor() {
super();
this._displays = [defaultDisplay()];
// Parse an optional env-provided display list (packaged apps can inject
// this to avoid a round-trip).
try {
if (process.env.GELECTRON_DISPLAYS) {
const parsed = JSON.parse(process.env.GELECTRON_DISPLAYS);
if (Array.isArray(parsed) && parsed.length > 0) {
this._displays = parsed.map(normalize);
}
}
} catch (e) {
// Ignore malformed env
}
// Refresh from the native process when available.
if (isNative) {
bridge.request('screen-get-displays', {}).then((res) => {
if (res && Array.isArray(res.displays) && res.displays.length > 0) {
this._displays = res.displays.map(normalize);
this.emit('display-added', this._displays[this._displays.length - 1]);
}
}).catch(() => {});
}
}
getAllDisplays() {
return this._displays;
}
getPrimaryDisplay() {
return this._displays[0] || defaultDisplay();
}
getDisplayNearestPoint(point) {
const p = point || { x: 0, y: 0 };
let best = this._displays[0];
let bestDist = Infinity;
for (const d of this._displays) {
const cx = d.bounds.x + d.bounds.width / 2;
const cy = d.bounds.y + d.bounds.height / 2;
const dist = (p.x - cx) ** 2 + (p.y - cy) ** 2;
if (dist < bestDist) {
bestDist = dist;
best = d;
}
}
return best;
}
getDisplayForPoint(point) {
return this.getDisplayNearestPoint(point);
}
getDisplayMatching(rect) {
const r = rect || { x: 0, y: 0, width: 0, height: 0 };
let best = this._displays[0];
let bestArea = 0;
for (const d of this._displays) {
if (rectsIntersect(d.bounds, r)) {
const area = d.bounds.width * d.bounds.height;
if (area > bestArea) {
bestArea = area;
best = d;
}
}
}
return best;
}
getCursorScreenPoint() {
// No native cursor query is implemented; return the center of the primary
// display as a harmless default.
const d = this.getPrimaryDisplay();
return {
x: Math.round(d.bounds.x + d.bounds.width / 2),
y: Math.round(d.bounds.y + d.bounds.height / 2),
};
}
on(eventName, listener) { super.on(eventName, listener); return this; }
once(eventName, listener) { super.once(eventName, listener); return this; }
}
module.exports = { Screen, defaultDisplay, normalize };