A long press now lifts the slot instead of opening its menu. Moving the finger slides it live to the slot under the finger and letting go drops it there; letting go without moving opens the menu as before. index.html does the dragging (and stops the page scrolling or swiping the drawer under a lifted slot). Native widget views take their own touches, so WidgetView forwards the moves after its long press as widgetDrag(x, y) and the release as widgetDrop(moved). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JfvcrzLMTeE68CpNffNDvE
196 lines
9.1 KiB
HTML
196 lines
9.1 KiB
HTML
<!doctype html>
|
|
<html lang="en" class="dark">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
|
<title>ultra · Launcher</title>
|
|
<link rel="stylesheet" href="ui.css">
|
|
<link rel="stylesheet" href="style.css">
|
|
</head>
|
|
<body>
|
|
<main id="app"></main>
|
|
<div id="toast" role="status"></div>
|
|
<script type="module">
|
|
import { mount } from "./ultra.js";
|
|
|
|
// On a phone, MainActivity.java adds window.Android, serves apps.json and
|
|
// widgets.json, and draws widgets over the slots reported by place().
|
|
// In a desktop browser the demo JSON files are served and these stand-ins run.
|
|
const toast = (s) => {
|
|
const t = document.getElementById("toast");
|
|
t.textContent = s; t.classList.add("show");
|
|
clearTimeout(toast.t); toast.t = setTimeout(() => t.classList.remove("show"), 1600);
|
|
};
|
|
let demoWidget = 100;
|
|
const host = window.Android || {
|
|
launch: (c) => toast("Open " + c),
|
|
appInfo: (c) => toast("App info for " + c),
|
|
addWidget: () => setTimeout(() => window.ultraHome.widgetAdded(demoWidget++), 0),
|
|
removeWidget: () => {},
|
|
placeWidgets: () => {},
|
|
openSettings: () => toast("Open system settings"),
|
|
pickWallpaper: () => toast("Pick a wallpaper"),
|
|
openRecents: () => toast("Show open windows"),
|
|
haptic: () => navigator.vibrate?.(10),
|
|
is24Hour: () => false,
|
|
};
|
|
if (!window.Android) document.documentElement.classList.add("demo");
|
|
|
|
let fmtTime, fmtDate;
|
|
const formats = () => {
|
|
fmtTime = new Intl.DateTimeFormat(undefined,
|
|
{ hour: "numeric", minute: "2-digit", hourCycle: host.is24Hour() ? "h23" : "h12" });
|
|
fmtDate = new Intl.DateTimeFormat(undefined, { weekday: "long", month: "long", day: "numeric" });
|
|
};
|
|
formats();
|
|
|
|
// Tell the shell where the live widget slots are (CSS px, viewport-relative).
|
|
let placed = "";
|
|
const place = () => {
|
|
const slots = [...document.querySelectorAll(".widget-slot[data-wid]")].map((s) => {
|
|
const r = s.getBoundingClientRect();
|
|
return [+s.dataset.wid, Math.round(r.left), Math.round(r.top), Math.round(r.width), Math.round(r.height)];
|
|
});
|
|
const j = JSON.stringify(slots);
|
|
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])));
|
|
};
|
|
// An icon the shell couldn't draw ("") is asked for again, up to 3 times, with backoff;
|
|
// until then, and after, it shows as a blank disc.
|
|
const tries = new Map();
|
|
const gotIcons = (batch) => {
|
|
const again = [];
|
|
for (const k in batch) {
|
|
iconSrc.set(k, batch[k]); asked.delete(k);
|
|
const n = tries.get(k) || 0;
|
|
if (!batch[k] && n < 3) { tries.set(k, n + 1); again.push(k); }
|
|
}
|
|
fillIcons();
|
|
if (again.length) setTimeout(() => {
|
|
for (const k of again) iconSrc.delete(k);
|
|
for (const img of document.querySelectorAll("img.icon[data-icon]"))
|
|
if (again.includes(img.dataset.icon)) img.__icon = undefined;
|
|
fillIcons();
|
|
}, 800 * (tries.get(again[0]) || 1));
|
|
};
|
|
window.ultraIcons = gotIcons; // the shell calls this before ultraHome exists, too
|
|
|
|
const app = await mount("#app", "./app.wasm", {
|
|
onRender: () => { fillIcons(); place(); },
|
|
imports: ({ strs }) => ({
|
|
launch: (h) => host.launch(strs[h]),
|
|
app_info: (h) => host.appInfo(strs[h]),
|
|
add_widget: (h) => host.addWidget(strs[h]),
|
|
remove_widget: (id) => host.removeWidget(id),
|
|
open_settings: () => host.openSettings(),
|
|
pick_wallpaper: () => host.pickWallpaper(),
|
|
open_recents: () => host.openRecents(),
|
|
haptic: () => host.haptic(),
|
|
clock_time: (h) => { strs[h] = fmtTime.format(new Date()).replace(/\s?[AP]M$/i, ""); },
|
|
clock_date: (h) => { strs[h] = fmtDate.format(new Date()); },
|
|
}),
|
|
});
|
|
|
|
// Messages the shell and the gestures below send (keep in sync with app.py).
|
|
const TICK = 1, RELOAD = 2, HOME = 3, BACK = 4, SWIPE_DOWN = 5, SWIPE_UP = 6,
|
|
WIDGET_ADDED = 7, WIDGET_MENU = 8, DRAG_TO = 35, DROP = 36;
|
|
|
|
// Dragging a home slot. A long press lifts it (app.py marks it .lifted); moving the
|
|
// finger then slides it to the slot under the finger, and letting go drops it there.
|
|
// Letting go without moving opens its menu instead. Native widget views take their
|
|
// own touches, so the shell reports those moves through widgetDrag/widgetDrop.
|
|
let drag = null, dragged = false; // drag: { x, y, moved } from pointerdown
|
|
const slotAt = (x, y) => {
|
|
let best = -1, bestD = Infinity;
|
|
for (const li of document.querySelectorAll(".slots > li[data-slot]")) {
|
|
const r = li.getBoundingClientRect();
|
|
const dx = Math.max(r.left - x, 0, x - r.right), dy = Math.max(r.top - y, 0, y - r.bottom);
|
|
const d = dx * dx + dy * dy;
|
|
if (d < bestD) { bestD = d; best = +li.dataset.slot; }
|
|
}
|
|
return best;
|
|
};
|
|
const dragTo = (x, y) => {
|
|
const lifted = document.querySelector(".slots > .lifted");
|
|
if (!lifted) return;
|
|
const to = slotAt(x, y);
|
|
if (to >= 0 && to !== +lifted.dataset.slot) app.dispatch(DRAG_TO, to);
|
|
};
|
|
const drop = (moved) => {
|
|
if (document.querySelector(".slots > .lifted")) app.dispatch(DROP, moved ? 1 : 0);
|
|
};
|
|
addEventListener("pointerdown", (e) => {
|
|
drag = e.isPrimary && e.target.closest?.(".slots > li") ? { x: e.clientX, y: e.clientY, moved: false } : null;
|
|
dragged = false;
|
|
});
|
|
addEventListener("pointermove", (e) => {
|
|
if (!drag || !document.querySelector(".slots > .lifted")) return;
|
|
if (!drag.moved && Math.hypot(e.clientX - drag.x, e.clientY - drag.y) < 10) return;
|
|
drag.moved = dragged = true;
|
|
dragTo(e.clientX, e.clientY);
|
|
});
|
|
addEventListener("pointerup", () => { if (drag) drop(drag.moved); drag = null; });
|
|
addEventListener("pointercancel", () => { if (drag) drop(drag.moved); drag = null; });
|
|
// Keep the page from scrolling under a lifted slot.
|
|
addEventListener("touchmove", (e) => {
|
|
if (drag && document.querySelector(".slots > .lifted") && e.cancelable) e.preventDefault();
|
|
}, { passive: false });
|
|
window.ultraHome = {
|
|
tick: () => { formats(); app.dispatch(TICK, 1); },
|
|
reload: () => app.dispatch(RELOAD, 0),
|
|
home: () => { app.dispatch(HOME, 0); window.scrollTo({ top: 0, behavior: "smooth" }); },
|
|
back: () => app.dispatch(BACK, 0),
|
|
widgetAdded: (id) => app.dispatch(WIDGET_ADDED, id),
|
|
widgetMenu: (id) => app.dispatch(WIDGET_MENU, id),
|
|
widgetDrag: (x, y) => { dragged = true; dragTo(x, y); },
|
|
widgetDrop: (moved) => drop(moved),
|
|
place: () => { placed = ""; place(); },
|
|
resetIcons: () => { iconSrc.clear(); asked.clear(); tries.clear();
|
|
for (const img of document.querySelectorAll("img.icon")) img.__icon = undefined; },
|
|
};
|
|
document.addEventListener("visibilitychange", () => { if (!document.hidden) window.ultraHome.tick(); });
|
|
addEventListener("scroll", () => requestAnimationFrame(place), { passive: true });
|
|
addEventListener("resize", () => requestAnimationFrame(place));
|
|
|
|
// Swipe down on the home screen opens the drawer; swipe up closes it once its
|
|
// list is scrolled to the end (or doesn't scroll).
|
|
let start = null;
|
|
addEventListener("touchstart", (e) => {
|
|
const t = e.touches[0];
|
|
start = e.touches.length === 1 ? { x: t.clientX, y: t.clientY, at: Date.now() } : null;
|
|
}, { passive: true });
|
|
addEventListener("touchend", (e) => {
|
|
if (!start || dragged || document.querySelector(".slots > .lifted")) { start = null; return; }
|
|
const t = e.changedTouches[0], dx = t.clientX - start.x, dy = t.clientY - start.y;
|
|
const quick = Date.now() - start.at < 700;
|
|
start = null;
|
|
if (!quick || Math.abs(dy) < 60 || Math.abs(dx) > Math.abs(dy)) return;
|
|
if (document.querySelector(".sheet")) return;
|
|
const list = document.querySelector(".drawer .apps");
|
|
if (dy > 0 && !list && window.scrollY <= 0) app.dispatch(SWIPE_DOWN, 0);
|
|
else if (dy < 0 && list && list.scrollTop + list.clientHeight >= list.scrollHeight - 2) app.dispatch(SWIPE_UP, 0);
|
|
}, { passive: true });
|
|
</script>
|
|
</body>
|
|
</html>
|