|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Import parts of electron to use |
| 4 | +const { app, BrowserWindow } = require('electron'); |
| 5 | +const path = require('path'); |
| 6 | +const url = require('url'); |
| 7 | + |
| 8 | +// Keep a global reference of the window object, if you don't, the window will |
| 9 | +// be closed automatically when the JavaScript object is garbage collected. |
| 10 | +let mainWindow; |
| 11 | + |
| 12 | +// Keep a reference for dev mode |
| 13 | +let dev = false; |
| 14 | +if ( process.defaultApp || /[\\/]electron-prebuilt[\\/]/.test(process.execPath) || /[\\/]electron[\\/]/.test(process.execPath) ) { |
| 15 | + dev = true; |
| 16 | +} |
| 17 | + |
| 18 | +function createWindow() { |
| 19 | + // Create the browser window. |
| 20 | + mainWindow = new BrowserWindow({ |
| 21 | + width: 1024, height: 768, show: false |
| 22 | + }); |
| 23 | + |
| 24 | + // and load the index.html of the app. |
| 25 | + let indexPath; |
| 26 | + if ( dev && process.argv.indexOf('--noDevServer') === -1 ) { |
| 27 | + indexPath = url.format({ |
| 28 | + protocol: 'http:', |
| 29 | + host: 'localhost:8080', |
| 30 | + pathname: 'index.html', |
| 31 | + slashes: true |
| 32 | + }); |
| 33 | + } else { |
| 34 | + indexPath = url.format({ |
| 35 | + protocol: 'file:', |
| 36 | + pathname: path.join(__dirname, 'dist', 'index.html'), |
| 37 | + slashes: true |
| 38 | + }); |
| 39 | + } |
| 40 | + mainWindow.loadURL( indexPath ); |
| 41 | + |
| 42 | + // Don't show until we are ready and loaded |
| 43 | + mainWindow.once('ready-to-show', () => { |
| 44 | + mainWindow.show(); |
| 45 | + // Open the DevTools automatically if developing |
| 46 | + if ( dev ) { |
| 47 | + mainWindow.webContents.openDevTools(); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + // Emitted when the window is closed. |
| 52 | + mainWindow.on('closed', function() { |
| 53 | + // Dereference the window object, usually you would store windows |
| 54 | + // in an array if your app supports multi windows, this is the time |
| 55 | + // when you should delete the corresponding element. |
| 56 | + mainWindow = null; |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +// This method will be called when Electron has finished |
| 61 | +// initialization and is ready to create browser windows. |
| 62 | +// Some APIs can only be used after this event occurs. |
| 63 | +app.on('ready', createWindow); |
| 64 | + |
| 65 | +// Quit when all windows are closed. |
| 66 | +app.on('window-all-closed', () => { |
| 67 | + // On macOS it is common for applications and their menu bar |
| 68 | + // to stay active until the user quits explicitly with Cmd + Q |
| 69 | + if (process.platform !== 'darwin') { |
| 70 | + app.quit(); |
| 71 | + } |
| 72 | +}); |
| 73 | + |
| 74 | +app.on('activate', () => { |
| 75 | + // On macOS it's common to re-create a window in the app when the |
| 76 | + // dock icon is clicked and there are no other windows open. |
| 77 | + if (mainWindow === null) { |
| 78 | + createWindow(); |
| 79 | + } |
| 80 | +}); |
0 commit comments