|
| 1 | +const electron = require('electron'); |
| 2 | +// Module to control application life. |
| 3 | +const app = electron.app; |
| 4 | +// Module to create native browser window. |
| 5 | +const BrowserWindow = electron.BrowserWindow; |
| 6 | + |
| 7 | +const path = require('path'); |
| 8 | +const url = require('url'); |
| 9 | + |
| 10 | +// Keep a global reference of the window object, if you don't, the window will |
| 11 | +// be closed automatically when the JavaScript object is garbage collected. |
| 12 | +let mainWindow; |
| 13 | +const createWindow = () => { |
| 14 | + const { displayWidth, displayHeight } = electron.screen.getPrimaryDisplay().size; |
| 15 | + |
| 16 | + const mainWindowParams = { |
| 17 | + width: +process.env.ELECTRON_WIDTH || 800, |
| 18 | + height: +process.env.ELECTRON_HEIGHT || 600, |
| 19 | + webPreferences: { |
| 20 | + webSecurity: process.env.NODE_ENV === 'development' ? false : true, |
| 21 | + }, |
| 22 | + }; |
| 23 | + const kioskParams = { |
| 24 | + displayWidth, |
| 25 | + displayHeight, |
| 26 | + frame: false, |
| 27 | + fullscreen: true, |
| 28 | + webPreferences: { |
| 29 | + webSecurity: process.env.NODE_ENV === 'development' ? false : true, |
| 30 | + }, |
| 31 | + }; |
| 32 | + |
| 33 | + // Create the browser window. |
| 34 | + mainWindow = new BrowserWindow(process.env.ELECTRON_KIOSK ? kioskParams : mainWindowParams); |
| 35 | + |
| 36 | + // and load the index.html of the app. |
| 37 | + const startUrl = process.env.ELECTRON_START_URL || url.format({ |
| 38 | + pathname: path.join(__dirname, 'index.html'), |
| 39 | + protocol: 'file:', |
| 40 | + slashes: true, |
| 41 | + }); |
| 42 | + console.log(`Using start url = ${startUrl}`); |
| 43 | + mainWindow.loadURL(startUrl); |
| 44 | + |
| 45 | + // Open the DevTools. |
| 46 | + process.env.ELECTRON_DEV_TOOLS && mainWindow.webContents.openDevTools(); |
| 47 | + |
| 48 | + // Emitted when the window is closed. |
| 49 | + mainWindow.on('closed', () => { |
| 50 | + // Dereference the window object, usually you would store windows |
| 51 | + // in an array if your app supports multi windows, this is the time |
| 52 | + // when you should delete the corresponding element. |
| 53 | + mainWindow = null; |
| 54 | + }); |
| 55 | +} |
| 56 | + |
| 57 | +// This method will be called when Electron has finished |
| 58 | +// initialization and is ready to create browser windows. |
| 59 | +// Some APIs can only be used after this event occurs. |
| 60 | +app.on('ready', createWindow); |
| 61 | + |
| 62 | +// Quit when all windows are closed. |
| 63 | +app.on('window-all-closed', () => { |
| 64 | + // On OS X it is common for applications and their menu bar |
| 65 | + // to stay active until the user quits explicitly with Cmd + Q |
| 66 | + if (process.platform !== 'darwin') { |
| 67 | + app.quit() |
| 68 | + } |
| 69 | +}); |
| 70 | + |
| 71 | +app.on('activate', () => { |
| 72 | + // On OS X it's common to re-create a window in the app when the |
| 73 | + // dock icon is clicked and there are no other windows open. |
| 74 | + if (mainWindow === null) { |
| 75 | + createWindow() |
| 76 | + } |
| 77 | +}); |
| 78 | + |
| 79 | +// In this file you can include the rest of your app's specific main process |
| 80 | +// code. You can also put them in separate files and require them here. |
| 81 | + |
0 commit comments