index.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { app, BrowserWindow } from "electron";
  2. import { createWindow, mainWindow } from "../win/mainWin";
  3. import { initIpcMain } from "./ipcMain";
  4. import { initTray } from "./tray";
  5. import { initExpress } from "./express";
  6. //平台
  7. // const isMac = process.platform === "darwin";
  8. //跨域警告关闭
  9. (process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] as unknown) = true;
  10. //环境
  11. // const isDev = process.env.IS_DEV == "true" ? true : false;
  12. if (!app.requestSingleInstanceLock()) {
  13. app.quit();
  14. process.exit(0);
  15. } else {
  16. app.on("second-instance", (event, commandLine, workingDirectory) => {
  17. if (mainWindow) {
  18. if (mainWindow.isMinimized()) mainWindow.restore();
  19. mainWindow.show();
  20. mainWindow.focus();
  21. }
  22. });
  23. }
  24. // 这段程序将会在 Electron 结束初始化
  25. // 和创建浏览器窗口的时候调用
  26. // 部分 API 在 ready 事件触发后才能使用。
  27. app.whenReady().then(() => {
  28. AtLogin();
  29. createWindow();
  30. initTray();
  31. initExpress();
  32. });
  33. // 开机自启动
  34. const AtLogin = async () => {
  35. if (app.isPackaged) {
  36. // 设置应用开机启动
  37. app.setLoginItemSettings({
  38. openAtLogin: true, // 设置为 true 可以隐藏启动时的窗口
  39. path: app.getPath("exe"), // 可选,指定要启动的应用路径
  40. args: ["--openAsHidden"],
  41. });
  42. }
  43. };
  44. // SqliteData.getInstance();
  45. app.on("activate", function () {
  46. // 通常在 macOS 上,当点击 dock 中的应用程序图标时,如果没有其他
  47. // 打开的窗口,那么程序会重新创建一个窗口。
  48. // if (BrowserWindow.getAllWindows().length === 0) createWindow();
  49. console.log("activate=====>");
  50. const allWindows = BrowserWindow.getAllWindows();
  51. if (!allWindows.length) {
  52. createWindow();
  53. }
  54. });
  55. // 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 因此,通常对程序和它们在
  56. // 任务栏上的图标来说,应当保持活跃状态,直到用户使用 Cmd + Q 退出。
  57. app.on("window-all-closed", function () {
  58. // SqliteData.getInstance().close();
  59. if (process.platform !== "darwin") app.quit();
  60. });
  61. // 在这个文件中,你可以包含应用程序剩余的所有部分的代码,
  62. // 也可以拆分成几个文件,然后用 require 导入。
  63. initIpcMain();