🐛 fix(window): 用 CSS zoom nudge 修复任务栏恢复字体变大且不引入重复最大化

- 撤回上次错误的 toggle 改动:恢复 9848b8b2 的 restore 不重新最大化取舍,避免用户在任务栏点击恢复时看到窗口"被弹两次"
- 新增 applyWindowsViewportZoomNudge:通过短暂将 documentElement.style.zoom 设为 1.0001 并在两帧内重置,强制 Chromium 重算 layout metrics 修复字体变大,零可见动画、不动窗口
- maximised + drift + restore 路径从仅 dispatch resize 改为先 zoom nudge 再 dispatch resize
- 锁定 windowStateUi.test.ts 中 shouldToggleMaximisedWindowForScaleFix('restore', true)=false 取舍并补注释禁止再次反转
- windowsScaleFix.test.ts 加 jsdom 环境,新增双帧 zoom nudge 行为测试
This commit is contained in:
Syngnat
2026-05-15 15:03:25 +08:00
parent c7b8663c06
commit 067cbd5ab2
5 changed files with 81 additions and 14 deletions

View File

@@ -44,3 +44,34 @@ export const getWindowsScaleFixNudgedWidth = (width: number): number => {
}
return normalizedWidth > 480 ? normalizedWidth - 1 : normalizedWidth + 1;
};
// applyWindowsViewportZoomNudge 通过短暂改变 documentElement 的 CSS zoom 触发 Chromium
// 重新计算 layout metrics。用于 maximised 窗口在 restore 场景下 viewport drift 修复的
// 无动画路径:不重新最大化(避免 9848b8b2 修复的可见"重复最大化"抖动),也不调 WebView2
// COM API避免 Windows 平台特定代码)。
//
// 为什么这样能修Chromium 在切换 zoom factor 时会重算所有 px 度量与 currentColor/font-size
// 派生值drift 后残留的旧度量被丢弃。1.0001 与 1 在视觉上不可分辨但属于 invalidation 阈值
// 之外,强制触发完整 layout 重排。
//
// 用 requestAnimationFrame 两帧而不是立即 reset让 Chromium 在第一帧完成 nudge layout、
// 第二帧恢复——避免单帧合成被合并掉。
export const applyWindowsViewportZoomNudge = (): void => {
if (typeof document === 'undefined' || !document.documentElement) {
return;
}
const root = document.documentElement;
const style = root.style as CSSStyleDeclaration & { zoom?: string };
const previousZoom = style.zoom ?? '';
style.zoom = '1.0001';
const reset = () => {
style.zoom = previousZoom;
};
if (typeof window === 'undefined' || typeof window.requestAnimationFrame !== 'function') {
reset();
return;
}
window.requestAnimationFrame(() => {
window.requestAnimationFrame(reset);
});
};