Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Photos: fix the viewer's torn photo; views switch without a flicker

Yashwanth Kamireddi0111b106fd17
diff --git a/.uiverify/photos-real.mjs b/.uiverify/photos-real.mjs
index 6bf3b5f..89bc9e1 100644
--- a/.uiverify/photos-real.mjs
+++ b/.uiverify/photos-real.mjs
@@ -32,6 +32,28 @@ ok(new Set(titles).size === titles.length && (await page.locator(".px-tile").cou
await page.locator(".px-source", { hasText: "Library" }).click();
await page.waitForTimeout(300);
+// Switching views, frame by frame: the title never stands over another view's photos, a photo
+// in both views keeps its picture (no reload, no flash), and the new view starts at its top.
+await page.locator(".px-scroll").evaluate((e) => (e.scrollTop = 200));
+await page.evaluate(() => {
+ window.__frames = [];
+ document.querySelector('.px-tile[data-id="p1"] img').__kept = true;
+ const tick = () => {
+ window.__frames.push({ title: document.querySelector(".px-bar h1")?.textContent, ids: [...document.querySelectorAll(".px-tile")].map((t) => t.dataset.id).join() });
+ if (window.__frames.length < 40) requestAnimationFrame(tick);
+ };
+ requestAnimationFrame(tick);
+});
+await page.locator(".px-source", { hasText: "Favorites" }).click();
+await page.waitForTimeout(900);
+const frames = await page.evaluate(() => window.__frames);
+const wrong = frames.filter((f) => f.title === "Favorites" && f.ids !== "p1");
+ok(wrong.length === 0, `no frame shows Favorites over other photos (${wrong.length} of ${frames.length})`);
+ok(await page.evaluate(() => document.querySelector('.px-tile[data-id="p1"] img').__kept === true), "a photo in both views keeps its picture across the switch");
+ok((await page.locator(".px-scroll").evaluate((e) => e.scrollTop)) === 0, "and the new view starts at its top");
+await page.locator(".px-source", { hasText: "Library" }).click();
+await page.waitForTimeout(300);
+
await page.locator(".nav-item", { hasText: "Photos" }).focus();
await page.keyboard.press("Control+a");
await page.waitForTimeout(100);
diff --git a/.uiverify/photos-viewer-real.mjs b/.uiverify/photos-viewer-real.mjs
new file mode 100644
index 0000000..ae736b6
--- /dev/null
+++ b/.uiverify/photos-viewer-real.mjs
@@ -0,0 +1,62 @@
+// The viewer with pictures of real size: the screen-sized one and the whole one (4000×3000)
+// lie exactly on each other, inside the stage, however large the whole one is — before, its
+// own size stretched the frame: the photo tore into two and ran off the window. The same
+// after the window is resized, and after stepping to the next photo.
+import { engine } from "./engine.mjs";
+import { SERVICES, installStub, openScreen } from "./stub.mjs";
+const b = await engine.launch();
+const ctx = await b.newContext({ viewport: { width: 1280, height: 800 } });
+await ctx.addInitScript(installStub, { screen: "library", services: SERVICES, twoFactor: null });
+await ctx.addInitScript(() => {
+ const big = (w, h, hue) => { const c = document.createElement("canvas"); c.width = w; c.height = h; const g = c.getContext("2d");
+ g.fillStyle = `hsl(${hue} 60% 50%)`; g.fillRect(0, 0, w, h); return c.toDataURL("image/jpeg", 0.5); };
+ const full = big(4000, 3000, 210), medium = big(1024, 768, 30);
+ new MutationObserver(() => document.querySelectorAll(".px-stage img").forEach((i) => {
+ if (i.src.startsWith("data:")) return;
+ i.src = i.classList.contains("px-sharp") ? full : medium;
+ })).observe(document, { subtree: true, childList: true, attributes: true, attributeFilter: ["src"] });
+});
+const page = await ctx.newPage();
+page.on("pageerror", (e) => { console.log(`FAIL page error: ${e.message}`); process.exitCode = 1; });
+await page.goto("http://localhost:4173", { waitUntil: "networkidle" });
+await openScreen(page, "Photos");
+await page.waitForTimeout(400);
+let failures = 0;
+const ok = (p, m) => { console.log(`${p ? "PASS" : "FAIL"} ${m}`); if (!p) failures++; };
+const boxes = () => page.evaluate(() => {
+ const r = (s) => { const e = document.querySelector(s); if (!e) return null; const b = e.getBoundingClientRect(); return { x: Math.round(b.x), y: Math.round(b.y), w: Math.round(b.width), h: Math.round(b.height) }; };
+ return { shown: r(".px-shown"), sharp: r(".px-sharp.is-in"), stage: r(".px-stage") };
+});
+const lies = (b) => b.sharp && b.shown && Math.abs(b.sharp.x - b.shown.x) <= 1 && Math.abs(b.sharp.y - b.shown.y) <= 1 &&
+ Math.abs(b.sharp.w - b.shown.w) <= 1 && Math.abs(b.sharp.h - b.shown.h) <= 1;
+const inside = (b) => b.shown.x >= b.stage.x - 1 && b.shown.y >= b.stage.y - 1 &&
+ b.shown.x + b.shown.w <= b.stage.x + b.stage.w + 1 && b.shown.y + b.shown.h <= b.stage.y + b.stage.h + 1;
+
+// The whole picture already there (on disk) as a photo opens: it must grow with the frame,
+// never stand full size over the one still growing.
+await page.locator(".px-tile").nth(1).dblclick();
+await page.waitForTimeout(1200);
+await page.keyboard.press("Escape");
+await page.waitForTimeout(700);
+await page.locator(".px-tile").nth(1).dblclick();
+await page.waitForTimeout(120);
+const mid = await page.evaluate(() => {
+ const a = document.querySelector(".px-shown")?.getBoundingClientRect(), s = document.querySelector(".px-sharp")?.getBoundingClientRect();
+ return a && s ? { a: Math.round(a.width), s: Math.round(s.width) } : null;
+});
+ok(mid && Math.abs(mid.a - mid.s) <= 1, `while it opens, the whole picture grows with it ${JSON.stringify(mid)}`);
+await page.waitForTimeout(1200);
+let b1 = await boxes();
+ok(lies(b1), `the whole picture lies exactly on the screen-sized one ${JSON.stringify(b1)}`);
+ok(inside(b1), "and both stay inside the stage");
+await page.setViewportSize({ width: 900, height: 620 });
+await page.waitForTimeout(500);
+b1 = await boxes();
+ok(lies(b1) && inside(b1), `after a resize too ${JSON.stringify(b1)}`);
+await page.setViewportSize({ width: 1280, height: 800 });
+await page.keyboard.press("ArrowLeft");
+await page.waitForTimeout(1200);
+b1 = await boxes();
+ok(lies(b1) && inside(b1), `and on the next photo ${JSON.stringify(b1)}`);
+await b.close();
+process.exitCode = failures ? 1 : (process.exitCode ?? 0);
diff --git a/check.sh b/check.sh
index 1e26753..9bbc8cc 100755
--- a/check.sh
+++ b/check.sh
@@ -65,6 +65,7 @@ step "photo captions" node .uiverify/photos-caption.mjs
step "photos real faults" node .uiverify/photos-real.mjs
step "whole photos" node .uiverify/photos-fit.mjs
step "star ratings" node .uiverify/photos-rating.mjs
+step "viewer, real sizes" node .uiverify/photos-viewer-real.mjs
step "notes delete" node .uiverify/notes-delete.mjs
step "empty today" node .uiverify/reminders-empty.mjs
step "calendar keys" node .uiverify/calendar-keys.mjs
@@ -113,6 +114,7 @@ step "webkit: captions" env ENGINE=webkit node .uiverify/photos-caption.mjs
step "webkit: real faults" env ENGINE=webkit node .uiverify/photos-real.mjs
step "webkit: whole photos" env ENGINE=webkit node .uiverify/photos-fit.mjs
step "webkit: ratings" env ENGINE=webkit node .uiverify/photos-rating.mjs
+step "webkit: viewer sizes" env ENGINE=webkit node .uiverify/photos-viewer-real.mjs
step "webkit: delete" env ENGINE=webkit node .uiverify/notes-delete.mjs
step "webkit: row hover" env ENGINE=webkit node .uiverify/row-hover.mjs
step "webkit: fold" env ENGINE=webkit node .uiverify/sidebar-fold.mjs
diff --git a/src/screens/PhotosView.tsx b/src/screens/PhotosView.tsx
index 0ff10d5..06c5711 100644
--- a/src/screens/PhotosView.tsx
+++ b/src/screens/PhotosView.tsx
@@ -316,12 +316,16 @@ export function PhotosView() {
// Each view keeps the sort chosen for it, on this machine. Albums keep their own order, and
// Recently Deleted its own, as in Photos.
+ // The view the photos on screen belong to. A view chosen changes the title, the months and
+ // the order only when its photos arrive, all with them — not a frame before, over the last
+ // view's photos.
+ const [drawn, setDrawn] = useState<{ view: PhotoView; album: PhotoAlbum | null }>({ view, album });
const [sorts, setSorts] = useState(readSorts);
- const sortable = !album && view !== "recentlyDeleted" && view !== "rated";
- const sort = sortable ? (sorts[view] ?? defaultSort(view)) : null;
+ const sortable = !drawn.album && drawn.view !== "recentlyDeleted" && drawn.view !== "rated";
+ const sort = sortable ? (sorts[drawn.view] ?? defaultSort(drawn.view)) : null;
const chooseSort = (next: Sort) => {
setSorts((all) => {
- const was = { ...all, [view]: next };
+ const was = { ...all, [drawn.view]: next };
try {
window.localStorage.setItem(SORTS_KEY, JSON.stringify(was));
} catch {
@@ -350,6 +354,7 @@ export function PhotosView() {
]);
if (mine !== reads.current) return;
setPhotos(list);
+ setDrawn({ view, album });
setCounts(Object.fromEntries(c));
setAlbums(a);
// An album deleted elsewhere is not left open, empty.
@@ -362,17 +367,17 @@ export function PhotosView() {
// Grouped by month, as the library reads; an album is one grid in its own order.
const months = useMemo(() => {
const out: { label: string; items: { photo: Photo; index: number }[] }[] = [];
- if (album) return [{ label: "", items: (photos ?? []).map((photo, index) => ({ photo, index })) }];
+ if (drawn.album) return [{ label: "", items: (photos ?? []).map((photo, index) => ({ photo, index })) }];
(photos ?? []).forEach((photo, index) => {
// Rated is grouped by how many stars, as icloud.com's Rated page is.
const stars = photo.rating ?? 0;
- const label = view === "rated" ? `${stars} ${stars === 1 ? "Star" : "Stars"}` : monthOf(sortKey(photo, sort?.by ?? "taken"));
+ const label = drawn.view === "rated" && !drawn.album ? `${stars} ${stars === 1 ? "Star" : "Stars"}` : monthOf(sortKey(photo, sort?.by ?? "taken"));
const last = out[out.length - 1];
if (last && last.label === label) last.items.push({ photo, index });
else out.push({ label, items: [{ photo, index }] });
});
return out;
- }, [photos, sort?.by, album, view]);
+ }, [photos, sort?.by, drawn]);
const shown = open !== null ? photos?.[open] ?? null : null;
@@ -445,7 +450,10 @@ export function PhotosView() {
origin.current = null;
const el = viewer.current;
if (!from || !el || still() || typeof el.animate !== "function") return;
- const img = el.querySelector<HTMLElement>(".px-shown");
+ // The photo's whole frame moves — the screen-sized picture and the whole one over it
+ // together. Moving the one alone, the whole one (already on disk) stood full size over
+ // it as it grew: two photos, torn.
+ const img = el.querySelector<HTMLElement>(".px-pan, .px-video");
const t = img && toTile(img, from);
if (img && t) img.animate([{ transform: t }, { transform: "none" }], { duration: 460, easing: spring() });
for (const part of el.querySelectorAll<HTMLElement>(".px-backdrop, .px-viewer-bar, .px-nav, .px-info")) {
@@ -457,7 +465,7 @@ export function PhotosView() {
const el = viewer.current;
const current = open !== null ? photos?.[open] : null;
const tile = current ? document.querySelector<HTMLElement>(`.px-tile[data-id="${CSS.escape(current.id)}"]`) : null;
- const img = el?.querySelector<HTMLElement>(".px-shown");
+ const img = el?.querySelector<HTMLElement>(".px-pan, .px-video");
const box = tile?.getBoundingClientRect();
const scroller = tile?.closest(".px-scroll")?.getBoundingClientRect();
// Only back into a tile that is on screen; otherwise the viewer just fades.
@@ -1285,7 +1293,13 @@ export function PhotosView() {
return () => window.removeEventListener("keydown", onKey);
}, []);
- const title = album?.name ?? [...VIEWS, ...MEDIA, ...UTILITIES].find((v) => v.key === view)?.label ?? "Photos";
+ const title = drawn.album
+ ? (album?.id === drawn.album.id ? album.name : drawn.album.name)
+ : [...VIEWS, ...MEDIA, ...UTILITIES].find((v) => v.key === drawn.view)?.label ?? "Photos";
+ // A view arrives at its top, not wherever the last one was scrolled to.
+ useLayoutEffect(() => {
+ if (scroller.current) scroller.current.scrollTop = 0;
+ }, [drawn]);
const across = ACROSS[zoom];
const source = (v: (typeof VIEWS)[number]) => (
@@ -1493,12 +1507,12 @@ export function PhotosView() {
}} data-fit={fit || undefined} style={{ "--across": across } as React.CSSProperties}>
{months.map((m, k) => (
<Month
- key={`${view}/${album?.id ?? ""}/${k}/${m.label}`}
+ key={`${k}/${m.label}`}
label={m.label}
items={m.items}
chosen={chosen}
anchor={anchor.current}
- deleted={view === "recentlyDeleted" && !album}
+ deleted={drawn.view === "recentlyDeleted" && !drawn.album}
eager={k < 3}
across={across}
fit={fit}
diff --git a/src/styles/photos.css b/src/styles/photos.css
index e28241f..1b462d6 100644
--- a/src/styles/photos.css
+++ b/src/styles/photos.css
@@ -318,7 +318,20 @@
.px-pan.is-zoomed { cursor: grab; }
.px-pan.is-zoomed:active { cursor: grabbing; }
.px-pan > img { grid-area: 1 / 1; }
-.px-sharp { width: 100%; height: 100%; max-width: none; max-height: none; opacity: 0; transition: opacity 180ms ease; pointer-events: none; }
+/* Laid exactly over the screen-sized picture, out of the layout: however large the whole picture
+ is, it never sizes the frame. */
+.px-sharp {
+ position: absolute;
+ inset: 0;
+ width: 100%;
+ height: 100%;
+ max-width: none;
+ max-height: none;
+ object-fit: contain;
+ opacity: 0;
+ transition: opacity 180ms ease;
+ pointer-events: none;
+}
.px-sharp.is-in { opacity: 1; }
/* The thumbnail, already on disk, fills the photo's box until the sharper picture covers it. */
.px-shown { background-size: 100% 100%; transform-origin: center; will-change: transform; }