main.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const { app, BrowserWindow } = require("electron");
  2. const path = require("path");
  3. const port = 3000;
  4. let mainWindow = null;
  5. const debug = /--debug/.test(process.argv[2]);
  6. function makeSingleInstance() {
  7. if (process.mas) return;
  8. app.requestSingleInstanceLock();
  9. app.on("second-instance", () => {
  10. if (mainWindow) {
  11. if (mainWindow.isMinimized()) mainWindow.restore();
  12. mainWindow.focus();
  13. }
  14. });
  15. }
  16. function createWindow() {
  17. const windowOptions = {
  18. width: 1024,
  19. height: 768,
  20. };
  21. mainWindow = new BrowserWindow(windowOptions);
  22. mainWindow.loadURL(`http://localhost:${port}`);
  23. // mainWindow.loadURL(path.join("file://", __dirname, "/build/index.html"));
  24. const ipc = require("electron").ipcMain;
  25. ipc.on("min", function () {
  26. mainWindow.minimize();
  27. });
  28. ipc.on("max", function () {
  29. mainWindow.maximize();
  30. });
  31. ipc.on("login", function () {
  32. mainWindow.maximize();
  33. });
  34. //如果是--debug 打开开发者工具,窗口最大化,
  35. if (debug) {
  36. mainWindow.webContents.openDevTools();
  37. require("devtron").install();
  38. }
  39. mainWindow.on("closed", () => {
  40. mainWindow = null;
  41. });
  42. }
  43. const express = require("express");
  44. const appExpress = express();
  45. const distPath = path.join(__dirname, "./build");
  46. appExpress.use("/", express.static(distPath));
  47. appExpress.listen(port, () => {
  48. console.log(`Example app listening at http://localhost:${port}`);
  49. makeSingleInstance();
  50. //app主进程的事件和方法
  51. app.on("ready", () => {
  52. createWindow();
  53. });
  54. app.on("window-all-closed", () => {
  55. if (process.platform !== "darwin") {
  56. app.quit();
  57. }
  58. });
  59. app.on("activate", () => {
  60. if (mainWindow === null) {
  61. createWindow();
  62. }
  63. });
  64. });
  65. module.exports = mainWindow;