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: Years and Months stay by date taken when All Photos is by Date Added

Yashwanth Kamireddia73699078a9d
diff --git a/.uiverify/photos-levels-added.mjs b/.uiverify/photos-levels-added.mjs
new file mode 100644
index 0000000..282f651
--- /dev/null
+++ b/.uiverify/photos-levels-added.mjs
@@ -0,0 +1,95 @@
+// Years and Months are by when photos were taken, whatever All Photos is sorted by. Sorted by
+// Date Added — an old camera roll brought in last month — a month picked in Months opens All
+// Photos at that month's own photo, not at the top, and stepping back out lands on the month
+// the photo at the top was taken in, not the month it was added.
+//
+// URL=http://localhost:4173 ENGINE=webkit node .uiverify/photos-levels-added.mjs
+import { engine } from "./engine.mjs";
+import { SERVICES, installStub, openScreen } from "./stub.mjs";
+import { servePictures } from "./arrival.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 servePictures(ctx);
+// Three years, nine photos a month from November 2023 to October 2026; everything taken before
+// 2025 was added on 20 October 2026, in one go.
+await ctx.addInitScript(() => {
+ if (window.__photos) return;
+ const P = (id, t, added) => ({ id, master: `m${id}`, takenAt: t, addedAt: added, kind: "image", width: 4032, height: 3024, duration: null,
+ filename: `IMG_${id}.JPG`, size: 1, uti: "public.jpeg", favorite: false, hidden: false, deleted: false, deletedAt: null, subtype: 0, changeTag: null });
+ const batch = new Date(2026, 9, 20, 12).getTime() / 1000;
+ const out = [];
+ for (let m = 0; m < 36; m++) {
+ const d = new Date(2026, 9 - m, 1);
+ for (let k = 0; k < 9; k++) {
+ const t = new Date(d.getFullYear(), d.getMonth(), 2 + k * 3, 12).getTime() / 1000;
+ out.push(P(`y${d.getFullYear()}m${d.getMonth() + 1}k${k}`, t, d.getFullYear() < 2025 ? batch - out.length : t));
+ }
+ }
+ window.__photos = out.sort((a, b) => b.takenAt - a.takenAt);
+});
+const page = await ctx.newPage();
+page.on("pageerror", (e) => { console.log(`FAIL page error: ${e.message}`); process.exitCode = 1; });
+await page.goto(process.env.URL ?? "http://localhost:4173", { waitUntil: "networkidle" });
+let failures = 0;
+const ok = (p, m) => { console.log(`${p ? "PASS" : "FAIL"} ${m}`); if (!p) failures++; };
+await openScreen(page, "Photos");
+await page.locator(".px-tile").first().waitFor();
+const seg = (name) => page.locator(".px-levels").getByRole("button", { name, exact: true });
+// The tiles in the grid's first row under the floating bar.
+const gridTopIds = () => page.evaluate(() => {
+ const el = document.querySelector(".px-scroll");
+ const edge = el.getBoundingClientRect().top + parseFloat(getComputedStyle(el).scrollPaddingTop);
+ const tiles = [...el.querySelectorAll(".px-tile")];
+ const first = tiles.find((t) => t.getBoundingClientRect().bottom > edge + 2);
+ if (!first) return [];
+ const top = first.getBoundingClientRect().top;
+ return tiles.filter((t) => Math.abs(t.getBoundingClientRect().top - top) < 2).map((t) => t.dataset.id);
+});
+
+// Sorted by Date Added: the batch is one month of the grid, October 2026.
+await page.getByRole("button", { name: "Sort" }).click();
+await page.locator(".row-menu").getByText("Date Added").click();
+await page.waitForTimeout(300);
+const titles = await page.locator(".px-month-title").allInnerTexts();
+ok(titles.length > 0 && !titles.some((t) => /2024|2023/.test(t)), `All Photos by Date Added groups by the month photos came in ${JSON.stringify(titles.slice(0, 4))}`);
+
+// Months still by date taken.
+await seg("Years").click();
+await page.locator('.px-period[data-period="2024"]').click();
+await page.locator('.px-periods[data-level="months"] .px-period').first().waitFor();
+await page.waitForTimeout(300);
+ok(await page.locator('.px-period[data-period="2024-3"]').count() === 1, "Months still has every month photos were taken in");
+
+// March 2024 picked: All Photos opens on its photo, inside the batch.
+const key = await page.evaluate(async () => (await window.__TAURI_INTERNALS__.invoke("photos_months")).find((m) => m.year === 2024 && m.month === 3).key);
+await page.locator('.px-period[data-period="2024-3"]').click();
+await page.waitForFunction(() => document.querySelector('.px-levels [aria-pressed="true"]')?.textContent === "All Photos");
+await page.waitForTimeout(400);
+const top = await gridTopIds();
+ok(top.includes(key), `a month opens All Photos at that month's photo (${key} in top row ${top.join(",")})`);
+
+// And back out: Months at the month the top photo was taken in, not October 2026.
+await page.evaluate(() => document.activeElement?.blur());
+await page.keyboard.press("Escape");
+await page.locator('.px-periods[data-level="months"]').waitFor();
+await page.waitForTimeout(300);
+// The month it lands on takes the keys (March 2024 is too near the end to scroll to the top).
+const at = await page.evaluate(() => document.activeElement?.dataset.period);
+const want = /y(\d+)m(\d+)/.exec(top[0] ?? "");
+ok(want && at === `${want[1]}-${want[2]}`, `stepping out lands on the month it was taken (${want?.slice(1).join("-")}; at ${at})`);
+
+// Date Captured again: the months are the months taken, and the same jump lands on the month.
+await seg("All Photos").click();
+await page.getByRole("button", { name: "Sort" }).click();
+await page.locator(".row-menu").getByText("Date Captured").click();
+await page.waitForTimeout(300);
+await seg("Years").click();
+await page.locator('.px-period[data-period="2024"]').click();
+await page.locator('.px-period[data-period="2024-3"]').click();
+await page.waitForFunction(() => document.querySelector('.px-levels [aria-pressed="true"]')?.textContent === "All Photos");
+await page.waitForTimeout(400);
+const heading = await page.locator(".px-heading .px-span").textContent();
+ok(/March 2024/.test(heading ?? ""), `by Date Captured a month opens at its own month (${heading})`);
+await b.close();
+process.exitCode = failures ? 1 : (process.exitCode ?? 0);
diff --git a/src/screens/PhotosView.tsx b/src/screens/PhotosView.tsx
index 5f53a7b..e0b2a08 100644
--- a/src/screens/PhotosView.tsx
+++ b/src/screens/PhotosView.tsx
@@ -2633,43 +2633,71 @@ export function PhotosView() {
return { label: m.month, year: d.getFullYear(), month: d.getMonth(), cover: { id: m.items[0].id } };
});
}, [months, drawn]); // eslint-disable-line react-hooks/exhaustive-deps
+ /**
+ * The grid scrolled so `offset` (how far what is wanted sits below the top, measured afresh)
+ * is nothing. Months drawn as they come into view take their real height, and those above the
+ * one chosen move it. Chromium's scroll anchoring holds it in place; WebKit has none, and a
+ * month twenty years down landed a year off. So, for a moment after an instant jump, it is held
+ * at the top — let go at once if the person scrolls or presses a key themselves.
+ */
+ const scrollHeld = (el: HTMLElement, offset: () => number | null, instant: boolean) => {
+ const first = offset();
+ if (first === null) return;
+ const now = instant || still();
+ el.scrollTo({ top: el.scrollTop + first, behavior: now ? "auto" : "smooth" });
+ if (!now) return;
+ let frames = 0;
+ let held = true;
+ const letGo = () => { held = false; };
+ const opts = { once: true, passive: true } as const;
+ el.addEventListener("wheel", letGo, opts);
+ el.addEventListener("pointerdown", letGo, opts);
+ el.addEventListener("touchstart", letGo, opts);
+ window.addEventListener("keydown", letGo, { once: true });
+ const hold = () => {
+ const off = held ? offset() : null;
+ if (off === null) return;
+ if (Math.abs(off) > 1) el.scrollTop += off;
+ if (++frames < 30) requestAnimationFrame(hold);
+ else {
+ el.removeEventListener("wheel", letGo);
+ el.removeEventListener("pointerdown", letGo);
+ el.removeEventListener("touchstart", letGo);
+ window.removeEventListener("keydown", letGo);
+ }
+ };
+ requestAnimationFrame(hold);
+ };
const goToMonth = (label: string, instant = false) => {
const el = scroller.current;
const at = el?.querySelector<HTMLElement>(`.px-month[data-month="${CSS.escape(label)}"]`);
// Measured against the scroller itself: offsetTop counts from whatever positioned box
// happens to hold the month, which is not always the scroller.
- if (el && at) {
- const top = at.getBoundingClientRect().top - el.getBoundingClientRect().top + el.scrollTop - 4;
- const now = instant || still();
- el.scrollTo({ top, behavior: now ? "auto" : "smooth" });
- // Months drawn as they come into view take their real height, and those above the one
- // chosen move it. Chromium's scroll anchoring holds it in place; WebKit has none, and a
- // month twenty years down landed a year off. So, for a moment after an instant jump, it
- // is held at the top — let go at once if the person scrolls or presses a key themselves.
- if (now) {
- let frames = 0;
- let held = true;
- const letGo = () => { held = false; };
- const opts = { once: true, passive: true } as const;
- el.addEventListener("wheel", letGo, opts);
- el.addEventListener("pointerdown", letGo, opts);
- el.addEventListener("touchstart", letGo, opts);
- window.addEventListener("keydown", letGo, { once: true });
- const hold = () => {
- if (!held || !at.isConnected) return;
- const off = at.getBoundingClientRect().top - el.getBoundingClientRect().top - 4;
- if (Math.abs(off) > 1) el.scrollTop += off;
- if (++frames < 30) requestAnimationFrame(hold);
- else {
- el.removeEventListener("wheel", letGo);
- el.removeEventListener("pointerdown", letGo);
- el.removeEventListener("touchstart", letGo);
- window.removeEventListener("keydown", letGo);
- }
- };
- requestAnimationFrame(hold);
- }
- }
+ if (el && at) scrollHeld(el, () => (at.isConnected ? at.getBoundingClientRect().top - el.getBoundingClientRect().top - 4 : null), instant);
+ };
+ /**
+ * How far below the grid's top the tile at an index is: its own box, or — its month not built
+ * yet — where its row will be in the month's block, whose height is exactly its rows'.
+ */
+ const tileBelow = (index: number): number | null => {
+ const el = scroller.current;
+ if (!el) return null;
+ const box = el.getBoundingClientRect();
+ const tile = el.querySelector<HTMLElement>(`.px-tile[data-index="${index}"]`);
+ if (tile) return tile.getBoundingClientRect().top - box.top;
+ const g = months.findIndex((m) => m.items.length && index >= m.items[0].index && index <= m.items[m.items.length - 1].index);
+ const sec = el.querySelectorAll<HTMLElement>(".px-month")[g];
+ if (!sec) return null;
+ const row = Math.floor((index - months[g].items[0].index) / across);
+ const r = photosArea(sec);
+ return r.top - box.top + (row * r.height) / Math.ceil(months[g].items.length / across);
+ };
+ /** The tile at an index brought to the top of the grid, under the bar. */
+ const goToPhoto = (index: number) => {
+ const el = scroller.current;
+ if (!el) return;
+ const pad = parseFloat(getComputedStyle(el).scrollPaddingTop) || 0;
+ scrollHeld(el, () => { const t = tileBelow(index); return t === null ? null : t - pad; }, true);
};
const scroller = useRef<HTMLDivElement | null>(null);
const looking = useRef(false);
@@ -2732,6 +2760,18 @@ export function PhotosView() {
const toAll = useRef<Place | null | undefined>(undefined);
/** The month at the top of All Photos. */
const gridPlace = (): Place | null => {
+ // Sorted by Date Added the grid's months are when photos came in; Years and Months are by
+ // when they were taken, so the place is the month the photo at the top was taken in.
+ if (sort?.by === "added" && photos) {
+ const el = scroller.current;
+ const box = el?.getBoundingClientRect();
+ const pad = el ? parseFloat(getComputedStyle(el).scrollPaddingTop) || 0 : 0;
+ const top = box && [...el!.querySelectorAll<HTMLElement>(".px-tile")].find((t) => t.getBoundingClientRect().bottom > box.top + pad);
+ const p = top ? photos.at(Number(top.dataset.index)) : photos.at(0);
+ if (!p) return null;
+ const d = new Date(p.takenAt * 1000);
+ return { year: d.getFullYear(), month: d.getMonth() + 1 };
+ }
const label = here ?? months[0]?.month;
const g = months.find((m) => m.month === label && m.items.length);
if (!g || !photos) return null;
@@ -2771,6 +2811,15 @@ export function PhotosView() {
return;
}
if (!to) return;
+ // Sorted by Date Added, a month taken is spread through the months photos came in: All
+ // Photos opens at the photo that stands for it in Months.
+ if (sort?.by === "added") {
+ const of = (periodMonths ?? []).filter((m) => m.year === to.year && (to.month === undefined || m.month === to.month));
+ const key = (sort.oldest ? of[of.length - 1] : of[0])?.key;
+ const index = key && photos ? photos.indexOf(key) : -1;
+ if (index >= 0) goToPhoto(index);
+ return;
+ }
// A year alone: its first month as the grid shows them.
const label = to.month
? monthName(to.year, to.month)
@@ -2862,22 +2911,8 @@ export function PhotosView() {
keepAt.current = null;
const el = scroller.current;
if (!k || !el) return;
- const box = el.getBoundingClientRect();
- const tile = el.querySelector<HTMLElement>(`.px-tile[data-index="${k.index}"]`);
- let top: number | null = null;
- if (tile) top = tile.getBoundingClientRect().top - box.top + el.scrollTop;
- else {
- // Its group not built yet: where its row will be, from the group's own block, whose
- // height is exactly its rows'.
- const g = months.findIndex((m) => m.items.length && k.index >= m.items[0].index && k.index <= m.items[m.items.length - 1].index);
- const sec = el.querySelectorAll<HTMLElement>(".px-month")[g];
- if (sec) {
- const row = Math.floor((k.index - months[g].items[0].index) / across);
- const r = photosArea(sec);
- top = r.top - box.top + el.scrollTop + (row * r.height) / Math.ceil(months[g].items.length / across);
- }
- }
- if (top !== null) el.scrollTop = Math.max(0, top - k.top);
+ const below = tileBelow(k.index);
+ if (below !== null) el.scrollTop = Math.max(0, below + el.scrollTop - k.top);
}, [across]); // eslint-disable-line react-hooks/exhaustive-deps
/*
* Photos landing above the view leave it on the photos it showed, as a chat keeps its place