From e8aa2f5afdfe81df751ba3254e66989e873a43ac Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Tue, 28 Jul 2026 22:36:39 -0400 Subject: [PATCH] fix(gallery): pack placeholder covers at the active boxart ratio MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- .../v2/components/Gallery/GalleryShell.vue | 4 ++ .../fallbackRatio.test.ts | 53 +++++++++++++++++++ .../useGalleryVirtualItems/index.ts | 26 +++++++-- 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 frontend/src/v2/composables/useGalleryVirtualItems/fallbackRatio.test.ts diff --git a/frontend/src/v2/components/Gallery/GalleryShell.vue b/frontend/src/v2/components/Gallery/GalleryShell.vue index 288f9436f..61e85a3a9 100644 --- a/frontend/src/v2/components/Gallery/GalleryShell.vue +++ b/frontend/src/v2/components/Gallery/GalleryShell.vue @@ -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 | null>(null); diff --git a/frontend/src/v2/composables/useGalleryVirtualItems/fallbackRatio.test.ts b/frontend/src/v2/composables/useGalleryVirtualItems/fallbackRatio.test.ts new file mode 100644 index 000000000..288c0e4b3 --- /dev/null +++ b/frontend/src/v2/composables/useGalleryVirtualItems/fallbackRatio.test.ts @@ -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 => 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], + ]); + }); +}); diff --git a/frontend/src/v2/composables/useGalleryVirtualItems/index.ts b/frontend/src/v2/composables/useGalleryVirtualItems/index.ts index 5641f0c7c..1af0e1eb4 100644 --- a/frontend/src/v2/composables/useGalleryVirtualItems/index.ts +++ b/frontend/src/v2/composables/useGalleryVirtualItems/index.ts @@ -141,8 +141,15 @@ interface Options { /** Horizontal gap between cards in px (default 12). */ gap?: MaybeRefOrGetter; /** 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; /** Bump to force a re-pack when measured ratios change (Vue tracks it). */ ratioVersion?: Ref | ComputedRef; } @@ -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).