Skip to content

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

:bug: Reveal detached windows after a short delay to avoid the white flash https://github.com/siyuan-note/siyuan/issues/20192 (#20518)

Jeffrey Chenfdc2eb6debaa
diff --git a/app/electron/main.js b/app/electron/main.js
index ec11f7fbd4..e46c181211 100644
--- a/app/electron/main.js
+++ b/app/electron/main.js
@@ -4042,7 +4042,9 @@ app.whenReady().then(() => {
const geometry = workspaceID ? normalizeWindowGeometry(data.windowGeometry, screen) : undefined;
const win = new BrowserWindow({
title: "SiYuan",
- show: true,
+ // 先在隐藏状态完成定位和加载,等画面就绪或短暂延迟后再显示,
+ // 避免窗口映射早于首帧提交时出现系统填充的近白色占位帧。
+ show: false,
backgroundColor,
trafficLightPosition: {x: 8, y: 13},
width: Math.floor(data.width || mainScreen.size.width * 0.7),
@@ -4073,13 +4075,6 @@ app.whenReady().then(() => {
windowWorkspaces.associate(win, kernelTarget.origin, workspaceID);
}
- if (geometry && data.windowGeometry.maximized) {
- win.maximize();
- }
- if (geometry && data.windowGeometry.fullscreen) {
- win.setFullScreen(true);
- }
-
if (!geometry && data.position) {
win.setPosition(data.position.x, data.position.y);
} else if (!geometry) {
@@ -4088,6 +4083,40 @@ app.whenReady().then(() => {
win.setAlwaysOnTop(data.alwaysOnTop, "win32" === process.platform ? "pop-up-menu" : "floating");
win.webContents.userAgent = "SiYuan/" + appVer + " https://b3log.org/siyuan Electron " + win.webContents.userAgent;
win.webContents.session.setSpellCheckerLanguages(["en-US"]);
+ // 显示时机:首帧(ready-to-show)或短延迟,谁先到用谁。
+ // ready-to-show 在本应用里可能晚到 1 秒以上,定时器保证窗口及时出现;
+ // 两者都晚于创建,因此首帧直接是主题背景,不会闪白。
+ const revealDelay = 150;
+ let revealed = false;
+ let revealTimer;
+ const revealWindow = () => {
+ if (revealed || win.isDestroyed()) {
+ return;
+ }
+ revealed = true;
+ clearTimeout(revealTimer);
+ // 隐藏窗口上最大化或进入全屏会直接映射窗口,因此和显示一起执行。
+ if (geometry && data.windowGeometry.maximized) {
+ win.maximize();
+ }
+ if (geometry && data.windowGeometry.fullscreen) {
+ win.setFullScreen(true);
+ }
+ showWindow(win);
+ };
+ win.once("ready-to-show", revealWindow);
+ revealTimer = setTimeout(revealWindow, revealDelay);
+ // 渲染异常时仍显示窗口,避免留下无法访问的隐藏窗口。
+ const forceRevealTimer = setTimeout(() => {
+ if (!revealed) {
+ writeLog("detached window ready-to-show timeout, show it anyway");
+ revealWindow();
+ }
+ }, 60000);
+ win.once("closed", () => {
+ clearTimeout(revealTimer);
+ clearTimeout(forceRevealTimer);
+ });
win.loadURL(windowURL.href);
windowNavigate(win, "window", kernelTarget.origin, kernelTarget.mode === "remote");
win.on("close", (event) => {
diff --git a/app/electron/windowGeometry.test.js b/app/electron/windowGeometry.test.js
index 36cb045f14..cb060edd4d 100644
--- a/app/electron/windowGeometry.test.js
+++ b/app/electron/windowGeometry.test.js
@@ -105,6 +105,7 @@ const openWindow = (windowGeometry, appearance = {}, mode = "local") => {
const start = source.indexOf('ipcMain.on("siyuan-open-window",');
const end = source.indexOf('ipcMain.on("siyuan-open-workspace",', start);
const calls = [];
+ const timers = new Set();
const ipcMain = new EventEmitter();
const initializedWindowIds = new Set();
let options;
@@ -152,6 +153,8 @@ const openWindow = (windowGeometry, appearance = {}, mode = "local") => {
windowNavigate: () => {},
showWindow: () => calls.push("show"), getWindowPathname: () => pathname, writeLog: () => {},
markExpectedRendererExit: () => calls.push("expected-exit"),
+ setTimeout: callback => { timers.add(callback); return callback; },
+ clearTimeout: timer => timers.delete(timer),
});
const open = () => ipcMain.emit("siyuan-open-window", {sender: {id: 1}}, {
url: "http://127.0.0.1:6806/stage/build/app/window.html?windowWorkspace=20260924100000-abcdefg",
@@ -160,7 +163,7 @@ const openWindow = (windowGeometry, appearance = {}, mode = "local") => {
});
open();
const win = windows[0];
- return {options, calls, workArea, win, ipcMain, initializedWindowIds, url, open,
+ return {options, calls, workArea, win, ipcMain, initializedWindowIds, url, open, timers,
navigate: value => { pathname = value; },
close: () => win.emit("close", {preventDefault: () => calls.push("prevent-close")}),
};
@@ -169,7 +172,13 @@ const openWindow = (windowGeometry, appearance = {}, mode = "local") => {
test("新窗口创建时使用保存边界,不被鼠标所在显示器覆盖", () => {
const f = openWindow(state({maximized: true}));
assert.deepEqual([f.options.x, f.options.y, f.options.width, f.options.height], [100, 80, 900, 700]);
- assert.deepEqual(f.calls, ["maximize"]);
+ // 首帧绘制(或短延迟)前不显示窗口,最大化也在那时才应用
+ assert.equal(f.options.show, false);
+ assert.deepEqual(f.calls, []);
+ f.win.emit("ready-to-show");
+ assert.deepEqual(f.calls, ["maximize", "show"]);
+ f.win.emit("ready-to-show");
+ assert.deepEqual(f.calls, ["maximize", "show"]);
});
test("旧窗口布局继续使用默认窗口位置", () => {
@@ -177,18 +186,36 @@ test("旧窗口布局继续使用默认窗口位置", () => {
assert.deepEqual(f.calls, ["center", f.workArea]);
});
-test("新窗口立即显示,重复打开激活已有窗口", () => {
+test("新窗口首帧后显示,定时器先到也显示,重复打开激活已有窗口", () => {
const f = openWindow(state());
- assert.equal(f.options.show, true);
+ assert.equal(f.options.show, false);
assert.equal(f.options.webPreferences.backgroundThrottling, undefined);
assert.equal(f.ipcMain.listenerCount("siyuan-window-ready"), 0);
- f.open();
+ assert.deepEqual(f.calls, []);
+ f.win.emit("ready-to-show");
assert.deepEqual(f.calls, ["show"]);
+ assert.equal(f.timers.size, 1, "ready-to-show 已显示,只保留兜底超时");
+ f.open();
+ assert.deepEqual(f.calls, ["show", "show"]);
+
+ // 首帧迟迟不来时由短延迟定时器显示,且不会重复显示
+ const timer = openWindow(state());
+ assert.equal(timer.timers.size, 2);
+ assert.ok([...timer.timers].some(fn => {
+ if (fn.name !== "revealWindow") return false;
+ fn();
+ return true;
+ }), "存在短延迟显示定时器");
+ assert.deepEqual(timer.calls, ["show"]);
+ timer.win.emit("ready-to-show");
+ assert.deepEqual(timer.calls, ["show"]);
});
-test("全屏窗口立即恢复状态", () => {
+test("全屏窗口首帧后恢复状态", () => {
const f = openWindow(state({fullscreen: true}));
- assert.deepEqual(f.calls, ["fullscreen"]);
+ assert.deepEqual(f.calls, []);
+ f.win.emit("ready-to-show");
+ assert.deepEqual(f.calls, ["fullscreen", "show"]);
});
test("原生窗口和页面使用同一实际主题颜色,非法颜色使用安全默认值", () => {