π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Make profile photos continuous and preserve their proportions
Anand Baburajanbe2c6061ee29
diff --git a/web/apps/space/src/components/PostPhotosBadge.tsx b/web/apps/space/src/components/PostPhotosBadge.tsxindex d8dd106255..a2619abb6b 100644--- a/web/apps/space/src/components/PostPhotosBadge.tsx+++ b/web/apps/space/src/components/PostPhotosBadge.tsx@@ -1,5 +1,3 @@-import { Album02Icon } from "@hugeicons/core-free-icons";-import { HugeiconsIcon } from "@hugeicons/react";import { Box } from "@mui/material";export const SpacePostPhotosBadge = ({@@ -18,17 +16,33 @@ export const SpacePostPhotosBadge = ({color: "#FFFFFF",display: "flex",filter: "drop-shadow(0 1px 2px rgba(0, 0, 0, 0.5))",- height: 30,+ height: 24,justifyContent: "center",- opacity: 0.8,pointerEvents: "none",position: "absolute",right: inset,top: inset,- width: 30,+ width: 24,zIndex: 2,}}>- <HugeiconsIcon icon={Album02Icon} size={18} strokeWidth={1.6} />+ <svg+ aria-hidden+ width={18}+ height={18}+ viewBox="0 0 24 24"+ fill="none"+ stroke="currentColor"+ strokeWidth={1.6}+ strokeLinecap="round"+ strokeLinejoin="round"+ style={{ transform: "scaleX(-1)" }}+ >+ <path+ d="M13 3H15C17.8284 3 19.2426 3 20.1213 3.87868C21 4.75736 21 6.17157 21 9V11C21 13.8284 21 15.2426 20.1213 16.1213C19.2426 17 17.8284 17 15 17H13C10.1716 17 8.75736 17 7.87868 16.1213C7 15.2426 7 13.8284 7 11V9C7 6.17157 7 4.75736 7.87868 3.87868C8.75736 3 10.1716 3 13 3Z"+ fill="currentColor"+ />+ <path d="M16 20.1213C15.1213 21 13.7071 21 10.8787 21H9C6.17157 21 4.75736 21 3.87868 20.1213C3 19.2426 3 17.8284 3 15V13.1213C3 10.2929 3 8.87868 3.87868 8" />+ </svg></Box>) : null;diff --git a/web/apps/space/src/screens/ProfileScreen.tsx b/web/apps/space/src/screens/ProfileScreen.tsxindex e7944aef46..9afa356d5a 100644--- a/web/apps/space/src/screens/ProfileScreen.tsx+++ b/web/apps/space/src/screens/ProfileScreen.tsx@@ -80,20 +80,17 @@ const profileAvatarTopOffset = 54;const profileAvatarSize = 132;const profileCoverHeight =profileHeaderHeight + profileAvatarTopOffset + profileAvatarSize / 2;-const photoMasonryGap = `${profilePhotoGap}px`;-const photoMasonryPlaceholderBackground = spaceSurface;-const photoMasonryRadius = `${spaceProfilePostRadius}px`;+const photoGridGap = `${profilePhotoGap}px`;+const photoGridPlaceholderBackground = spaceSurface;+const photoGridRadius = `${spaceProfilePostRadius}px`;const profileCoverRadius = "12px";-const photoMasonryLoadRootMargin = "800px 0px";-const publicPhotoMasonryLoadRootMargin = "400px 0px";+const photoGridLoadRootMargin = "800px 0px";+const publicPhotoGridLoadRootMargin = "400px 0px";interface ProfilePhotoDimensions {height: number;width: number;}-const photoAspectRatio = ({ height, width }: ProfilePhotoDimensions): number =>- height > 0 && width > 0 ? width / height : 1;-export interface ProfilePostItem {avatarUrl?: string | null;caption?: string;@@ -119,64 +116,12 @@ interface SelectedProfilePost {photoIndex: number;}-interface PostMasonryTile {+interface ProfilePhotoTile {aspectRatio: number;index: number;item: ProfilePostItem;}-const buildPostMasonrySections = (- items: ProfilePostItem[],- loadedDimensionsByID: Record<string, ProfilePhotoDimensions>,- width: number,-) => {- const now = new Date();- const dayMs = 24 * 60 * 60 * 1000;- const sections = [- {- title: "Today",- sinceMs: new Date(- now.getFullYear(),- now.getMonth(),- now.getDate(),- ).getTime(),- },- {- title: "Yesterday",- sinceMs: new Date(- now.getFullYear(),- now.getMonth(),- now.getDate() - 1,- ).getTime(),- },- { title: "Last 7 days", sinceMs: now.getTime() - 7 * dayMs },- { title: "Last 30 days", sinceMs: now.getTime() - 30 * dayMs },- { title: "Older", sinceMs: -Infinity },- ].map((section) => ({ ...section, tiles: new Array<PostMasonryTile>() }));-- items.forEach((item, index) => {- const dimensions = loadedDimensionsByID[item.id] ?? {- height: item.height ?? 1,- width: item.width ?? 1,- };- const section = sections.find(- ({ sinceMs }) => item.timestampMs >= sinceMs,- )!;- section.tiles.push({- aspectRatio: photoAspectRatio(dimensions),- index,- item,- });- });-- return sections- .filter(({ tiles }) => tiles.length > 0)- .map(({ title, tiles }) => ({- title,- rows: profilePhotoRows(tiles, width),- }));-};-const profilePostImageCacheKey = (item: ProfilePostItem) =>[item.id, item.imageAsset?.objectKey ?? item.imageUrl ?? ""].join(":");@@ -197,7 +142,7 @@ const ProfileStatsSkeleton: React.FC = () => (<Skeletonvariant="rectangular"sx={{- bgcolor: photoMasonryPlaceholderBackground,+ bgcolor: photoGridPlaceholderBackground,borderRadius: "999px",height: 18,transform: "none",@@ -217,7 +162,7 @@ const ProfileStatsSkeleton: React.FC = () => (<Skeletonvariant="rectangular"sx={{- bgcolor: photoMasonryPlaceholderBackground,+ bgcolor: photoGridPlaceholderBackground,borderRadius: "999px",height: 18,transform: "none",@@ -384,11 +329,12 @@ const ProfilePostTile: React.FC<ProfilePostTileProps> = ({}}sx={{appearance: "none",- bgcolor: photoMasonryPlaceholderBackground,+ bgcolor: photoGridPlaceholderBackground,border: 0,+ borderRadius: photoGridRadius,cursor: imageUrl && !isUnavailable ? "pointer" : "default",display: "block",- flex: `${flexGrow} 1 0`,+ flex: `${flexGrow} 1 0px`,height: "100%",minWidth: 0,opacity: 1,@@ -440,7 +386,7 @@ const ProfilePostTile: React.FC<ProfilePostTileProps> = ({display: "block",height: "100%",inset: 0,- objectFit: "cover",+ objectFit: "contain",objectPosition: "center",opacity:isCurrentImageReady || !thumbHashDataURL ? 1 : 0,@@ -650,13 +596,8 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({? "hidden": "like-only";const postImageLoadRootMargin = isAnonymousPublicProfile- ? publicPhotoMasonryLoadRootMargin- : photoMasonryLoadRootMargin;- const masonrySections = buildPostMasonrySections(- visiblePostItems,- loadedPhotoDimensionsByID,- postGridWidth,- );+ ? publicPhotoGridLoadRootMargin+ : photoGridLoadRootMargin;const closeFriendActions = () => setFriendActionsAnchor(null);const requestUnfriend = () => {@@ -793,6 +734,18 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({[loadedPhotoDimensionsByID],);+ const photoRows = profilePhotoRows(+ visiblePostItems.map((item, index) => {+ const { width, height } = dimensionsForPost(item);+ return {+ aspectRatio: width > 0 && height > 0 ? width / height : 1,+ index,+ item,+ };+ }),+ postGridWidth,+ );+const profileViewerPhotos = viewerPostItems.flatMap((item) =>viewerPhotosFromPost({...item,@@ -844,16 +797,16 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({};const renderPostTile = (- { aspectRatio, index, item }: PostMasonryTile,+ { aspectRatio, index, item }: ProfilePhotoTile,rowAspectRatio: number,) => {const imageUrl = loadedPostImageURLFor(item);const isUnavailable = !viewerPostIndexByID.has(item.id);return (<ProfilePostTile- key={`${item.id}-${index}`}- flexGrow={aspectRatio / rowAspectRatio}+ key={item.id}displayName={displayName}+ flexGrow={aspectRatio / rowAspectRatio}imageUrl={imageUrl}index={index}isUnavailable={isUnavailable}@@ -1328,7 +1281,7 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({variant="rounded"sx={{bgcolor:- photoMasonryPlaceholderBackground,+ photoGridPlaceholderBackground,height: 26,transform: "none",width: 112,@@ -1634,61 +1587,26 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({sx={{display: "flex",flexDirection: "column",- gap: "24px",+ gap: photoGridGap,mt: "6px",mx: "16px",width: "calc(100% - 32px)",}}>- {masonrySections.map(({ title, rows }) => (- <Box component="section" key={title}>- <Box- component="h2"- sx={{- alignItems: "baseline",- color: textSoft,- display: "flex",- fontFamily:- '"Inter Variable", Inter, sans-serif',- fontSize: 13,- fontWeight: 700,- gap: "4px",- lineHeight: "18px",- m: 0,- pb: "8px",- }}- >- {title}- </Box>- <Box- sx={{- borderRadius: photoMasonryRadius,- display: "flex",- flexDirection: "column",- gap: photoMasonryGap,- overflow: "hidden",- }}- >- {rows.map((row) => (- <Box- key={row.tiles[0]!.item.id}- sx={{- display: "flex",- flexShrink: 0,- gap: photoMasonryGap,- height: row.height,- width: "100%",- }}- >- {row.tiles.map((tile) =>- renderPostTile(- tile,- row.aspectRatio,- ),- )}- </Box>- ))}- </Box>+ {photoRows.map((row) => (+ <Box+ key={row.tiles[0]!.item.id}+ sx={{+ display: "flex",+ flexShrink: 0,+ gap: photoGridGap,+ height: row.height,+ width: "100%",+ }}+ >+ {row.tiles.map((tile) =>+ renderPostTile(tile, row.aspectRatio),+ )}</Box>))}</Box>diff --git a/web/apps/space/src/utils/profile-photo-layout.ts b/web/apps/space/src/utils/profile-photo-layout.tsindex 0410391604..32ad2b8f3b 100644--- a/web/apps/space/src/utils/profile-photo-layout.ts+++ b/web/apps/space/src/utils/profile-photo-layout.ts@@ -1,4 +1,4 @@-export const profilePhotoGap = 3;+export const profilePhotoGap = 6;export const profilePhotoMinRowHeight = 100;export const profilePhotoRows = <Tile extends { aspectRatio: number }>(@@ -8,7 +8,7 @@ export const profilePhotoRows = <Tile extends { aspectRatio: number }>(const rows: { aspectRatio: number; height: number; tiles: Tile[] }[] = [];if (width <= 0) return rows;- const targetRowHeight = width * 0.55;+ const targetRowHeight = width / 2;const costs = new Array<number>(tiles.length + 1).fill(Infinity);const rowSizes = new Array<number>(tiles.length);costs[tiles.length] = 0;@@ -17,18 +17,17 @@ export const profilePhotoRows = <Tile extends { aspectRatio: number }>(let aspectRatio = 0;for (let rowSize = 1;- rowSize <= 2 && index + rowSize <= tiles.length;+ rowSize <= (tiles.length < 5 ? 1 : 3) &&+ index + rowSize <= tiles.length;rowSize++) {aspectRatio += tiles[index + rowSize - 1]!.aspectRatio;const height =(width - (rowSize - 1) * profilePhotoGap) / aspectRatio;- if (rowSize == 2 && height < profilePhotoMinRowHeight) continue;+ if (rowSize > 1 && height < profilePhotoMinRowHeight) continue;const cost =- (height / targetRowHeight - 1) ** 2 +- (rowSize == 1 ? 0.25 : 0) +- costs[index + rowSize]!;+ (height / targetRowHeight - 1) ** 2 + costs[index + rowSize]!;if (cost <= costs[index]!) {costs[index] = cost;rowSizes[index] = rowSize;@@ -45,10 +44,7 @@ export const profilePhotoRows = <Tile extends { aspectRatio: number }>();rows.push({aspectRatio,- height:- rowSize == 1- ? width / Math.max(aspectRatio, 4 / 5)- : (width - profilePhotoGap) / aspectRatio,+ height: (width - (rowSize - 1) * profilePhotoGap) / aspectRatio,tiles: rowTiles,});index += rowSize;diff --git a/web/apps/space/tests/profile-photo-layout.test.ts b/web/apps/space/tests/profile-photo-layout.test.tsindex cb9f20d05a..00475994f1 100644--- a/web/apps/space/tests/profile-photo-layout.test.ts+++ b/web/apps/space/tests/profile-photo-layout.test.ts@@ -8,78 +8,94 @@ import {const photos = (ratios: number[]) =>ratios.map((aspectRatio, id) => ({ id, aspectRatio }));-test("a portrait and landscape share a row at their allocated widths", () => {- const tiles = photos([3 / 4, 3 / 2]);- expect(profilePhotoRows(tiles, 363)).toEqual([- { aspectRatio: 2.25, height: 160, tiles },- ]);-});+test.each([1, 2, 3, 4])(+ "a profile with %i photos shows each at full width and its original height",+ (count) => {+ const tiles = photos([4 / 3, 3 / 2, 1, 9 / 16].slice(0, count));+ const rows = profilePhotoRows(tiles, 352);+ expect(rows).toEqual(+ tiles.map((tile) => ({+ aspectRatio: tile.aspectRatio,+ height: 352 / tile.aspectRatio,+ tiles: [tile],+ })),+ );+ },+);-test("short landscape rows split without changing photo dimensions", () => {- const tiles = photos([4 / 3, 5 / 4, 7 / 4]);- const rows = profilePhotoRows(tiles, 328);- expect(rows.map(({ tiles }) => tiles.length)).toEqual([2, 1]);- expect(rows.flatMap(({ tiles }) => tiles)).toEqual(tiles);+test("a profile with exactly five photos uses the adaptive grid throughout", () => {+ const tiles = photos([3 / 2, 3 / 2, 3 / 2, 3 / 2, 3 / 2]);+ const rows = profilePhotoRows(tiles, 352);+ expect(rows.map(({ tiles }) => tiles.map(({ id }) => id))).toEqual([+ [0, 1],+ [2, 3],+ [4],+ ]);});-test("portrait photos are limited to two per row", () => {+test("three portraits share a compact row", () => {expect(- profilePhotoRows(photos([2 / 3, 3 / 4, 2 / 3]), 328).map(- ({ tiles }) => tiles.length,- ),- ).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));+ profilePhotoRows(+ photos([2 / 3, 3 / 4, 2 / 3, 2 / 3, 3 / 4, 2 / 3]),+ 328,+ ).map(({ tiles }) => tiles.length),+ ).toEqual([3, 3]);});-test("row grouping can shift earlier pairs to avoid a final portrait on its own", () => {+test("a landscape can share a row with two portraits to avoid a lone portrait", () => {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]]);+ ).toEqual([+ [0, 1],+ [2, 3, 4],+ ]);});-test("an unavoidable standalone portrait is capped at a 4:5 preview", () => {+test("a standalone portrait keeps its original proportions", () => {const tiles = photos([9 / 16]);expect(profilePhotoRows(tiles, 328)).toEqual([- { aspectRatio: 9 / 16, height: 410, tiles },+ { aspectRatio: 9 / 16, height: 328 / (9 / 16), 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("similar landscapes are not forced into full-width rows", () => {+ const tiles = photos(new Array<number>(10).fill(3 / 2));+ const rows = profilePhotoRows(tiles, 358);+ expect(rows.map(({ tiles }) => tiles.length)).toEqual([2, 2, 2, 2, 2]);+ expect(rows.flatMap(({ tiles }) => tiles)).toEqual(tiles);+});++test("wider photos stand alone when that fits better than a short paired row", () => {+ const tiles = photos([3 / 2, 3 / 2, 5 / 3, 5 / 3, 3 / 2, 3 / 2]);+ const rows = profilePhotoRows(tiles, 358);+ expect(rows.map(({ tiles }) => tiles.length)).toEqual([2, 1, 1, 2]);+ expect(rows.flatMap(({ tiles }) => tiles)).toEqual(tiles);});test("row grouping adapts to the available width and accounts for gaps", () => {- const tiles = photos([1.5, 1.5, 1.5]);+ const tiles = photos([1.5, 1.5, 1.5, 1.5, 1.5, 1.5]);expect(- profilePhotoRows(tiles, 302).map(({ tiles }) => tiles.length),- ).toEqual([1, 1, 1]);+ profilePhotoRows(tiles, 299 + profilePhotoGap).map(+ ({ tiles }) => tiles.length,+ ),+ ).toEqual([1, 1, 1, 1, 1, 1]);expect(- profilePhotoRows(tiles, 303).map(({ tiles }) => tiles.length),- ).toEqual([2, 1]);+ profilePhotoRows(tiles, 300 + profilePhotoGap).map(+ ({ tiles }) => tiles.length,+ ),+ ).toEqual([2, 2, 2]);expect(profilePhotoRows(tiles, 456).map(({ tiles }) => tiles.length),- ).toEqual([2, 1]);+ ).toEqual([2, 2, 2]);});test("a panorama keeps its aspect ratio even when it cannot reach the minimum", () => {- const tiles = photos([8, 1, 1]);+ const tiles = photos([8, 1, 1, 1, 1]);const rows = profilePhotoRows(tiles, 328);- expect(rows.map(({ tiles }) => tiles.length)).toEqual([1, 2]);+ expect(rows.map(({ tiles }) => tiles.length)).toEqual([1, 2, 2]);expect(rows[0]!.aspectRatio).toBe(8);});@@ -90,7 +106,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);+ expect(row.tiles.length).toBeLessThanOrEqual(3);const gaps = (row.tiles.length - 1) * profilePhotoGap;expect(row.tiles.reduce(