Skip to content

Commit 067db07

Browse files
authored
🎨 Fix focus issues on Windows after dialog interactions (#16073)
fix #16071
1 parent 8725e5d commit 067db07

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

app/electron/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,13 @@ app.whenReady().then(() => {
866866
event.sender.send("siyuan-event", "leave-full-screen");
867867
});
868868
});
869+
ipcMain.on("siyuan-focus-fix", (event) => {
870+
const currentWindow = getWindowByContentId(event.sender.id);
871+
if (currentWindow && process.platform === "win32") {
872+
currentWindow.blur();
873+
currentWindow.focus();
874+
}
875+
});
869876
ipcMain.on("siyuan-cmd", (event, data) => {
870877
let cmd = data;
871878
let webContentsId = event.sender.id;

app/src/boot/onGetConfig.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as fs from "fs";
77
import * as path from "path";
88
import {afterExport} from "../protyle/export/util";
99
import {onWindowsMsg} from "../window/onWindowsMsg";
10+
import {initFocusFix} from "../protyle/util/compatibility";
1011
/// #endif
1112
import {Constants} from "../constants";
1213
import {appearance} from "../config/appearance";
@@ -68,6 +69,9 @@ export const onGetConfig = (isStart: boolean, app: App) => {
6869
initBar(app);
6970
initStatus();
7071
initWindow(app);
72+
/// #if !BROWSER
73+
initFocusFix();
74+
/// #endif
7175
appearance.onSetAppearance(window.siyuan.config.appearance);
7276
initAssets();
7377
setInlineStyle();

app/src/protyle/util/compatibility.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {fetchPost, fetchSyncPost} from "../../util/fetch";
33
import {Constants} from "../../constants";
44
/// #if !BROWSER
55
import {clipboard} from "electron";
6+
import {ipcRenderer} from "electron";
67
/// #endif
78

89
export const encodeBase64 = (text: string): string => {
@@ -512,3 +513,38 @@ export const setStorageVal = (key: string, val: any, cb?: () => void) => {
512513
}
513514
});
514515
};
516+
517+
/// #if !BROWSER
518+
export const initFocusFix = () => {
519+
if (!isWindows()) {
520+
return;
521+
}
522+
const originalAlert = window.alert;
523+
const originalConfirm = window.confirm;
524+
const fixFocusAfterDialog = () => {
525+
ipcRenderer.send("siyuan-focus-fix");
526+
};
527+
window.alert = function(message: string) {
528+
try {
529+
const result = originalAlert.call(this, message);
530+
fixFocusAfterDialog();
531+
return result;
532+
} catch (error) {
533+
console.error("alert error:", error);
534+
fixFocusAfterDialog();
535+
return undefined;
536+
}
537+
};
538+
window.confirm = function(message: string) {
539+
try {
540+
const result = originalConfirm.call(this, message);
541+
fixFocusAfterDialog();
542+
return result;
543+
} catch (error) {
544+
console.error("confirm error:", error);
545+
fixFocusAfterDialog();
546+
return false;
547+
}
548+
};
549+
};
550+
/// #endif

0 commit comments

Comments
 (0)