361 lines
19 KiB
HTML
361 lines
19 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),
|
|
uninstall: (c) => toast("Uninstall " + c),
|
|
addWidget: () => setTimeout(() => window.ultraHome.widgetAdded(demoWidget++), 0),
|
|
removeWidget: () => {},
|
|
placeWidgets: () => {},
|
|
widgetLimits: () => JSON.stringify({ minW: 120, minH: 80, maxW: 0, maxH: 0, h: true, v: true }),
|
|
openSettings: () => toast("Open system settings"),
|
|
pickWallpaper: () => toast("Pick a wallpaper"),
|
|
openRecents: () => toast("Show open windows"),
|
|
checkUpdates: () => toast("Check for updates"),
|
|
showKeyboard: () => {},
|
|
hideKeyboard: () => {},
|
|
haptic: () => navigator.vibrate?.(10),
|
|
};
|
|
if (!window.Android) document.documentElement.classList.add("demo");
|
|
|
|
// 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),
|
|
s.classList.contains("resizing") ? 1 : 0]; // its body moves it instead of taking touches
|
|
});
|
|
const j = JSON.stringify(slots);
|
|
// The drawer unmarks the slots, but the home screen under it doesn't move, so the
|
|
// widgets stay where they were and cover() hides them as it slides over them.
|
|
const underDrawer = document.querySelector(".drawer") && !document.querySelector(".sheet-overlay");
|
|
if (j !== placed && !underDrawer) { placed = j; host.placeWidgets(j); }
|
|
// Keep the system's edge swipes (back) off the resize handles.
|
|
const handles = JSON.stringify([...document.querySelectorAll(".resize-handle")].map((h) => {
|
|
const r = h.getBoundingClientRect();
|
|
return [Math.round(r.left), Math.round(r.top), Math.round(r.width), Math.round(r.height)];
|
|
}));
|
|
if (handles !== excluded) { excluded = handles; host.excludeGestures?.(handles); }
|
|
};
|
|
let excluded = "[]";
|
|
|
|
// Native widgets sit above the page, so the drawer can't cover them by itself. While it
|
|
// slides down the shell hides them above its bottom edge each frame (coverWidgets(y),
|
|
// CSS px; -1 once it covers the screen), so they stay until the drawer reaches them.
|
|
// The edge is reported a little ahead (by its speed), since the shell trails a frame.
|
|
let coverY = 0, coverLast = 0, coverLoop = false;
|
|
const cover = () => {
|
|
const d = document.querySelector(".drawer");
|
|
let y = 0;
|
|
if (d) {
|
|
const b = d.getBoundingClientRect().bottom;
|
|
y = b >= innerHeight - 1 ? -1 : Math.max(0, Math.ceil(b + 2 * Math.max(0, b - coverLast)));
|
|
coverLast = b;
|
|
if (y >= innerHeight) y = innerHeight - 1;
|
|
} else coverLast = 0;
|
|
if (y !== coverY) { coverY = y; host.coverWidgets?.(y); }
|
|
if (d && y !== -1 && !coverLoop) {
|
|
coverLoop = true;
|
|
requestAnimationFrame(() => { coverLoop = false; cover(); });
|
|
}
|
|
};
|
|
|
|
// Opening the drawer puts the cursor in its search box and raises the keyboard
|
|
// (a WebView won't show it for a focus() the user didn't tap, so the shell does).
|
|
// Picking apps to add is for scrolling, so that stays keyboard-free.
|
|
let drawerOpen = false;
|
|
const watchDrawer = () => {
|
|
const d = document.querySelector(".drawer");
|
|
if (!!d === drawerOpen) return;
|
|
drawerOpen = !!d;
|
|
if (d) {
|
|
if (d.classList.contains("picking")) return;
|
|
d.querySelector(".drawer-head input")?.focus({ preventScroll: true });
|
|
host.showKeyboard?.();
|
|
} else {
|
|
document.activeElement?.blur?.();
|
|
host.hideKeyboard?.();
|
|
}
|
|
};
|
|
|
|
// 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
|
|
|
|
let ready = false; // the grid code below exists and app is mounted
|
|
const app = await mount("#app", "./app.wasm", {
|
|
onRender: () => { fillIcons(); place(); cover(); watchDrawer(); if (ready) measureGrid(); },
|
|
imports: ({ strs }) => ({
|
|
launch: (h) => host.launch(strs[h]),
|
|
app_info: (h) => host.appInfo(strs[h]),
|
|
uninstall: (h) => host.uninstall(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(),
|
|
check_updates: () => host.checkUpdates(),
|
|
haptic: () => host.haptic(),
|
|
}),
|
|
});
|
|
|
|
ready = true;
|
|
|
|
// Messages the shell and the gestures below send (keep in sync with app.py).
|
|
const RELOAD = 2, HOME = 3, BACK = 4, SWIPE_DOWN = 5, SWIPE_UP = 6,
|
|
WIDGET_ADDED = 7, WIDGET_MENU = 8, DRAG_TO = 35, DROP = 36, RESIZE = 40, GRAB = 42, WIDGET_COLS = 44,
|
|
GRID = 45, MOVE_CELL = 47;
|
|
|
|
// The home grid: its columns, and as many rows as fit down to the bottom of the
|
|
// screen (app.py lays those out, so the empty cells there are real cells).
|
|
const grid = () => {
|
|
const slots = document.querySelector(".slots");
|
|
if (!slots) return null;
|
|
const cs = getComputedStyle(slots), r = slots.getBoundingClientRect();
|
|
const colW = parseFloat(cs.gridTemplateColumns), gap = parseFloat(cs.columnGap) || 0;
|
|
return { left: r.left, top: r.top, cols: cs.gridTemplateColumns.split(" ").length,
|
|
colW, gap, cell: parseFloat(cs.gridAutoRows) || 64, rgap: parseFloat(cs.rowGap) || 0,
|
|
rows: cs.gridTemplateRows.split(" ").length };
|
|
};
|
|
let gridSent = -1;
|
|
const measureGrid = () => {
|
|
const g = grid(), home = document.querySelector(".home");
|
|
if (!g || !home) return;
|
|
const bottom = innerHeight - parseFloat(getComputedStyle(home).paddingBottom); // of the first screen
|
|
const rows = Math.max(1, Math.floor((bottom - (g.top + scrollY) + g.rgap) / (g.cell + g.rgap)));
|
|
const v = g.cols * 4096 + rows;
|
|
if (v !== gridSent) { gridSent = v; app.dispatch(GRID, v); }
|
|
};
|
|
const cellAt = (g, x, y) => [Math.max(0, Math.round((x - g.left) / (g.colW + g.gap))),
|
|
Math.max(0, Math.round((y - g.top) / (g.cell + g.rgap)))];
|
|
|
|
// Dragging a home slot. A long press lifts it (app.py marks it .lifted). Moving the
|
|
// finger over another slot moves it there in the order (the grid stays packed);
|
|
// over an empty cell it is left in that cell (top left by where it was held), and
|
|
// letting go drops it; whatever it then covers moves on to free cells.
|
|
// Letting go without moving opens its menu instead. A widget showing resize handles
|
|
// needs no long press: pressing its body picks it up right away (GRAB), and it keeps
|
|
// its handles when let go. Native widget views take their own touches, so the shell
|
|
// reports those through widgetGrab/widgetDrag/widgetDrop.
|
|
let drag = null, dragged = false; // drag: { x, y, moved } from pointerdown
|
|
let carry = null; // { dx, dy, cell }: where the lifted slot was held, the cell sent last
|
|
const inside = (r, x, y) => x >= r.left && x <= r.right && y >= r.top && y <= r.bottom;
|
|
const slotAt = (x, y) => {
|
|
for (const li of document.querySelectorAll(".slots > li[data-slot]:not(.lifted)"))
|
|
if (inside(li.getBoundingClientRect(), x, y)) return +li.dataset.slot;
|
|
return -1;
|
|
};
|
|
const dragTo = (x, y, x0 = x, y0 = y) => { // x0, y0: where the finger went down
|
|
const lifted = document.querySelector(".slots > .lifted"), g = grid();
|
|
if (!lifted || !g) return;
|
|
if (!carry) {
|
|
const r = lifted.getBoundingClientRect();
|
|
carry = { dx: x0 - r.left, dy: y0 - r.top, cell: -1 };
|
|
}
|
|
// A widget goes to the cell under it wherever that is (apps flow around it), so it
|
|
// can sit on any row; an app over another app takes its place in the order.
|
|
const widget = lifted.classList.contains("widget-slot");
|
|
if (!widget && inside(lifted.getBoundingClientRect(), x, y)) return;
|
|
// the cell under the finger: a slot there takes the lifted one's place in the order
|
|
const [fc, fr] = cellAt(g, x - g.colW / 2, y - g.cell / 2);
|
|
const to = slotAt(x, y) >= 0 ? slotAt(x, y)
|
|
: slotAt(g.left + fc * (g.colW + g.gap) + g.colW / 2, g.top + fr * (g.cell + g.rgap) + g.cell / 2);
|
|
if (to >= 0 && !widget) { carry.cell = -1; app.dispatch(DRAG_TO, to); return; }
|
|
if (y > g.top + g.rows * (g.cell + g.rgap)) return; // below the grid
|
|
const [col, row] = cellAt(g, x - carry.dx, y - carry.dy);
|
|
const cell = row * 64 + Math.min(col, 63);
|
|
if (cell !== carry.cell) { carry.cell = cell; app.dispatch(MOVE_CELL, cell); }
|
|
};
|
|
const drop = (moved) => {
|
|
carry = null;
|
|
if (document.querySelector(".slots > .lifted")) app.dispatch(DROP, moved ? 1 : 0);
|
|
};
|
|
let grabbed = false;
|
|
const grab = (li) => { grabbed = true; app.dispatch(GRAB, +li.dataset.slot); };
|
|
addEventListener("pointerdown", (e) => {
|
|
const li = e.isPrimary && !e.target.closest?.(".resize-handle") && e.target.closest?.(".slots > li.resizing");
|
|
if (li) grab(li);
|
|
}, true);
|
|
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, drag.x, drag.y);
|
|
});
|
|
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 });
|
|
|
|
// Resizing a widget. Holding a widget and letting go without moving shows handles on
|
|
// its right edge, bottom edge and corner (app.py). Dragging one snaps the widget to
|
|
// whole grid columns and rows, within the shell's limits for that widget
|
|
// (widgetLimits, CSS px: min/max size, 0 = no max, and whether it resizes h and v).
|
|
let rs = null, swallowClick = false;
|
|
const span = (k, size, gap) => k * size + (k - 1) * gap;
|
|
const fit = (want, lo, hi, size, gap, min, max) => {
|
|
// the step nearest to want, kept within [min, max] px where it can be
|
|
let k = Math.round((want + gap) / (size + gap));
|
|
while (k < hi && span(k, size, gap) < min - 8) k++;
|
|
while (k > lo && max > 0 && span(k, size, gap) > max + 8) k--;
|
|
return Math.max(lo, Math.min(hi, k));
|
|
};
|
|
addEventListener("pointerdown", (e) => {
|
|
const h = e.target.closest?.(".resize-handle");
|
|
if (!h || !e.isPrimary) return;
|
|
e.stopPropagation(); e.preventDefault(); // not a long press on the slot under it
|
|
const li = h.closest("li"), slots = li.parentElement, cs = getComputedStyle(slots);
|
|
const cols = cs.gridTemplateColumns.split(" ").map(parseFloat);
|
|
const wid = +li.dataset.wid || 0;
|
|
let lim = {};
|
|
try { lim = JSON.parse(host.widgetLimits?.(wid) || "{}"); } catch {}
|
|
const r = li.getBoundingClientRect();
|
|
rs = { li, edge: h.dataset.edge, left: r.left, top: r.top, lim, n: cols.length, colW: cols[0],
|
|
gap: parseFloat(cs.columnGap) || 0, rgap: parseFloat(cs.rowGap) || 0,
|
|
cell: parseFloat(cs.gridAutoRows) || 88, // a row of app tiles
|
|
cols: Math.round((r.width + (parseFloat(cs.columnGap) || 0)) / (cols[0] + (parseFloat(cs.columnGap) || 0))),
|
|
h: Math.round(r.height) };
|
|
dragged = true;
|
|
h.setPointerCapture?.(e.pointerId);
|
|
}, true);
|
|
addEventListener("pointermove", (e) => {
|
|
if (!rs) return;
|
|
const { lim, n, colW, gap, rgap, cell } = rs;
|
|
let cols = rs.cols, h = rs.h;
|
|
if (rs.edge !== "b" && lim.h !== false)
|
|
cols = fit(e.clientX - rs.left, 1, n, colW, gap, lim.minW || 0, lim.maxW || 0);
|
|
if (rs.edge !== "r" && lim.v !== false) {
|
|
const k = fit(e.clientY - rs.top, 1, 20, cell, rgap, lim.minH || 0, lim.maxH || 0);
|
|
h = Math.round(span(k, cell, rgap));
|
|
}
|
|
if (cols === rs.cols && h === rs.h) return;
|
|
rs.cols = cols; rs.h = h;
|
|
host.haptic();
|
|
app.dispatch(RESIZE, (cols >= n ? 0 : cols) * 4096 + h);
|
|
}, true);
|
|
const endResize = () => { if (rs || grabbed) { rs = null; grabbed = false; swallowClick = true; setTimeout(() => swallowClick = false, 0); } };
|
|
addEventListener("pointerup", endResize, true);
|
|
addEventListener("pointercancel", endResize, true);
|
|
addEventListener("click", (e) => { // the click that ends a handle drag or a move isn't a tap beside the widget
|
|
if (swallowClick || e.target.closest?.(".resize-handle")) { swallowClick = false; e.stopPropagation(); e.preventDefault(); }
|
|
}, true);
|
|
addEventListener("touchmove", (e) => { if (rs && e.cancelable) e.preventDefault(); }, { passive: false });
|
|
|
|
// A new widget starts as many columns wide as its natural width needs (widgetLimits
|
|
// "w", CSS px), like its height starts at whole rows; 0 is the whole row.
|
|
const widgetCols = (id) => {
|
|
let lim = {};
|
|
try { lim = JSON.parse(host.widgetLimits?.(id) || "{}"); } catch {}
|
|
const slots = document.querySelector(".slots");
|
|
if (!lim.w || !slots) return 0;
|
|
const cs = getComputedStyle(slots), cols = cs.gridTemplateColumns.split(" ").map(parseFloat);
|
|
const gap = parseFloat(cs.columnGap) || 0;
|
|
const k = Math.max(1, Math.ceil((lim.w + gap - 8) / (cols[0] + gap)));
|
|
return k >= cols.length ? 0 : k;
|
|
};
|
|
|
|
window.ultraHome = {
|
|
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); app.dispatch(WIDGET_COLS, widgetCols(id)); },
|
|
widgetMenu: (id) => app.dispatch(WIDGET_MENU, id),
|
|
widgetGrab: (id) => { const li = document.querySelector(`.widget-slot.resizing[data-wid="${id}"]`);
|
|
if (li) app.dispatch(GRAB, +li.dataset.slot); },
|
|
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; },
|
|
};
|
|
addEventListener("scroll", () => requestAnimationFrame(place), { passive: true });
|
|
addEventListener("resize", () => requestAnimationFrame(() => { place(); measureGrid(); }));
|
|
measureGrid();
|
|
|
|
// 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>
|