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 keeps pictures near the view; closed windows give their memory back (#11)

Yashwanth Kamireddi1af17aac71f2
diff --git a/.uiverify/photos-memory.mjs b/.uiverify/photos-memory.mjs
new file mode 100644
index 0000000..1f54aa2
--- /dev/null
+++ b/.uiverify/photos-memory.mjs
@@ -0,0 +1,75 @@
+// Photos holds pictures only near the view (#11). Every picture drawn is decoded memory the
+// engine keeps while it is there; built 4000px either side and never let go, a scroll through
+// twenty thousand photos kept thirteen screens of them (2,300, about 400 MB) and kept them while
+// another app was in front. Now: pictures from a screen away, let go two screens away, the ones
+// on screen always drawn once a scroll settles, only those on and beside the screen kept while
+// another app is in front, and the screen drawn at once on coming back.
+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(() => {
+ window.__picBase = "http://localhost:4173/test-pictures";
+ 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; });
+// A small real picture for every request, as the disk cache answers.
+const jpeg = Buffer.from(
+ "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/wAALCAABAAEBAREA/8QAFAABAAAAAAAAAAAAAAAAAAAACf/EABQQAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQEAAD8AVN//2Q==",
+ "base64",
+);
+await page.route("**/test-pictures/**", (route) => route.fulfill({ contentType: "image/jpeg", body: jpeg }));
+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 img[src]").first().waitFor();
+
+// What is drawn, against what is on screen.
+const census = () => page.evaluate(() => {
+ const view = document.querySelector(".px-scroll").getBoundingClientRect();
+ let onScreen = 0, onScreenDrawn = 0, drawn = 0;
+ for (const t of document.querySelectorAll(".px-tile")) {
+ const r = t.getBoundingClientRect();
+ const has = t.querySelector("img")?.hasAttribute("src");
+ if (has) drawn++;
+ if (r.bottom > view.top && r.top < view.bottom) { onScreen++; if (has) onScreenDrawn++; }
+ }
+ return { onScreen, onScreenDrawn, drawn, perScreen: onScreen };
+});
+const first = await census();
+ok(first.onScreen > 0 && first.onScreenDrawn === first.onScreen, `the first screen is drawn at once (${first.onScreenDrawn} of ${first.onScreen})`);
+
+// Down through the whole library a screen at a time, as a steady scroll goes: the most drawn
+// at any point, and every stop's screen drawn once it settles.
+let most = 0, blank = 0, stops = 0;
+const H = await page.locator(".px-scroll").evaluate((el) => el.scrollHeight);
+for (let y = 0; y < H; y += 700) {
+ await page.locator(".px-scroll").evaluate((el, y) => { el.scrollTop = y; }, y);
+ await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))));
+ if (++stops % 25) continue;
+ await page.waitForTimeout(120);
+ const c = await census();
+ most = Math.max(most, c.drawn);
+ blank += c.onScreen - c.onScreenDrawn;
+}
+ok(blank === 0, `every screen it stops on is drawn (${blank} tiles left blank)`);
+// A month at a time (one here is about a screen and a half), let go once the screen is idle: a
+// few screens either side, never the 2,300 of before.
+ok(most <= first.perScreen * 10, `pictures stay near the view (at most ${most} drawn; a screen holds ${first.perScreen})`);
+
+// Another app in front: only what was on and beside the screen is kept.
+await openScreen(page, "Notes");
+await page.waitForTimeout(400);
+const away = await page.evaluate(() => document.querySelectorAll(".px-tile img[src]").length);
+ok(away <= first.perScreen * 4, `another app in front keeps only the screen and beside it (${away} pictures)`);
+// Back: drawn at once, from what was kept.
+await openScreen(page, "Photos");
+const again = await census();
+ok(again.onScreen > 0 && again.onScreenDrawn === again.onScreen, `coming back, the screen is drawn at once (${again.onScreenDrawn} of ${again.onScreen})`);
+await b.close();
+process.exitCode = failures ? 1 : (process.exitCode ?? 0);
diff --git a/check.sh b/check.sh
index 83f8961..dacb0dd 100755
--- a/check.sh
+++ b/check.sh
@@ -15,11 +15,12 @@ step() {
# side: the Rust lane beside the web lane, and the browser checks in a pool. Run one by one they
# took about forty minutes at a load of 1.5 on twelve cores. Each browser is several processes, so
# the pool is half the cores; CHECK_JOBS changes it. Checks that measure time run alone at the
-# end, where a busy machine can't make them fail. Results print in this file's order.
+# end, where a busy machine can't make them fail (run six at a time, the scroll and paging
+# checks measured frames of 450–1000 ms that alone are 17–575 ms). Results print in this file's order.
JOBS=${CHECK_JOBS:-$(( $(nproc) / 2 ))}; [ "$JOBS" -ge 1 ] || JOBS=1
out=$(mktemp -d)
names=() pids=() later=()
-timed() { case "$*" in *notes-long.mjs*|*photos-scale.mjs*) return 0 ;; esac; return 1; }
+timed() { case "$*" in *notes-long.mjs*|*photos-scale.mjs*|*photos-scroll.mjs*|*photos-paging.mjs*|*photos-levels-scale.mjs*) return 0 ;; esac; return 1; }
pooled() {
local name=$1; shift
if timed "$@"; then later+=("$name"$'\x1f'"$(printf '%q ' "$@")"); return; fi
@@ -113,6 +114,12 @@ pooled "photos" node .uiverify/photos.mjs
pooled "photos preparing" node .uiverify/photos-preparing.mjs
pooled "photo albums" node .uiverify/photos-albums.mjs
pooled "photos at scale" node .uiverify/photos-scale.mjs
+pooled "photos memory" node .uiverify/photos-memory.mjs
+pooled "webkit: photos memory" env ENGINE=webkit node .uiverify/photos-memory.mjs
+pooled "photos scrolling" node .uiverify/photos-scroll.mjs
+pooled "webkit: photos scrolling" env ENGINE=webkit node .uiverify/photos-scroll.mjs
+pooled "photos paging" node .uiverify/photos-paging.mjs
+pooled "photos levels" env ENGINE=webkit node .uiverify/photos-levels-scale.mjs
pooled "photos drag" node .uiverify/photos-drag.mjs
pooled "drag pictures" node .uiverify/drag-picture.mjs
pooled "photos keys" node .uiverify/photos-keys.mjs
diff --git a/src-tauri/src/apps.rs b/src-tauri/src/apps.rs
index 54743fe..d02f7a0 100644
--- a/src-tauri/src/apps.rs
+++ b/src-tauri/src/apps.rs
@@ -223,8 +223,10 @@ fn make(app: &AppHandle, key: &str, asking: &str) {
}
/// How long a closed window — an app's, or the whole-library one — is kept, put away, before
-/// its memory is given back.
-const KEPT: std::time::Duration = std::time::Duration::from_secs(10 * 60);
+/// its memory is given back. Reopened within it, a window is back at once; after it, it is made
+/// again in about half a second. A minute: in the tray Skykeep is its engine, not its windows
+/// (#11) — ten minutes kept each closed window's web view, 100 MB or more, long after use.
+const KEPT: std::time::Duration = std::time::Duration::from_secs(60);
/// When each put-away app window was closed: shown again, it is taken off the list, so a
/// window closed, reopened and closed again is kept the full while from its last close.
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index c2b26b1..852be27 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -6271,8 +6271,10 @@ fn window_ready(app: tauri::AppHandle, window: tauri::WebviewWindow, needs_you:
// Asked in this order: started any other way the answer is no, and the tray's question —
// a call on the session bus — was asked on the main thread just before every launch's show.
if login::stay_hidden(needs_you.unwrap_or(true), || tray_reachable(&app)) {
- // Its page has done what a launch needs of it (the session taken up again): it waits,
- // drawn and hidden, so the tray opens it at once.
+ // Its page has done what a launch needs of it (the session taken up again): put away,
+ // as a closed window is — opened from the tray soon, it is there at once; otherwise
+ // its web view is given back and the engine runs on behind the tray (#11).
+ apps::put_away(&window.as_ref().window());
return;
}
show_window(&app);
@@ -6928,10 +6930,11 @@ fn main() {
}
if window.label() == "main" {
api.prevent_close();
- // Hidden, never given back: the tray's click shows it at once, already
- // drawn. Remade on demand it took about half a second, which the founder
- // asked to be gone; keeping its web view costs about 190 MB in the tray.
+ // Put away as the apps' windows are (apps::KEPT): reopened soon it is back
+ // at once, and after that its web view is given back. Kept for good, it
+ // held about 190 MB in the tray all day (#11).
window.hide().ok();
+ apps::put_away(window);
updates::put_away(window.app_handle(), window.label());
} else if window.label().starts_with("app-") {
api.prevent_close();
@@ -7268,21 +7271,13 @@ fn main() {
LAUNCHED_AS_APP.store(true, Ordering::SeqCst);
apps::open(app.handle(), key);
}
- // The whole-library window, unless this launch is one app's or one file's — then
- // made a little later, hidden, once that app's window is up, so the tray still
- // opens the whole library at once.
+ // The whole-library window, unless this launch is one app's or one file's: then it is
+ // made when it is first asked for (the tray, its launcher), about half a second the
+ // first time and at once after. Made hidden beside the app's window instead, it kept a
+ // second web view of about 115 MB all day for people who never opened it (#11);
+ // every window takes up the session itself (App.tsx), so nothing waited on it.
if !LAUNCHED_AS_APP.load(Ordering::SeqCst) {
make_main(app.handle());
- } else {
- let app = app.handle().clone();
- tauri::async_runtime::spawn(async move {
- tokio::time::sleep(Duration::from_secs(4)).await;
- let warm = app.clone();
- app.run_on_main_thread(move || {
- make_main(&warm);
- })
- .ok();
- });
}
show_when_ready(app.handle());
timing::mark("setup done");
diff --git a/src/lib/near.ts b/src/lib/near.ts
new file mode 100644
index 0000000..96a26b0
--- /dev/null
+++ b/src/lib/near.ts
@@ -0,0 +1,50 @@
+/**
+ * Whether an element is within some distance of its scroll area's view, told to a callback as
+ * it changes: one IntersectionObserver per scroll area and distance, shared by every element
+ * watched there, rather than one per element — hundreds of months, each with an observer of
+ * its own, each its own work in every frame.
+ *
+ * `margin` is how far beyond the view counts as near, as a CSS margin (`"100% 0px"`: a whole
+ * view above and below).
+ */
+type Seen = (near: boolean, entry: IntersectionObserverEntry) => void;
+
+const watchers = new WeakMap<Element | Document, Map<string, { io: IntersectionObserver; seen: Map<Element, Seen> }>>();
+
+export function watchNear(el: Element, root: Element | null, margin: string, seen: Seen): () => void {
+ if (typeof IntersectionObserver === "undefined") {
+ seen(true, {} as IntersectionObserverEntry);
+ return () => {};
+ }
+ const key = root ?? document;
+ let byMargin = watchers.get(key);
+ if (!byMargin) watchers.set(key, (byMargin = new Map()));
+ let w = byMargin.get(margin);
+ if (!w) {
+ const told = new Map<Element, Seen>();
+ const io = new IntersectionObserver(
+ (entries) => {
+ // The latest for each element: a layout that moved it out and back within a frame
+ // comes as two entries, and the earlier one alone is wrong.
+ const last = new Map<Element, IntersectionObserverEntry>();
+ for (const e of entries) last.set(e.target, e);
+ for (const [target, e] of last) told.get(target)?.(e.isIntersecting, e);
+ },
+ { root, rootMargin: margin },
+ );
+ w = { io, seen: told };
+ byMargin.set(margin, w);
+ }
+ const mine = w;
+ mine.seen.set(el, seen);
+ mine.io.observe(el);
+ return () => {
+ mine.io.unobserve(el);
+ mine.seen.delete(el);
+ };
+}
+
+/** Whether `el` is out of sight altogether: in a screen put away (`hidden`, its slot not drawn). */
+export function putAway(el: Element): boolean {
+ return (el as HTMLElement).offsetParent === null && getComputedStyle(el).position !== "fixed";
+}
diff --git a/src/screens/PhotosView.tsx b/src/screens/PhotosView.tsx
index 0962b45..9b3bae4 100644
--- a/src/screens/PhotosView.tsx
+++ b/src/screens/PhotosView.tsx
@@ -4,6 +4,7 @@
* from the disk cache and are fetched once.
*/
import { useActiveRef, useScreenActive } from "../lib/screen";
+import { putAway, watchNear } from "../lib/near";
import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
import { listen } from "@tauri-apps/api/event";
import { getCurrentWebview } from "@tauri-apps/api/webview";
@@ -359,6 +360,7 @@ const Tile = memo(function Tile({
soon,
fit,
cell,
+ pics,
act,
}: {
photo: Photo;
@@ -370,6 +372,8 @@ const Tile = memo(function Tile({
fit: boolean;
/** A cell's width in device pixels. */
cell: number;
+ /** Its month's: whether it is near the view, so its picture is drawn (see Month: pics). */
+ pics: { current: boolean };
act: Acts;
}) {
const born = useRef(performance.now());
@@ -383,11 +387,22 @@ const Tile = memo(function Tile({
// picture, decoded and then covered. Set on the element itself: no tile draws again for it.
const pic = useRef<HTMLSpanElement | null>(null);
const landed = useRef(false);
+ const img = useRef<HTMLImageElement | null>(null);
const under = useCallback(() => {
- if (!landed.current && pic.current) pic.current.style.backgroundImage = `url("${pictureSrc(photo.id, "thumb")}")`;
+ // Only under a picture that is drawn: a tile far from the view keeps neither (see Month).
+ if (!landed.current && pic.current && img.current?.hasAttribute("src"))
+ pic.current.style.backgroundImage = `url("${pictureSrc(photo.id, "thumb")}")`;
}, [photo.id]);
const src = tileSrc(photo, cell, fit);
const first = useRef(true);
+ // Its picture drawn at once when its month is near as it is built, as every tile always was;
+ // a new size asked for (the window resized, zoomed) is drawn in its place. Drawn and let go
+ // otherwise by its month, on the element itself, so no tile draws again for it (see Month).
+ useLayoutEffect(() => {
+ const el = img.current;
+ if (!el) return;
+ if (el.hasAttribute("src") || pics.current) el.src = src;
+ }, [src, pics]);
useEffect(() => {
// A new size asked for (the window resized, zoomed): the thumbnail may be wanted again.
if (!first.current) landed.current = false;
@@ -447,11 +462,12 @@ const Tile = memo(function Tile({
// The thumbnail underneath while the picture made for this tile's size comes (see under).
style={fit && photo.width && photo.height ? fitFrame(photo) : undefined}
>
- {/* Fetched as soon as its month is built, well ahead of the screen — not left to the
- engine's own lazy margin, which a fast scroll outran — and made the size it is
- drawn at, so the engine never shrinks it. */}
+ {/* Drawn once its month is within a screen of the view — not left to the engine's own
+ lazy margin, which a fast scroll outran — and made the size it is drawn at, so the
+ engine never shrinks it. Let go again two screens off (see Month: pics). */}
<img
- src={src}
+ ref={img}
+ data-src={src}
alt=""
decoding="async"
draggable={false}
@@ -667,6 +683,77 @@ const Month = memo(function Month({
useEffect(() => {
if (eager && items.length) void list.need(items[0].index, items[items.length - 1].index);
}, [eager, list, items]);
+ /*
+ * Its pictures, drawn only near the view: from a screen away, let go two screens away. Each
+ * picture drawn is decoded memory the engine keeps for as long as it is there: drawn as
+ * months were built, 4000px either side, and never let go, the grid held thirteen screens of
+ * them (2,300, about 400 MB) and kept them while another app was in front (#11).
+ *
+ * Drawn and let go on the elements themselves, never by drawing tiles again: a month's tiles
+ * all drawn again as it came near made a fling's longest frame 217 ms (33 ms before). Drawn a
+ * slice a frame (buildSoon), and let go only once the screen is idle, so neither ever costs a
+ * frame of a scroll. A tile built while its month is near draws its own at once, as before.
+ * While the screen is put away (another app in front), what was on and beside the screen
+ * stays, so coming back is drawn at once.
+ */
+ const pics = useRef(eager);
+ useEffect(() => {
+ const el = ref.current;
+ if (!el) return undefined;
+ const root = el.closest(".px-scroll");
+ let gone = false;
+ let drawing = false;
+ let letting = 0;
+ const draw = () => {
+ if (drawing) return;
+ drawing = true;
+ buildSoon(() => {
+ if (gone || !pics.current) return (drawing = false), true;
+ const waiting = el.querySelectorAll<HTMLImageElement>("img[data-src]:not([src])");
+ for (let i = 0; i < Math.min(BUILD_TILES, waiting.length); i++) waiting[i].src = waiting[i].dataset.src!;
+ if (waiting.length > BUILD_TILES) return false;
+ drawing = false;
+ return true;
+ });
+ };
+ const letGo = () => {
+ if (letting) return;
+ const idle = (fn: () => void): number =>
+ typeof window.requestIdleCallback === "function" ? window.requestIdleCallback(fn, { timeout: 2000 }) : window.setTimeout(fn, 300);
+ letting = idle(() => {
+ letting = 0;
+ if (gone || pics.current) return;
+ for (const im of el.querySelectorAll<HTMLImageElement>("img[src]")) {
+ im.removeAttribute("src");
+ delete im.dataset.in;
+ const frame = im.parentElement;
+ if (frame) frame.style.backgroundImage = "";
+ }
+ });
+ };
+ if (pics.current) draw();
+ // Whether it was near the last time the screen was in sight.
+ let near = eager;
+ const stopNear = watchNear(el, root, "100% 0px", (on) => {
+ if (putAway(el)) return;
+ near = on;
+ if (!on) return;
+ pics.current = true;
+ draw();
+ });
+ const stopFar = watchNear(el, root, "200% 0px", (on) => {
+ if (on) return;
+ // Put away, everything reads as far: only what was not near the screen is let go.
+ if (putAway(el) && near) return;
+ pics.current = false;
+ letGo();
+ });
+ return () => {
+ gone = true;
+ stopNear();
+ stopFar();
+ };
+ }, [eager]);
// One watcher: near the screen (within 4000px), its photos are asked for and it is built a few
// rows a frame; gone far again before it was built, its building waits until it comes back —
// a fling to the foot of the library builds the months it lands on, not every month it
@@ -749,6 +836,7 @@ const Month = memo(function Month({
soon={t?.soon ?? false}
fit={fit}
cell={cell}
+ pics={pics}
act={act}
/>
);