mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-27 01:19:25 +08:00
fix(panel): eliminate tooltip and highlight flicker across heatmap cell gaps
This commit is contained in:
+105
-4
@@ -5722,6 +5722,8 @@ function createTrendAreaChart() {
|
||||
let currentPts = [];
|
||||
let currentIsHeatmap = false;
|
||||
let currentHeatmapWeeks = 0;
|
||||
let currentHeatmapFirstDow = 0;
|
||||
let currentHeatmapGapPx = 2;
|
||||
let currentCellEls = [];
|
||||
let tickElsByIdx = new Map();
|
||||
let hoveredIdx = -1;
|
||||
@@ -5886,21 +5888,117 @@ function createTrendAreaChart() {
|
||||
showLineHoverAtIndex(idx);
|
||||
}
|
||||
|
||||
function findHeatmapCellFromEvent(e) {
|
||||
function findDirectHeatmapCell(e) {
|
||||
let cur = e?.target || null;
|
||||
while (cur && cur !== hmGridEl) {
|
||||
const idxAttr = cur.getAttribute?.('data-slot-idx');
|
||||
if (idxAttr !== null && idxAttr !== undefined && idxAttr !== '') {
|
||||
return { idx: Number(idxAttr), cellEl: cur };
|
||||
const idx = Number(idxAttr);
|
||||
if (Number.isFinite(idx) && idx >= 0 && idx < currentSlots.length) {
|
||||
return { idx, cellEl: cur };
|
||||
}
|
||||
}
|
||||
cur = cur.parentNode || null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function resolveHeatmapCellFromPointer(e) {
|
||||
const directHit = findDirectHeatmapCell(e);
|
||||
if (directHit) return directHit;
|
||||
|
||||
const clientX = Number(e?.clientX);
|
||||
const clientY = Number(e?.clientY);
|
||||
if (!Number.isFinite(clientX) || !Number.isFinite(clientY)) {
|
||||
if (hoveredIdx >= 0 && hoveredIdx < currentSlots.length && currentCellEls[hoveredIdx]) {
|
||||
return { idx: hoveredIdx, cellEl: currentCellEls[hoveredIdx] };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const rect = hmGridEl.getBoundingClientRect?.();
|
||||
if (!rect || !(rect.width > 0) || !(rect.height > 0) || currentHeatmapWeeks <= 0) {
|
||||
if (hoveredIdx >= 0 && hoveredIdx < currentSlots.length && currentCellEls[hoveredIdx]) {
|
||||
return { idx: hoveredIdx, cellEl: currentCellEls[hoveredIdx] };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const W = currentHeatmapWeeks;
|
||||
const G = currentHeatmapGapPx;
|
||||
const relX = clientX - rect.left;
|
||||
const relY = clientY - rect.top;
|
||||
if (
|
||||
relX < -(G + 1) ||
|
||||
relX > rect.width + G + 1 ||
|
||||
relY < -(G + 1) ||
|
||||
relY > rect.height + G + 1
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const pitchX = (rect.width + G) / W;
|
||||
const pitchY = (rect.height + G) / 7;
|
||||
const cellW = Math.max(1, pitchX - G);
|
||||
const cellH = Math.max(1, pitchY - G);
|
||||
|
||||
function distanceToCell(c, r) {
|
||||
const x0 = c * pitchX;
|
||||
const y0 = r * pitchY;
|
||||
const dx = relX < x0 ? x0 - relX : relX > x0 + cellW ? relX - (x0 + cellW) : 0;
|
||||
const dy = relY < y0 ? y0 - relY : relY > y0 + cellH ? relY - (y0 + cellH) : 0;
|
||||
return { chebyshev: Math.max(dx, dy), distSq: dx * dx + dy * dy };
|
||||
}
|
||||
|
||||
// 指针滑入当前悬停格四周的空隙(gap)时保持原高亮,彻底消除跨格空隙闪烁与边缘微抖
|
||||
if (hoveredIdx >= 0 && hoveredIdx < currentSlots.length && currentCellEls[hoveredIdx]) {
|
||||
const curGridPos = currentHeatmapFirstDow + hoveredIdx;
|
||||
const curCol = Math.floor(curGridPos / 7);
|
||||
const curRow = curGridPos % 7;
|
||||
if (distanceToCell(curCol, curRow).chebyshev <= G + 0.75) {
|
||||
return { idx: hoveredIdx, cellEl: currentCellEls[hoveredIdx] };
|
||||
}
|
||||
}
|
||||
|
||||
// 快速扫过或初始落入格子间隙时,按网格步距吸附到最近的有效日期格
|
||||
const approxCol = Math.max(0, Math.min(W - 1, Math.floor((relX + G * 0.5) / pitchX)));
|
||||
const approxRow = Math.max(0, Math.min(6, Math.floor((relY + G * 0.5) / pitchY)));
|
||||
const directSlotIdx = approxCol * 7 + approxRow - currentHeatmapFirstDow;
|
||||
if (
|
||||
directSlotIdx >= 0 &&
|
||||
directSlotIdx < currentSlots.length &&
|
||||
currentCellEls[directSlotIdx]
|
||||
) {
|
||||
return { idx: directSlotIdx, cellEl: currentCellEls[directSlotIdx] };
|
||||
}
|
||||
|
||||
// 首周补位或末周未来空白边缘:仅当仍在相邻有效格的间隙光晕内才吸附,深入纯空白角则清空
|
||||
let bestIdx = -1;
|
||||
let bestDistSq = Infinity;
|
||||
for (let dc = -1; dc <= 1; dc++) {
|
||||
const c = approxCol + dc;
|
||||
if (c < 0 || c >= W) continue;
|
||||
for (let dr = -1; dr <= 1; dr++) {
|
||||
const r = approxRow + dr;
|
||||
if (r < 0 || r > 6) continue;
|
||||
const sIdx = c * 7 + r - currentHeatmapFirstDow;
|
||||
if (sIdx < 0 || sIdx >= currentSlots.length || !currentCellEls[sIdx]) continue;
|
||||
const d = distanceToCell(c, r);
|
||||
if (d.chebyshev <= G + 1.5 && d.distSq < bestDistSq) {
|
||||
bestDistSq = d.distSq;
|
||||
bestIdx = sIdx;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bestIdx >= 0) {
|
||||
return { idx: bestIdx, cellEl: currentCellEls[bestIdx] };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function handleHeatmapPointer(e) {
|
||||
if (!currentIsHeatmap || !currentSlots.length) return;
|
||||
const hit = findHeatmapCellFromEvent(e);
|
||||
const hit = resolveHeatmapCellFromPointer(e);
|
||||
if (!hit || !Number.isFinite(hit.idx)) {
|
||||
clearHover();
|
||||
return;
|
||||
@@ -6037,10 +6135,13 @@ function createTrendAreaChart() {
|
||||
: 0;
|
||||
const totalCells = firstDow + slots.length;
|
||||
const totalWeeks = Math.max(1, Math.ceil(totalCells / 7));
|
||||
const gapPx = totalWeeks > 42 ? 1.5 : 2;
|
||||
currentHeatmapWeeks = totalWeeks;
|
||||
currentHeatmapFirstDow = firstDow;
|
||||
currentHeatmapGapPx = gapPx;
|
||||
|
||||
patchStyleVar(hmBodyEl, '--hm-weeks', String(totalWeeks));
|
||||
patchStyleVar(hmBodyEl, '--hm-gap', totalWeeks > 42 ? '1.5px' : '2px');
|
||||
patchStyleVar(hmBodyEl, '--hm-gap', `${gapPx}px`);
|
||||
|
||||
// 重建顶部对齐月份标签
|
||||
while (hmMonthsEl.firstChild) hmMonthsEl.removeChild(hmMonthsEl.firstChild);
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>用量监控</title>
|
||||
<link rel="stylesheet" href="/styles.css?v=1794916257143">
|
||||
<link rel="stylesheet" href="/styles.css?v=1794918424113">
|
||||
<script src="/preload.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app-root"></div>
|
||||
<script src="/app.js?v=1794916257143"></script>
|
||||
<script src="/app.js?v=1794918424113"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -349,6 +349,8 @@ export function createTrendAreaChart() {
|
||||
let currentPts = [];
|
||||
let currentIsHeatmap = false;
|
||||
let currentHeatmapWeeks = 0;
|
||||
let currentHeatmapFirstDow = 0;
|
||||
let currentHeatmapGapPx = 2;
|
||||
let currentCellEls = [];
|
||||
let tickElsByIdx = new Map();
|
||||
let hoveredIdx = -1;
|
||||
@@ -513,21 +515,117 @@ export function createTrendAreaChart() {
|
||||
showLineHoverAtIndex(idx);
|
||||
}
|
||||
|
||||
function findHeatmapCellFromEvent(e) {
|
||||
function findDirectHeatmapCell(e) {
|
||||
let cur = e?.target || null;
|
||||
while (cur && cur !== hmGridEl) {
|
||||
const idxAttr = cur.getAttribute?.('data-slot-idx');
|
||||
if (idxAttr !== null && idxAttr !== undefined && idxAttr !== '') {
|
||||
return { idx: Number(idxAttr), cellEl: cur };
|
||||
const idx = Number(idxAttr);
|
||||
if (Number.isFinite(idx) && idx >= 0 && idx < currentSlots.length) {
|
||||
return { idx, cellEl: cur };
|
||||
}
|
||||
}
|
||||
cur = cur.parentNode || null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function resolveHeatmapCellFromPointer(e) {
|
||||
const directHit = findDirectHeatmapCell(e);
|
||||
if (directHit) return directHit;
|
||||
|
||||
const clientX = Number(e?.clientX);
|
||||
const clientY = Number(e?.clientY);
|
||||
if (!Number.isFinite(clientX) || !Number.isFinite(clientY)) {
|
||||
if (hoveredIdx >= 0 && hoveredIdx < currentSlots.length && currentCellEls[hoveredIdx]) {
|
||||
return { idx: hoveredIdx, cellEl: currentCellEls[hoveredIdx] };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const rect = hmGridEl.getBoundingClientRect?.();
|
||||
if (!rect || !(rect.width > 0) || !(rect.height > 0) || currentHeatmapWeeks <= 0) {
|
||||
if (hoveredIdx >= 0 && hoveredIdx < currentSlots.length && currentCellEls[hoveredIdx]) {
|
||||
return { idx: hoveredIdx, cellEl: currentCellEls[hoveredIdx] };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const W = currentHeatmapWeeks;
|
||||
const G = currentHeatmapGapPx;
|
||||
const relX = clientX - rect.left;
|
||||
const relY = clientY - rect.top;
|
||||
if (
|
||||
relX < -(G + 1) ||
|
||||
relX > rect.width + G + 1 ||
|
||||
relY < -(G + 1) ||
|
||||
relY > rect.height + G + 1
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const pitchX = (rect.width + G) / W;
|
||||
const pitchY = (rect.height + G) / 7;
|
||||
const cellW = Math.max(1, pitchX - G);
|
||||
const cellH = Math.max(1, pitchY - G);
|
||||
|
||||
function distanceToCell(c, r) {
|
||||
const x0 = c * pitchX;
|
||||
const y0 = r * pitchY;
|
||||
const dx = relX < x0 ? x0 - relX : relX > x0 + cellW ? relX - (x0 + cellW) : 0;
|
||||
const dy = relY < y0 ? y0 - relY : relY > y0 + cellH ? relY - (y0 + cellH) : 0;
|
||||
return { chebyshev: Math.max(dx, dy), distSq: dx * dx + dy * dy };
|
||||
}
|
||||
|
||||
// 指针滑入当前悬停格四周的空隙(gap)时保持原高亮,彻底消除跨格空隙闪烁与边缘微抖
|
||||
if (hoveredIdx >= 0 && hoveredIdx < currentSlots.length && currentCellEls[hoveredIdx]) {
|
||||
const curGridPos = currentHeatmapFirstDow + hoveredIdx;
|
||||
const curCol = Math.floor(curGridPos / 7);
|
||||
const curRow = curGridPos % 7;
|
||||
if (distanceToCell(curCol, curRow).chebyshev <= G + 0.75) {
|
||||
return { idx: hoveredIdx, cellEl: currentCellEls[hoveredIdx] };
|
||||
}
|
||||
}
|
||||
|
||||
// 快速扫过或初始落入格子间隙时,按网格步距吸附到最近的有效日期格
|
||||
const approxCol = Math.max(0, Math.min(W - 1, Math.floor((relX + G * 0.5) / pitchX)));
|
||||
const approxRow = Math.max(0, Math.min(6, Math.floor((relY + G * 0.5) / pitchY)));
|
||||
const directSlotIdx = approxCol * 7 + approxRow - currentHeatmapFirstDow;
|
||||
if (
|
||||
directSlotIdx >= 0 &&
|
||||
directSlotIdx < currentSlots.length &&
|
||||
currentCellEls[directSlotIdx]
|
||||
) {
|
||||
return { idx: directSlotIdx, cellEl: currentCellEls[directSlotIdx] };
|
||||
}
|
||||
|
||||
// 首周补位或末周未来空白边缘:仅当仍在相邻有效格的间隙光晕内才吸附,深入纯空白角则清空
|
||||
let bestIdx = -1;
|
||||
let bestDistSq = Infinity;
|
||||
for (let dc = -1; dc <= 1; dc++) {
|
||||
const c = approxCol + dc;
|
||||
if (c < 0 || c >= W) continue;
|
||||
for (let dr = -1; dr <= 1; dr++) {
|
||||
const r = approxRow + dr;
|
||||
if (r < 0 || r > 6) continue;
|
||||
const sIdx = c * 7 + r - currentHeatmapFirstDow;
|
||||
if (sIdx < 0 || sIdx >= currentSlots.length || !currentCellEls[sIdx]) continue;
|
||||
const d = distanceToCell(c, r);
|
||||
if (d.chebyshev <= G + 1.5 && d.distSq < bestDistSq) {
|
||||
bestDistSq = d.distSq;
|
||||
bestIdx = sIdx;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bestIdx >= 0) {
|
||||
return { idx: bestIdx, cellEl: currentCellEls[bestIdx] };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function handleHeatmapPointer(e) {
|
||||
if (!currentIsHeatmap || !currentSlots.length) return;
|
||||
const hit = findHeatmapCellFromEvent(e);
|
||||
const hit = resolveHeatmapCellFromPointer(e);
|
||||
if (!hit || !Number.isFinite(hit.idx)) {
|
||||
clearHover();
|
||||
return;
|
||||
@@ -664,10 +762,13 @@ export function createTrendAreaChart() {
|
||||
: 0;
|
||||
const totalCells = firstDow + slots.length;
|
||||
const totalWeeks = Math.max(1, Math.ceil(totalCells / 7));
|
||||
const gapPx = totalWeeks > 42 ? 1.5 : 2;
|
||||
currentHeatmapWeeks = totalWeeks;
|
||||
currentHeatmapFirstDow = firstDow;
|
||||
currentHeatmapGapPx = gapPx;
|
||||
|
||||
patchStyleVar(hmBodyEl, '--hm-weeks', String(totalWeeks));
|
||||
patchStyleVar(hmBodyEl, '--hm-gap', totalWeeks > 42 ? '1.5px' : '2px');
|
||||
patchStyleVar(hmBodyEl, '--hm-gap', `${gapPx}px`);
|
||||
|
||||
// 重建顶部对齐月份标签
|
||||
while (hmMonthsEl.firstChild) hmMonthsEl.removeChild(hmMonthsEl.firstChild);
|
||||
|
||||
@@ -511,6 +511,7 @@
|
||||
grid-auto-flow: column;
|
||||
grid-auto-columns: var(--hm-cell);
|
||||
gap: var(--hm-gap);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.trend-hm-cell {
|
||||
|
||||
@@ -2361,6 +2361,7 @@ html[data-standalone="true"] .app-shell {
|
||||
grid-auto-flow: column;
|
||||
grid-auto-columns: var(--hm-cell);
|
||||
gap: var(--hm-gap);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.trend-hm-cell {
|
||||
|
||||
@@ -2235,6 +2235,47 @@ test('Compiled app.js mounts dual Shadow DOM hosts, tracks /c/<uuid> routes, pro
|
||||
assert.equal(trendTooltipEl.hasAttribute('hidden'), false, 'Tooltip opens on heatmap cell hover');
|
||||
assert.equal(activeCell.classList.contains('is-hovered'), true);
|
||||
assert.match(trendTooltipEl.querySelector('.trend-tip-tokens').textContent, /100\.0k tok/);
|
||||
|
||||
// Crossing 2px inter-cell gaps keeps highlight/tooltip steady without flickering, and fast jumps snap to nearest cell
|
||||
const hmBodyEl = heatmapStageEl.querySelector('.trend-heatmap-body');
|
||||
const hmTotalWeeks = Number(hmBodyEl.style.getPropertyValue('--hm-weeks'));
|
||||
const origGridRectFn = hmGridEl.getBoundingClientRect;
|
||||
hmGridEl.getBoundingClientRect = () => ({
|
||||
left: 100,
|
||||
top: 200,
|
||||
width: hmTotalWeeks * 10 - 2,
|
||||
height: 7 * 10 - 2,
|
||||
right: 100 + hmTotalWeeks * 10 - 2,
|
||||
bottom: 200 + 7 * 10 - 2,
|
||||
});
|
||||
const activeCol = Number(activeCell.getAttribute('data-hm-col'));
|
||||
const activeRow = Number(activeCell.getAttribute('data-hm-row'));
|
||||
hmGridEl.dispatchEvent({ type: 'mouseover', target: hmGridEl });
|
||||
assert.equal(trendTooltipEl.hasAttribute('hidden'), false, 'Container mouseover in gap does not blink tooltip');
|
||||
hmGridEl.dispatchEvent({
|
||||
type: 'mousemove',
|
||||
target: hmGridEl,
|
||||
clientX: 100 + activeCol * 10 + 9,
|
||||
clientY: 200 + activeRow * 10 + 4,
|
||||
});
|
||||
assert.equal(trendTooltipEl.hasAttribute('hidden'), false, 'Tooltip stays visible in 2px gap beside active cell');
|
||||
assert.equal(activeCell.classList.contains('is-hovered'), true, 'Active cell stays highlighted across bordering gap');
|
||||
|
||||
const snappedCol5Cell = realDayCells.find(
|
||||
(c) => c.getAttribute('data-hm-col') === '5' && c.getAttribute('data-hm-row') === '2'
|
||||
);
|
||||
assert.ok(snappedCol5Cell, 'Target cell at col=5,row=2 exists');
|
||||
hmGridEl.dispatchEvent({
|
||||
type: 'mousemove',
|
||||
target: hmGridEl,
|
||||
clientX: 100 + 5 * 10 + 8.4,
|
||||
clientY: 200 + 2 * 10 + 4,
|
||||
});
|
||||
assert.equal(trendTooltipEl.hasAttribute('hidden'), false, 'Tooltip stays visible when snapping across distant gap');
|
||||
assert.equal(activeCell.classList.contains('is-hovered'), false);
|
||||
assert.equal(snappedCol5Cell.classList.contains('is-hovered'), true, 'Fast pointer jump into distant gap snaps to nearest cell');
|
||||
hmGridEl.getBoundingClientRect = origGridRectFn;
|
||||
|
||||
hmGridEl.dispatchEvent({ type: 'mouseleave' });
|
||||
assert.equal(trendTooltipEl.hasAttribute('hidden'), true, 'Tooltip hides on mouseleave');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user