Files
MyGoNavi/frontend/src/utils/windowRestoreBounds.ts
Syngnat 9613b2a8eb 🐛 fix(window): 修正启动窗口恢复到不可见区域
- 启动恢复普通窗口时先校验持久化 bounds 是否仍与可视区域相交
- 完全掉出可视区域时自动回正并回写新的窗口位置到 store
- 补充窗口恢复 helper 回归测试并验证前端构建通过

Fixes #384
2026-04-17 18:19:42 +08:00

48 lines
1.4 KiB
TypeScript

export type WindowRestoreBounds = {
width: number;
height: number;
x: number;
y: number;
};
type VisibleViewport = {
availWidth: number;
availHeight: number;
availLeft?: number;
availTop?: number;
};
const MIN_VISIBLE_WIDTH = 160;
const MIN_VISIBLE_HEIGHT = 120;
export const resolveVisibleStartupWindowBounds = (
bounds: WindowRestoreBounds,
viewport: VisibleViewport,
): WindowRestoreBounds => {
const visibleWidth = Math.trunc(Number(viewport.availWidth) || 0);
const visibleHeight = Math.trunc(Number(viewport.availHeight) || 0);
if (visibleWidth <= 0 || visibleHeight <= 0) {
return bounds;
}
const visibleLeft = Math.trunc(Number(viewport.availLeft) || 0);
const visibleTop = Math.trunc(Number(viewport.availTop) || 0);
const visibleRight = visibleLeft + visibleWidth;
const visibleBottom = visibleTop + visibleHeight;
const overlapWidth = Math.min(bounds.x + bounds.width, visibleRight) - Math.max(bounds.x, visibleLeft);
const overlapHeight = Math.min(bounds.y + bounds.height, visibleBottom) - Math.max(bounds.y, visibleTop);
if (
overlapWidth >= Math.min(MIN_VISIBLE_WIDTH, bounds.width) &&
overlapHeight >= Math.min(MIN_VISIBLE_HEIGHT, bounds.height)
) {
return bounds;
}
return {
...bounds,
x: visibleLeft + Math.max(0, Math.trunc((visibleWidth - bounds.width) / 2)),
y: visibleTop + Math.max(0, Math.trunc((visibleHeight - bounds.height) / 2)),
};
};