main.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. "use strict";
  2. const { app, BrowserWindow, ipcMain, shell, session, Menu, Tray, nativeImage } = require("electron");
  3. const { join } = require("path");
  4. require("fs");
  5. let mainWindow = null;
  6. let tray = null;
  7. let isQuitting = false;
  8. const VITE_DEV_SERVER_URL = process.env.VITE_DEV_SERVER_URL;
  9. function getIconPath() {
  10. return VITE_DEV_SERVER_URL ? join(__dirname, "../public/favicon.svg") : join(__dirname, "../dist/favicon.svg");
  11. }
  12. function createTrayIcon() {
  13. const iconSize = 16;
  14. const canvas = `
  15. <svg xmlns="http://www.w3.org/2000/svg" width="${iconSize}" height="${iconSize}" viewBox="0 0 512 512">
  16. <rect x="32" y="32" width="448" height="448" rx="96" fill="#4f8cff"/>
  17. <circle cx="256" cy="256" r="100" fill="#fff"/>
  18. <path d="M 228 190 L 228 322 L 330 256 Z" fill="#4f8cff"/>
  19. </svg>
  20. `;
  21. const dataUrl = `data:image/svg+xml;base64,${Buffer.from(canvas).toString("base64")}`;
  22. return nativeImage.createFromDataURL(dataUrl);
  23. }
  24. function createTray() {
  25. const trayIcon = createTrayIcon();
  26. tray = new Tray(trayIcon);
  27. const contextMenu = Menu.buildFromTemplate([
  28. {
  29. label: "显示主窗口",
  30. click: () => {
  31. if (mainWindow) {
  32. mainWindow.show();
  33. mainWindow.focus();
  34. }
  35. }
  36. },
  37. {
  38. label: "最小化到托盘",
  39. click: () => {
  40. mainWindow == null ? void 0 : mainWindow.hide();
  41. }
  42. },
  43. { type: "separator" },
  44. {
  45. label: "退出",
  46. click: () => {
  47. isQuitting = true;
  48. app.quit();
  49. }
  50. }
  51. ]);
  52. tray.setToolTip("多平台媒体管理系统");
  53. tray.setContextMenu(contextMenu);
  54. tray.on("click", () => {
  55. if (mainWindow) {
  56. if (mainWindow.isVisible()) {
  57. mainWindow.focus();
  58. } else {
  59. mainWindow.show();
  60. mainWindow.focus();
  61. }
  62. }
  63. });
  64. tray.on("double-click", () => {
  65. if (mainWindow) {
  66. mainWindow.show();
  67. mainWindow.focus();
  68. }
  69. });
  70. }
  71. function createWindow() {
  72. Menu.setApplicationMenu(null);
  73. const iconPath = getIconPath();
  74. mainWindow = new BrowserWindow({
  75. width: 1400,
  76. height: 900,
  77. minWidth: 1200,
  78. minHeight: 700,
  79. icon: iconPath,
  80. webPreferences: {
  81. preload: join(__dirname, "preload.js"),
  82. nodeIntegration: false,
  83. contextIsolation: true,
  84. webviewTag: true
  85. // 启用 webview 标签
  86. },
  87. frame: false,
  88. // 无边框窗口,自定义标题栏
  89. transparent: false,
  90. backgroundColor: "#f0f2f5",
  91. show: false
  92. });
  93. mainWindow.once("ready-to-show", () => {
  94. mainWindow == null ? void 0 : mainWindow.show();
  95. setupWindowEvents();
  96. });
  97. if (VITE_DEV_SERVER_URL) {
  98. mainWindow.loadURL(VITE_DEV_SERVER_URL);
  99. mainWindow.webContents.openDevTools();
  100. } else {
  101. mainWindow.loadFile(join(__dirname, "../dist/index.html"));
  102. }
  103. mainWindow.webContents.setWindowOpenHandler(({ url }) => {
  104. shell.openExternal(url);
  105. return { action: "deny" };
  106. });
  107. mainWindow.on("close", (event) => {
  108. if (!isQuitting) {
  109. event.preventDefault();
  110. mainWindow == null ? void 0 : mainWindow.hide();
  111. if (tray && !app.isPackaged) ;
  112. }
  113. });
  114. mainWindow.on("closed", () => {
  115. mainWindow = null;
  116. });
  117. }
  118. const gotTheLock = app.requestSingleInstanceLock();
  119. if (!gotTheLock) {
  120. app.quit();
  121. } else {
  122. app.on("second-instance", () => {
  123. if (mainWindow) {
  124. mainWindow.show();
  125. if (mainWindow.isMinimized()) mainWindow.restore();
  126. mainWindow.focus();
  127. }
  128. });
  129. app.whenReady().then(() => {
  130. createTray();
  131. createWindow();
  132. app.on("activate", () => {
  133. if (BrowserWindow.getAllWindows().length === 0) {
  134. createWindow();
  135. } else if (mainWindow) {
  136. mainWindow.show();
  137. }
  138. });
  139. });
  140. }
  141. app.on("window-all-closed", () => {
  142. });
  143. app.on("before-quit", () => {
  144. isQuitting = true;
  145. });
  146. app.on("quit", () => {
  147. if (tray) {
  148. tray.destroy();
  149. tray = null;
  150. }
  151. });
  152. ipcMain.handle("get-app-version", () => {
  153. return app.getVersion();
  154. });
  155. ipcMain.handle("get-platform", () => {
  156. return process.platform;
  157. });
  158. ipcMain.on("window-minimize", () => {
  159. mainWindow == null ? void 0 : mainWindow.minimize();
  160. });
  161. ipcMain.on("window-maximize", () => {
  162. if (mainWindow == null ? void 0 : mainWindow.isMaximized()) {
  163. mainWindow.unmaximize();
  164. } else {
  165. mainWindow == null ? void 0 : mainWindow.maximize();
  166. }
  167. });
  168. ipcMain.on("window-close", () => {
  169. mainWindow == null ? void 0 : mainWindow.hide();
  170. });
  171. ipcMain.on("app-quit", () => {
  172. isQuitting = true;
  173. app.quit();
  174. });
  175. ipcMain.handle("window-is-maximized", () => {
  176. return (mainWindow == null ? void 0 : mainWindow.isMaximized()) || false;
  177. });
  178. function setupWindowEvents() {
  179. mainWindow == null ? void 0 : mainWindow.on("maximize", () => {
  180. mainWindow == null ? void 0 : mainWindow.webContents.send("window-maximized", true);
  181. });
  182. mainWindow == null ? void 0 : mainWindow.on("unmaximize", () => {
  183. mainWindow == null ? void 0 : mainWindow.webContents.send("window-maximized", false);
  184. });
  185. }
  186. ipcMain.handle("get-webview-cookies", async (_event, partition, url) => {
  187. try {
  188. const ses = session.fromPartition(partition);
  189. const cookies = await ses.cookies.get({ url });
  190. return cookies;
  191. } catch (error) {
  192. console.error("获取 cookies 失败:", error);
  193. return [];
  194. }
  195. });
  196. ipcMain.handle("clear-webview-cookies", async (_event, partition) => {
  197. try {
  198. const ses = session.fromPartition(partition);
  199. await ses.clearStorageData({ storages: ["cookies"] });
  200. return true;
  201. } catch (error) {
  202. console.error("清除 cookies 失败:", error);
  203. return false;
  204. }
  205. });
  206. //# sourceMappingURL=main.js.map