background.js 2.6 KB

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