background.js 2.8 KB

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