From fb873ec2c3cb71cd478e6d48b424ddbba67cab0a Mon Sep 17 00:00:00 2001 From: Priyansh Date: Tue, 22 Dec 2020 18:39:20 +0530 Subject: App rewrite using typescript --- src/app.ts | 4 ++++ src/preload.ts | 14 ++++++++++++++ src/renderer.ts | 6 ++++++ src/windowCreator.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 src/app.ts create mode 100644 src/preload.ts create mode 100644 src/renderer.ts create mode 100644 src/windowCreator.ts (limited to 'src') diff --git a/src/app.ts b/src/app.ts new file mode 100644 index 0000000..b85e318 --- /dev/null +++ b/src/app.ts @@ -0,0 +1,4 @@ +import { app, BrowserWindow } from 'electron'; +import windowCreator from './windowCreator'; + +windowCreator.main(app, BrowserWindow); diff --git a/src/preload.ts b/src/preload.ts new file mode 100644 index 0000000..30290eb --- /dev/null +++ b/src/preload.ts @@ -0,0 +1,14 @@ +// All of the Node.js APIs are available in the preload process. +// It has the same sandbox as a Chrome extension. +window.addEventListener("DOMContentLoaded", () => { + const replaceText = (selector: string, text: string) => { + const element = document.getElementById(selector); + if (element) { + element.innerText = text; + } + }; + + for (const type of ["chrome", "node", "electron"]) { + replaceText(`${type}-version`, process.versions[type as keyof NodeJS.ProcessVersions]); + } +}); diff --git a/src/renderer.ts b/src/renderer.ts new file mode 100644 index 0000000..66042ac --- /dev/null +++ b/src/renderer.ts @@ -0,0 +1,6 @@ +// This file is required by the app.html file and will +// be executed in the renderer process for that window. +// No Node.js APIs are available in this process unless +// nodeIntegration is set to true in webPreferences. +// Use preload.js to selectively enable features +// needed in the renderer process. diff --git a/src/windowCreator.ts b/src/windowCreator.ts new file mode 100644 index 0000000..859ee03 --- /dev/null +++ b/src/windowCreator.ts @@ -0,0 +1,43 @@ +import { BrowserWindow } from 'electron'; +import * as path from 'path'; + +export default class windowCreator { + static mainWindow: Electron.BrowserWindow; + static application: Electron.App; + static BrowserWindow; + private static onWindowAllClosed() { + if (process.platform !== 'darwin') { + windowCreator.application.quit(); + } + } + + private static onClose() { + // Dereference the window object. + windowCreator.mainWindow = null; + } + + private static onReady() { + windowCreator.mainWindow = new windowCreator.BrowserWindow({ + width: 800, + height: 600, + webPreferences: { + preload: path.join(__dirname, 'preload.js'), + nodeIntegration: true + }, + }); + windowCreator.mainWindow + .loadURL('file://' + __dirname + '/../app.html'); + windowCreator.mainWindow.on('closed', windowCreator.onClose); + } + + static main(app: Electron.App, browserWindow: typeof BrowserWindow) { + // we pass the Electron.App object and the + // Electron.BrowserWindow into this function + // so this class has no dependencies. This + // makes the code easier to write tests for + windowCreator.BrowserWindow = browserWindow; + windowCreator.application = app; + windowCreator.application.on('window-all-closed', windowCreator.onWindowAllClosed); + windowCreator.application.on('ready', windowCreator.onReady); + } +} -- cgit v1.2.3