mirror of
https://github.com/mileswolfallen2/gelectron.git
synced 2026-12-04 07:57:44 +00:00
update
This commit is contained in:
1 parent
d4620e5e8e
commit
cedb606500
15 files changed
+1114
-29
No files matched your search
@@ -47,6 +47,15 @@ class App extends EventEmitter {
|
||||
} : null;
|
||||
|
||||
this._argv = process.argv.slice();
|
||||
|
||||
// Emit 'ready' automatically on the next tick, matching real Electron
|
||||
// where the app becomes ready once the process has initialised.
|
||||
process.nextTick(() => {
|
||||
if (!this._ready) {
|
||||
this._ready = true;
|
||||
this.emit('ready');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
get commandLine() {
|
||||
|
||||
@@ -3,7 +3,15 @@
|
||||
/**
|
||||
* Gelectron - autoUpdater module (Electron compatible)
|
||||
* Stub implementation for electron-updater compatibility.
|
||||
* electron-updater uses require("electron").autoUpdater as its native backend.
|
||||
*
|
||||
* electron-updater's MacUpdater / NsisUpdater / AppImageUpdater all
|
||||
* access `require("electron").autoUpdater` and expect it to be an
|
||||
* EventEmitter with the native Electron autoUpdater API surface:
|
||||
* .on("error", …), .on("update-downloaded", …)
|
||||
* .setFeedURL(), .getFeedURL()
|
||||
* .checkForUpdates()
|
||||
* .quitAndInstall()
|
||||
* .removeListener()
|
||||
*/
|
||||
|
||||
const { EventEmitter } = require('events');
|
||||
@@ -13,6 +21,7 @@ class AutoUpdater extends EventEmitter {
|
||||
super();
|
||||
this._isUpdateAvailable = false;
|
||||
this._updateInfo = null;
|
||||
this._feedURL = null;
|
||||
this.autoDownload = true;
|
||||
this.autoInstallOnAppQuit = false;
|
||||
this.autoRunAppAfterInstall = true;
|
||||
@@ -20,10 +29,12 @@ class AutoUpdater extends EventEmitter {
|
||||
}
|
||||
|
||||
getFeedURL() {
|
||||
return null;
|
||||
return this._feedURL;
|
||||
}
|
||||
|
||||
setFeedURL() {}
|
||||
setFeedURL(options) {
|
||||
this._feedURL = options;
|
||||
}
|
||||
|
||||
async checkForUpdates() {
|
||||
return {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
const { EventEmitter } = require('events');
|
||||
const path = require('path');
|
||||
const { bridge, isNative } = require('./native-bridge');
|
||||
|
||||
class WebContents extends EventEmitter {
|
||||
constructor(id) {
|
||||
@@ -52,22 +53,41 @@ class WebContents extends EventEmitter {
|
||||
this._url = targetUrl;
|
||||
this._isLoading = true;
|
||||
this.emit('did-start-loading');
|
||||
if (isNative) {
|
||||
bridge.loadUrl(this.id, targetUrl);
|
||||
}
|
||||
setTimeout(() => {
|
||||
this._isLoading = false;
|
||||
this.emit('did-stop-loading');
|
||||
this.emit('did-finish-load');
|
||||
this.emit('dom-ready');
|
||||
}, 100);
|
||||
}, isNative ? 500 : 100);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
loadFile(filePath) {
|
||||
return this.loadURL(`file://${path.resolve(filePath)}`);
|
||||
const url = `file://${path.resolve(filePath)}`;
|
||||
this._url = url;
|
||||
this._isLoading = true;
|
||||
this.emit('did-start-loading');
|
||||
if (isNative) {
|
||||
bridge.loadFile(this.id, filePath);
|
||||
}
|
||||
setTimeout(() => {
|
||||
this._isLoading = false;
|
||||
this.emit('did-stop-loading');
|
||||
this.emit('did-finish-load');
|
||||
this.emit('dom-ready');
|
||||
}, isNative ? 500 : 100);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
reload() {
|
||||
this._isLoading = true;
|
||||
this.emit('did-start-loading');
|
||||
if (isNative && this._url) {
|
||||
bridge.loadUrl(this.id, this._url);
|
||||
}
|
||||
setTimeout(() => {
|
||||
this._isLoading = false;
|
||||
this.emit('did-stop-loading');
|
||||
@@ -82,6 +102,9 @@ class WebContents extends EventEmitter {
|
||||
goForward() {}
|
||||
|
||||
executeJavaScript(code, userGesture = true) {
|
||||
if (isNative) {
|
||||
bridge.evalJs(this.id, code);
|
||||
}
|
||||
return Promise.resolve(null);
|
||||
}
|
||||
|
||||
@@ -89,7 +112,11 @@ class WebContents extends EventEmitter {
|
||||
insertJS(code, hasUserGesture = true) { return this.executeJavaScript(code, hasUserGesture); }
|
||||
|
||||
send(channel, ...args) {
|
||||
console.log(`[gelectron] webContents.send('${channel}')`);
|
||||
if (isNative) {
|
||||
bridge.sendToRenderer(this.id, channel, ...args);
|
||||
} else {
|
||||
console.log(`[gelectron] webContents.send('${channel}')`);
|
||||
}
|
||||
}
|
||||
|
||||
sendInputEvent() {}
|
||||
@@ -178,6 +205,18 @@ class BrowserWindow extends EventEmitter {
|
||||
|
||||
BrowserWindow._windows.set(this.id, this);
|
||||
|
||||
if (isNative) {
|
||||
bridge.createWindow(this.id, {
|
||||
width: this._options.width,
|
||||
height: this._options.height,
|
||||
title: this._options.title,
|
||||
show: this._options.show,
|
||||
resizable: this._options.resizable,
|
||||
alwaysOnTop: this._options.alwaysOnTop,
|
||||
fullscreen: this._options.fullscreen,
|
||||
});
|
||||
}
|
||||
|
||||
if (this._options.show) {
|
||||
process.nextTick(() => {
|
||||
if (!this._isDestroyed) {
|
||||
@@ -209,23 +248,28 @@ class BrowserWindow extends EventEmitter {
|
||||
|
||||
loadURL(targetUrl) {
|
||||
this._url = targetUrl;
|
||||
if (isNative) {
|
||||
bridge.loadUrl(this.id, targetUrl);
|
||||
}
|
||||
return this.webContents.loadURL(targetUrl);
|
||||
}
|
||||
|
||||
loadFile(filePath) {
|
||||
return this.loadURL(`file://${path.resolve(filePath)}`);
|
||||
return this.webContents.loadFile(filePath);
|
||||
}
|
||||
|
||||
show() {
|
||||
if (this._isDestroyed) return;
|
||||
this._isVisible = true;
|
||||
this._isMinimized = false;
|
||||
if (isNative) bridge.showWindow(this.id);
|
||||
this.emit('show');
|
||||
}
|
||||
|
||||
hide() {
|
||||
if (this._isDestroyed) return;
|
||||
this._isVisible = false;
|
||||
if (isNative) bridge.hideWindow(this.id);
|
||||
this.emit('hide');
|
||||
}
|
||||
|
||||
@@ -238,15 +282,16 @@ class BrowserWindow extends EventEmitter {
|
||||
destroy() {
|
||||
if (this._isDestroyed) return;
|
||||
this._isDestroyed = true;
|
||||
if (isNative) bridge.destroyWindow(this.id);
|
||||
BrowserWindow._windows.delete(this.id);
|
||||
this.emit('closed');
|
||||
}
|
||||
|
||||
focus() { if (!this._isDestroyed) this.emit('focus'); }
|
||||
focus() { if (!this._isDestroyed) { if (isNative) bridge.focusWindow(this.id); this.emit('focus'); } }
|
||||
blur() {}
|
||||
|
||||
minimize() { if (!this._isDestroyed) { this._isMinimized = true; this.emit('minimize'); } }
|
||||
maximize() { if (!this._isDestroyed) { this._isMaximized = true; this.emit('maximize'); } }
|
||||
minimize() { if (!this._isDestroyed) { this._isMinimized = true; if (isNative) bridge.minimizeWindow(this.id); this.emit('minimize'); } }
|
||||
maximize() { if (!this._isDestroyed) { this._isMaximized = true; if (isNative) bridge.maximizeWindow(this.id); this.emit('maximize'); } }
|
||||
unmaximize() { if (!this._isDestroyed) { this._isMaximized = false; this.emit('unmaximize'); } }
|
||||
restore() { if (!this._isDestroyed) { this._isMinimized = false; this._isMaximized = false; this.emit('restore'); } }
|
||||
|
||||
@@ -282,7 +327,7 @@ class BrowserWindow extends EventEmitter {
|
||||
setClosable(v) { this._options.closable = v; }
|
||||
isClosable() { return this._options.closable; }
|
||||
|
||||
setTitle(title) { this._options.title = title; this.webContents._title = title; }
|
||||
setTitle(title) { this._options.title = title; this.webContents._title = title; if (isNative) bridge.setTitle(this.id, title); }
|
||||
getTitle() { return this._options.title; }
|
||||
|
||||
setSkipTaskbar() {}
|
||||
|
||||
@@ -18,6 +18,14 @@ const safeStorage = require('./safe-storage');
|
||||
const contextBridge = require('./context-bridge');
|
||||
const webContents = require('./web-contents');
|
||||
const { autoUpdater, AutoUpdater } = require('./auto-updater');
|
||||
const { bridge, isNative } = require('./native-bridge');
|
||||
|
||||
if (isNative) {
|
||||
bridge.on('ipc-message', (windowId, channel, data) => {
|
||||
const event = { sender: { id: windowId }, channel };
|
||||
ipcMain._emit(channel, event, data);
|
||||
});
|
||||
}
|
||||
|
||||
// Session stub (electron-updater calls session.fromPartition)
|
||||
const sessionStub = {
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Gelectron Native Bridge - IPC between Node.js and the Rust binary.
|
||||
* When GELECTRON_NATIVE=1, communicates via stdin/stdout JSON lines.
|
||||
*/
|
||||
|
||||
const readline = require('readline');
|
||||
const { EventEmitter } = require('events');
|
||||
|
||||
const isNative = process.env.GELECTRON_NATIVE === '1';
|
||||
|
||||
class NativeBridge extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this._ready = false;
|
||||
this._readyCallbacks = [];
|
||||
this._windowListeners = new Map();
|
||||
|
||||
if (isNative) {
|
||||
this._setupStdio();
|
||||
}
|
||||
}
|
||||
|
||||
_setupStdio() {
|
||||
const rl = readline.createInterface({ input: process.stdin, terminal: false });
|
||||
|
||||
rl.on('line', (line) => {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || !trimmed.startsWith('{')) return;
|
||||
try {
|
||||
const msg = JSON.parse(trimmed);
|
||||
this._handleMessage(msg);
|
||||
} catch (e) {
|
||||
// Not IPC, ignore
|
||||
}
|
||||
});
|
||||
|
||||
process.stdout.on('error', () => {});
|
||||
}
|
||||
|
||||
_handleMessage(msg) {
|
||||
switch (msg.type) {
|
||||
case 'ready':
|
||||
this._ready = true;
|
||||
for (const cb of this._readyCallbacks) cb();
|
||||
this._readyCallbacks = [];
|
||||
this.emit('ready');
|
||||
break;
|
||||
case 'window-closed':
|
||||
this.emit('window-closed', msg.id);
|
||||
break;
|
||||
case 'window-focus':
|
||||
this.emit('window-focus', msg.id);
|
||||
break;
|
||||
case 'ipc-message':
|
||||
this.emit('ipc-message', msg.id, msg.channel, msg.data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_send(msg) {
|
||||
if (!isNative) return;
|
||||
try {
|
||||
process.stdout.write(JSON.stringify(msg) + '\n');
|
||||
} catch (e) {
|
||||
// stdout may be closed
|
||||
}
|
||||
}
|
||||
|
||||
onReady(cb) {
|
||||
if (this._ready) {
|
||||
cb();
|
||||
} else {
|
||||
this._readyCallbacks.push(cb);
|
||||
}
|
||||
}
|
||||
|
||||
createWindow(id, options) {
|
||||
this._send({ type: 'create-window', id, options });
|
||||
}
|
||||
|
||||
loadUrl(id, url) {
|
||||
this._send({ type: 'load-url', id, url });
|
||||
}
|
||||
|
||||
loadFile(id, filePath) {
|
||||
this._send({ type: 'load-file', id, path: filePath });
|
||||
}
|
||||
|
||||
destroyWindow(id) {
|
||||
this._send({ type: 'destroy-window', id });
|
||||
}
|
||||
|
||||
setTitle(id, title) {
|
||||
this._send({ type: 'set-title', id, title });
|
||||
}
|
||||
|
||||
setSize(id, width, height) {
|
||||
this._send({ type: 'set-size', id, width, height });
|
||||
}
|
||||
|
||||
showWindow(id) {
|
||||
this._send({ type: 'show', id });
|
||||
}
|
||||
|
||||
hideWindow(id) {
|
||||
this._send({ type: 'hide', id });
|
||||
}
|
||||
|
||||
focusWindow(id) {
|
||||
this._send({ type: 'focus', id });
|
||||
}
|
||||
|
||||
minimizeWindow(id) {
|
||||
this._send({ type: 'minimize', id });
|
||||
}
|
||||
|
||||
maximizeWindow(id) {
|
||||
this._send({ type: 'maximize', id });
|
||||
}
|
||||
|
||||
closeWindow(id) {
|
||||
this._send({ type: 'close', id });
|
||||
}
|
||||
|
||||
sendToRenderer(id, channel, ...data) {
|
||||
this._send({ type: 'ipc-message', id, channel, data: data.length === 1 ? data[0] : data });
|
||||
}
|
||||
|
||||
evalJs(id, script) {
|
||||
this._send({ type: 'eval-js', id, script });
|
||||
}
|
||||
|
||||
quit() {
|
||||
this._send({ type: 'quit' });
|
||||
}
|
||||
}
|
||||
|
||||
const bridge = new NativeBridge();
|
||||
|
||||
module.exports = { bridge, isNative };
|
||||
+37
-8
@@ -14,24 +14,53 @@ function run(mainScript, env) {
|
||||
|
||||
// Patch require so that 'electron' resolves to gelectron's compat layer
|
||||
const electronCompatPath = path.join(__dirname, 'index.js');
|
||||
const ipcRendererPath = path.join(__dirname, 'ipc-renderer.js');
|
||||
|
||||
// Purge any cached 'electron' module from the real npm package so that
|
||||
// our _resolveFilename patch takes absolute priority. The real 'electron'
|
||||
// npm package (devDependency in the target app) exports a *path string*
|
||||
// to the Electron binary – it does NOT have .autoUpdater, .app, etc.
|
||||
for (const key of Object.keys(Module._cache)) {
|
||||
const normalized = key.replace(/\\/g, '/');
|
||||
if (
|
||||
normalized.endsWith('/electron') ||
|
||||
normalized.endsWith('/electron/index.js') ||
|
||||
normalized.endsWith('/electron/index.cjs')
|
||||
) {
|
||||
delete Module._cache[key];
|
||||
}
|
||||
}
|
||||
|
||||
const originalResolveFilename = Module._resolveFilename;
|
||||
Module._resolveFilename = function (request, parent, isMain, options) {
|
||||
if (request === 'electron') {
|
||||
return electronCompatPath;
|
||||
}
|
||||
if (request === 'electron/main') {
|
||||
if (request === 'electron' || request === 'electron/main' || request === 'electron/common') {
|
||||
return electronCompatPath;
|
||||
}
|
||||
if (request === 'electron/renderer') {
|
||||
return path.join(__dirname, 'ipc-renderer.js');
|
||||
}
|
||||
if (request === 'electron/common') {
|
||||
return electronCompatPath;
|
||||
return ipcRendererPath;
|
||||
}
|
||||
return originalResolveFilename.call(this, request, parent, isMain, options);
|
||||
};
|
||||
|
||||
// Also monkey-patch Module._resolveRequest for Node ≥ 22 where it may
|
||||
// be used internally instead of _resolveFilename.
|
||||
if (typeof Module._resolveRequest === 'function') {
|
||||
const originalResolveRequest = Module._resolveRequest;
|
||||
Module._resolveRequest = function (request, parent, isMain, options) {
|
||||
if (request === 'electron' || request === 'electron/main' || request === 'electron/common') {
|
||||
return electronCompatPath;
|
||||
}
|
||||
if (request === 'electron/renderer') {
|
||||
return ipcRendererPath;
|
||||
}
|
||||
return originalResolveRequest.call(this, request, parent, isMain, options);
|
||||
};
|
||||
}
|
||||
|
||||
// Pre-load the gelectron shim into the module cache so that every
|
||||
// subsequent require('electron') hits cache immediately.
|
||||
require(electronCompatPath);
|
||||
|
||||
// Require the app's main script
|
||||
try {
|
||||
require(mainScript);
|
||||
|
||||
Reference in new issue
Block a user