background.ts 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, { 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. fullscreen: true,
  16. webPreferences: {
  17. // Use pluginOptions.nodeIntegration, leave this alone
  18. // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
  19. nodeIntegration: process.env
  20. .ELECTRON_NODE_INTEGRATION as unknown as boolean,
  21. contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
  22. },
  23. });
  24. if (process.env.WEBPACK_DEV_SERVER_URL) {
  25. // Load the url of the dev server if in development mode
  26. await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL as string);
  27. if (!process.env.IS_TEST) win.webContents.openDevTools();
  28. } else {
  29. createProtocol("app");
  30. // Load the index.html when not in development
  31. win.loadURL("app://./index.html");
  32. }
  33. win.setMenu(null);
  34. win.webContents.openDevTools();
  35. }
  36. // Quit when all windows are closed.
  37. app.on("window-all-closed", () => {
  38. // On macOS it is common for applications and their menu bar
  39. // to stay active until the user quits explicitly with Cmd + Q
  40. if (process.platform !== "darwin") {
  41. app.quit();
  42. }
  43. });
  44. app.on("activate", () => {
  45. // On macOS it's common to re-create a window in the app when the
  46. // dock icon is clicked and there are no other windows open.
  47. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  48. });
  49. // This method will be called when Electron has finished
  50. // initialization and is ready to create browser windows.
  51. // Some APIs can only be used after this event occurs.
  52. app.on("ready", async () => {
  53. if (isDevelopment && !process.env.IS_TEST) {
  54. // Install Vue Devtools
  55. try {
  56. await installExtension(VUEJS_DEVTOOLS);
  57. } catch (e) {
  58. // @ts-ignore
  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. }