From 9c57d101f0181aa134190edb0b2e7aabdba1151f Mon Sep 17 00:00:00 2001 From: Valentin Popov Date: Tue, 20 Aug 2019 14:51:06 +0400 Subject: Added example application Signed-off-by: Valentin Popov --- example/application/src/index.html | 45 ++++++++++++++++++++++ example/application/src/main.js | 63 +++++++++++++++++++++++++++++++ example/application/src/renderer.js | 28 ++++++++++++++ example/application/src/utils/get-json.js | 12 ++++++ example/application/src/utils/reloader.js | 20 ++++++++++ 5 files changed, 168 insertions(+) create mode 100644 example/application/src/index.html create mode 100644 example/application/src/main.js create mode 100644 example/application/src/renderer.js create mode 100644 example/application/src/utils/get-json.js create mode 100644 example/application/src/utils/reloader.js (limited to 'example/application/src') diff --git a/example/application/src/index.html b/example/application/src/index.html new file mode 100644 index 0000000..9b8aba9 --- /dev/null +++ b/example/application/src/index.html @@ -0,0 +1,45 @@ + + + + + + + + + <%= htmlWebpackPlugin.options.title %> + + + + + +
+
+

Last reload

+
+ +
+
+ Main Process: + +
+ +
+ Renderer Process: + +
+
+ +
+
+ + +
+
+
+ + + + + + + diff --git a/example/application/src/main.js b/example/application/src/main.js new file mode 100644 index 0000000..162768b --- /dev/null +++ b/example/application/src/main.js @@ -0,0 +1,63 @@ +// Modules to control application life and create native browser window +import { app, BrowserWindow } from 'electron' + +import './utils/reloader' +import './utils/get-json' + +// Keep a global reference of the window object, if you don't, the window will +// be closed automatically when the JavaScript object is garbage collected. +let mainWindow + +function createWindow() { + // Create the browser window. + mainWindow = new BrowserWindow({ + width: 800, + height: 600, + show: false, + webPreferences: { + nodeIntegration: true + } + }) + + // and load the index.html of the app. + mainWindow.loadFile('dist/index.html') + + // Open the DevTools. + // mainWindow.webContents.openDevTools() + + // Emitted when the window is closed. + mainWindow.on('closed', function () { + // Dereference the window object, usually you would store windows + // in an array if your app supports multi windows, this is the time + // when you should delete the corresponding element. + mainWindow = null + }) + + // While loading the page, the ready - to - show event will be emitted + // when the renderer process has rendered the page for the first time + // if the window has not been shown yet. + mainWindow.on('ready-to-show', () => { + mainWindow.show(); + }); +} + +// This method will be called when Electron has finished +// initialization and is ready to create browser windows. +// Some APIs can only be used after this event occurs. +app.on('ready', createWindow) + +// Quit when all windows are closed. +app.on('window-all-closed', function () { + // On macOS it is common for applications and their menu bar + // to stay active until the user quits explicitly with Cmd + Q + if (process.platform !== 'darwin') app.quit() +}) + +app.on('activate', function () { + // On macOS it's common to re-create a window in the app when the + // dock icon is clicked and there are no other windows open. + if (mainWindow === null) createWindow() +}) + +// In this file you can include the rest of your app's specific main process +// code. You can also put them in separate files and require them here. diff --git a/example/application/src/renderer.js b/example/application/src/renderer.js new file mode 100644 index 0000000..f4376e9 --- /dev/null +++ b/example/application/src/renderer.js @@ -0,0 +1,28 @@ +// This file is required by the index.html file and will +// be executed in the renderer process for that window. +// All of the Node.js APIs are available in this process. + +import { ipcRenderer } from 'electron' +import moment from 'moment' + +ipcRenderer.on('get:json:result', (event, { main, renderer }) => { + const mainTime = moment(main.time || 0).startOf('minute').fromNow() + const rendererTime = moment(renderer.time || 0).startOf('minute').fromNow() + + document.getElementById("main").innerHTML = mainTime; + document.getElementById("renderer").innerHTML = rendererTime; +}) + +ipcRenderer.send('get:json') + +setTimeout(() => { + ipcRenderer.send('get:json') +}, 3000); + +document.querySelector('#btnMain').addEventListener('click', () => { + ipcRenderer.send('reload:main') +}) + +document.querySelector('#btnRend').addEventListener('click', () => { + ipcRenderer.send('reload:renderer') +}) diff --git a/example/application/src/utils/get-json.js b/example/application/src/utils/get-json.js new file mode 100644 index 0000000..18f4403 --- /dev/null +++ b/example/application/src/utils/get-json.js @@ -0,0 +1,12 @@ +import { app, ipcMain } from 'electron' +import jsonfile from 'jsonfile' +import path from 'path' + +ipcMain.on('get:json', async (event) => { + const tempDir = path.join(app.getAppPath(), 'temp') + + const main = await jsonfile.readFile(path.join(tempDir, 'main.json')) + const renderer = await jsonfile.readFile(path.join(tempDir, 'renderer.json')) + + event.sender.send('get:json:result', { main, renderer }) +}) diff --git a/example/application/src/utils/reloader.js b/example/application/src/utils/reloader.js new file mode 100644 index 0000000..ad78496 --- /dev/null +++ b/example/application/src/utils/reloader.js @@ -0,0 +1,20 @@ +import { app, ipcMain } from 'electron' +import jsonfile from 'jsonfile' +import path from 'path' + +import { mainReloader, rendererReloader } from '../../../../dist' + +const tempDir = path.join(app.getAppPath(), 'temp') +const tempMain = path.join(tempDir, 'main.json') +const tempRend = path.join(tempDir, 'renderer.json') + +mainReloader(tempMain) +rendererReloader(tempRend) + +ipcMain.on('reload:main', async () => { + await jsonfile.writeFile(tempMain, { time: Date.now() }) +}) + +ipcMain.on('reload:renderer', async () => { + await jsonfile.writeFile(tempRend, { time: Date.now() }) +}) -- cgit v1.2.3