Vanessa 2024-12-06 11:12:33 +08:00
parent ca5d906612
commit 0d03024238

View file

@ -41,6 +41,7 @@ const appVer = app.getVersion();
const confDir = path.join(app.getPath("home"), ".config", "siyuan"); const confDir = path.join(app.getPath("home"), ".config", "siyuan");
const windowStatePath = path.join(confDir, "windowState.json"); const windowStatePath = path.join(confDir, "windowState.json");
let bootWindow; let bootWindow;
let latestActiveWindow;
let firstOpen = false; let firstOpen = false;
let workspaces = []; // workspaceDir, id, browserWindow, tray let workspaces = []; // workspaceDir, id, browserWindow, tray
let kernelPort = 6806; let kernelPort = 6806;
@ -700,7 +701,10 @@ app.whenReady().then(() => {
const hideWindow = (wnd) => { const hideWindow = (wnd) => {
// 通过 `Alt+M` 最小化后焦点回到先前的窗口 https://github.com/siyuan-note/siyuan/issues/7275 // 通过 `Alt+M` 最小化后焦点回到先前的窗口 https://github.com/siyuan-note/siyuan/issues/7275
wnd.minimize(); wnd.minimize();
// Mac 隐藏后无法再 Dock 中显示
if ("win32" === process.platform || "linux" === process.platform) {
wnd.hide(); wnd.hide();
}
}; };
const showHideWindow = (tray, lang, mainWindow) => { const showHideWindow = (tray, lang, mainWindow) => {
if (!mainWindow.isVisible()) { if (!mainWindow.isVisible()) {
@ -815,8 +819,10 @@ app.whenReady().then(() => {
if (!currentWindow) { if (!currentWindow) {
return; return;
} }
latestActiveWindow = currentWindow;
currentWindow.on("focus", () => { currentWindow.on("focus", () => {
event.sender.send("siyuan-event", "focus"); event.sender.send("siyuan-event", "focus");
latestActiveWindow = currentWindow;
}); });
currentWindow.on("blur", () => { currentWindow.on("blur", () => {
event.sender.send("siyuan-event", "blur"); event.sender.send("siyuan-event", "blur");
@ -1102,9 +1108,18 @@ app.whenReady().then(() => {
} }
if (index === 0) { if (index === 0) {
globalShortcut.register(shortcut, () => { globalShortcut.register(shortcut, () => {
let currentWorkspace;
const currentWebContentsId = (latestActiveWindow && !latestActiveWindow.isDestroyed()) ? latestActiveWindow.webContents.id : undefined
workspaces.find(workspaceItem => { workspaces.find(workspaceItem => {
const mainWindow = workspaceItem.browserWindow; if ((currentWebContentsId || event.sender.id) === workspaceItem.browserWindow.webContents.id) {
if (event.sender.id === mainWindow.webContents.id) { currentWorkspace = workspaceItem;
return true;
}
});
if (!currentWorkspace) {
return;
}
const mainWindow = currentWorkspace.browserWindow;
if (mainWindow.isMinimized()) { if (mainWindow.isMinimized()) {
mainWindow.restore(); mainWindow.restore();
mainWindow.show(); // 按 `Alt+M` 后隐藏窗口,再次按 `Alt+M` 显示窗口后会卡住不能编辑 https://github.com/siyuan-note/siyuan/issues/8456 mainWindow.show(); // 按 `Alt+M` 后隐藏窗口,再次按 `Alt+M` 显示窗口后会卡住不能编辑 https://github.com/siyuan-note/siyuan/issues/8456
@ -1120,11 +1135,8 @@ app.whenReady().then(() => {
} }
} }
if ("win32" === process.platform || "linux" === process.platform) { if ("win32" === process.platform || "linux" === process.platform) {
resetTrayMenu(workspaceItem.tray, data.languages, mainWindow); resetTrayMenu(currentWorkspace.tray, data.languages, mainWindow);
} }
return true;
}
});
}); });
} else { } else {
globalShortcut.register(shortcut, () => { globalShortcut.register(shortcut, () => {
@ -1331,7 +1343,7 @@ app.on("second-instance", (event, argv) => {
app.on("activate", () => { app.on("activate", () => {
if (workspaces.length > 0) { if (workspaces.length > 0) {
const mainWindow = workspaces[0].browserWindow; const mainWindow = (latestActiveWindow && !latestActiveWindow.isDestroyed()) ? latestActiveWindow : workspaces[0].browserWindow;
if (mainWindow && !mainWindow.isDestroyed()) { if (mainWindow && !mainWindow.isDestroyed()) {
mainWindow.show(); mainWindow.show();
} }