fix(ui): rails take obstacles across the middle together, so their order never picks the side

Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
Sameh KhamisandManjunath Paparaju committed 2026-11-18 22:39:29 -08:00
1 parent 31a7b6a0a5
commit feedd88614
7 files changed
+99 -45

No files matched your search

+21 -15
View File
@@ -118,7 +118,10 @@ export function layoutLeftPanelRail({
let safeTop = baseTop;
let safeBottom = viewportHeight - baseBottomInset;
const bottomObstacles = [];
const straddling = [];
// Obstacles across the midpoint all cover it, so together they cover one
// span: from the highest top to the lowest bottom.
let spanTop = Infinity;
let spanBottom = -Infinity;
for (const obstacle of obstacles) {
if (stack.contains(obstacle)) continue;
@@ -152,28 +155,31 @@ export function layoutLeftPanelRail({
const obstacleBottom = rect.bottom + safeGap;
obstacleSafeTop = Math.max(obstacleSafeTop, obstacleBottom);
safeTop = Math.max(safeTop, obstacleBottom);
} else if (rect.top >= baseTop) {
bottomObstacles.push({ top: rect.top });
} else if (
rect.top < viewportHeight * 0.5 &&
rect.bottom > viewportHeight * 0.5
) {
spanTop = Math.min(spanTop, rect.top);
spanBottom = Math.max(spanBottom, rect.bottom);
} else {
straddling.push(rect);
bottomObstacles.push({ top: rect.top });
}
}
// An obstacle from above the corridor across the midpoint (common in short
// windows) still limits it: the panels go on whichever side of it leaves
// more room.
for (const rect of straddling) {
const roomAbove = rect.top - safeGap - obstacleSafeTop;
// The panels go on whichever side of that span leaves more room (common in
// short windows), above it on a tie. With no room on either side the
// corridor stays as it is.
if (spanBottom > spanTop) {
const roomAbove = spanTop - safeGap - obstacleSafeTop;
const lowerBottom = resolveLeftStackBottomBoundary({
baseBottom: safeBottom,
obstacles: bottomObstacles,
safeGap,
});
const roomBelow = lowerBottom - (rect.bottom + safeGap);
if (roomBelow > roomAbove) {
const obstacleBottom = rect.bottom + safeGap;
obstacleSafeTop = Math.max(obstacleSafeTop, obstacleBottom);
safeTop = Math.max(safeTop, obstacleBottom);
} else bottomObstacles.push({ top: rect.top });
const roomBelow = lowerBottom - (spanBottom + safeGap);
if (roomBelow > roomAbove && roomBelow > 0) {
obstacleSafeTop = Math.max(obstacleSafeTop, spanBottom + safeGap);
safeTop = Math.max(safeTop, spanBottom + safeGap);
} else if (roomAbove > 0) bottomObstacles.push({ top: spanTop });
}
safeBottom = resolveLeftStackBottomBoundary({
baseBottom: safeBottom,
+17 -10
View File
@@ -2,7 +2,8 @@
* Resolve a vertically centered panel slot inside a HUD rail while respecting
* live rectangles that intersect that rail. A rectangle above the viewport
* center lowers the top, one below it raises the bottom, and one across the
* center does whichever leaves the band more room.
* center puts the band on whichever side of it has more room, or leaves the
* band as it is when neither side has any.
*/
export function resolveHudRailLayout({
viewportHeight,
@@ -31,7 +32,10 @@ export function resolveHudRailLayout({
const midpoint = viewportHeight * 0.5;
let safeTop = Math.max(0, baseTop);
let safeBottom = Math.min(viewportHeight, baseBottom);
const straddling = [];
// Every obstacle across the midpoint covers it, so together they cover one
// span: from the highest top to the lowest bottom.
let spanTop = Infinity;
let spanBottom = -Infinity;
for (const rect of obstacles) {
if (
@@ -47,15 +51,18 @@ export function resolveHudRailLayout({
if (rect.bottom <= midpoint) safeTop = Math.max(safeTop, rect.bottom + gap);
else if (rect.top >= midpoint)
safeBottom = Math.min(safeBottom, rect.top - gap);
else straddling.push(rect);
else {
spanTop = Math.min(spanTop, rect.top);
spanBottom = Math.max(spanBottom, rect.bottom);
}
}
// An obstacle across the midpoint (common in short windows) still limits
// the band: the band goes on whichever side of it leaves more room.
for (const rect of straddling) {
const roomAbove = rect.top - gap - safeTop;
const roomBelow = safeBottom - (rect.bottom + gap);
if (roomBelow > roomAbove) safeTop = Math.max(safeTop, rect.bottom + gap);
else safeBottom = Math.min(safeBottom, rect.top - gap);
// The band goes on whichever side of that span leaves more room, above it
// on a tie. With no room on either side the band stays as it is.
if (spanBottom > spanTop) {
const roomAbove = spanTop - gap - safeTop;
const roomBelow = safeBottom - (spanBottom + gap);
if (roomBelow > roomAbove && roomBelow > 0) safeTop = spanBottom + gap;
else if (roomAbove > 0) safeBottom = spanTop - gap;
}
safeBottom = Math.max(safeTop, safeBottom);
+27 -7
View File
@@ -220,22 +220,42 @@ test('left corridor respects a lower obstacle but ignores an obstacle hidden by
});
test('in a short window the left corridor stays clear of an obstacle across the midpoint', () => {
// 480px window, midpoint 240. Neither card is wholly above or below it.
// 480px window, midpoint 240.
const f = fixture('left');
f.options.windowRef.innerHeight = 480;
const low = element('card', { top: 100, height: 200 });
f.options.obstacles = [low];
f.options.obstacles = [element('card', { top: 100, height: 200 })];
f.run();
// More room below the card than above it: the corridor starts under it.
assert(Number(f.stack.dataset.safeTopPct) * 4.8 >= 300);
assert(Number(f.stack.dataset.safeBottomPct) * 4.8 <= 480);
const high = element('card', { top: 200, height: 200 });
f.options.obstacles = [high];
// A card starting below the corridor's usual top (26%, 124.8px) that
// crosses the midpoint counts the same way: more room below it.
f.options.obstacles = [element('card', { top: 130, height: 120 })];
f.run();
assert(Number(f.stack.dataset.safeTopPct) * 4.8 >= 250);
// More room above it: the corridor ends over it.
f.options.obstacles = [element('card', { top: 200, height: 250 })];
f.run();
assert(Number(f.stack.dataset.safeBottomPct) * 4.8 <= 200);
assert(Number(f.stack.dataset.safeTopPct) * 4.8 >= 0);
});
test('several cards across the midpoint give the left corridor the same room in any order', () => {
const f = fixture('left');
f.options.windowRef.innerHeight = 480;
const a = element('card', { top: 100, height: 160 });
const b = element('card', { top: 110, height: 360 });
f.options.obstacles = [a, b];
f.run();
const first = [f.stack.dataset.safeTopPct, f.stack.dataset.safeBottomPct];
f.options.obstacles = [b, a];
f.run();
assert.deepEqual(
[f.stack.dataset.safeTopPct, f.stack.dataset.safeBottomPct],
first,
);
// Together they cover 100 to 470: the corridor ends above them.
assert(Number(first[1]) * 4.8 <= 100);
});
test('a centered timeline only shortens the corridor when it reaches a laid-out panel', () => {