🎨 Fix npe

This commit is contained in:
Daniel 2024-03-25 11:36:19 +08:00
parent 1362def271
commit daceb8f7a8
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -683,7 +683,11 @@ app.whenReady().then(() => {
resetTrayMenu(tray, lang, mainWindow); resetTrayMenu(tray, lang, mainWindow);
}; };
const getWindowByContentId = (id) => { const getWindowByContentId = (id) => {
return BrowserWindow.fromId(BrowserWindow.getAllWindows().find((win) => win.webContents.id === id).id); const wnd = BrowserWindow.getAllWindows().find((win) => win.webContents.id === id);
if (!wnd) {
return null;
}
return BrowserWindow.fromId(wnd.id);
}; };
ipcMain.on("siyuan-open-folder", (event, filePath) => { ipcMain.on("siyuan-open-folder", (event, filePath) => {
@ -703,10 +707,18 @@ app.whenReady().then(() => {
return dialog.showSaveDialog(data); return dialog.showSaveDialog(data);
} }
if (data.cmd === "isFullScreen") { if (data.cmd === "isFullScreen") {
return getWindowByContentId(event.sender.id).isFullScreen(); const wnd = getWindowByContentId(event.sender.id);
if (!wnd) {
return false;
}
return wnd.isFullScreen();
} }
if (data.cmd === "isMaximized") { if (data.cmd === "isMaximized") {
return getWindowByContentId(event.sender.id).isMaximized(); const wnd = getWindowByContentId(event.sender.id);
if (!wnd) {
return false;
}
return wnd.isMaximized();
} }
if (data.cmd === "getMicrophone") { if (data.cmd === "getMicrophone") {
return systemPreferences.getMediaAccessStatus("microphone"); return systemPreferences.getMediaAccessStatus("microphone");
@ -715,7 +727,11 @@ app.whenReady().then(() => {
return systemPreferences.askForMediaAccess("microphone"); return systemPreferences.askForMediaAccess("microphone");
} }
if (data.cmd === "printToPDF") { if (data.cmd === "printToPDF") {
return getWindowByContentId(data.webContentsId).webContents.printToPDF(data.pdfOptions); const wnd = getWindowByContentId(event.sender.id);
if (!wnd) {
return;
}
return wnd.webContents.printToPDF(data.pdfOptions);
} }
if (data.cmd === "siyuan-open-file") { if (data.cmd === "siyuan-open-file") {
let hasMatch = false; let hasMatch = false;
@ -743,6 +759,9 @@ app.whenReady().then(() => {
} }
initEventId.push(event.sender.id); initEventId.push(event.sender.id);
const currentWindow = getWindowByContentId(event.sender.id); const currentWindow = getWindowByContentId(event.sender.id);
if (!currentWindow) {
return;
}
currentWindow.on("focus", () => { currentWindow.on("focus", () => {
event.sender.send("siyuan-event", "focus"); event.sender.send("siyuan-event", "focus");
}); });
@ -782,18 +801,33 @@ app.whenReady().then(() => {
globalShortcut.unregisterAll(); globalShortcut.unregisterAll();
break; break;
case "show": case "show":
if (!currentWindow) {
return;
}
showWindow(currentWindow); showWindow(currentWindow);
break; break;
case "hide": case "hide":
if (!currentWindow) {
return;
}
currentWindow.hide(); currentWindow.hide();
break; break;
case "minimize": case "minimize":
if (!currentWindow) {
return;
}
currentWindow.minimize(); currentWindow.minimize();
break; break;
case "maximize": case "maximize":
if (!currentWindow) {
return;
}
currentWindow.maximize(); currentWindow.maximize();
break; break;
case "restore": case "restore":
if (!currentWindow) {
return;
}
if (currentWindow.isFullScreen()) { if (currentWindow.isFullScreen()) {
currentWindow.setFullScreen(false); currentWindow.setFullScreen(false);
} else { } else {
@ -801,12 +835,21 @@ app.whenReady().then(() => {
} }
break; break;
case "focus": case "focus":
if (!currentWindow) {
return;
}
currentWindow.focus(); currentWindow.focus();
break; break;
case "setAlwaysOnTopFalse": case "setAlwaysOnTopFalse":
if (!currentWindow) {
return;
}
currentWindow.setAlwaysOnTop(false); currentWindow.setAlwaysOnTop(false);
break; break;
case "setAlwaysOnTopTrue": case "setAlwaysOnTopTrue":
if (!currentWindow) {
return;
}
currentWindow.setAlwaysOnTop(true); currentWindow.setAlwaysOnTop(true);
break; break;
case "clearCache": case "clearCache":
@ -819,9 +862,15 @@ app.whenReady().then(() => {
event.sender.undo(); event.sender.undo();
break; break;
case "destroy": case "destroy":
if (!currentWindow) {
return;
}
currentWindow.destroy(); currentWindow.destroy();
break; break;
case "closeButtonBehavior": case "closeButtonBehavior":
if (!currentWindow) {
return;
}
if (currentWindow.isFullScreen()) { if (currentWindow.isFullScreen()) {
currentWindow.once("leave-full-screen", () => { currentWindow.once("leave-full-screen", () => {
currentWindow.hide(); currentWindow.hide();
@ -983,6 +1032,9 @@ app.whenReady().then(() => {
tray = new Tray(path.join(appDir, "stage", "icon-large.png")); tray = new Tray(path.join(appDir, "stage", "icon-large.png"));
tray.setToolTip(`${path.basename(data.workspaceDir)} - SiYuan v${appVer}`); tray.setToolTip(`${path.basename(data.workspaceDir)} - SiYuan v${appVer}`);
const mainWindow = getWindowByContentId(event.sender.id); const mainWindow = getWindowByContentId(event.sender.id);
if (!mainWindow) {
return;
}
resetTrayMenu(tray, data.languages, mainWindow); resetTrayMenu(tray, data.languages, mainWindow);
tray.on("click", () => { tray.on("click", () => {
showHideWindow(tray, data.languages, mainWindow); showHideWindow(tray, data.languages, mainWindow);