From 0d030242380ebff31772af031547fc9d2c227966 Mon Sep 17 00:00:00 2001 From: Vanessa Date: Fri, 6 Dec 2024 11:12:33 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/siyuan/issues/12656 --- app/electron/main.js | 54 +++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/app/electron/main.js b/app/electron/main.js index 1eaf3de1b..c912e3b86 100644 --- a/app/electron/main.js +++ b/app/electron/main.js @@ -41,6 +41,7 @@ const appVer = app.getVersion(); const confDir = path.join(app.getPath("home"), ".config", "siyuan"); const windowStatePath = path.join(confDir, "windowState.json"); let bootWindow; +let latestActiveWindow; let firstOpen = false; let workspaces = []; // workspaceDir, id, browserWindow, tray let kernelPort = 6806; @@ -700,7 +701,10 @@ app.whenReady().then(() => { const hideWindow = (wnd) => { // 通过 `Alt+M` 最小化后焦点回到先前的窗口 https://github.com/siyuan-note/siyuan/issues/7275 wnd.minimize(); - wnd.hide(); + // Mac 隐藏后无法再 Dock 中显示 + if ("win32" === process.platform || "linux" === process.platform) { + wnd.hide(); + } }; const showHideWindow = (tray, lang, mainWindow) => { if (!mainWindow.isVisible()) { @@ -815,8 +819,10 @@ app.whenReady().then(() => { if (!currentWindow) { return; } + latestActiveWindow = currentWindow; currentWindow.on("focus", () => { event.sender.send("siyuan-event", "focus"); + latestActiveWindow = currentWindow; }); currentWindow.on("blur", () => { event.sender.send("siyuan-event", "blur"); @@ -1102,29 +1108,35 @@ app.whenReady().then(() => { } if (index === 0) { globalShortcut.register(shortcut, () => { + let currentWorkspace; + const currentWebContentsId = (latestActiveWindow && !latestActiveWindow.isDestroyed()) ? latestActiveWindow.webContents.id : undefined workspaces.find(workspaceItem => { - const mainWindow = workspaceItem.browserWindow; - if (event.sender.id === mainWindow.webContents.id) { - if (mainWindow.isMinimized()) { - mainWindow.restore(); - mainWindow.show(); // 按 `Alt+M` 后隐藏窗口,再次按 `Alt+M` 显示窗口后会卡住不能编辑 https://github.com/siyuan-note/siyuan/issues/8456 - } else { - if (mainWindow.isVisible()) { - if (!mainWindow.isFocused()) { - mainWindow.show(); - } else { - hideWindow(mainWindow); - } - } else { - mainWindow.show(); - } - } - if ("win32" === process.platform || "linux" === process.platform) { - resetTrayMenu(workspaceItem.tray, data.languages, mainWindow); - } + if ((currentWebContentsId || event.sender.id) === workspaceItem.browserWindow.webContents.id) { + currentWorkspace = workspaceItem; return true; } }); + if (!currentWorkspace) { + return; + } + const mainWindow = currentWorkspace.browserWindow; + if (mainWindow.isMinimized()) { + mainWindow.restore(); + mainWindow.show(); // 按 `Alt+M` 后隐藏窗口,再次按 `Alt+M` 显示窗口后会卡住不能编辑 https://github.com/siyuan-note/siyuan/issues/8456 + } else { + if (mainWindow.isVisible()) { + if (!mainWindow.isFocused()) { + mainWindow.show(); + } else { + hideWindow(mainWindow); + } + } else { + mainWindow.show(); + } + } + if ("win32" === process.platform || "linux" === process.platform) { + resetTrayMenu(currentWorkspace.tray, data.languages, mainWindow); + } }); } else { globalShortcut.register(shortcut, () => { @@ -1331,7 +1343,7 @@ app.on("second-instance", (event, argv) => { app.on("activate", () => { if (workspaces.length > 0) { - const mainWindow = workspaces[0].browserWindow; + const mainWindow = (latestActiveWindow && !latestActiveWindow.isDestroyed()) ? latestActiveWindow : workspaces[0].browserWindow; if (mainWindow && !mainWindow.isDestroyed()) { mainWindow.show(); }