Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Fix focus issues on Windows after dialog interactions (#16073)

Jeffrey Chen067db0791fce
diff --git a/app/electron/main.js b/app/electron/main.js
index 98c5e7e65b..01521c4dd1 100644
--- a/app/electron/main.js
+++ b/app/electron/main.js
@@ -866,6 +866,13 @@ app.whenReady().then(() => {
event.sender.send("siyuan-event", "leave-full-screen");
});
});
+ ipcMain.on("siyuan-focus-fix", (event) => {
+ const currentWindow = getWindowByContentId(event.sender.id);
+ if (currentWindow && process.platform === "win32") {
+ currentWindow.blur();
+ currentWindow.focus();
+ }
+ });
ipcMain.on("siyuan-cmd", (event, data) => {
let cmd = data;
let webContentsId = event.sender.id;
diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts
index decd7a88e6..ec4d031e70 100644
--- a/app/src/boot/onGetConfig.ts
+++ b/app/src/boot/onGetConfig.ts
@@ -7,6 +7,7 @@ import * as fs from "fs";
import * as path from "path";
import {afterExport} from "../protyle/export/util";
import {onWindowsMsg} from "../window/onWindowsMsg";
+import {initFocusFix} from "../protyle/util/compatibility";
/// #endif
import {Constants} from "../constants";
import {appearance} from "../config/appearance";
@@ -68,6 +69,9 @@ export const onGetConfig = (isStart: boolean, app: App) => {
initBar(app);
initStatus();
initWindow(app);
+ /// #if !BROWSER
+ initFocusFix();
+ /// #endif
appearance.onSetAppearance(window.siyuan.config.appearance);
initAssets();
setInlineStyle();
diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.ts
index f418110cfc..d0bb8c31af 100644
--- a/app/src/protyle/util/compatibility.ts
+++ b/app/src/protyle/util/compatibility.ts
@@ -3,6 +3,7 @@ import {fetchPost, fetchSyncPost} from "../../util/fetch";
import {Constants} from "../../constants";
/// #if !BROWSER
import {clipboard} from "electron";
+import {ipcRenderer} from "electron";
/// #endif
export const encodeBase64 = (text: string): string => {
@@ -512,3 +513,38 @@ export const setStorageVal = (key: string, val: any, cb?: () => void) => {
}
});
};
+
+/// #if !BROWSER
+export const initFocusFix = () => {
+ if (!isWindows()) {
+ return;
+ }
+ const originalAlert = window.alert;
+ const originalConfirm = window.confirm;
+ const fixFocusAfterDialog = () => {
+ ipcRenderer.send("siyuan-focus-fix");
+ };
+ window.alert = function(message: string) {
+ try {
+ const result = originalAlert.call(this, message);
+ fixFocusAfterDialog();
+ return result;
+ } catch (error) {
+ console.error("alert error:", error);
+ fixFocusAfterDialog();
+ return undefined;
+ }
+ };
+ window.confirm = function(message: string) {
+ try {
+ const result = originalConfirm.call(this, message);
+ fixFocusAfterDialog();
+ return result;
+ } catch (error) {
+ console.error("confirm error:", error);
+ fixFocusAfterDialog();
+ return false;
+ }
+ };
+};
+/// #endif
\ No newline at end of file