appManage.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { app, shell } from "electron";
  2. import { isExistMainWindow, showWindow } from "./windowManage";
  3. import { join } from "node:path";
  4. import { release } from "node:os";
  5. import { isMac, isProd, isWin } from "../utils";
  6. import { getStore } from "./storeManage";
  7. const store = getStore();
  8. export const setSingleInstance = () => {
  9. if (!app.requestSingleInstanceLock()) {
  10. app.quit();
  11. process.exit(0);
  12. }
  13. app.on("second-instance", () => {
  14. showWindow();
  15. });
  16. };
  17. export const setAppListener = (startApp: () => void) => {
  18. app.on("web-contents-created", (event, contents) => {
  19. contents.setWindowOpenHandler(({ url }) => {
  20. if (!/^devtools/.test(url) && /^https?:\/\//.test(url)) {
  21. shell.openExternal(url);
  22. }
  23. return { action: "deny" };
  24. });
  25. });
  26. app.on("activate", () => {
  27. if (isExistMainWindow()) {
  28. showWindow();
  29. } else {
  30. startApp();
  31. }
  32. });
  33. app.on("window-all-closed", () => {
  34. if (isMac && !getIsForceQuit()) return;
  35. app.quit();
  36. });
  37. };
  38. export const performAppStartup = () => {
  39. app.setAppUserModelId(app.getName());
  40. app.commandLine.appendSwitch("--autoplay-policy", "no-user-gesture-required");
  41. app.commandLine.appendSwitch(
  42. "disable-features",
  43. "HardwareMediaKeyHandling,MediaSessionService",
  44. );
  45. // Disable GPU Acceleration for Windows 7
  46. if (release().startsWith("6.1")) app.disableHardwareAcceleration();
  47. };
  48. export const setAppGlobalData = () => {
  49. const electronDistPath = join(__dirname, "../");
  50. const distPath = join(electronDistPath, "../dist");
  51. const publicPath = isProd ? distPath : join(electronDistPath, "../public");
  52. const asarPath = join(distPath, "/../..");
  53. global.pathConfig = {
  54. electronDistPath,
  55. distPath,
  56. publicPath,
  57. asarPath,
  58. trayIcon: join(publicPath, `/icons/${isWin ? "icon.ico" : "tray.png"}`),
  59. indexHtml: join(distPath, "index.html"),
  60. callingHtml: join(distPath, "index.html/#/calling"),
  61. splashHtml: join(distPath, "splash.html"),
  62. preload: join(__dirname, "../preload/index.js"),
  63. };
  64. };
  65. export const getIsForceQuit = () =>
  66. store.get("closeAction") === "quit" || global.forceQuit;