Launcher: deliver icons through the bridge instead of intercepted image requests

Icons loaded as <img src="icon?c=..."> through shouldInterceptRequest
kept failing on a real phone for the first screenful of apps and for
every widget, even with retries. The page now asks the shell for icons
by key (Android.loadIcons); the shell draws them on one background
thread and returns them in batches of data: URIs via ultraIcons(). An
icon that can't be drawn shows as a blank disc.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019EEjkJVV72Lu8ndDVWg5BT
This commit is contained in:
Claude committed 2026-10-21 19:01:50 +00:00
1 parent d34b753434
commit a52108e95f
4 files changed
+75 -17

No files matched your search

+1 -1
View File
@@ -489,7 +489,7 @@ def view_icon(h: i32) -> None:
cls("icon")
attr("alt", "")
attr("draggable", "false")
attr_ref("src", h)
attr_ref("data-icon", h) # index.html turns this into a src
end()
BIN
View File
Binary file not shown.
+29 -16
View File
@@ -54,8 +54,34 @@
if (j !== placed) { placed = j; host.placeWidgets(j); }
};
// Icons: each <img class="icon" data-icon=KEY> gets its src from here. On a phone the
// shell draws icons on its own thread and hands them back as data: URIs in batches
// (ultraIcons), so the WebView never loads icons through request interception.
// In a desktop browser the key is already a URL.
const iconSrc = new Map(), asked = new Set();
const setIcon = (img, src) => {
if (src) { img.src = src; img.classList.remove("missing"); } else img.classList.add("missing");
};
const fillIcons = () => {
const want = [];
for (const img of document.querySelectorAll("img.icon[data-icon]")) {
const k = img.dataset.icon;
if (img.__icon === k) continue;
if (iconSrc.has(k)) { img.__icon = k; setIcon(img, iconSrc.get(k)); }
else if (!asked.has(k)) { asked.add(k); want.push(k); }
}
if (!want.length) return;
if (host.loadIcons) host.loadIcons(JSON.stringify(want));
else gotIcons(Object.fromEntries(want.map((k) => [k, k])));
};
const gotIcons = (batch) => {
for (const k in batch) { iconSrc.set(k, batch[k]); asked.delete(k); }
fillIcons();
};
window.ultraIcons = gotIcons; // the shell calls this before ultraHome exists, too
const app = await mount("#app", "./app.wasm", {
onRender: place,
onRender: () => { fillIcons(); place(); },
imports: ({ strs }) => ({
launch: (h) => host.launch(strs[h]),
app_info: (h) => host.appInfo(strs[h]),
@@ -80,22 +106,9 @@
widgetAdded: (id) => app.dispatch(WIDGET_ADDED, id),
widgetMenu: (id) => app.dispatch(WIDGET_MENU, id),
place: () => { placed = ""; place(); },
resetIcons: () => { iconSrc.clear(); asked.clear();
for (const img of document.querySelectorAll("img.icon")) img.__icon = undefined; },
};
// An icon that fails to load is retried twice with backoff, then drawn as a blank
// disc instead of the browser's broken-image box.
document.addEventListener("error", (e) => {
const img = e.target;
if (!(img instanceof HTMLImageElement) || !img.classList.contains("icon")) return;
const n = (img.__retries = (img.__retries || 0) + 1);
if (n > 2) { img.classList.add("missing"); return; }
setTimeout(() => {
const base = img.getAttribute("src").replace(/[?&]retry=\d+$/, "");
img.src = base + (base.includes("?") ? "&" : "?") + "retry=" + n;
}, 300 * n);
}, true);
document.addEventListener("load", (e) => {
if (e.target instanceof HTMLImageElement) e.target.classList.remove("missing");
}, true);
document.addEventListener("visibilitychange", () => { if (!document.hidden) window.ultraHome.tick(); });
addEventListener("scroll", () => requestAnimationFrame(place), { passive: true });
addEventListener("resize", () => requestAnimationFrame(place));