Skip to content

A platform for community discussion. Free, open, simple.

UX: Fill the live room stage with uniform cropped video tiles (#45177)

Rafael dos Santos Silva8f31fb605033
diff --git a/plugins/live/assets/javascripts/discourse/components/live/room-page.gjs b/plugins/live/assets/javascripts/discourse/components/live/room-page.gjs
index 70b4172a56f..c632bee906e 100644
--- a/plugins/live/assets/javascripts/discourse/components/live/room-page.gjs
+++ b/plugins/live/assets/javascripts/discourse/components/live/room-page.gjs
@@ -24,8 +24,7 @@ import {
import { activeRingingEntries } from "../../lib/live/ringing.js";
import { speakQueue } from "../../lib/live/speak-queue.js";
import {
- bestRowHeight,
- DEFAULT_TILE_ASPECT,
+ computeRoomGrid,
trackGridSize,
} from "../../lib/live/video-grid-layout.js";
import LiveInviteAgentButton from "../live-invite-agent-button.gjs";
@@ -62,7 +61,6 @@ export default class LiveRoomPage extends Component {
@tracked gridWidth = 0;
@tracked gridHeight = 0;
@tracked gridGap = 0;
- @tracked tileAspects = new Map();
@tracked gridFullscreen = false;
// Stage rooms mirror a workshop: chat panel open, presenter featured.
@tracked chatOpen = !!this.args.openChat || this.#isStageRoom;
@@ -192,6 +190,24 @@ export default class LiveRoomPage extends Component {
return this.tiles.filter((tile) => tile.participant.id !== featuredId);
}
+ // In a one-to-one call your own camera is the least useful tile to you, so
+ // it floats in a corner and the other person gets the whole stage.
+ get pipTile() {
+ if (
+ !this.tiledLayout ||
+ this.tiles.length !== 2 ||
+ this.ringingEntries.length
+ ) {
+ return null;
+ }
+ return this.tiles.find((tile) => tile.isSelf) ?? null;
+ }
+
+ get gridTiles() {
+ const pipId = this.pipTile?.participant.id;
+ return this.tiles.filter((tile) => tile.participant.id !== pipId);
+ }
+
get tiledLayout() {
return this.layoutMode === LAYOUT_TILED;
}
@@ -220,26 +236,20 @@ export default class LiveRoomPage extends Component {
return null;
}
- const aspects = [
- ...this.tiles.map(
- (tile) =>
- this.tileAspects.get(tile.participant.id) ?? DEFAULT_TILE_ASPECT
- ),
- ...this.ringingEntries.map(() => DEFAULT_TILE_ASPECT),
- ];
-
- const rowHeight = bestRowHeight(
- this.gridWidth,
- this.gridHeight,
- aspects,
- this.gridGap
- );
+ const grid = computeRoomGrid({
+ width: this.gridWidth,
+ height: this.gridHeight,
+ count: this.gridTiles.length + this.ringingEntries.length,
+ gap: this.gridGap,
+ });
- if (rowHeight <= 0) {
+ if (!grid) {
return null;
}
- return trustHTML(`--live-tile-height: ${rowHeight}px;`);
+ return trustHTML(
+ `--live-tile-width: ${grid.tileWidth}px; --live-tile-height: ${grid.tileHeight}px; --live-grid-line-width: ${grid.lineWidth}px;`
+ );
}
get chatAvailable() {
@@ -364,22 +374,6 @@ export default class LiveRoomPage extends Component {
toggleFullscreen(this.gridElement);
}
- @action
- reportTileAspect(participantId, aspect) {
- const current = this.tileAspects.get(participantId) ?? null;
- if (current === aspect) {
- return;
- }
-
- const nextAspects = new Map(this.tileAspects);
- if (aspect) {
- nextAspects.set(participantId, aspect);
- } else {
- nextAspects.delete(participantId);
- }
- this.tileAspects = nextAspects;
- }
-
@action
joinRoom() {
if (!this.currentUser) {
@@ -578,7 +572,6 @@ export default class LiveRoomPage extends Component {
<div class="live-room-page__presentation-main">
<LiveVideoTile
@isSelf={{this.presentationTile.isSelf}}
- @onAspect={{this.reportTileAspect}}
@onSpotlight={{this.toggleSpotlight}}
@participant={{this.presentationTile.participant}}
@room={{this.room}}
@@ -599,7 +592,6 @@ export default class LiveRoomPage extends Component {
}}
<LiveVideoTile
@isSelf={{tile.isSelf}}
- @onAspect={{this.reportTileAspect}}
@onSpotlight={{this.toggleSpotlight}}
@participant={{tile.participant}}
@room={{this.room}}
@@ -630,20 +622,32 @@ export default class LiveRoomPage extends Component {
@translatedTitle={{this.gridFullscreenTitle}}
/>
{{/if}}
- {{#each this.tiles key="participant.id" as |tile|}}
+ <div class="live-room-page__tiles">
+ {{#each this.gridTiles key="participant.id" as |tile|}}
+ <LiveVideoTile
+ @isSelf={{tile.isSelf}}
+ @onSpotlight={{this.toggleSpotlight}}
+ @participant={{tile.participant}}
+ @room={{this.room}}
+ @showVideo={{tile.showVideo}}
+ @spotlighted={{tile.spotlighted}}
+ />
+ {{/each}}
+ {{#each this.ringingEntries key="user.id" as |entry|}}
+ <LiveRingingTile @user={{entry.user}} />
+ {{/each}}
+ </div>
+ {{#if this.pipTile}}
<LiveVideoTile
- @isSelf={{tile.isSelf}}
- @onAspect={{this.reportTileAspect}}
+ class="live-room-page__pip"
+ @isSelf={{this.pipTile.isSelf}}
@onSpotlight={{this.toggleSpotlight}}
- @participant={{tile.participant}}
+ @participant={{this.pipTile.participant}}
@room={{this.room}}
- @showVideo={{tile.showVideo}}
- @spotlighted={{tile.spotlighted}}
+ @showVideo={{this.pipTile.showVideo}}
+ @spotlighted={{this.pipTile.spotlighted}}
/>
- {{/each}}
- {{#each this.ringingEntries key="user.id" as |entry|}}
- <LiveRingingTile @user={{entry.user}} />
- {{/each}}
+ {{/if}}
</div>
{{/if}}
{{else}}
diff --git a/plugins/live/assets/javascripts/discourse/components/live/video-tile.gjs b/plugins/live/assets/javascripts/discourse/components/live/video-tile.gjs
index d98991160f9..5b2355a42a2 100644
--- a/plugins/live/assets/javascripts/discourse/components/live/video-tile.gjs
+++ b/plugins/live/assets/javascripts/discourse/components/live/video-tile.gjs
@@ -49,6 +49,10 @@ export default class LiveVideoTile extends Component {
return trustHTML(`aspect-ratio: ${this.aspect ?? DEFAULT_TILE_ASPECT};`);
}
+ get isPortrait() {
+ return this.aspect !== null && this.aspect < 1;
+ }
+
// The menu only offers audio controls for a call the user is in, so
// page visitors who haven't joined the room don't get one.
get canShowMenu() {
@@ -163,11 +167,13 @@ export default class LiveVideoTile extends Component {
"live-video-tile"
(if this.showVideo "--video" "--avatar")
(if (eq this.publishingKind "screen") "--screen")
+ (if this.isPortrait "--portrait")
(if @isSelf "--self")
(if this.isSpeaking "--speaking")
}}
data-user-id={{this.participant.id}}
style={{this.tileStyle}}
+ ...attributes
{{this.trackFullscreen this.setFullscreen}}
{{on "contextmenu" this.openContextMenu}}
>
diff --git a/plugins/live/assets/javascripts/discourse/lib/live/video-grid-layout.js b/plugins/live/assets/javascripts/discourse/lib/live/video-grid-layout.js
index b86bedd7732..da330e13b93 100644
--- a/plugins/live/assets/javascripts/discourse/lib/live/video-grid-layout.js
+++ b/plugins/live/assets/javascripts/discourse/lib/live/video-grid-layout.js
@@ -4,76 +4,100 @@ import { modifier } from "ember-modifier";
// intrinsic size yet, and for avatar-only tiles. Matches a landscape camera.
export const DEFAULT_TILE_ASPECT = 16 / 9;
-// Greedy in-order row packing at a candidate uniform row height: walk the
-// tiles, start a new row whenever the next tile would overflow the width.
-// Returns the row count, or Infinity if a single tile is wider than the row.
-function rowsAtHeight(aspects, containerWidth, gap, height) {
- let rows = 1;
- let rowWidth = 0;
-
- for (const aspect of aspects) {
- const tileWidth = height * aspect;
- if (tileWidth > containerWidth) {
- return Infinity;
- }
+// Both grids use uniform tiles rather than each feed's own aspect: a stage is
+// rarely a multiple of 16:9, so aspect-faithful tiles leave large empty bands
+// at most participant counts. Cover-cropping absorbs the difference between a
+// feed's real aspect and the cell's, within each grid's aspect band.
+
+// Widest cell a tile may take: a wider one would crop the top and bottom of a
+// landscape camera, where the head is.
+const TILE_MAX_ASPECT = DEFAULT_TILE_ASPECT;
+
+// Every column count's uniform tile for `slots` tiles in a width×height box:
+// size the cell, clamp the tile's aspect to the band, fit the tile in the cell.
+function gridCandidates(slots, width, height, gap, minAspect) {
+ const candidates = [];
- const needed = rowWidth === 0 ? tileWidth : gap + tileWidth;
- if (rowWidth + needed > containerWidth) {
- rows += 1;
- rowWidth = tileWidth;
- } else {
- rowWidth += needed;
+ for (let cols = 1; cols <= slots; cols++) {
+ const rows = Math.ceil(slots / cols);
+ const cellWidth = (width - (cols - 1) * gap) / cols;
+ const cellHeight = (height - (rows - 1) * gap) / rows;
+ if (cellWidth <= 0 || cellHeight <= 0) {
+ continue;
}
+
+ const aspect = Math.min(
+ Math.max(cellWidth / cellHeight, minAspect),
+ TILE_MAX_ASPECT
+ );
+ const tileWidth = Math.min(cellWidth, cellHeight * aspect);
+ const tileHeight = tileWidth / aspect;
+
+ candidates.push({
+ cols,
+ rows,
+ tileWidth,
+ tileHeight,
+ area: tileWidth * tileHeight,
+ emptySlots: cols * rows - slots,
+ });
}
- return rows;
+ return candidates;
}
-// Largest uniform row height at which all tiles — each rendered at its own
-// aspect ratio (width = height * aspect) — fit the container, packed into rows.
-// Tiles share a height and vary in width, so portrait and landscape feeds sit
-// side by side without cropping or distortion. For a room where every tile has
-// the same aspect this converges to the same optimum as a column-count search;
-// mixed-aspect rooms get a justified-gallery layout. The grid uses
-// `contain: size`, so the result never feeds back into the container size —
-// without it, oversized tiles (e.g. right after exiting fullscreen) would
-// grow the grid, the remeasure would see the inflated box, and the layout
-// would lock there.
-export function bestRowHeight(containerWidth, containerHeight, aspects, gap) {
- const count = aspects.length;
- if (!count || containerWidth <= 0 || containerHeight <= 0) {
- return 0;
- }
+// A webcam frame keeps the face near the center, so the room page can crop
+// down to square cells, which lets a landscape stage fit more columns.
+const ROOM_TILE_MIN_ASPECT = 1;
- const fits = (height) => {
- const rows = rowsAtHeight(aspects, containerWidth, gap, height);
- if (!isFinite(rows)) {
- return false;
- }
- return rows * height + (rows - 1) * gap <= containerHeight;
- };
+// A slightly smaller tile is a better trade than a ragged last row.
+const ROOM_FULL_ROW_TOLERANCE = 0.92;
- let low = 0;
- let high = containerHeight;
- for (let i = 0; i < 40; i++) {
- const mid = (low + high) / 2;
- if (fits(mid)) {
- low = mid;
- } else {
- high = mid;
- }
+// Uniform grid for the room page: the largest tile for `count` tiles, except
+// that an arrangement with fewer empty slots wins when its tile is within
+// tolerance of the largest. `lineWidth` caps the tile container so flex
+// wrapping breaks exactly at `cols` — the preferred arrangement isn't always
+// the largest one, so an uncapped line could fit an extra tile.
+export function computeRoomGrid({ width, height, count, gap }) {
+ if (!count || width <= 0 || height <= 0) {
+ return null;
+ }
+
+ const candidates = gridCandidates(
+ count,
+ width,
+ height,
+ gap,
+ ROOM_TILE_MIN_ASPECT
+ );
+ if (!candidates.length) {
+ return null;
}
- // The search converges from just below the true maximum, so flooring can
- // shed the last pixel at an exact boundary (e.g. one tile in a container of
- // its own height). Reclaim it when the rounded-up height still fits.
- const rowHeight = Math.floor(low);
- return fits(rowHeight + 1) ? rowHeight + 1 : rowHeight;
+ const largest = Math.max(...candidates.map((grid) => grid.area));
+ const best = candidates
+ .filter((grid) => grid.area >= largest * ROOM_FULL_ROW_TOLERANCE)
+ .reduce((a, b) =>
+ b.emptySlots < a.emptySlots ||
+ (b.emptySlots === a.emptySlots && b.area > a.area)
+ ? b
+ : a
+ );
+
+ const tileWidth = Math.floor(best.tileWidth);
+
+ return {
+ cols: best.cols,
+ rows: best.rows,
+ tileWidth,
+ tileHeight: Math.floor(best.tileHeight),
+ // One spare pixel absorbs subpixel rounding in the flex line; it can never
+ // admit another tile.
+ lineWidth: best.cols * tileWidth + (best.cols - 1) * gap + 1,
+ };
}
-// The widget grid trades the room page's per-tile aspect fidelity for uniform
-// tiles that pack a small box predictably; cover-cropping absorbs the
-// difference between a feed's real aspect and the cell's.
+// The widget stays closer to the feed's own framing than the room page.
const WIDGET_TILE_MIN_ASPECT = 4 / 3;
// Smallest tile that still reads at a glance: avatar, name, status icons.
@@ -85,33 +109,22 @@ const WIDGET_TILE_MIN_HEIGHT = 66;
// big grids.
const WIDGET_MAX_SLOTS = 12;
-// Largest uniform tile for `slots` tiles in a width×height box: try every
-// column count, size the cell, clamp the tile's aspect to the allowed band,
-// keep the arrangement with the biggest tile. Ties prefer more columns — when
+// Largest uniform tile for `slots` tiles. Ties prefer more columns — when
// tiles are height-constrained a wider grid uses the slack width, and it
// makes an unfloored tile width that fits `cols + 1` per line impossible
// (that arrangement would have scored at least as high and won the tie).
function bestGridForSlots(slots, width, height, gap) {
let best = null;
- for (let cols = 1; cols <= slots; cols++) {
- const rows = Math.ceil(slots / cols);
- const cellWidth = (width - (cols - 1) * gap) / cols;
- const cellHeight = (height - (rows - 1) * gap) / rows;
- if (cellWidth <= 0 || cellHeight <= 0) {
- continue;
- }
-
- const aspect = Math.min(
- Math.max(cellWidth / cellHeight, WIDGET_TILE_MIN_ASPECT),
- DEFAULT_TILE_ASPECT
- );
- const tileWidth = Math.min(cellWidth, cellHeight * aspect);
- const tileHeight = tileWidth / aspect;
- const area = tileWidth * tileHeight;
-
- if (!best || area >= best.area) {
- best = { cols, rows, tileWidth, tileHeight, area };
+ for (const grid of gridCandidates(
+ slots,
+ width,
+ height,
+ gap,
+ WIDGET_TILE_MIN_ASPECT
+ )) {
+ if (!best || grid.area >= best.area) {
+ best = grid;
}
}
diff --git a/plugins/live/assets/stylesheets/common/live-room-page.scss b/plugins/live/assets/stylesheets/common/live-room-page.scss
index c620c512be8..cf4382d63de 100644
--- a/plugins/live/assets/stylesheets/common/live-room-page.scss
+++ b/plugins/live/assets/stylesheets/common/live-room-page.scss
@@ -344,19 +344,19 @@ html.discourse-touch .live-call-widget {
flex: 1;
min-height: 0;
- // The tile height is computed from this element's measured size, so its
+ // The tile size is computed from this element's measured size, so its
// own size must never derive from the tiles: after exiting fullscreen the
// still-huge tiles would inflate the grid (the page only has a
// min-height), the remeasure would see that inflated box, and the layout
// would lock there instead of shrinking back.
contain: size;
display: flex;
- flex-wrap: wrap;
- gap: 0.5em;
- align-content: center;
align-items: center;
justify-content: center;
+ // Read by the layout as the spacing between tiles.
+ gap: 0.5em;
+
// While the sidebar animates, tiles rewrap faster than their height
// recomputes — clip the transient overflow so it can't spill over the
// control bar below.
@@ -372,6 +372,65 @@ html.discourse-touch .live-call-widget {
}
}
+ // Uniform tiles sized by computeRoomGrid; the line width cap makes flex
+ // wrapping break at the solved column count and centers a partial last row.
+ &__tiles {
+ display: flex;
+ flex-wrap: wrap;
+ align-content: center;
+ justify-content: center;
+ gap: inherit;
+ width: var(--live-grid-line-width, 100%);
+ max-width: 100%;
+ max-height: 100%;
+
+ .live-video-tile {
+ width: var(--live-tile-width, auto);
+ transition:
+ width 0.2s ease-out,
+ height 0.2s ease-out;
+
+ @media (prefers-reduced-motion: reduce) {
+ transition: none;
+ }
+ }
+
+ // Cropping would cut a portrait feed down to a face-height strip.
+ .live-video-tile.--portrait .live-video-tile__video {
+ object-fit: contain;
+ background: var(--primary-very-high);
+ }
+ }
+
+ // Self view in a one-to-one call. Sized by its own feed's aspect (set
+ // inline by the tile), not by the grid's tile size. Scoped to the grid to
+ // outrank the base tile rules.
+ &__grid &__pip {
+ position: absolute;
+ inset-block-end: var(--space-3);
+ inset-inline-end: var(--space-3);
+ z-index: 1;
+ width: clamp(10rem, 18%, 17.5rem);
+ height: auto;
+ max-height: 40%;
+ box-shadow: 0 0.35rem 1.25rem rgb(0 0 0 / 35%);
+ transition: none;
+
+ // A portrait feed sized by width would blow past the height cap, so size
+ // it by height and let the inline aspect-ratio set the width.
+ &.--portrait {
+ width: auto;
+ height: min(40%, 20rem);
+ }
+
+ // Any remaining mismatch between the capped box and the feed letterboxes
+ // rather than cropping your own camera.
+ .live-video-tile__video {
+ object-fit: contain;
+ background: var(--primary-very-high);
+ }
+ }
+
// Top-left so it never collides with the per-tile fullscreen buttons
// (top-right).
&__fullscreen {
@@ -746,10 +805,10 @@ body.has-sidebar-page #main-outlet-wrapper:has(.live-room-header) {
position: relative;
flex: 0 0 auto;
- // Rows share a height (computed per layout, published as
- // --live-tile-height); each tile sets its own aspect-ratio inline from
- // its measured video, so portrait and landscape feeds coexist without
- // cropping. The fallback height covers first paint before measurement.
+ // Grids publish a uniform --live-tile-height (and width). Each tile also
+ // sets its own aspect-ratio inline from its measured video, which sizes it
+ // wherever only one dimension is given. The fallback height covers first
+ // paint before measurement.
height: var(--live-tile-height, 12em);
// The computed height always lags the layout by a beat (resize observer →
diff --git a/plugins/live/test/javascripts/integration/components/live-room-page-test.gjs b/plugins/live/test/javascripts/integration/components/live-room-page-test.gjs
index 848bb805229..333deefb459 100644
--- a/plugins/live/test/javascripts/integration/components/live-room-page-test.gjs
+++ b/plugins/live/test/javascripts/integration/components/live-room-page-test.gjs
@@ -170,6 +170,33 @@ module("Integration | Component | Live | RoomPage", function (hooks) {
.exists("exposes the layout switcher in the overflow menu");
});
+ test("a one-to-one call floats the self view over the other participant", async function (assert) {
+ this.room.active_participants = this.room.active_participants.slice(0, 2);
+
+ await render(<template><LiveRoomPage @room={{this.room}} /></template>);
+
+ assert
+ .dom(
+ `.live-room-page__grid > .live-room-page__pip[data-user-id='${this.currentUser.id}']`
+ )
+ .exists("renders the current user as the floating self view");
+ assert
+ .dom(".live-room-page__tiles .live-video-tile")
+ .exists({ count: 1 }, "leaves only the other participant in the grid");
+ assert
+ .dom(".live-room-page__tiles .live-video-tile[data-user-id='2']")
+ .exists("the other participant gets the stage");
+ });
+
+ test("calls with more than two participants keep everyone in the grid", async function (assert) {
+ await render(<template><LiveRoomPage @room={{this.room}} /></template>);
+
+ assert.dom(".live-room-page__pip").doesNotExist("no floating self view");
+ assert
+ .dom(".live-room-page__tiles .live-video-tile")
+ .exists({ count: 3 }, "every participant is tiled");
+ });
+
test("a stage room defaults to the presentation layout", async function (assert) {
this.room.room_type = "stage";
// Keep the chat panel out of this test; the chat-open default is covered
diff --git a/plugins/live/test/javascripts/unit/lib/video-grid-layout-test.js b/plugins/live/test/javascripts/unit/lib/video-grid-layout-test.js
index 77168aa2daa..0f724f3e17a 100644
--- a/plugins/live/test/javascripts/unit/lib/video-grid-layout-test.js
+++ b/plugins/live/test/javascripts/unit/lib/video-grid-layout-test.js
@@ -1,86 +1,102 @@
import { module, test } from "qunit";
import {
- bestRowHeight,
+ computeRoomGrid,
computeWidgetGrid,
DEFAULT_TILE_ASPECT,
} from "discourse/plugins/live/discourse/lib/live/video-grid-layout";
-const LANDSCAPE = DEFAULT_TILE_ASPECT; // 16 / 9
-const PORTRAIT = 9 / 16;
-
-module("Live | Unit | Lib | video-grid-layout", function () {
- test("a single tile fills the constraining dimension", function (assert) {
- // Wide container: a landscape tile is limited by height.
- assert.strictEqual(
- bestRowHeight(4000, 900, [LANDSCAPE], 0),
- 900,
- "height-limited single landscape tile spans the full height"
- );
-
- // Narrow container: limited by width.
+module("Live | Unit | Lib | video-grid-layout | room grid", function () {
+ test("a lone tile fills the box up to a landscape aspect", function (assert) {
+ const grid = computeRoomGrid({
+ width: 1423,
+ height: 719,
+ count: 1,
+ gap: 8,
+ });
+ assert.strictEqual(grid.tileHeight, 719, "spans the full height");
assert.strictEqual(
- bestRowHeight(1000, 4000, [LANDSCAPE], 0),
- 562,
- "width-limited single landscape tile spans the full width"
+ grid.tileWidth,
+ Math.floor(719 * DEFAULT_TILE_ASPECT),
+ "and stops widening at 16:9"
);
});
- test("a portrait tile fills the height of a landscape container", function (assert) {
- const height = bestRowHeight(1920, 1080, [PORTRAIT], 0);
- assert.strictEqual(
- height,
- 1080,
- "portrait tile uses the full height instead of being letterboxed"
- );
- assert.true(
- height * PORTRAIT <= 1920,
- "and stays within the container width"
- );
+ test("two tiles crop toward square instead of leaving empty bands", function (assert) {
+ const grid = computeRoomGrid({
+ width: 1423,
+ height: 719,
+ count: 2,
+ gap: 8,
+ });
+ assert.strictEqual(grid.cols, 2, "sit side by side");
+ assert.strictEqual(grid.tileWidth, 707, "fill their half of the width");
+ assert.strictEqual(grid.tileHeight, 707, "at a square aspect");
});
- test("more tiles shrink the row height without overflowing", function (assert) {
- const width = 1920;
- const height = 1080;
- const counts = [1, 2, 4, 9];
-
- const heights = counts.map((count) =>
- bestRowHeight(width, height, Array(count).fill(LANDSCAPE), 8)
- );
-
- assert.deepEqual(
- [...heights].sort((a, b) => b - a),
- heights,
- "row height is monotonically non-increasing as tiles are added"
- );
-
- heights.forEach((rowHeight, index) => {
- assert.true(rowHeight > 0, `${counts[index]} tiles get a usable height`);
- assert.true(
- rowHeight <= height,
- `${counts[index]} tiles fit within the container height`
- );
+ test("prefers a full row over a slightly larger ragged layout", function (assert) {
+ // 2+1 gives a marginally larger tile here; three across is within
+ // tolerance and leaves no orphan.
+ const grid = computeRoomGrid({
+ width: 1423,
+ height: 719,
+ count: 3,
+ gap: 8,
});
+ assert.strictEqual(grid.cols, 3, "three across");
+ assert.strictEqual(grid.rows, 1, "in one row");
});
- test("mixed portrait and landscape tiles share a row at a common height", function (assert) {
- const rowHeight = bestRowHeight(1920, 1080, [LANDSCAPE, PORTRAIT], 8);
+ test("tiles always fit the box and lines break at the solved column count", function (assert) {
+ const gap = 8;
+ const boxes = [
+ { width: 1423, height: 719 },
+ { width: 1151, height: 719 },
+ { width: 799, height: 719 },
+ { width: 360, height: 640 },
+ { width: 1903, height: 899 },
+ ];
- // One row beats stacking (which would cap height near 536), so the shared
- // height lands well above that.
- assert.true(rowHeight > 600, "lays the two tiles out side by side");
+ for (const { width, height } of boxes) {
+ for (let count = 1; count <= 16; count++) {
+ const grid = computeRoomGrid({ width, height, count, gap });
+ const label = `${count} in ${width}×${height}`;
+ const { cols, rows, tileWidth, tileHeight, lineWidth } = grid;
- const totalWidth = rowHeight * LANDSCAPE + 8 + rowHeight * PORTRAIT;
- assert.true(
- totalWidth <= 1920,
- "the mixed-aspect row fits the container width"
- );
+ assert.true(
+ cols * tileWidth + (cols - 1) * gap <= width,
+ `${label}: fits the width`
+ );
+ assert.true(
+ rows * tileHeight + (rows - 1) * gap <= height,
+ `${label}: fits the height`
+ );
+ assert.true(
+ cols * tileWidth + (cols - 1) * gap <= lineWidth,
+ `${label}: a full line fits the line width`
+ );
+ assert.true(
+ (cols + 1) * tileWidth + cols * gap > lineWidth,
+ `${label}: an extra tile never fits the line width`
+ );
+ }
+ }
});
- test("returns zero when nothing can fit", function (assert) {
+ test("returns null for degenerate input", function (assert) {
+ assert.strictEqual(
+ computeRoomGrid({ width: 1000, height: 600, count: 0, gap: 8 }),
+ null,
+ "no tiles"
+ );
assert.strictEqual(
- bestRowHeight(10, 10, Array(50).fill(LANDSCAPE), 8),
- 0,
- "degenerate space yields no usable height"
+ computeRoomGrid({ width: 0, height: 600, count: 3, gap: 8 }),
+ null,
+ "no measured space"
+ );
+ assert.strictEqual(
+ computeRoomGrid({ width: 10, height: 10, count: 50, gap: 8 }),
+ null,
+ "no room for any cell"
);
});
});