|
| 1 | +'use strict' |
| 2 | + |
| 3 | +import { app, protocol, BrowserWindow } from 'electron' |
| 4 | +import { createProtocol } from 'vue-cli-plugin-electron-builder/lib' |
| 5 | +import installExtension, { VUEJS3_DEVTOOLS } from 'electron-devtools-installer' |
| 6 | +const path = require('path') |
| 7 | +const isDevelopment = process.env.NODE_ENV !== 'production' |
| 8 | + |
| 9 | +// Scheme must be registered before the app is ready |
| 10 | +protocol.registerSchemesAsPrivileged([ |
| 11 | + { scheme: 'app', privileges: { secure: true, standard: true } } |
| 12 | +]) |
| 13 | + |
| 14 | +async function createWindow() { |
| 15 | + // We cannot require the screen module until the app is ready. |
| 16 | + const { screen } = require('electron') |
| 17 | + |
| 18 | + // Create a window that fills the screen's available work area. |
| 19 | + const primaryDisplay = screen.getPrimaryDisplay() |
| 20 | + const { width, height } = primaryDisplay.workAreaSize |
| 21 | + |
| 22 | + // Create the browser window. |
| 23 | + const win = new BrowserWindow({ |
| 24 | + width: width, |
| 25 | + height: height, |
| 26 | + // Don't show the window until it's ready, this prevents any white flickering |
| 27 | + show: false, |
| 28 | + icon: path.join(__dirname, '/jsmla_logo.png'), |
| 29 | + webPreferences: { |
| 30 | + |
| 31 | + // Use pluginOptions.nodeIntegration, leave this alone |
| 32 | + // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info |
| 33 | + nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION, |
| 34 | + contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION |
| 35 | + } |
| 36 | + }) |
| 37 | + |
| 38 | + // Hide App Menu & maximize the window |
| 39 | + win.setMenu(null); |
| 40 | + win.maximize() |
| 41 | + |
| 42 | + if (process.env.WEBPACK_DEV_SERVER_URL) { |
| 43 | + // Load the url of the dev server if in development mode |
| 44 | + await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL) |
| 45 | + if (!process.env.IS_TEST) win.webContents.openDevTools({ |
| 46 | + mode: "detach" |
| 47 | + }) |
| 48 | + win.show(); |
| 49 | + } else { |
| 50 | + createProtocol('app') |
| 51 | + // Load the index.html when not in development |
| 52 | + win.loadURL('app://./index.html') |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// Quit when all windows are closed. |
| 57 | +app.on('window-all-closed', () => { |
| 58 | + // On macOS it is common for applications and their menu bar |
| 59 | + // to stay active until the user quits explicitly with Cmd + Q |
| 60 | + if (process.platform !== 'darwin') { |
| 61 | + app.quit() |
| 62 | + } |
| 63 | +}) |
| 64 | + |
| 65 | +app.on('activate', () => { |
| 66 | + // On macOS it's common to re-create a window in the app when the |
| 67 | + // dock icon is clicked and there are no other windows open. |
| 68 | + if (BrowserWindow.getAllWindows().length === 0) createWindow() |
| 69 | +}) |
| 70 | + |
| 71 | +// This method will be called when Electron has finished |
| 72 | +// initialization and is ready to create browser windows. |
| 73 | +// Some APIs can only be used after this event occurs. |
| 74 | +app.on('ready', async () => { |
| 75 | + if (isDevelopment && !process.env.IS_TEST) { |
| 76 | + // Install Vue Devtools |
| 77 | + try { |
| 78 | + await installExtension(VUEJS3_DEVTOOLS) |
| 79 | + } catch (e) { |
| 80 | + console.error('Vue Devtools failed to install:', e.toString()) |
| 81 | + } |
| 82 | + } |
| 83 | + createWindow() |
| 84 | +}) |
| 85 | + |
| 86 | +// Exit cleanly on request from parent process in development mode. |
| 87 | +if (isDevelopment) { |
| 88 | + if (process.platform === 'win32') { |
| 89 | + process.on('message', (data) => { |
| 90 | + if (data === 'graceful-exit') { |
| 91 | + app.quit() |
| 92 | + } |
| 93 | + }) |
| 94 | + } else { |
| 95 | + process.on('SIGTERM', () => { |
| 96 | + app.quit() |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments