From c45cabfcdaf55544eef1eb2672392fa75e4e41c3 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Sat, 18 Mar 2023 23:38:27 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BD=BF=E7=94=A8=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E5=90=8C=E6=AD=A5=E7=9B=98=E6=97=B6=E5=BC=B9=E5=87=BA?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E5=B9=B6=E9=80=80=E5=87=BA=E5=86=85=E6=A0=B8?= =?UTF-8?q?=20https://github.com/siyuan-note/siyuan/issues/7683?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/electron/main.js | 98 ++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 41 deletions(-) diff --git a/app/electron/main.js b/app/electron/main.js index 84cb32fc5..92b91590b 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -38,6 +38,7 @@ let bootWindow; let firstOpen = false; let workspaces = []; // workspaceDir, id, browserWindow, tray let kernelPort = 6806; +let resetWindowStateOnRestart = false; require("@electron/remote/main").initialize(); if (!app.requestSingleInstanceLock()) { @@ -57,6 +58,60 @@ try { app.exit(); } +// type: port/id +const exitApp = (type, id) => { + let tray; + workspaces.find((item, index) => { + if (type === "id") { + if (item.id === id) { + 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 === 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)) { + tray.destroy(); + } + if (workspaces.length === 1) { + try { + if (resetWindowStateOnRestart) { + fs.writeFileSync(windowStatePath, "{}"); + } else { + const mainWindow = workspaces[0].browserWindow; + const bounds = mainWindow.getBounds(); + fs.writeFileSync(windowStatePath, JSON.stringify({ + isMaximized: mainWindow.isMaximized(), + fullscreen: mainWindow.isFullScreen(), + isDevToolsOpened: mainWindow.webContents.isDevToolsOpened(), + x: bounds.x, + y: bounds.y, + width: bounds.width, + height: bounds.height, + })); + } + } catch (e) { + writeLog(e); + } + app.exit(); + globalShortcut.unregisterAll(); + writeLog("exited ui"); + } +} + const getServer = (port = kernelPort) => { return "http://127.0.0.1:" + port; }; @@ -497,7 +552,7 @@ const initKernel = (workspace, port, lang) => {
SiYuan Kernel exited for unknown reasons [code=${code}], please try to reboot your operating system and then start SiYuan again. If occurs this problem still, please check your anti-virus software whether kill the SiYuan Kernel.
`); break; } - + exitApp("port", kernelPort); bootWindow.destroy(); resolve(false); } @@ -583,8 +638,6 @@ app.commandLine.appendSwitch("enable-features", "PlatformHEVCDecoderSupport"); app.setPath("userData", app.getPath("userData") + "-Electron"); // `~/.config` 下 Electron 相关文件夹名称改为 `SiYuan-Electron` https://github.com/siyuan-note/siyuan/issues/3349 app.whenReady().then(() => { - - let resetWindowStateOnRestart = false; const resetTrayMenu = (tray, lang, mainWindow) => { const trayMenuTemplate = [ { @@ -696,44 +749,7 @@ app.whenReady().then(() => { }); }); ipcMain.on("siyuan-quit", (event, id) => { - const mainWindow = BrowserWindow.fromId(id); - let tray; - workspaces.find((item, index) => { - if (item.id === id) { - if (workspaces.length > 1) { - mainWindow.destroy(); - } - tray = item.tray; - workspaces.splice(index, 1); - return true; - } - }); - if (tray && ("win32" === process.platform || "linux" === process.platform)) { - tray.destroy(); - } - if (workspaces.length === 0) { - try { - if (resetWindowStateOnRestart) { - fs.writeFileSync(windowStatePath, "{}"); - } else { - const bounds = mainWindow.getBounds(); - fs.writeFileSync(windowStatePath, JSON.stringify({ - isMaximized: mainWindow.isMaximized(), - fullscreen: mainWindow.isFullScreen(), - isDevToolsOpened: mainWindow.webContents.isDevToolsOpened(), - x: bounds.x, - y: bounds.y, - width: bounds.width, - height: bounds.height, - })); - } - } catch (e) { - writeLog(e); - } - app.exit(); - globalShortcut.unregisterAll(); - writeLog("exited ui"); - } + exitApp("id", id) }); ipcMain.on("siyuan-openwindow", (event, data) => { const mainWindow = BrowserWindow.fromId(data.id);