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: animations that cannot flash; the grid not drawn behind an open photo

Yashwanth Kamireddi08747ce03538
diff --git a/.uiverify/photos-close.mjs b/.uiverify/photos-close.mjs
index 2792a1e..7ab8971 100644
--- a/.uiverify/photos-close.mjs
+++ b/.uiverify/photos-close.mjs
@@ -35,6 +35,32 @@ for (const round of [1, 2]) {
ok(!grew && trace.at(-1) === null, `close ${round}: the photo only shrinks, then the viewer is gone — no frame drawn larger ${JSON.stringify(seen)}`);
await page.waitForTimeout(300);
}
+// No frame can flash however the engine starts or ends an animation: the backdrop's own
+// style is its first frame (hidden) the moment the viewer appears, its last (shown) once the
+// fade runs; closing, its own style is gone (hidden) while it fades out.
+const own = await page.evaluate(() => new Promise((resolve) => {
+ const seen = {};
+ new MutationObserver((_, mo) => {
+ const bd = document.querySelector(".px-backdrop");
+ if (bd && seen.first === undefined) {
+ seen.first = bd.style.opacity;
+ mo.disconnect();
+ document.getAnimations().forEach((a) => a.ready.then(() => { seen.running = bd.style.opacity; }));
+ setTimeout(() => resolve(seen), 400);
+ }
+ }).observe(document.body, { childList: true, subtree: true });
+ document.querySelectorAll(".px-tile")[2].dispatchEvent(new MouseEvent("dblclick", { bubbles: true }));
+}));
+ok(own.first === "0" && own.running === "1", `opening: the backdrop's own style is hidden before its fade, shown once it runs ${JSON.stringify(own)}`);
+await page.waitForTimeout(500);
+const closingOwn = await page.evaluate(() => new Promise((resolve) => {
+ window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
+ const bd = document.querySelector(".px-backdrop");
+ Promise.all(bd.getAnimations().map((a) => a.ready)).then(() => resolve(bd.style.opacity));
+}));
+ok(closingOwn === "0", `closing: its own style is already gone while it fades ${JSON.stringify(closingOwn)}`);
+await page.waitForTimeout(700);
+
// The window's own colour follows what fills its edges, so a resize shows no lighter band.
const rgb = (sel) => page.evaluate((s) => /(\d+), (\d+), (\d+)/.exec(getComputedStyle(document.querySelector(s)).backgroundColor).slice(1).join(","), sel);
const body = await rgb("body");
diff --git a/.uiverify/photos-strip.mjs b/.uiverify/photos-strip.mjs
index 322ae09..0626f89 100644
--- a/.uiverify/photos-strip.mjs
+++ b/.uiverify/photos-strip.mjs
@@ -36,5 +36,19 @@ ok((await centred()) <= 1, "and the strip glides to keep it in the middle");
for (let i = 0; i < 40; i++) await page.keyboard.press("ArrowRight");
await page.waitForTimeout(700);
ok((await centred()) <= 1 && (await page.locator(".px-slip").count()) <= 61, "stepping far along, still in the middle and still few");
+// A wheel or a two-finger swipe over the strip skims: four notches of 40 go four photos on,
+// and the slips glide on the compositor, never jump.
+const which = async () => (await page.locator(".px-slip.is-shown img").getAttribute("src")).split("pic%2F")[1].split("%2F")[0];
+const before = await which();
+const strip = await page.locator(".px-strip").boundingBox();
+await page.mouse.move(strip.x + strip.width / 2, strip.y + strip.height / 2);
+for (let i = 0; i < 4; i++) { await page.mouse.wheel(0, 40); await page.waitForTimeout(60); }
+await page.waitForTimeout(700);
+const after = await which();
+const ids = await page.evaluate(() => window.__photos.map((p) => p.id));
+ok(ids.indexOf(after) - ids.indexOf(before) === 4, `scrolling over the strip skims through the photos (${ids.indexOf(before)} → ${ids.indexOf(after)})`);
+ok((await centred()) <= 1, "and the strip keeps the shown one in the middle");
+ok(await page.locator(".px-slip").first().evaluate((e) => getComputedStyle(e).transform !== "none" && /transform/.test(getComputedStyle(e).transitionProperty)),
+ "slips are placed and eased by transform");
await b.close();
process.exitCode = failures ? 1 : (process.exitCode ?? 0);
diff --git a/src/lib/motion.ts b/src/lib/motion.ts
new file mode 100644
index 0000000..cf959cd
--- /dev/null
+++ b/src/lib/motion.ts
@@ -0,0 +1,45 @@
+/**
+ * An animation that can never flash.
+ *
+ * WebKitGTK hands an animation to its compositor a frame after it is asked for, and takes it
+ * back a frame after it ends. For that frame the element is drawn as its own style says, not
+ * as the animation does: a backdrop fading in drew fully black for one frame before its fade
+ * began, and a viewer fading out drew fully there for one frame after it had gone — the
+ * whole-window flicker on every open and close, frame by frame in his recordings.
+ *
+ * So the element's own style is always where the animation is: its first frame before the
+ * animation starts, its last frame once the engine says it is running (`ready`). Whenever the
+ * engine draws the element without the animation, it draws the right thing.
+ */
+export function glide(
+ el: HTMLElement,
+ frames: [Keyframe, Keyframe],
+ options: KeyframeAnimationOptions,
+): Animation {
+ const [from, to] = frames;
+ set(el, from);
+ const a = el.animate(frames, { ...options, fill: "both" });
+ a.ready.then(
+ () => {
+ if (a.playState !== "idle") set(el, to);
+ },
+ () => {},
+ );
+ // Done, it lets go: its last frame is the element's own style by then, so nothing moves —
+ // and nothing held over that style (a zoom writes the photo's transform itself).
+ a.finished.then(
+ () => {
+ set(el, to);
+ a.cancel();
+ },
+ () => {},
+ );
+ return a;
+}
+
+function set(el: HTMLElement, frame: Keyframe) {
+ for (const [k, v] of Object.entries(frame)) {
+ if (k === "offset" || k === "easing" || k === "composite") continue;
+ el.style.setProperty(k.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`), v == null ? "" : String(v));
+ }
+}
diff --git a/src/screens/PhotosView.tsx b/src/screens/PhotosView.tsx
index e7c0567..cb4da01 100644
--- a/src/screens/PhotosView.tsx
+++ b/src/screens/PhotosView.tsx
@@ -28,6 +28,7 @@ import { Slideshow } from "./photos/Slideshow";
import { useAsk } from "./photos/Ask";
import { VideoPlayer } from "./photos/VideoPlayer";
import { tintFrom } from "../lib/tint";
+import { glide } from "../lib/motion";
import { DownloadOptions, type Want } from "./photos/DownloadOptions";
import { AdjustDate } from "./photos/AdjustDate";
@@ -142,7 +143,9 @@ function Filmstrip({ photos, at, onPick }: { photos: Photo[]; at: number; onPick
<button
key={p.id}
className={`px-slip${i === at ? " is-shown" : ""}`}
- style={{ left: i * (SLIP + GAP) + (i > at ? SHOWN - SLIP : 0) }}
+ // Placed by transform, eased with its neighbours: the shown one widens as they make
+ // room, all on the compositor — before, each step jumped every slip into place.
+ style={{ transform: `translateX(${i * (SLIP + GAP) + (i > at ? SHOWN - SLIP : 0)}px)` }}
aria-label={p.filename ?? "Photo"}
aria-current={i === at}
onClick={() => i !== at && onPick(i)}
@@ -151,8 +154,20 @@ function Filmstrip({ photos, at, onPick }: { photos: Photo[]; at: number; onPick
</button>,
);
}
+ // A wheel or a two-finger swipe over the strip skims through the photos, as in Photos: one
+ // photo every 40 pixels of scroll, a fling several.
+ const spun = useRef(0);
+ const onWheel = (e: React.WheelEvent) => {
+ const d = Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.deltaY;
+ spun.current += e.deltaMode === 1 ? d * 40 : d;
+ const by = Math.trunc(spun.current / 40);
+ if (!by) return;
+ spun.current -= by * 40;
+ const to = Math.max(0, Math.min(photos.length - 1, at + by));
+ if (to !== at) onPick(to);
+ };
return (
- <div className="px-strip" role="toolbar" aria-label="Photos">
+ <div className="px-strip" role="toolbar" aria-label="Photos" onWheel={onWheel}>
<div className="px-strip-run" style={{ transform: `translateX(${shift}px)` }}>
{slips}
</div>
@@ -559,6 +574,16 @@ export function PhotosView() {
const stage = useRef<HTMLDivElement | null>(null);
const origin = useRef<DOMRect | null>(null);
const closing = useRef(false);
+ /**
+ * While a photo is open the grid behind it is not drawn at all (content-visibility): each
+ * step to the next photo moved the selection in it, and the engine laid out and painted
+ * thousands of tiles no one could see — 80 ms a step, the stutter in going through photos.
+ * It keeps its place and state, and is back the moment the viewer starts to close.
+ */
+ const [leaving, setLeaving] = useState(false);
+ useEffect(() => {
+ if (open === null) setLeaving(false);
+ }, [open === null]); // eslint-disable-line react-hooks/exhaustive-deps
// A close still running: its animations and its fallback, so a photo opened in the middle
// of it is not shut by the close finishing, nor left faded out by it.
const closingRun = useRef<{ anims: Animation[]; timer: number } | null>(null);
@@ -572,6 +597,16 @@ export function PhotosView() {
a.onfinish = null;
a.cancel();
});
+ // What the close wrote as the elements' own style (see glide) goes with it: the viewer stays,
+ // whole, as it was before the close began.
+ const el = viewer.current;
+ if (el) {
+ el.style.opacity = "";
+ el.style.visibility = "";
+ for (const part of el.querySelectorAll<HTMLElement>(".px-backdrop, .px-viewer-bar, .px-nav, .px-info, .px-strip")) part.style.opacity = "";
+ const img = el.querySelector<HTMLElement>(".px-pan, .px-video");
+ if (img) img.style.transform = "";
+ }
};
// The photo's own box, laid out by the engine itself against the stage (a size container):
// as large as fits, never past 1.5× its own pixels, its shape exact. Known before it loads,
@@ -617,9 +652,9 @@ export function PhotosView() {
// 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() });
+ if (img && t) glide(img, [{ transform: t }, { transform: "none" }], { duration: 460, easing: spring() });
for (const part of el.querySelectorAll<HTMLElement>(".px-backdrop, .px-viewer-bar, .px-nav, .px-info, .px-strip")) {
- part.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 160, easing: "ease-out" });
+ glide(part, [{ opacity: "0" }, { opacity: "1" }], { duration: 160, easing: "ease-out" });
}
}, [open !== null]); // eslint-disable-line react-hooks/exhaustive-deps
@@ -632,6 +667,10 @@ export function PhotosView() {
}, [open !== null]); // eslint-disable-line react-hooks/exhaustive-deps
const close = useCallback(() => {
+ // The grid back in the page first (see `behind`): the photo goes back into its tile, which
+ // must be laid out to be found.
+ document.querySelector(".px-scroll")?.removeAttribute("data-behind");
+ setLeaving(true);
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;
@@ -667,10 +706,10 @@ export function PhotosView() {
};
const t = seen && img ? toTile(img, box) : null;
const parts = [...el.querySelectorAll<HTMLElement>(".px-backdrop, .px-viewer-bar, .px-nav, .px-info, .px-strip")];
- const anims = parts.map((part) => part.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 140, easing: "ease-in", fill: "forwards" }));
+ const anims = parts.map((part) => glide(part, [{ opacity: "1" }, { opacity: "0" }], { duration: 140, easing: "ease-in" }));
const last = t && img
- ? img.animate([{ transform: "none" }, { transform: t }], { duration: 340, easing: spring(), fill: "forwards" })
- : el.animate([{ opacity: 1 }, { opacity: 0 }], { duration: 200, easing: "ease-in", fill: "forwards" });
+ ? glide(img, [{ transform: "none" }, { transform: t }], { duration: 340, easing: spring() })
+ : glide(el, [{ opacity: "1" }, { opacity: "0" }], { duration: 200, easing: "ease-in" });
anims.push(last);
last.onfinish = done;
// A finish that never comes (a hidden window stops the clock) must not leave it open.
@@ -1039,6 +1078,7 @@ export function PhotosView() {
// its thumbnail and screen-sized picture, and the whole one follows.
if (p.kind !== "video") await Promise.race([warm(p), new Promise((r) => window.setTimeout(r, 150))]);
stopClosing();
+ setLeaving(false);
const tile = document.querySelector<HTMLElement>(`.px-tile[data-id="${CSS.escape(p.id)}"]`);
origin.current = tile?.getBoundingClientRect() ?? null;
setOpen(index);
@@ -2075,7 +2115,7 @@ export function PhotosView() {
onClick={(e) => {
// 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());
- }} data-fit={fit || undefined} style={{ "--across": across } as React.CSSProperties}>
+ }} data-fit={fit || undefined} data-behind={(open !== null && !leaving) || undefined} style={{ "--across": across } as React.CSSProperties}>
{months.map((m, k) => (
<Month
key={`${k}/${m.label}`}
diff --git a/src/styles/photos.css b/src/styles/photos.css
index 9ac6b0f..186e23d 100644
--- a/src/styles/photos.css
+++ b/src/styles/photos.css
@@ -672,7 +672,6 @@
z-index: 2;
height: 44px;
overflow: hidden;
- pointer-events: none;
}
.px-strip-run {
position: absolute;
@@ -684,7 +683,9 @@
}
.px-slip {
position: absolute;
+ left: 0;
top: 2px;
+ transition: transform 260ms var(--spring-smooth), width 260ms var(--spring-smooth), height 260ms var(--spring-smooth), top 260ms var(--spring-smooth), opacity 160ms ease;
width: 26px;
height: 40px;
padding: 0;
@@ -695,12 +696,21 @@
pointer-events: auto;
cursor: default;
opacity: 0.82;
- transition: opacity 140ms ease;
}
.px-slip:hover { opacity: 1; }
.px-slip img { width: 100%; height: 100%; object-fit: cover; display: block; }
-.px-slip.is-shown { width: 54px; top: 0; height: 44px; opacity: 1; box-shadow: 0 0 0 2px var(--accent); }
-@media (prefers-reduced-motion: reduce) { .px-strip-run { transition: none; } }
+.px-slip.is-shown { width: 54px; top: 0; height: 44px; opacity: 1; }
+/* The shown one's frame drawn inside it, over the picture: drawn outside, the strip's own edge
+ cut its top and bottom off and only its sides showed. */
+.px-slip.is-shown::after {
+ content: "";
+ position: absolute;
+ inset: 0;
+ border-radius: inherit;
+ box-shadow: inset 0 0 0 2px var(--accent), inset 0 0 0 3px rgba(0, 0, 0, 0.35);
+ pointer-events: none;
+}
+@media (prefers-reduced-motion: reduce) { .px-strip-run, .px-slip { transition: none; } }
/* Info's place and keywords. */
.px-place { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; font-variant-numeric: tabular-nums; }
@@ -977,3 +987,6 @@
.px-player-bar .px-scrub-track { background: rgba(255, 255, 255, 0.24); }
.px-player-bar .px-scrub-fill, .px-player-bar .px-scrub-knob { background: #fff; }
.px-player-bar .px-scrub-tip { color: #fff; background: rgba(28, 28, 30, 0.9); }
+
+/* Behind an open photo the grid is kept but not drawn (see `leaving` in PhotosView). */
+.px-scroll[data-behind] { content-visibility: hidden; }