mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-26 17:09:28 +08:00
fix(panel): add popup collision clamping and modern-ui capsule odometer
This commit is contained in:
@@ -202,3 +202,194 @@ export function patchKeyedList(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const CLIPPING_OVERFLOW_RE = /(?:hidden|clip|auto|scroll)/i;
|
||||
|
||||
/**
|
||||
* 解析浮层所在的可视裁剪边界(兼容 Shadow DOM 悬浮窗 `.wm-popover-panel`、
|
||||
* 侧栏 `.app-shell`、自定义滚动容器 `.custom-scroll-content` 与窗口视口)。
|
||||
*
|
||||
* @param {HTMLElement} anchorRootEl
|
||||
* @returns {{ left: number, top: number, right: number, bottom: number, width: number, height: number } | null}
|
||||
*/
|
||||
function resolvePopupBoundaryRect(anchorRootEl) {
|
||||
if (!anchorRootEl) return null;
|
||||
const winW = typeof window !== 'undefined' ? Number(window.innerWidth || 0) : 0;
|
||||
const winH = typeof window !== 'undefined' ? Number(window.innerHeight || 0) : 0;
|
||||
let boundLeft = 0;
|
||||
let boundTop = 0;
|
||||
let boundRight = winW > 0 ? winW : Infinity;
|
||||
let boundBottom = winH > 0 ? winH : Infinity;
|
||||
let hasBoundary = winW > 0 && winH > 0;
|
||||
|
||||
const getStyle =
|
||||
typeof window !== 'undefined' && typeof window.getComputedStyle === 'function'
|
||||
? window.getComputedStyle
|
||||
: null;
|
||||
|
||||
let cur = anchorRootEl.parentNode || null;
|
||||
while (cur) {
|
||||
if (cur.nodeType === 1 || typeof cur.getBoundingClientRect === 'function') {
|
||||
const cls = cur.classList;
|
||||
const isShell = Boolean(
|
||||
cls?.contains?.('wm-popover-panel') ||
|
||||
cls?.contains?.('wm-multi-panel') ||
|
||||
cls?.contains?.('app-shell') ||
|
||||
cls?.contains?.('custom-scroll-host') ||
|
||||
cls?.contains?.('custom-scroll-content')
|
||||
);
|
||||
let clipsX = isShell;
|
||||
let clipsY = isShell;
|
||||
if (!isShell && getStyle) {
|
||||
try {
|
||||
const cs = getStyle(cur);
|
||||
if (cs) {
|
||||
clipsX = CLIPPING_OVERFLOW_RE.test(cs.overflowX || cs.overflow || '');
|
||||
clipsY = CLIPPING_OVERFLOW_RE.test(cs.overflowY || cs.overflow || '');
|
||||
}
|
||||
} catch {
|
||||
// 忽略 Mock DOM 样式查询异常
|
||||
}
|
||||
}
|
||||
if (clipsX || clipsY) {
|
||||
const r = cur.getBoundingClientRect?.();
|
||||
if (r && r.width > 0 && r.height > 0) {
|
||||
hasBoundary = true;
|
||||
if (clipsX) {
|
||||
boundLeft = Math.max(boundLeft, r.left);
|
||||
boundRight = Math.min(boundRight, r.right);
|
||||
}
|
||||
if (clipsY) {
|
||||
boundTop = Math.max(boundTop, r.top);
|
||||
boundBottom = Math.min(boundBottom, r.bottom);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 到达 ShadowRoot 或文档顶层后停止,避免误受宿主页面外层容器干扰
|
||||
if (!cur.parentNode) break;
|
||||
cur = cur.parentNode;
|
||||
}
|
||||
|
||||
if (!hasBoundary || !Number.isFinite(boundRight) || !Number.isFinite(boundBottom)) {
|
||||
return null;
|
||||
}
|
||||
const width = Math.max(0, boundRight - boundLeft);
|
||||
const height = Math.max(0, boundBottom - boundTop);
|
||||
if (width <= 0 || height <= 0) return null;
|
||||
return { left: boundLeft, top: boundTop, right: boundRight, bottom: boundBottom, width, height };
|
||||
}
|
||||
|
||||
/**
|
||||
* 为锚定在触发器上的绝对定位下拉浮层执行水平/垂直碰撞检测与边界夹紧:
|
||||
* - 水平方向优先左对齐,右侧溢出时切右对齐,仍越界则直接夹紧在容器可视安全区内;
|
||||
* - 垂直方向底部空间不足且上方更宽裕时自动向上翻转(追加 `.is-above`),并按可用空间收紧 `max-height`。
|
||||
*
|
||||
* @param {HTMLElement} rootEl 定位基准容器 (`position: relative`)
|
||||
* @param {HTMLElement} triggerEl 触发按钮
|
||||
* @param {HTMLElement} popupEl 下拉浮层 (`position: absolute`)
|
||||
* @param {{ margin?: number, gap?: number, maxHeight?: number, minFlipSpace?: number }} [opts]
|
||||
*/
|
||||
export function positionAnchoredPopup(
|
||||
rootEl,
|
||||
triggerEl,
|
||||
popupEl,
|
||||
{ margin = 6, gap = 4, maxHeight = 220, minFlipSpace = 140 } = {}
|
||||
) {
|
||||
if (!rootEl || !triggerEl || !popupEl || !popupEl.style) return;
|
||||
if (popupEl.hasAttribute?.('hidden')) return;
|
||||
|
||||
const rootRect = rootEl.getBoundingClientRect?.();
|
||||
const triggerRect = triggerEl.getBoundingClientRect?.();
|
||||
if (
|
||||
!rootRect ||
|
||||
!triggerRect ||
|
||||
!(triggerRect.width > 0 && triggerRect.height > 0)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const bound = resolvePopupBoundaryRect(rootEl);
|
||||
if (!bound) return;
|
||||
|
||||
// 清除旧的动态夹紧值以便读取自然布局宽度
|
||||
popupEl.style.left = '';
|
||||
popupEl.style.right = '';
|
||||
popupEl.style.top = '';
|
||||
popupEl.style.bottom = '';
|
||||
popupEl.style.minWidth = '';
|
||||
popupEl.style.maxWidth = '';
|
||||
popupEl.style.maxHeight = '';
|
||||
|
||||
const availWidth = Math.max(120, Math.floor(bound.width - margin * 2));
|
||||
popupEl.style.maxWidth = `${availWidth}px`;
|
||||
|
||||
const rawRect = popupEl.getBoundingClientRect?.();
|
||||
// 优先读取 offsetWidth,避免入场动画 scale(0.975) 导致测宽偏小
|
||||
const naturalW = Math.max(
|
||||
Number(popupEl.offsetWidth || 0),
|
||||
Math.ceil(Number(rawRect?.width || 0))
|
||||
);
|
||||
if (naturalW > availWidth) {
|
||||
popupEl.style.minWidth = '0px';
|
||||
}
|
||||
const popupW = Math.min(availWidth, naturalW > 0 ? naturalW : Math.max(triggerRect.width, 210));
|
||||
|
||||
const safeLeft = bound.left + margin;
|
||||
const safeRight = bound.right - margin;
|
||||
const rawLeft = Number(rawRect?.left);
|
||||
const rawRight = Number(rawRect?.right);
|
||||
const hasRawBox = Number.isFinite(rawLeft) && Number.isFinite(rawRight) && rawRight > rawLeft;
|
||||
// 若入场缩放动画(如 scale(0.975))正在首帧生效,按对称补偿还原终态边界再判断是否越界
|
||||
const scalePadX = hasRawBox ? Math.max(0, (popupW - (rawRight - rawLeft)) / 2) : 0;
|
||||
const naturalLeft = hasRawBox ? rawLeft - scalePadX : triggerRect.left;
|
||||
const naturalRight = hasRawBox
|
||||
? Math.max(rawRight + scalePadX, naturalLeft + popupW)
|
||||
: naturalLeft + popupW;
|
||||
const fitsAsDefault = hasRawBox && naturalLeft >= safeLeft && naturalRight <= safeRight;
|
||||
|
||||
if (!fitsAsDefault) {
|
||||
let targetLeft = triggerRect.left;
|
||||
if (targetLeft + popupW > safeRight) {
|
||||
const rightAlignedLeft = triggerRect.right - popupW;
|
||||
if (rightAlignedLeft >= safeLeft) {
|
||||
targetLeft = rightAlignedLeft;
|
||||
} else {
|
||||
targetLeft = Math.max(safeLeft, safeRight - popupW);
|
||||
}
|
||||
}
|
||||
if (targetLeft < safeLeft) {
|
||||
targetLeft = safeLeft;
|
||||
}
|
||||
|
||||
const offsetLeft = Math.round(targetLeft - rootRect.left);
|
||||
popupEl.style.left = `${offsetLeft}px`;
|
||||
popupEl.style.right = 'auto';
|
||||
}
|
||||
|
||||
const spaceBelow = Math.max(0, bound.bottom - triggerRect.bottom - gap - margin);
|
||||
const spaceAbove = Math.max(0, triggerRect.top - bound.top - gap - margin);
|
||||
const naturalH = Math.max(
|
||||
Number(popupEl.scrollHeight || 0),
|
||||
Number(popupEl.offsetHeight || 0),
|
||||
Math.ceil(Number(rawRect?.height || 0))
|
||||
);
|
||||
const desiredH = maxHeight > 0 ? Math.min(maxHeight, naturalH || maxHeight) : naturalH || 240;
|
||||
const shouldOpenAbove =
|
||||
spaceBelow < Math.min(desiredH, minFlipSpace) && spaceAbove > spaceBelow;
|
||||
|
||||
patchClasses(popupEl, { 'is-above': shouldOpenAbove });
|
||||
if (shouldOpenAbove) {
|
||||
popupEl.style.top = 'auto';
|
||||
popupEl.style.bottom = `calc(100% + ${gap}px)`;
|
||||
if (maxHeight > 0) {
|
||||
popupEl.style.maxHeight = `${Math.max(88, Math.floor(Math.min(maxHeight, spaceAbove)))}px`;
|
||||
}
|
||||
} else {
|
||||
popupEl.style.top = `calc(100% + ${gap}px)`;
|
||||
popupEl.style.bottom = 'auto';
|
||||
if (maxHeight > 0) {
|
||||
popupEl.style.maxHeight = `${Math.max(88, Math.floor(Math.min(maxHeight, spaceBelow)))}px`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user