Electron Layer
A Javascript overlay for clearer process
management of the electron
package.
Example
const { app } = require('electron');
const AppCore = require('@@focussly/electron-layer/app');
const MainRenderer = require('./main-renderer.js');
class App extends AppCore {
renderers = {
main: new MainRenderer,
}
onAppReady() {
this.renderers.main.mount();
}
onAppWindowAllClosed() {
app.quit();
}
}
new App();
const path = require('path');
const url = require('url');
const RendererCore = require('@@focussly/electron-layer/renderer');
const { appPath, preloadPath } = require('@@focussly/electron-layer/helpers/path');
class MainRenderer extends RendererCore {
windowConfig = {
width: 1440,
height: 1024,
x: 0,
y: 0,
show: true,
backgroundColor: '#FFFFFF',
webPreferences: {
webSecurity: true,
nodeIntegration: true,
contextIsolation: true,
preload: preloadPath,
},
};
constructor(appCore) {
super(appCore);
this.windowLoadFile = url.pathToFileURL(path.resolve(appPath, '/index.html')).pathname;
}
onWindowReadyToShow() {
this.window.toggleDevTools();
}
onWindowClose() {
this.unMountWindowEvents();
this.unMountIPCEvents();
}
handleIPCSendMessage(event, arg) {
console.log(arg)
return 'pong';
}
onIPCSendSyncMessage(event, arg) {
console.log(arg)
event.returnValue = 'sync pong';
}
}
module.exports = MainRenderer;
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Electron Atom</title>
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0">
</head>
<body>
<div>
Hello world!
</div>
<script>
process.communication.invoke('send-message', 'ping').then(response => {
console.log(response);
}).catch(error => {
console.error(error);
});
try {
const syncResponse = process.communication.sendSync('send-sync-message', 'sync ping');
console.log(syncResponse);
} catch (error) {
console.error(error);
}
</script>
</body>
</html>