background.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict'
  2. import { app, protocol, BrowserWindow } from 'electron'
  3. import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
  4. import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
  5. const isDevelopment = process.env.NODE_ENV !== 'production'
  6. // Scheme must be registered before the app is ready
  7. protocol.registerSchemesAsPrivileged([
  8. { scheme: 'app', privileges: { secure: true, standard: true }}
  9. ])
  10. async function createWindow() {
  11. // Create the browser window.
  12. const win = new BrowserWindow({
  13. width: 800,
  14. height: 600,
  15. webPreferences: {
  16. // Use pluginOptions.nodeIntegration, leave this alone
  17. // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
  18. nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
  19. contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
  20. }
  21. })
  22. if (process.env.WEBPACK_DEV_SERVER_URL) {
  23. // Load the url of the dev server if in development mode
  24. await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
  25. if (!process.env.IS_TEST) win.webContents.openDevTools()
  26. } else {
  27. createProtocol('app')
  28. // Load the index.html when not in development
  29. win.loadURL('app://./index.html')
  30. }
  31. }
  32. // Quit when all windows are closed.
  33. app.on('window-all-closed', () => {
  34. // On macOS it is common for applications and their menu bar
  35. // to stay active until the user quits explicitly with Cmd + Q
  36. if (process.platform !== 'darwin') {
  37. app.quit()
  38. }
  39. })
  40. app.on('activate', () => {
  41. // On macOS it's common to re-create a window in the app when the
  42. // dock icon is clicked and there are no other windows open.
  43. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  44. })
  45. // This method will be called when Electron has finished
  46. // initialization and is ready to create browser windows.
  47. // Some APIs can only be used after this event occurs.
  48. app.on('ready', async() => {
  49. if (isDevelopment && !process.env.IS_TEST) {
  50. // Install Vue Devtools
  51. try {
  52. await installExtension(VUEJS_DEVTOOLS)
  53. } catch (e) {
  54. console.error('Vue Devtools failed to install:', e.toString())
  55. }
  56. }
  57. createWindow()
  58. })
  59. // Exit cleanly on request from parent process in development mode.
  60. if (isDevelopment) {
  61. if (process.platform === 'win32') {
  62. process.on('message', (data) => {
  63. if (data === 'graceful-exit') {
  64. app.quit()
  65. }
  66. })
  67. } else {
  68. process.on('SIGTERM', () => {
  69. app.quit()
  70. })
  71. }
  72. }