fix(gallery): pack placeholder covers at the active boxart ratio

The grid flow-packs rows in JS from each card's real width
(cardHeight * coverRatio), and rows render nowrap with flex-shrink: 0 —
so an underestimated width overflows the viewport instead of wrapping.
Ratios are measured on cover load, but a rom with no cover paints its
placeholder at the boxart style's ratio and never measures, so the
packer's hardcoded 2/3 under-reserved its width and let an extra card
into the row.

The packer now takes the style ratio as its fallback for any position
without a measured cover, which also covers the pre-load paint of every
card in a non-2/3 gallery.

Fixes #3984

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-07-28 22:36:39 -04:00
parent 963e4cab48
commit e8aa2f5afd
3 changed files with 79 additions and 4 deletions

View File

@@ -302,6 +302,9 @@ const { columns, usableWidth } = useResponsiveColumns(sectionEl, {
// Fallback cover ratio (boxart style) — the per-card `--r-cover-ratio` seed
// before GameCover measures the real image, plus the bootstrap skeletons.
// The flow-packer takes it too (`fallbackRatio`): a card with no artwork
// paints its placeholder at this ratio and never reports a measured one, so
// packing it as box art would under-reserve its width and overflow the row.
const { boxartStyle } = useUISettings();
const coverAspectRatio = computed(() =>
coverRatio(
@@ -378,6 +381,7 @@ const { virtualItems, letterToIndex, availableLetters, getItemHeight } =
gap: CARD_GAP_PX,
ratioAt,
ratioVersion,
fallbackRatio: coverAspectRatio,
});
const scrollerRef = ref<InstanceType<typeof RVirtualScroller> | null>(null);

View File

@@ -0,0 +1,53 @@
import { describe, expect, it } from "vitest";
import { computed, ref } from "vue";
import { useGalleryVirtualItems, type GalleryItem } from "./index";
// A 240px-tall card is 160px wide at box art (2/3) and 240px wide at square
// (miximage / physical). In an 800px row that is 4 per row vs 3 per row —
// pack a square-painted card as box art and the row overflows.
const CARD_HEIGHT = 240;
const ROW_WIDTH = 800;
function packedRows(fallbackRatio?: number): Array<[number, number]> {
const { virtualItems } = useGalleryVirtualItems({
layout: ref("grid"),
groupBy: ref("none"),
total: ref(6),
charIndex: ref({ a: 0 }),
columns: ref(6),
loadingInitial: computed(() => false),
emptyMessage: ref(""),
cardHeight: CARD_HEIGHT,
rowWidth: ROW_WIDTH,
gap: 12,
fallbackRatio,
});
return virtualItems.value
.filter((i): i is Extract<GalleryItem, { kind: "row" }> => i.kind === "row")
.map((r) => [r.startPosition, r.endPosition]);
}
describe("useGalleryVirtualItems — unmeasured cover ratio", () => {
it("packs unmeasured positions as box art by default", () => {
expect(packedRows()).toEqual([
[0, 4],
[4, 6],
]);
});
it("packs unmeasured positions at the active style's ratio", () => {
// Square style (miximage / physical): a card with no artwork paints its
// placeholder square and never measures, so it must reserve 240px.
expect(packedRows(1)).toEqual([
[0, 3],
[3, 6],
]);
});
it("ignores a non-positive fallback ratio", () => {
expect(packedRows(0)).toEqual([
[0, 4],
[4, 6],
]);
});
});

View File

@@ -141,8 +141,15 @@ interface Options {
/** Horizontal gap between cards in px (default 12). */
gap?: MaybeRefOrGetter<number>;
/** Natural cover ratio (w / h) for a position (card width =
* `cardHeight * ratioAt(p)`). Defaults to 2/3 until the image is measured. */
* `cardHeight * ratioAt(p)`). Falls back to `fallbackRatio` until the
* image is measured. */
ratioAt?: (position: number) => number;
/** Ratio to pack an unmeasured position at — must be the ratio its card
* actually paints, which is the active boxart style's box ratio: a card
* with no artwork paints its placeholder at that ratio and never
* measures, and one waiting on its image paints there until it loads.
* Defaults to box art (2/3). */
fallbackRatio?: MaybeRefOrGetter<number>;
/** Bump to force a re-pack when measured ratios change (Vue tracks it). */
ratioVersion?: Ref<number> | ComputedRef<number>;
}
@@ -225,9 +232,19 @@ export function useGalleryVirtualItems(opts: Options) {
// exact-offset regardless of how many variable-width cards a row holds.
const rowHeightPx = computed(() => cardHeightPx() + ROW_CHROME_PX);
const ratioAt = (position: number): number => {
const r = opts.ratioAt?.(position);
return r != null && r > 0 ? r : DEFAULT_COVER_RATIO;
const fallbackRatio = () => {
const r = opts.fallbackRatio != null ? toValue(opts.fallbackRatio) : 0;
return r > 0 ? r : DEFAULT_COVER_RATIO;
};
/** Per-build ratio lookup — resolves the fallback once so the packer's
* hot path (one call per position) stays a single map read. */
const makeRatioAt = () => {
const fallback = fallbackRatio();
return (position: number): number => {
const r = opts.ratioAt?.(position);
return r != null && r > 0 ? r : fallback;
};
};
// `unknown` matches RVirtualScroller's generic prop. Row / skeleton-row
@@ -379,6 +396,7 @@ export function useGalleryVirtualItems(opts: Options) {
);
// Track the version so a measured-ratio change re-runs the pack.
void (opts.ratioVersion ? opts.ratioVersion.value : 0);
const ratioAt = makeRatioAt();
if (opts.groupBy.value === "letter") {
// Header + own flow-packed rows per letter (rows restart per letter).