Electron fixes

This commit is contained in:
WardPearce
2026-04-23 22:40:54 +12:00
parent fa01e45eb3
commit a6adbdea34
4 changed files with 25 additions and 23 deletions
@@ -1,6 +1,4 @@
/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-var-requires */
const CapacitorNodejs = require('../../../node_modules/capacitor-nodejs/electron/dist/plugin.js');
module.exports = { module.exports = {
CapacitorNodejs,
} }
+3 -3
View File
@@ -33,11 +33,11 @@ Object.keys(plugins).forEach((pluginKey) => {
// Events // Events
if (plugins[pluginKey][classKey].prototype instanceof EventEmitter) { if (plugins[pluginKey][classKey].prototype instanceof EventEmitter) {
const listeners: { [key: string]: { type: string; listener: (...args: any[]) => void } } = {}; const listeners: { [key: string]: { type: string; listener: (...args: any[]) => void } } = {};
const listenersOfTypeExist = (type) => const listenersOfTypeExist = (type: any) =>
!!Object.values(listeners).find((listenerObj) => listenerObj.type === type); !!Object.values(listeners).find((listenerObj) => listenerObj.type === type);
Object.assign(contextApi[classKey], { Object.assign(contextApi[classKey], {
addListener(type: string, callback: (...args) => void) { addListener(type: string, callback: (...args: any[]) => void) {
const id = randomId(); const id = randomId();
// Deduplicate events // Deduplicate events
@@ -45,7 +45,7 @@ Object.keys(plugins).forEach((pluginKey) => {
ipcRenderer.send(`event-add-${classKey}`, type); ipcRenderer.send(`event-add-${classKey}`, type);
} }
const eventHandler = (_, ...args) => callback(...args); const eventHandler = (_: any, ...args: any[]) => callback(...args);
ipcRenderer.addListener(`event-${classKey}-${type}`, eventHandler); ipcRenderer.addListener(`event-${classKey}-${type}`, eventHandler);
listeners[id] = { type, listener: eventHandler }; listeners[id] = { type, listener: eventHandler };
+19 -15
View File
@@ -6,7 +6,7 @@ import {
} from '@capacitor-community/electron'; } from '@capacitor-community/electron';
import { USER_AGENT } from 'bgutils-js'; import { USER_AGENT } from 'bgutils-js';
import path from 'node:path'; import path from 'node:path';
import chokidar from 'chokidar'; import chokidar, { FSWatcher } from 'chokidar';
import type { MenuItemConstructorOptions } from 'electron'; import type { MenuItemConstructorOptions } from 'electron';
import { import {
app, app,
@@ -25,8 +25,12 @@ import windowStateKeeper from 'electron-window-state';
import { join } from 'path'; import { join } from 'path';
// Define components for a watcher to detect when the webapp is changed so we can reload in Dev mode. // Define components for a watcher to detect when the webapp is changed so we can reload in Dev mode.
const reloadWatcher = { const reloadWatcher: {
debouncer: null, debouncer: undefined | ReturnType<typeof setTimeout>,
ready: boolean,
watcher: FSWatcher | null
} = {
debouncer: undefined,
ready: false, ready: false,
watcher: null watcher: null
}; };
@@ -46,7 +50,7 @@ export function setupReloadWatcher(electronCapacitorApp: ElectronCapacitorApp):
electronCapacitorApp.getMainWindow().webContents.reload(); electronCapacitorApp.getMainWindow().webContents.reload();
reloadWatcher.ready = false; reloadWatcher.ready = false;
clearTimeout(reloadWatcher.debouncer); clearTimeout(reloadWatcher.debouncer);
reloadWatcher.debouncer = null; reloadWatcher.debouncer = undefined;
reloadWatcher.watcher = null; reloadWatcher.watcher = null;
setupReloadWatcher(electronCapacitorApp); setupReloadWatcher(electronCapacitorApp);
}, 1500); }, 1500);
@@ -56,14 +60,14 @@ export function setupReloadWatcher(electronCapacitorApp: ElectronCapacitorApp):
// Define our class to manage our app. // Define our class to manage our app.
export class ElectronCapacitorApp { export class ElectronCapacitorApp {
private MainWindow: BrowserWindow | null = null; private MainWindow: BrowserWindow | undefined = undefined;
private SplashScreen: CapacitorSplashScreen | null = null; private SplashScreen: CapacitorSplashScreen | undefined = undefined;
private TrayIcon: Tray | null = null; private TrayIcon: Tray | undefined = undefined;
private CapacitorFileConfig: CapacitorElectronConfig; private CapacitorFileConfig: CapacitorElectronConfig;
private TrayMenuTemplate: (MenuItem | MenuItemConstructorOptions)[] = [ private TrayMenuTemplate: (MenuItem | MenuItemConstructorOptions)[] = [
new MenuItem({ label: 'Quit App', role: 'quit' }) new MenuItem({ label: 'Quit App', role: 'quit' })
]; ];
private mainWindowState; private mainWindowState: windowStateKeeper.State | undefined = undefined;
private loadWebApp; private loadWebApp;
private customScheme: string; private customScheme: string;
@@ -93,7 +97,7 @@ export class ElectronCapacitorApp {
// Expose the mainWindow ref for use outside of the class. // Expose the mainWindow ref for use outside of the class.
getMainWindow(): BrowserWindow { getMainWindow(): BrowserWindow {
return this.MainWindow; return this.MainWindow as BrowserWindow;
} }
getCustomURLScheme(): string { getCustomURLScheme(): string {
@@ -128,7 +132,7 @@ export class ElectronCapacitorApp {
}); });
this.mainWindowState.manage(this.MainWindow); this.mainWindowState.manage(this.MainWindow);
if (this.CapacitorFileConfig.backgroundColor) { if (this.CapacitorFileConfig?.electron?.backgroundColor) {
this.MainWindow.setBackgroundColor(this.CapacitorFileConfig.electron.backgroundColor); this.MainWindow.setBackgroundColor(this.CapacitorFileConfig.electron.backgroundColor);
} }
@@ -188,7 +192,7 @@ export class ElectronCapacitorApp {
this.loadMainWindow(this); this.loadMainWindow(this);
} }
globalShortcut.register('Control+Shift+I', () => { globalShortcut.register('Control+Shift+I', () => {
this.MainWindow.webContents.toggleDevTools(); this.MainWindow?.webContents.toggleDevTools();
}); });
// Security // Security
this.MainWindow.webContents.setWindowOpenHandler((details) => { this.MainWindow.webContents.setWindowOpenHandler((details) => {
@@ -196,7 +200,7 @@ export class ElectronCapacitorApp {
return { action: 'deny' }; return { action: 'deny' };
}); });
this.MainWindow.webContents.on('will-navigate', (event, _newURL) => { this.MainWindow.webContents.on('will-navigate', (event, _newURL) => {
if (!this.MainWindow.webContents.getURL().includes(this.customScheme)) { if (!this.MainWindow?.webContents?.getURL().includes(this.customScheme)) {
event.preventDefault(); event.preventDefault();
} }
}); });
@@ -205,15 +209,15 @@ export class ElectronCapacitorApp {
// When the web app is loaded we hide the splashscreen if needed and show the mainwindow. // When the web app is loaded we hide the splashscreen if needed and show the mainwindow.
this.MainWindow.webContents.on('dom-ready', () => { this.MainWindow.webContents.on('dom-ready', () => {
if (this.CapacitorFileConfig.electron?.splashScreenEnabled) { if (this.CapacitorFileConfig.electron?.splashScreenEnabled && this.SplashScreen) {
this.SplashScreen.getSplashWindow().hide(); this.SplashScreen.getSplashWindow().hide();
} }
if (!this.CapacitorFileConfig.electron?.hideMainWindowOnLaunch) { if (!this.CapacitorFileConfig.electron?.hideMainWindowOnLaunch) {
this.MainWindow.show(); this.MainWindow?.show();
} }
setTimeout(() => { setTimeout(() => {
if (electronIsDev) { if (electronIsDev) {
this.MainWindow.webContents.openDevTools(); this.MainWindow?.webContents?.openDevTools();
} }
CapElectronEventEmitter.emit('CAPELECTRON_DeeplinkListenerInitialized', ''); CapElectronEventEmitter.emit('CAPELECTRON_DeeplinkListenerInitialized', '');
}, 400); }, 400);
+3 -3
View File
@@ -9,8 +9,8 @@
"outDir": "./build", "outDir": "./build",
"importHelpers": true, "importHelpers": true,
"target": "ES2017", "target": "ES2017",
"module": "CommonJS", "module": "nodenext",
"moduleResolution": "node", "moduleResolution": "nodenext",
"esModuleInterop": true, "esModuleInterop": true,
"typeRoots": [ "typeRoots": [
"./node_modules/@types" "./node_modules/@types"
@@ -19,4 +19,4 @@
"rootDir": ".", "rootDir": ".",
"skipLibCheck": true "skipLibCheck": true
} }
} }