From b524a3c72f435b97904fc6dd17389b14474b0f6f Mon Sep 17 00:00:00 2001 From: Vanessa Date: Sun, 19 Mar 2023 11:57:22 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/siyuan/issues/7708 --- app/electron/main.js | 47 +++++++++++++++------------------ app/src/dialog/processSystem.ts | 8 +++--- app/src/util/fetch.ts | 2 +- 3 files changed, 27 insertions(+), 30 deletions(-) diff --git a/app/electron/main.js b/app/electron/main.js index 0bf22cf02..3dd4368fe 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -51,31 +51,29 @@ try { } // type: port/id -const exitApp = (type, id, errorWindowId) => { +const exitApp = (port, errorWindowId) => { let tray; let mainWindow; + + // 关闭端口相同的所有非主窗口 + BrowserWindow.getAllWindows().forEach((item) => { + const currentURL = new URL(item.getURL()) + if (port.toString() === currentURL.port.toString()) { + if (currentURL.href.indexOf("/stage/build/app/?v=") > -1) { + mainWindow = item; + } else { + item.destroy(); + } + } + }); workspaces.find((item, index) => { - if (type === "id") { - if (item.id === id) { - mainWindow = item.browserWindow; - if (workspaces.length > 1) { - item.browserWindow.destroy(); - } - workspaces.splice(index, 1); - tray = item.tray; - return true; - } - } else { - const currentURL = new URL(item.browserWindow.getURL()); - if (currentURL.port.toString() === id.toString()) { - mainWindow = item.browserWindow; - if (workspaces.length > 1) { - item.browserWindow.destroy(); - } - workspaces.splice(index, 1); - tray = item.tray; - return true; + if (mainWindow.id === item.browserWindow.id) { + if (workspaces.length > 1) { + item.browserWindow.destroy(); } + workspaces.splice(index, 1); + tray = item.tray; + return true; } }); if (tray && ("win32" === process.platform || "linux" === process.platform)) { @@ -103,7 +101,6 @@ const exitApp = (type, id, errorWindowId) => { if (errorWindowId) { BrowserWindow.getAllWindows().forEach((item) => { - writeLog(errorWindowId + " " + item.id); if (errorWindowId !== item.id) { item.destroy(); } @@ -491,7 +488,7 @@ const initKernel = (workspace, port, lang) => { break; } - exitApp("port", currentKernelPort, errorWindowId); + exitApp(currentKernelPort, errorWindowId); bootWindow.destroy(); resolve(false); } @@ -666,8 +663,8 @@ app.whenReady().then(() => { shell.openExternal(url); }); }); - ipcMain.on("siyuan-quit", (event, id) => { - exitApp("id", id) + ipcMain.on("siyuan-quit", (event, port) => { + exitApp(port); }); ipcMain.on("siyuan-openwindow", (event, data) => { const mainWindow = BrowserWindow.fromId(data.id); diff --git a/app/src/dialog/processSystem.ts b/app/src/dialog/processSystem.ts index fcfd6b24e..1197fb377 100644 --- a/app/src/dialog/processSystem.ts +++ b/app/src/dialog/processSystem.ts @@ -111,7 +111,7 @@ export const exitSiYuan = () => { buttonElement.addEventListener("click", () => { fetchPost("/api/system/exit", {force: true}, () => { /// #if !BROWSER - ipcRenderer.send(Constants.SIYUAN_QUIT, getCurrentWindow().id); + ipcRenderer.send(Constants.SIYUAN_QUIT, location.port); /// #else if (["ios", "android"].includes(window.siyuan.config.system.container) && (window.webkit?.messageHandlers || window.JSAndroid)) { window.location.href = "siyuan://api/system/exit"; @@ -135,7 +135,7 @@ export const exitSiYuan = () => { }, 2000); // 然后等待一段时间后再退出,避免界面主进程退出以后内核子进程被杀死 setTimeout(() => { - ipcRenderer.send(Constants.SIYUAN_QUIT, getCurrentWindow().id); + ipcRenderer.send(Constants.SIYUAN_QUIT, location.port); }, 4000); /// #endif }); @@ -145,13 +145,13 @@ export const exitSiYuan = () => { execInstallPkg: 1 // 0:默认检查新版本,1:不执行新版本安装,2:执行新版本安装 }, () => { /// #if !BROWSER - ipcRenderer.send(Constants.SIYUAN_QUIT, getCurrentWindow().id); + ipcRenderer.send(Constants.SIYUAN_QUIT, location.port); /// #endif }); }); } else { // 正常退出 /// #if !BROWSER - ipcRenderer.send(Constants.SIYUAN_QUIT, getCurrentWindow().id); + ipcRenderer.send(Constants.SIYUAN_QUIT, location.port); /// #else if (["ios", "android"].includes(window.siyuan.config.system.container) && (window.webkit?.messageHandlers || window.JSAndroid)) { window.location.href = "siyuan://api/system/exit"; diff --git a/app/src/util/fetch.ts b/app/src/util/fetch.ts index c4e547aba..c21d6ee70 100644 --- a/app/src/util/fetch.ts +++ b/app/src/util/fetch.ts @@ -49,7 +49,7 @@ export const fetchPost = (url: string, data?: any, cb?: (response: IWebSocketDat if (url === "/api/system/exit" || url === "/api/system/setWorkspaceDir" || ( ["/api/system/setUILayout"].includes(url) && data.errorExit // 内核中断,点关闭处理 )) { - ipcRenderer.send(Constants.SIYUAN_QUIT, getCurrentWindow().id); + ipcRenderer.send(Constants.SIYUAN_QUIT, location.port); } /// #endif });