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: quick at twenty thousand photos; the arrows keep the focus in WebKit

Yashwanth Kamireddic04e9591d30f
diff --git a/.uiverify/photos-scale.mjs b/.uiverify/photos-scale.mjs
new file mode 100644
index 0000000..28a12ec
--- /dev/null
+++ b/.uiverify/photos-scale.mjs
@@ -0,0 +1,57 @@
+// Photos at the size of a real library — twenty thousand: only the months near the view are
+// built, a month not yet built is exactly as tall as it will be (nothing jumps), a click that
+// selects stays quick, and the arrows keep going into months that were not built.
+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 P = (id, t) => ({ id, master: `m${id}`, takenAt: t, addedAt: t, kind: "image", width: 4032, height: 3024, duration: null,
+ filename: `IMG_${id}.JPG`, size: 2400000, uti: "public.jpeg", favorite: false, hidden: false, deleted: false, deletedAt: null, subtype: 0, changeTag: null });
+ window.__photos = Array.from({ length: 20000 }, (_, i) => P(`q${i}`, 1792600000 - i * 40000));
+});
+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" });
+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();
+await page.waitForTimeout(500);
+
+const built = await page.locator(".px-tile").count();
+ok(built > 0 && built < 1500, `only the months near the view are built (${built} of 20000 tiles)`);
+
+const sizes = await page.evaluate(async () => {
+ const months = [...document.querySelectorAll(".px-month")];
+ const later = months.find((s) => s.querySelector(".px-later"));
+ const before = later.getBoundingClientRect().height;
+ later.scrollIntoView();
+ await new Promise((r) => setTimeout(r, 400));
+ return { before, after: later.getBoundingClientRect().height, built: !later.querySelector(".px-later") };
+});
+ok(sizes.built && Math.abs(sizes.before - sizes.after) < 1,
+ `a month is as tall before it is built as after ${sizes.before.toFixed(1)} → ${sizes.after.toFixed(1)}px`);
+
+await page.locator(".px-scroll").evaluate((e) => (e.scrollTop = 0));
+await page.waitForTimeout(300);
+const ms = await page.evaluate(async () => {
+ const t = performance.now();
+ document.querySelectorAll(".px-tile")[3].click();
+ await new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)));
+ return performance.now() - t;
+});
+ok(ms < 150, `a click that selects stays quick (${Math.round(ms)}ms, two frames included)`);
+
+// Down, row after row, past the months that were built when it began — after a real click,
+// with the app list holding the focus first, as it does once Photos is chosen from it.
+await page.locator(".nav-item", { hasText: "Photos" }).focus();
+await page.locator(".px-tile").nth(3).click();
+const start = await page.locator('.px-tile[aria-selected="true"]').getAttribute("data-index");
+for (let i = 0; i < 60; i++) await page.keyboard.press("ArrowDown");
+await page.waitForTimeout(300);
+const end = await page.locator('.px-tile[aria-selected="true"]').getAttribute("data-index");
+ok(Number(end) > built, `the down arrow keeps going into months not built at first (tile ${start} → ${end}, ${built} built at first)`);
+await b.close();
+process.exitCode = failures ? 1 : (process.exitCode ?? 0);
diff --git a/.uiverify/photos.mjs b/.uiverify/photos.mjs
index 532ad55..401f0b6 100644
--- a/.uiverify/photos.mjs
+++ b/.uiverify/photos.mjs
@@ -128,7 +128,11 @@ await page.keyboard.press("ArrowRight");
ok((await page.locator(".px-tile").nth(1).getAttribute("aria-selected")) === "true", "the right arrow moves the selection on");
await page.keyboard.press("ArrowDown");
const downAt = await page.locator(".px-tile").evaluateAll((t) => t.findIndex((x) => x.getAttribute("aria-selected") === "true"));
-ok(downAt > 1, `the down arrow goes to the row below, across a month → tile ${downAt}`);
+ok(downAt === 4, `the down arrow goes to the same column of the next month → tile ${downAt}`);
+await page.keyboard.press("ArrowUp");
+const upAt = await page.locator(".px-tile").evaluateAll((t) => t.findIndex((x) => x.getAttribute("aria-selected") === "true"));
+ok(upAt === 1, `and the up arrow comes back to it → tile ${upAt}`);
+await page.keyboard.press("ArrowDown");
await page.keyboard.press("Enter");
await page.waitForTimeout(500);
ok((await page.locator(".px-viewer").count()) === 1, "Return opens the selected photo");
diff --git a/check.sh b/check.sh
index f29409d..3654150 100755
--- a/check.sh
+++ b/check.sh
@@ -56,6 +56,7 @@ step "notes autoformat" node .uiverify/notes-autoformat.mjs
step "reminder ticks" node .uiverify/reminders-tick.mjs
step "photos" node .uiverify/photos.mjs
step "photo albums" node .uiverify/photos-albums.mjs
+step "photos at scale" node .uiverify/photos-scale.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
@@ -95,6 +96,7 @@ step "webkit: autoformat" env ENGINE=webkit node .uiverify/notes-autoformat.mjs
step "webkit: ticks" env ENGINE=webkit node .uiverify/reminders-tick.mjs
step "webkit: photos" env ENGINE=webkit node .uiverify/photos.mjs
step "webkit: albums" env ENGINE=webkit node .uiverify/photos-albums.mjs
+step "webkit: at scale" env ENGINE=webkit node .uiverify/photos-scale.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 89ba4fa..12be2d0 100644
--- a/src/screens/PhotosView.tsx
+++ b/src/screens/PhotosView.tsx
@@ -3,7 +3,7 @@
* for how many across; a photo opens on a dark stage with the arrows to go through them. Everything draws from the local library at once; thumbnails come
* from the disk cache and are fetched once.
*/
-import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
+import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import { listen } from "@tauri-apps/api/event";
import { getCurrentWebview } from "@tauri-apps/api/webview";
import { open as openFileDialog } from "@tauri-apps/plugin-dialog";
@@ -82,6 +82,136 @@ const clock = (s: number | null) => {
return `${m}:${String(Math.round(s % 60)).padStart(2, "0")}`;
};
+/** What a tile can ask of the screen; one object, kept the same, so tiles need not redraw. */
+type Acts = {
+ pick: (index: number, e: React.MouseEvent) => void;
+ open: (index: number) => void;
+ menu: (photo: Photo, e: React.MouseEvent) => void;
+};
+
+/**
+ * One photo in the grid. It redraws only when its own photo, its selection or its place
+ * changes: a click that selects one of twenty thousand redraws two tiles, not all of them.
+ */
+const Tile = memo(function Tile({
+ photo,
+ index,
+ selected,
+ tabbable,
+ left,
+ soon,
+ act,
+}: {
+ photo: Photo;
+ index: number;
+ selected: boolean;
+ tabbable: boolean;
+ left: string | null;
+ soon: boolean;
+ act: Acts;
+}) {
+ return (
+ <button
+ role="listitem"
+ className="px-tile"
+ aria-label={`${photo.filename ?? "Photo"}, ${new Date(photo.takenAt * 1000).toLocaleString()}`}
+ data-id={photo.id}
+ data-index={index}
+ aria-selected={selected}
+ tabIndex={tabbable ? 0 : -1}
+ onClick={(e) => {
+ // The keys follow the click, as in Photos: WebKit does not focus a button it clicks,
+ // and the arrows went on to whatever had the focus before — the app list, which they
+ // moved through instead of the photos.
+ e.currentTarget.focus({ preventScroll: true });
+ act.pick(index, e);
+ }}
+ onDoubleClick={() => act.open(index)}
+ onContextMenu={(e) => {
+ e.preventDefault();
+ act.menu(photo, e);
+ }}
+ >
+ <img src={pictureSrc(photo.id, "thumb")} alt="" loading="lazy" decoding="async" draggable={false} />
+ {photo.kind === "video" && (
+ <span className="px-badge px-duration">
+ <Play size={10} strokeWidth={0} fill="currentColor" aria-hidden="true" />
+ {clock(photo.duration)}
+ </span>
+ )}
+ {left && <span className={`px-badge px-left${soon ? " is-soon" : ""}`}>{left}</span>}
+ {photo.favorite && <Heart className="px-heart" size={13} strokeWidth={0} fill="currentColor" aria-label="Favorite" />}
+ </button>
+ );
+});
+
+/**
+ * A month of the grid. Only months near the view are built: one further off is an empty
+ * block exactly as tall as its rows will be (worked out in CSS from the grid's own width),
+ * so a library of tens of thousands opens at once and scrolls without the bar jumping.
+ */
+const Month = memo(function Month({
+ label,
+ items,
+ chosen,
+ anchor,
+ deleted,
+ eager,
+ across,
+ act,
+}: {
+ label: string;
+ items: { photo: Photo; index: number }[];
+ chosen: Set<string>;
+ anchor: number | null;
+ deleted: boolean;
+ eager: boolean;
+ across: number;
+ act: Acts;
+}) {
+ const ref = useRef<HTMLElement | null>(null);
+ const [live, setLive] = useState(eager);
+ useEffect(() => {
+ const el = ref.current;
+ if (!el || typeof IntersectionObserver === "undefined") {
+ setLive(true);
+ return undefined;
+ }
+ const watch = new IntersectionObserver(([e]) => setLive(e.isIntersecting), {
+ root: el.closest(".px-scroll"),
+ rootMargin: "1600px 0px",
+ });
+ watch.observe(el);
+ return () => watch.disconnect();
+ }, []);
+ return (
+ <section ref={ref} className="px-month" aria-label={label || undefined}>
+ {label && <h2 className="px-month-title">{label}</h2>}
+ {live ? (
+ <div className="px-grid" role="list">
+ {items.map(({ photo, index }) => {
+ const t = deleted ? timeLeft(photo, true) : null;
+ return (
+ <Tile
+ key={photo.id}
+ photo={photo}
+ index={index}
+ selected={chosen.has(photo.id)}
+ tabbable={anchor === index || (anchor === null && index === 0)}
+ left={t?.words ?? null}
+ soon={t?.soon ?? false}
+ act={act}
+ />
+ );
+ })}
+ </div>
+ ) : (
+ <div className="px-later" style={{ "--rows": Math.ceil(items.length / across) } as React.CSSProperties} aria-hidden="true" />
+ )}
+ </section>
+ );
+});
+
export function PhotosView() {
const { revision } = useSync();
const [view, setView] = useState<PhotoView>("library");
@@ -539,26 +669,47 @@ export function PhotosView() {
// The tile to land on from another by an arrow: the nearest in that direction as laid out,
// so moving down from the last row of a month goes into the next month's first row.
const neighbour = (from: number, key: string): number | null => {
- const tiles = [...document.querySelectorAll<HTMLElement>(".px-scroll .px-tile")];
- const at = tiles[from]?.getBoundingClientRect();
- if (!at) return null;
+ const count = photos?.length ?? 0;
if (key === "ArrowLeft") return from > 0 ? from - 1 : null;
- if (key === "ArrowRight") return from < tiles.length - 1 ? from + 1 : null;
- const down = key === "ArrowDown";
- let best: number | null = null;
- let bestScore = Infinity;
- const cx = at.left + at.width / 2;
- tiles.forEach((t, i) => {
- const r = t.getBoundingClientRect();
- const ahead = down ? r.top >= at.bottom - 1 : r.bottom <= at.top + 1;
- if (!ahead) return;
- const score = Math.abs(down ? r.top - at.bottom : at.top - r.bottom) * 1000 + Math.abs(r.left + r.width / 2 - cx);
- if (score < bestScore) {
- bestScore = score;
- best = i;
+ if (key === "ArrowRight") return from < count - 1 ? from + 1 : null;
+ // Up and down follow the grid as laid out: the same column a row away, and across a
+ // month's edge into the next month's first row or the last month's last. Worked out
+ // from the months themselves, so it reaches months that are not built yet.
+ const k = months.findIndex((m) => m.items.length && from >= m.items[0].index && from <= m.items[m.items.length - 1].index);
+ if (k < 0) return null;
+ const m = months[k];
+ const at = from - m.items[0].index;
+ const col = at % across;
+ if (key === "ArrowDown") {
+ if (at + across < m.items.length) return from + across;
+ // Still a row below in this month, but shorter: its last photo.
+ if (Math.floor(at / across) < Math.floor((m.items.length - 1) / across)) return m.items[m.items.length - 1].index;
+ const next = months[k + 1];
+ return next ? next.items[Math.min(col, next.items.length - 1)].index : null;
+ }
+ if (at - across >= 0) return from - across;
+ const last = months[k - 1];
+ if (!last) return null;
+ const lastRow = Math.floor((last.items.length - 1) / across) * across;
+ return last.items[Math.min(lastRow + col, last.items.length - 1)].index;
+ };
+
+ // The tile at an index, focused and in view; built first if its month is not yet.
+ const reach = (to: number) => {
+ const sel = `.px-scroll .px-tile[data-index="${to}"]`;
+ let tries = 0;
+ const go = () => {
+ const tile = document.querySelector<HTMLElement>(sel);
+ if (tile) {
+ tile.focus({ preventScroll: true });
+ tile.scrollIntoView({ block: "nearest" });
+ return;
}
- });
- return best;
+ const k = months.findIndex((m) => m.items.some((x) => x.index === to));
+ document.querySelectorAll<HTMLElement>(".px-scroll .px-month")[k]?.scrollIntoView({ block: "nearest" });
+ if (++tries < 12) requestAnimationFrame(go);
+ };
+ go();
};
// The grid's keys, while no photo is open and nothing is being typed.
@@ -583,9 +734,7 @@ export function PhotosView() {
if (e.shiftKey && anchor.current !== null) setChosen((c) => new Set([...c, list[to].id]));
else setChosen(new Set([list[to].id]));
anchor.current = to;
- const tile = document.querySelectorAll<HTMLElement>(".px-scroll .px-tile")[to];
- tile?.focus({ preventScroll: true });
- tile?.scrollIntoView({ block: "nearest" });
+ reach(to);
} else if ((e.key === "Delete" || e.key === "Backspace") && selected.length) {
if (album) void removeFrom(selected);
else if (view !== "recentlyDeleted") void changeAll(selected, "deleted", true);
@@ -667,6 +816,18 @@ export function PhotosView() {
setMenu({ at, entries });
};
+ // The tiles' handlers, one object for the life of the screen; each call reaches the newest.
+ const acts = useRef({ pick, openAt, photoMenu });
+ acts.current = { pick, openAt, photoMenu };
+ const act = useMemo<Acts>(
+ () => ({
+ pick: (i, e) => acts.current.pick(i, e),
+ open: (i) => acts.current.openAt(i),
+ menu: (p, e) => acts.current.photoMenu(p, { x: e.clientX, y: e.clientY, alignRight: false }),
+ }),
+ [],
+ );
+
// Enter submits and closes the field, and closing it blurs it, which would submit again
// with the same name still in hand: one album per name typed.
const namedOnce = useRef(false);
@@ -1006,43 +1167,18 @@ export function PhotosView() {
// A click on the space between photos lets the selection go, as in Photos.
if (!(e.target as HTMLElement).closest(".px-tile")) setChosen(new Set());
}} style={{ "--across": across } as React.CSSProperties}>
- {months.map((m) => (
- <section key={m.label} className="px-month" aria-label={m.label}>
- {m.label && <h2 className="px-month-title">{m.label}</h2>}
- <div className="px-grid" role="list">
- {m.items.map(({ photo, index }) => (
- <button
- key={photo.id}
- role="listitem"
- className="px-tile"
- aria-label={`${photo.filename ?? "Photo"}, ${new Date(photo.takenAt * 1000).toLocaleString()}`}
- data-id={photo.id}
- aria-selected={chosen.has(photo.id)}
- tabIndex={anchor.current === index || (anchor.current === null && index === 0) ? 0 : -1}
- onClick={(e) => pick(index, e)}
- onDoubleClick={() => openAt(index)}
- onContextMenu={(e) => {
- e.preventDefault();
- photoMenu(photo, { x: e.clientX, y: e.clientY, alignRight: false });
- }}
- >
- <img src={pictureSrc(photo.id, "thumb")} alt="" loading="lazy" decoding="async" draggable={false} />
- {photo.kind === "video" && (
- <span className="px-badge px-duration">
- <Play size={10} strokeWidth={0} fill="currentColor" aria-hidden="true" />
- {clock(photo.duration)}
- </span>
- )}
- {view === "recentlyDeleted" && !album && timeLeft(photo, true) && (
- <span className={`px-badge px-left${timeLeft(photo, true)!.soon ? " is-soon" : ""}`}>{timeLeft(photo, true)!.words}</span>
- )}
- {photo.favorite && (
- <Heart className="px-heart" size={13} strokeWidth={0} fill="currentColor" aria-label="Favorite" />
- )}
- </button>
- ))}
- </div>
- </section>
+ {months.map((m, k) => (
+ <Month
+ key={`${view}/${album?.id ?? ""}/${m.label}`}
+ label={m.label}
+ items={m.items}
+ chosen={chosen}
+ anchor={anchor.current}
+ deleted={view === "recentlyDeleted" && !album}
+ eager={k < 3}
+ across={across}
+ act={act}
+ />
))}
{tally && <p className="px-tally">{tally}</p>}
</div>
diff --git a/src/styles/photos.css b/src/styles/photos.css
index 13f27f8..826c298 100644
--- a/src/styles/photos.css
+++ b/src/styles/photos.css
@@ -202,9 +202,18 @@
-webkit-mask-image: linear-gradient(to bottom, transparent 0, transparent 44px, #000 104px);
mask-image: linear-gradient(to bottom, transparent 0, transparent 44px, #000 104px);
}
-/* A month off screen is not laid out or painted until it nears it: a library of thousands
- scrolls as lightly as one of a hundred. */
-.px-month { content-visibility: auto; contain-intrinsic-size: auto 600px; }
+/* A month not yet built: one cell laid out on the very grid its photos will use, as tall as
+ all its rows — square tiles, 2px between — so nothing moves when it is built. */
+.px-later {
+ display: grid;
+ grid-template-columns: repeat(var(--across, 7), minmax(0, 1fr));
+ column-gap: 2px;
+}
+.px-later::before {
+ content: "";
+ aspect-ratio: 1 / var(--rows);
+ margin-bottom: calc((var(--rows) - 1) * 2px);
+}
.px-month-title {
margin: 0;
padding: 12px 16px 8px;