diff --git a/materialious/electron/src/rt/electron-plugins.js b/materialious/electron/src/rt/electron-plugins.js index 5f312366..b33b2826 100644 --- a/materialious/electron/src/rt/electron-plugins.js +++ b/materialious/electron/src/rt/electron-plugins.js @@ -1,6 +1,4 @@ /* eslint-disable @typescript-eslint/no-var-requires */ -const CapacitorNodejs = require('../../../node_modules/capacitor-nodejs/electron/dist/plugin.js'); module.exports = { - CapacitorNodejs, } \ No newline at end of file diff --git a/materialious/electron/src/rt/electron-rt.ts b/materialious/electron/src/rt/electron-rt.ts index 55d67c30..f8ac1fe6 100644 --- a/materialious/electron/src/rt/electron-rt.ts +++ b/materialious/electron/src/rt/electron-rt.ts @@ -33,11 +33,11 @@ Object.keys(plugins).forEach((pluginKey) => { // Events if (plugins[pluginKey][classKey].prototype instanceof EventEmitter) { 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.assign(contextApi[classKey], { - addListener(type: string, callback: (...args) => void) { + addListener(type: string, callback: (...args: any[]) => void) { const id = randomId(); // Deduplicate events @@ -45,7 +45,7 @@ Object.keys(plugins).forEach((pluginKey) => { ipcRenderer.send(`event-add-${classKey}`, type); } - const eventHandler = (_, ...args) => callback(...args); + const eventHandler = (_: any, ...args: any[]) => callback(...args); ipcRenderer.addListener(`event-${classKey}-${type}`, eventHandler); listeners[id] = { type, listener: eventHandler }; diff --git a/materialious/electron/src/setup.ts b/materialious/electron/src/setup.ts index 9b3768bc..6a82a52e 100644 --- a/materialious/electron/src/setup.ts +++ b/materialious/electron/src/setup.ts @@ -6,7 +6,7 @@ import { } from '@capacitor-community/electron'; import { USER_AGENT } from 'bgutils-js'; import path from 'node:path'; -import chokidar from 'chokidar'; +import chokidar, { FSWatcher } from 'chokidar'; import type { MenuItemConstructorOptions } from 'electron'; import { app, @@ -25,8 +25,12 @@ import windowStateKeeper from 'electron-window-state'; import { join } from 'path'; // Define components for a watcher to detect when the webapp is changed so we can reload in Dev mode. -const reloadWatcher = { - debouncer: null, +const reloadWatcher: { + debouncer: undefined | ReturnType, + ready: boolean, + watcher: FSWatcher | null +} = { + debouncer: undefined, ready: false, watcher: null }; @@ -46,7 +50,7 @@ export function setupReloadWatcher(electronCapacitorApp: ElectronCapacitorApp): electronCapacitorApp.getMainWindow().webContents.reload(); reloadWatcher.ready = false; clearTimeout(reloadWatcher.debouncer); - reloadWatcher.debouncer = null; + reloadWatcher.debouncer = undefined; reloadWatcher.watcher = null; setupReloadWatcher(electronCapacitorApp); }, 1500); @@ -56,14 +60,14 @@ export function setupReloadWatcher(electronCapacitorApp: ElectronCapacitorApp): // Define our class to manage our app. export class ElectronCapacitorApp { - private MainWindow: BrowserWindow | null = null; - private SplashScreen: CapacitorSplashScreen | null = null; - private TrayIcon: Tray | null = null; + private MainWindow: BrowserWindow | undefined = undefined; + private SplashScreen: CapacitorSplashScreen | undefined = undefined; + private TrayIcon: Tray | undefined = undefined; private CapacitorFileConfig: CapacitorElectronConfig; private TrayMenuTemplate: (MenuItem | MenuItemConstructorOptions)[] = [ new MenuItem({ label: 'Quit App', role: 'quit' }) ]; - private mainWindowState; + private mainWindowState: windowStateKeeper.State | undefined = undefined; private loadWebApp; private customScheme: string; @@ -93,7 +97,7 @@ export class ElectronCapacitorApp { // Expose the mainWindow ref for use outside of the class. getMainWindow(): BrowserWindow { - return this.MainWindow; + return this.MainWindow as BrowserWindow; } getCustomURLScheme(): string { @@ -128,7 +132,7 @@ export class ElectronCapacitorApp { }); this.mainWindowState.manage(this.MainWindow); - if (this.CapacitorFileConfig.backgroundColor) { + if (this.CapacitorFileConfig?.electron?.backgroundColor) { this.MainWindow.setBackgroundColor(this.CapacitorFileConfig.electron.backgroundColor); } @@ -188,7 +192,7 @@ export class ElectronCapacitorApp { this.loadMainWindow(this); } globalShortcut.register('Control+Shift+I', () => { - this.MainWindow.webContents.toggleDevTools(); + this.MainWindow?.webContents.toggleDevTools(); }); // Security this.MainWindow.webContents.setWindowOpenHandler((details) => { @@ -196,7 +200,7 @@ export class ElectronCapacitorApp { return { action: 'deny' }; }); 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(); } }); @@ -205,15 +209,15 @@ export class ElectronCapacitorApp { // When the web app is loaded we hide the splashscreen if needed and show the mainwindow. this.MainWindow.webContents.on('dom-ready', () => { - if (this.CapacitorFileConfig.electron?.splashScreenEnabled) { + if (this.CapacitorFileConfig.electron?.splashScreenEnabled && this.SplashScreen) { this.SplashScreen.getSplashWindow().hide(); } if (!this.CapacitorFileConfig.electron?.hideMainWindowOnLaunch) { - this.MainWindow.show(); + this.MainWindow?.show(); } setTimeout(() => { if (electronIsDev) { - this.MainWindow.webContents.openDevTools(); + this.MainWindow?.webContents?.openDevTools(); } CapElectronEventEmitter.emit('CAPELECTRON_DeeplinkListenerInitialized', ''); }, 400); diff --git a/materialious/electron/tsconfig.json b/materialious/electron/tsconfig.json index 43075746..1123cf76 100644 --- a/materialious/electron/tsconfig.json +++ b/materialious/electron/tsconfig.json @@ -9,8 +9,8 @@ "outDir": "./build", "importHelpers": true, "target": "ES2017", - "module": "CommonJS", - "moduleResolution": "node", + "module": "nodenext", + "moduleResolution": "nodenext", "esModuleInterop": true, "typeRoots": [ "./node_modules/@types" @@ -19,4 +19,4 @@ "rootDir": ".", "skipLibCheck": true } -} \ No newline at end of file +}