Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Improve Space profile photo row grouping

Anand Baburajan2a41dc41697b
diff --git a/web/apps/space/src/utils/profile-photo-layout.ts b/web/apps/space/src/utils/profile-photo-layout.ts
index 1723b37335..0410391604 100644
--- a/web/apps/space/src/utils/profile-photo-layout.ts
+++ b/web/apps/space/src/utils/profile-photo-layout.ts
@@ -6,39 +6,52 @@ export const profilePhotoRows = <Tile extends { aspectRatio: number }>(
width: number,
) => {
const rows: { aspectRatio: number; height: number; tiles: Tile[] }[] = [];
- let nextTileIndex = 0;
+ if (width <= 0) return rows;
- while (nextTileIndex < tiles.length) {
- const remainingTiles = tiles.length - nextTileIndex;
- let rowSize =
- remainingTiles <= 3
- ? remainingTiles
- : remainingTiles == 4 || remainingTiles == 5
- ? 2
- : remainingTiles % 2 == 0
- ? 2
- : 3;
- const rowTiles = tiles.slice(nextTileIndex, nextTileIndex + rowSize);
- let aspectRatio = rowTiles.reduce(
- (sum, tile) => sum + tile.aspectRatio,
- 0,
- );
+ const targetRowHeight = width * 0.55;
+ const costs = new Array<number>(tiles.length + 1).fill(Infinity);
+ const rowSizes = new Array<number>(tiles.length);
+ costs[tiles.length] = 0;
- while (
- rowSize > 1 &&
- (width - (rowSize - 1) * profilePhotoGap) / aspectRatio <
- profilePhotoMinRowHeight
+ for (let index = tiles.length - 1; index >= 0; index--) {
+ let aspectRatio = 0;
+ for (
+ let rowSize = 1;
+ rowSize <= 2 && index + rowSize <= tiles.length;
+ rowSize++
) {
- aspectRatio -= rowTiles.pop()!.aspectRatio;
- rowSize--;
+ aspectRatio += tiles[index + rowSize - 1]!.aspectRatio;
+ const height =
+ (width - (rowSize - 1) * profilePhotoGap) / aspectRatio;
+ if (rowSize == 2 && height < profilePhotoMinRowHeight) continue;
+
+ const cost =
+ (height / targetRowHeight - 1) ** 2 +
+ (rowSize == 1 ? 0.25 : 0) +
+ costs[index + rowSize]!;
+ if (cost <= costs[index]!) {
+ costs[index] = cost;
+ rowSizes[index] = rowSize;
+ }
}
+ }
+ for (let index = 0; index < tiles.length; ) {
+ const rowSize = rowSizes[index]!;
+ const rowTiles = tiles.slice(index, index + rowSize);
+ const aspectRatio = rowTiles.reduce(
+ (sum, tile) => sum + tile.aspectRatio,
+ 0,
+ );
rows.push({
aspectRatio,
- height: (width - (rowSize - 1) * profilePhotoGap) / aspectRatio,
+ height:
+ rowSize == 1
+ ? width / Math.max(aspectRatio, 4 / 5)
+ : (width - profilePhotoGap) / aspectRatio,
tiles: rowTiles,
});
- nextTileIndex += rowSize;
+ index += rowSize;
}
return rows;
diff --git a/web/apps/space/tests/profile-photo-layout.test.ts b/web/apps/space/tests/profile-photo-layout.test.ts
index 5c530959be..cb9f20d05a 100644
--- a/web/apps/space/tests/profile-photo-layout.test.ts
+++ b/web/apps/space/tests/profile-photo-layout.test.ts
@@ -22,12 +22,45 @@ test("short landscape rows split without changing photo dimensions", () => {
expect(rows.flatMap(({ tiles }) => tiles)).toEqual(tiles);
});
-test("portrait photos can still share a three-photo row", () => {
+test("portrait photos are limited to two per row", () => {
expect(
profilePhotoRows(photos([2 / 3, 3 / 4, 2 / 3]), 328).map(
({ tiles }) => tiles.length,
),
- ).toEqual([3]);
+ ).toEqual([2, 1]);
+});
+
+test("a landscape stands alone so the final portrait can share a row", () => {
+ const tiles = photos([16 / 9, 16 / 9, 9 / 16]);
+ const rows = profilePhotoRows(tiles, 568);
+ expect(rows.map(({ tiles }) => tiles.map(({ id }) => id))).toEqual([
+ [0],
+ [1, 2],
+ ]);
+ expect(rows[1]!.height).toBeCloseTo(565 / (16 / 9 + 9 / 16));
+});
+
+test("row grouping can shift earlier pairs to avoid a final portrait on its own", () => {
+ const tiles = photos([3 / 4, 3 / 4, 16 / 9, 3 / 4, 9 / 16]);
+ expect(
+ profilePhotoRows(tiles, 363).map(({ tiles }) =>
+ tiles.map(({ id }) => id),
+ ),
+ ).toEqual([[0, 1], [2], [3, 4]]);
+});
+
+test("an unavoidable standalone portrait is capped at a 4:5 preview", () => {
+ const tiles = photos([9 / 16]);
+ expect(profilePhotoRows(tiles, 328)).toEqual([
+ { aspectRatio: 9 / 16, height: 410, tiles },
+ ]);
+});
+
+test("paired portraits keep their original proportions", () => {
+ const tiles = photos([9 / 16, 9 / 16]);
+ expect(profilePhotoRows(tiles, 363)).toEqual([
+ { aspectRatio: 18 / 16, height: 320, tiles },
+ ]);
});
test("row grouping adapts to the available width and accounts for gaps", () => {
@@ -40,7 +73,7 @@ test("row grouping adapts to the available width and accounts for gaps", () => {
).toEqual([2, 1]);
expect(
profilePhotoRows(tiles, 456).map(({ tiles }) => tiles.length),
- ).toEqual([3]);
+ ).toEqual([2, 1]);
});
test("a panorama keeps its aspect ratio even when it cannot reach the minimum", () => {
@@ -57,6 +90,7 @@ test.each([288, 328, 358, 568])(
const rows = profilePhotoRows(tiles, width);
expect(rows.flatMap(({ tiles }) => tiles)).toEqual(tiles);
for (const row of rows) {
+ expect(row.tiles.length).toBeLessThanOrEqual(2);
const gaps = (row.tiles.length - 1) * profilePhotoGap;
expect(
row.tiles.reduce(
@@ -77,3 +111,7 @@ test.each([288, 328, 358, 568])(
test("an empty grid has no rows", () => {
expect(profilePhotoRows([], 328)).toEqual([]);
});
+
+test("the grid waits for its width to be measured", () => {
+ expect(profilePhotoRows(photos([3 / 4, 3 / 2]), 0)).toEqual([]);
+});