Skip to content

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

:bug: Account for display scaling in block panel tests https://github.com/siyuan-note/siyuan/issues/20483

Daniel8e7afea0a376
diff --git a/app/src/block/panelPosition.test.ts b/app/src/block/panelPosition.test.ts
index b7067d5f24..d663458462 100644
--- a/app/src/block/panelPosition.test.ts
+++ b/app/src/block/panelPosition.test.ts
@@ -22,6 +22,9 @@ const browserCases = (source: string, css: string) => {
document.body.append(toolbar);
const anchor = (top: number, left = 100) => new DOMRect(left, top, 160, 30);
+ // 边框会吸附到设备像素,布局量允许小于一个 CSS 像素的误差。
+ const assertPixel = (actual: number, expected: number) =>
+ check.ok(Math.abs(actual - expected) < 1, `expected ${actual} to be within 1px of ${expected}`);
const createPanel = (height: number) => {
document.querySelector(".block__popover")?.remove();
const element = document.createElement("div");
@@ -56,25 +59,27 @@ const browserCases = (source: string, css: string) => {
target = anchor(180);
element = createPanel(1000);
position(element, target);
- check.equal(element.getBoundingClientRect().top, target.bottom + 4);
+ assertPixel(element.getBoundingClientRect().top, target.bottom + 4);
check.ok(element.getBoundingClientRect().bottom <= window.innerHeight - 8);
check.ok(element.getBoundingClientRect().height > 600);
// 内容较少时保留向下展开,后续内容可在可用范围内增长。
target = anchor(500);
element = createPanel(250);
+ let naturalHeight = element.getBoundingClientRect().height;
position(element, target);
- check.equal(element.getBoundingClientRect().top, target.bottom + 4);
- check.equal(element.getBoundingClientRect().height, 250);
+ assertPixel(element.getBoundingClientRect().top, target.bottom + 4);
+ assertPixel(element.getBoundingClientRect().height, naturalHeight);
(element.lastElementChild.firstElementChild as HTMLElement).style.height = "1000px";
check.ok(element.getBoundingClientRect().bottom <= window.innerHeight - 8);
// 上方短浮窗应保持与锚点相邻,并限制后续内容向下侵入锚点。
target = anchor(760);
element = createPanel(250);
+ naturalHeight = element.getBoundingClientRect().height;
position(element, target);
- check.equal(element.getBoundingClientRect().bottom, target.top - 8);
- check.equal(element.getBoundingClientRect().height, 250);
+ assertPixel(element.getBoundingClientRect().bottom, target.top - 8);
+ assertPixel(element.getBoundingClientRect().height, naturalHeight);
(element.lastElementChild.firstElementChild as HTMLElement).style.height = "1000px";
check.ok(element.getBoundingClientRect().bottom <= target.top - 8);
@@ -105,7 +110,7 @@ const browserCases = (source: string, css: string) => {
element.style.width = "80vw";
position(element, target);
assertAbove(element, target);
- check.equal(element.getBoundingClientRect().top, 0);
+ assertPixel(element.getBoundingClientRect().top, 0);
// 可用空间较小时,最小高度也必须服从窗口边界。
toolbar.style.height = "600px";
@@ -116,24 +121,26 @@ const browserCases = (source: string, css: string) => {
position(element, target);
check.ok(element.getBoundingClientRect().top >= 600);
check.ok(element.getBoundingClientRect().bottom <= window.innerHeight - 8);
- check.equal(element.getBoundingClientRect().top, target.bottom + 4);
+ assertPixel(element.getBoundingClientRect().top, target.bottom + 4);
return "Block panel positioning cases passed";
};
-test("block popovers use available space without collapsing or covering their anchors", {
- skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
- timeout: 20000,
-}, async () => {
- const source = ["../layout/getTopBarHeight.ts", "../util/setPosition.ts", "panelPosition.ts"].map(file =>
- transpileModule(readFileSync(path.join(__dirname, file), "utf8")
- .replace(/^import [\s\S]*?;\r?\n/gm, "").replace(/^export /gm, ""),
- {compilerOptions: {target: ScriptTarget.ES2021}}).outputText).join("\n");
- const css = compile(path.join(__dirname, "../assets/scss/business/_block.scss")).css;
- const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-panel-position-test-"));
- const script = path.join(temporary, "run.cjs");
- writeFileSync(script, `const {app, BrowserWindow} = require("electron");
+for (const scale of [1, 1.25, 1.5, 2]) {
+ test(`block popovers use available space without covering anchors at ${scale}x display scaling`, {
+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,
+ timeout: 20000,
+ }, async () => {
+ const source = ["../layout/getTopBarHeight.ts", "../util/setPosition.ts", "panelPosition.ts"].map(file =>
+ transpileModule(readFileSync(path.join(__dirname, file), "utf8")
+ .replace(/^import [\s\S]*?;\r?\n/gm, "").replace(/^export /gm, ""),
+ {compilerOptions: {target: ScriptTarget.ES2021}}).outputText).join("\n");
+ const css = compile(path.join(__dirname, "../assets/scss/business/_block.scss")).css;
+ const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-panel-position-test-"));
+ const script = path.join(temporary, "run.cjs");
+ writeFileSync(script, `const {app, BrowserWindow} = require("electron");
app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});
app.commandLine.appendSwitch("disable-gpu");
+app.commandLine.appendSwitch("force-device-scale-factor", ${JSON.stringify(String(scale))});
app.whenReady().then(async () => {
for (const width of [1200, 400]) {
const win = new BrowserWindow({show: false, width, height: 900, useContentSize: true,
@@ -143,7 +150,10 @@ app.whenReady().then(async () => {
win.setContentSize(width, 900);
await win.loadURL("data:text/html,<html><body></body></html>");
const viewport = await win.webContents.executeJavaScript("[innerWidth, innerHeight]");
- require("node:assert/strict").deepEqual(viewport, [width, 900]);
+ // 原生窗口边框换算到分数缩放比例时,内容尺寸可能产生两个 CSS 像素以内的取整误差。
+ require("node:assert/strict").ok(Math.abs(viewport[0] - width) <= 2 && Math.abs(viewport[1] - 900) <= 2);
+ const deviceScale = await win.webContents.executeJavaScript("devicePixelRatio");
+ require("node:assert/strict").ok(Math.abs(deviceScale - ${scale}) < 0.01);
const result = await win.webContents.executeJavaScript(${JSON.stringify(
`(() => { const __name = value => value; try {
return (${browserCases.toString()})(${JSON.stringify(source)}, ${JSON.stringify(css)});
@@ -162,16 +172,17 @@ app.whenReady().then(async () => {
}
app.exit(0);
});`, "utf8");
- const env = {...process.env};
- delete env.ELECTRON_RUN_AS_NODE;
- try {
- const executable = require("electron") as unknown as string;
- const result = await promisify(execFile)(executable, [script], {env, timeout: 15000, windowsHide: true});
- assert.match(result.stdout, /Block panel positioning cases passed/);
- } finally {
- const resolvedTemporary = path.resolve(temporary);
- assert.equal(path.dirname(resolvedTemporary), path.resolve(tmpdir()));
- assert.ok(path.basename(resolvedTemporary).startsWith("siyuan-panel-position-test-"));
- rmSync(resolvedTemporary, {recursive: true, force: true});
- }
-});
+ const env = {...process.env};
+ delete env.ELECTRON_RUN_AS_NODE;
+ try {
+ const executable = require("electron") as unknown as string;
+ const result = await promisify(execFile)(executable, [script], {env, timeout: 15000, windowsHide: true});
+ assert.match(result.stdout, /Block panel positioning cases passed/);
+ } finally {
+ const resolvedTemporary = path.resolve(temporary);
+ assert.equal(path.dirname(resolvedTemporary), path.resolve(tmpdir()));
+ assert.ok(path.basename(resolvedTemporary).startsWith("siyuan-panel-position-test-"));
+ rmSync(resolvedTemporary, {recursive: true, force: true});
+ }
+ });
+}