index.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import path from "path";
  2. import fs from "fs";
  3. import { app, BrowserWindow, Menu, screen, dialog, ipcMain } from "electron";
  4. import installExtension, { VUEJS3_DEVTOOLS } from "electron-devtools-installer";
  5. import { exec } from "child_process";
  6. import process from "process";
  7. const isDev = process.env.NODE_ENV === "development";
  8. let win: any = null;
  9. let loadWin: any = null;
  10. function createWin() {
  11. const { width, height } = screen.getPrimaryDisplay().bounds;
  12. // Menu.setApplicationMenu(null);
  13. // 创建浏览器窗口
  14. win = new BrowserWindow({
  15. width: 840,
  16. // width: width,
  17. height: 500,
  18. // height: height,
  19. frame: false,
  20. resizable: false,
  21. transparent: true,
  22. webPreferences: {
  23. preload: path.resolve(__dirname, "preload.js"),
  24. nodeIntegration: true,
  25. },
  26. });
  27. if (isDev) {
  28. win.loadURL(`http://localhost:8090`);
  29. } else {
  30. win.loadFile(path.resolve(__dirname, "index.html"));
  31. }
  32. win.webContents.once("dom-ready", () => {
  33. win?.show();
  34. });
  35. win.webContents.on("before-input-event", (event: any, input: any) => {
  36. if (input.key === "F12") {
  37. win.webContents.openDevTools();
  38. }
  39. });
  40. // loadWin.destroy();
  41. win.on("closed", () => {
  42. win = null;
  43. });
  44. }
  45. function createLoadWin() {
  46. Menu.setApplicationMenu(null);
  47. loadWin = new BrowserWindow({
  48. width: 840,
  49. height: 500,
  50. backgroundColor: "#222",
  51. frame: false,
  52. transparent: true,
  53. skipTaskbar: true,
  54. resizable: false,
  55. webPreferences: { experimentalFeatures: true },
  56. });
  57. loadWin.loadFile(path.resolve(__dirname, "static/load/index.html"));
  58. loadWin.show();
  59. setTimeout(async () => {
  60. if (isDev) {
  61. try {
  62. await installExtension(VUEJS3_DEVTOOLS);
  63. } catch (e: any) {
  64. console.error("Vue Devtools failed to install:", e.toString());
  65. }
  66. }
  67. createWin();
  68. }, 2200);
  69. loadWin.on("closed", () => {
  70. loadWin = null;
  71. });
  72. }
  73. // app.isReady()
  74. // ? createLoadWin()
  75. // : app.on("ready", () => {
  76. // createLoadWin();
  77. // });
  78. ipcMain.on("change-win-size", (event, args: string) => {
  79. const { width, height } = screen.getPrimaryDisplay().workAreaSize;
  80. let w = args === "big" ? width : 840;
  81. let h = args === "big" ? height : 500;
  82. if (args === "small") {
  83. win.setMinimumSize(w, h);
  84. }
  85. win.setSize(w, h);
  86. win.center();
  87. });
  88. ipcMain.on("window-min", () => {
  89. win.minimize();
  90. });
  91. function startExe(exePath: string) {
  92. console.log("主进程接收到的exe路径:", exePath);
  93. let checkPath = exePath.includes(".exe ")
  94. ? exePath.split(".exe ")[0] + ".exe"
  95. : exePath;
  96. const fileExists = fs.existsSync(checkPath);
  97. if (fileExists) {
  98. exec(exePath);
  99. } else {
  100. dialog.showErrorBox("tip", `${checkPath}不存在!`);
  101. }
  102. }
  103. ipcMain.on("startExe", (event, args: string) => {
  104. startExe(args);
  105. });
  106. app.on("ready", async () => {
  107. if (isDev) {
  108. try {
  109. await installExtension(VUEJS3_DEVTOOLS);
  110. } catch (e: any) {
  111. console.error("Vue Devtools failed to install:", e.toString());
  112. }
  113. }
  114. createWin();
  115. });