diff --git a/tests/e2e.cjs b/tests/e2e.cjs index 845b9ec..260e32a 100644 --- a/tests/e2e.cjs +++ b/tests/e2e.cjs @@ -26,7 +26,9 @@ const BASE = process.env.BASE || "http://localhost:8000"; await p.click(".sheet-item >> text=Check for updates"); await settle(); assert.equal(await p.textContent("#toast"), "Check for updates"); assert.ok(!(await p.$(".sheet")), "the menu closes"); - const homeApps = () => p.$$eval(".slots .name", (ns) => ns.map((n) => n.textContent)); + // home apps in reading order on the grid + const homeApps = () => p.$$eval(".slots .tile", (ts) => ts.map((t) => [t.getBoundingClientRect(), t.textContent]) + .sort(([a], [b]) => Math.round(a.top - b.top) || a.left - b.left).map(([, n]) => n)); // long press on the blank home: add apps through the drawer's pick mode await longpress(".hint"); await p.click(".sheet-item >> text=Add apps"); await settle(); @@ -52,27 +54,40 @@ const BASE = process.env.BASE || "http://localhost:8000"; assert.equal(await p.getAttribute(".widget-slot", "data-wid"), "100"); assert.equal(await p.textContent(".widget-label"), "Clock"); // hold a home app and drag it: reorder - const drag = async (from, to) => { - const a = await p.locator(from).first().boundingBox(), z = await p.locator(to).first().boundingBox(); + const app = (name) => `.slots .tile:has-text("${name}")`; + const dragBy = async (from, dx, dy) => { + const a = await p.locator(from).first().boundingBox(); await p.mouse.move(a.x + a.width / 2, a.y + a.height / 2); await p.mouse.down(); await p.waitForTimeout(650); await settle(); assert.ok(await p.$(".slots > .lifted"), "a long press lifts the slot"); - await p.mouse.move(z.x + z.width / 2, z.y + z.height / 2, { steps: 5 }); await settle(); + await p.mouse.move(a.x + a.width / 2 + dx, a.y + a.height / 2 + dy, { steps: 5 }); await settle(); await p.mouse.up(); await settle(); }; - await drag(".slots .tile:nth-child(2)", ".slots .tile:nth-child(1)"); + const drag = async (from, to) => { + const a = await p.locator(from).first().boundingBox(), z = await p.locator(to).first().boundingBox(); + await dragBy(from, z.x - a.x, z.y - a.y); + }; + // the grid runs to the bottom of the screen + const rows = await p.$eval(".slots", (s) => getComputedStyle(s).gridTemplateRows.split(" ").length); + assert.ok(rows >= 6, `rows fill the screen: ${rows}`); + // dropping on an app takes its cell; that app moves to the first free cell + await drag(app("Gmail"), app("Chrome")); assert.deepEqual(await homeApps(), ["Gmail", "Chrome", "Phone"]); assert.ok(!(await p.$(".sheet")), "a drag doesn't open the menu"); assert.ok(!(await p.$(".slots > .lifted"))); - await drag(".slots .tile:nth-child(1)", ".slots .tile:nth-child(3)"); - assert.deepEqual(await homeApps(), ["Chrome", "Phone", "Gmail"]); - // hold without moving: the menu - await longpress(".slots .tile:nth-child(3)"); - await p.click(".sheet-item >> text=Move earlier"); await settle(); - assert.deepEqual(await homeApps(), ["Chrome", "Gmail", "Phone"]); - await longpress(".slots .tile:nth-child(1)"); - await p.click(".sheet-item >> text=Move later"); await settle(); + // an empty cell far down takes an app, and the others stay put + const before = await p.locator(app("Chrome")).boundingBox(); + await dragBy(app("Gmail"), 0, 4 * 84); + const g = await p.locator(app("Gmail")).boundingBox(), c = await p.locator(app("Chrome")).boundingBox(); + assert.ok(Math.abs(g.y - before.y - 4 * 84) < 3, `dropped four rows down: ${g.y - before.y}`); + assert.deepEqual([c.x, c.y], [before.x, before.y], "the other apps stay where they were"); + await p.reload(); await p.waitForSelector(".slots .tile"); + assert.ok(Math.abs((await p.locator(app("Gmail")).boundingBox()).y - g.y) < 3, "the cell is saved"); + // and back up to the empty first cell, left of Chrome + const ph = await p.locator(app("Phone")).boundingBox(); + await dragBy(app("Gmail"), c.x - (ph.x - c.x) - g.x, c.y - g.y); assert.deepEqual(await homeApps(), ["Gmail", "Chrome", "Phone"]); + await p.mouse.click(5, 300); await settle(); // a tap on empty home ends the drag gesture // swipe down opens the drawer (and takes the widget off screen), search, long press adds const swipe = (y0, y1) => p.evaluate(([y0, y1]) => { const at = (y) => new Touch({ identifier: 1, target: document.body, clientX: 200, clientY: y }); @@ -98,7 +113,7 @@ const BASE = process.env.BASE || "http://localhost:8000"; // persisted; tap launches; remove await p.reload(); await p.waitForSelector(".slots .tile"); assert.equal(await p.$$eval(".slots > li", (ls) => ls.length), 5); - await p.click(".slots .tile:first-child"); await settle(); + await p.click(app("Gmail")); await settle(); assert.equal(await p.textContent("#toast"), "Open com.example.gmail/.MainActivity"); // hold a widget without moving: resize handles; drag them to snap it to the grid await longpress(".widget-slot"); @@ -123,15 +138,18 @@ const BASE = process.env.BASE || "http://localhost:8000"; assert.deepEqual(await size(), [w1, h1], "the size is saved"); // while it shows handles, dragging the widget itself moves it, without another long press await longpress(".widget-slot"); - const order = () => p.$$eval(".slots > li", (ls) => ls.map((l) => l.classList.contains("widget-slot") ? "W" : "a")); - assert.notEqual((await order())[0], "W"); + const atStart = () => p.$eval(".widget-slot", (w) => { + const r = w.getBoundingClientRect(), s = w.parentElement.getBoundingClientRect(); + return Math.abs(r.left - s.left) < 2 && Math.abs(r.top - s.top) < 2; + }); + assert.ok(!(await atStart())); const wb = await p.locator(".widget-slot").boundingBox(), first = await p.locator(".slots > li").first().boundingBox(); await p.mouse.move(wb.x + wb.width / 2, wb.y + wb.height / 2); await p.mouse.down(); await settle(); assert.ok(await p.$(".widget-slot.lifted"), "pressing a widget with handles picks it up"); await p.mouse.move(first.x + first.width / 2, first.y + first.height / 2, { steps: 8 }); await settle(); await p.mouse.up(); await settle(); - assert.equal((await order())[0], "W", "dragged to the front"); + assert.ok(await atStart(), "dragged to the first cell"); // the apps flow into the cells beside it: a 2x2 widget in 5 columns leaves room for all four const beside = await p.$eval(".widget-slot", (w) => { const r = w.getBoundingClientRect(); diff --git a/web/app.py b/web/app.py index 76f836f..2124a13 100644 --- a/web/app.py +++ b/web/app.py @@ -9,12 +9,14 @@ from ui import btn_open, input_open, alert # An Android home screen. The home starts blank: you add apps and widgets to it # with a long press. Swiping down opens the drawer with every app and a search. -# A long press on a home app or widget picks it up: drag it to move it, or let go -# without moving to get its menu (index.html does the dragging). Letting go of a +# The home grid runs to the bottom of the screen. A long press on a home app or +# widget picks it up: drag it to any cell to move it there (what it lands on +# moves to the first free cells), or let go without moving to get its menu +# (index.html does the dragging). Letting go of a # widget that way shows resize handles on its right and bottom edges instead: # dragging them snaps the widget to whole grid columns and rows of app tiles, # within the sizes it allows, and dragging the widget itself moves it, staying -# in that mode once let go. Apps flow into the free cells beside widgets. +# in that mode once let go. New apps flow into the first free cells. # # The native shell (android/) serves this folder in a WebView, answers # apps.json and widgets.json, hosts the widgets as native views over the @@ -86,14 +88,13 @@ PICK_APPS: Const[i32] = 24 PICK_WIDGET: Const[i32] = 25 CHOOSE_WIDGET: Const[i32] = 26 # arg: row in the widget list REMOVE: Const[i32] = 27 # arg: home slot -MOVE: Const[i32] = 28 # arg: home slot; moves it one earlier INFO: Const[i32] = 29 # arg: home slot SETTINGS: Const[i32] = 30 WALLPAPER: Const[i32] = 31 NOTICE_DONE: Const[i32] = 32 RECENTS: Const[i32] = 33 LIFT: Const[i32] = 34 # arg: home slot (long press picks it up) -DRAG_TO: Const[i32] = 35 # arg: home slot the lifted one is dragged over +MOVE_CELL: Const[i32] = 35 # arg: row * 64 + column the lifted slot is dragged to DROP: Const[i32] = 36 # arg: 1 if it was dragged, 0 if just held (opens its menu) WQUERY: Const[i32] = 37 # widget picker search WCLEAR: Const[i32] = 38 @@ -103,6 +104,8 @@ RESIZE_DONE: Const[i32] = 41 GRAB: Const[i32] = 42 # arg: the widget slot showing handles, picked up by its body CHECK_UPDATES: Const[i32] = 43 WIDGET_COLS: Const[i32] = 44 # arg: columns for the widget just added (0: the whole row) +GRID: Const[i32] = 45 # arg: columns * 4096 + rows that fit on the screen +PIN: Const[i32] = 46 # arg: slot * 65536 + row * 64 + column it shows at now CAP: Const[i32] = 512 HOME_CAP: Const[i32] = 200 @@ -136,7 +139,13 @@ hwid: i32[512] = empty(512, dtype=int32) hh: i32[512] = empty(512, dtype=int32) hw: i32[512] = empty(512, dtype=int32) hkey: i32[512] = empty(512, dtype=int32) +# The grid cell a slot is pinned to (column hx, row hy, from 0), or -1: placed +# by the grid's dense flow into the first free cells that fit it. +hx: i32[512] = empty(512, dtype=int32) +hy: i32[512] = empty(512, dtype=int32) nh: i32 = 0 +ncols: i32 = 5 # grid columns, and rows that fill the screen (index.html measures them) +nrows: i32 = 0 next_key: i32 = 1 # Widget providers, for the picker. wlabels: i32[512] = empty(512, dtype=int32) @@ -185,6 +194,8 @@ def init() -> None: hwid[nh] = load_i32_at("ultra.home.wid", i, 0) hh[nh] = load_i32_at("ultra.home.h", i, 120) hw[nh] = load_i32_at("ultra.home.w", i, 0) + hx[nh] = load_i32_at("ultra.home.x", i, -1) + hy[nh] = load_i32_at("ultra.home.y", i, -1) hkey[nh] = next_key next_key += 1 nh += 1 @@ -202,6 +213,8 @@ def save_home() -> None: store_i32_at("ultra.home.wid", i, hwid[i]) store_i32_at("ultra.home.h", i, hh[i]) store_i32_at("ultra.home.w", i, hw[i]) + store_i32_at("ultra.home.x", i, hx[i]) + store_i32_at("ultra.home.y", i, hy[i]) def say(msg: str) -> None: @@ -342,6 +355,8 @@ def push_slot(kind: i32, s: i32, wid: i32, h: i32) -> None: hwid[nh] = wid hh[nh] = h hw[nh] = 0 + hx[nh] = -1 + hy[nh] = -1 hkey[nh] = next_key next_key += 1 nh += 1 @@ -370,45 +385,55 @@ def remove_slot(at: i32) -> None: hwid[j] = hwid[j + 1] hh[j] = hh[j + 1] hw[j] = hw[j + 1] + hx[j] = hx[j + 1] + hy[j] = hy[j + 1] hkey[j] = hkey[j + 1] nh -= 1 save_home() -def swap_slots(a: i32, b: i32) -> None: - t: i32 - t = hkind[a] - hkind[a] = hkind[b] - hkind[b] = t - t = hstr[a] - hstr[a] = hstr[b] - hstr[b] = t - t = hwid[a] - hwid[a] = hwid[b] - hwid[b] = t - t = hh[a] - hh[a] = hh[b] - hh[b] = t - t = hw[a] - hw[a] = hw[b] - hw[b] = t - t = hkey[a] - hkey[a] = hkey[b] - hkey[b] = t +def slot_w(i: i32) -> i32: + """Columns slot i spans.""" + if hkind[i] == APP: + return 1 + if hw[i] <= 0 or hw[i] > ncols: + return ncols + return hw[i] + + +def slot_h(i: i32) -> i32: + """Rows slot i spans.""" + if hkind[i] == APP: + return 1 + return widget_rows(hh[i]) + + +def clear_cells(i: i32) -> None: + """Unpin the slots under pinned slot i, so they flow into free cells.""" + j: i32 + for j in range(nh): + if j != i and hx[j] >= 0 and hx[j] < hx[i] + slot_w(i) and hx[i] < hx[j] + slot_w(j) \ + and hy[j] < hy[i] + slot_h(i) and hy[i] < hy[j] + slot_h(j): + hx[j] = -1 + hy[j] = -1 + + +def pin_slot(i: i32, col: i32, row: i32) -> None: + """Pin slot i with its top left at col, row (kept on the grid). It may cover others + while it is carried; clear_cells moves them off once it is put down.""" + x: i32 = col + y: i32 = row + if x + slot_w(i) > ncols: + x = ncols - slot_w(i) + if x < 0: + x = 0 + if y < 0: + y = 0 + hx[i] = x + hy[i] = y save_home() -def move_slot(a: i32, b: i32) -> None: - """Take slot a out and put it back at b, shifting the slots between.""" - i: i32 = a - while i < b: - swap_slots(i, i + 1) - i += 1 - while i > b: - swap_slots(i, i - 1) - i -= 1 - - def slot_of_widget(wid: i32) -> i32: i: i32 for i in range(nh): @@ -430,7 +455,7 @@ def close_all() -> None: # ---- Update def update(msg: i32, arg: i32) -> None: - global loading, drawer, picking, sheet, item, pending, wloading, chosen, lifted, resizing, regrab + global loading, drawer, picking, sheet, item, pending, wloading, chosen, lifted, resizing, regrab, ncols, nrows i: i32 if msg == LOADED: load_apps(arg) @@ -493,11 +518,22 @@ def update(msg: i32, arg: i32) -> None: resizing = -1 regrab = 1 lifted = arg - elif msg == DRAG_TO: - if lifted >= 0 and arg >= 0 and arg < nh and arg != lifted: - move_slot(lifted, arg) - lifted = arg + elif msg == MOVE_CELL: + if lifted >= 0 and (hx[lifted] != arg % 64 or hy[lifted] != arg // 64): + pin_slot(lifted, arg % 64, arg // 64) + elif msg == GRID: + ncols = arg // 4096 + nrows = arg % 4096 + elif msg == PIN: + i = arg // 65536 + if i >= 0 and i < nh: + hx[i] = arg % 64 + hy[i] = (arg % 65536) // 64 + save_home() elif msg == DROP: + if lifted >= 0 and hx[lifted] >= 0: + clear_cells(lifted) + save_home() if lifted >= 0 and (arg == 0 or regrab == 1): if hkind[lifted] == WIDGET: resizing = lifted @@ -510,6 +546,9 @@ def update(msg: i32, arg: i32) -> None: if resizing >= 0: hw[resizing] = arg // 4096 hh[resizing] = arg % 4096 + if hx[resizing] >= 0: + pin_slot(resizing, hx[resizing], hy[resizing]) + clear_cells(resizing) save_home() elif msg == RESIZE_DONE: resizing = -1 @@ -549,11 +588,6 @@ def update(msg: i32, arg: i32) -> None: remove_slot(arg) sheet = NONE item = -1 - elif msg == MOVE: - if arg > 0: - swap_slots(arg, arg - 1) - sheet = NONE - item = -1 elif msg == INFO: app_info(hstr[arg]) sheet = NONE @@ -635,6 +669,24 @@ def widget_rows(h: i32) -> i32: return k +def view_cell(i: i32) -> None: + """Slot i's place on the grid: its pinned cell if it fits, and its span.""" + s_clear(scratch) + if hx[i] >= 0 and hx[i] + slot_w(i) <= ncols: + s_add(scratch, "grid-column:") + s_add_num(scratch, hx[i] + 1) + s_add(scratch, ";grid-row:") + s_add_num(scratch, hy[i] + 1) + s_add(scratch, "/") + else: + s_add(scratch, "grid-row:") + s_add(scratch, "span ") + s_add_num(scratch, slot_h(i)) + s_add(scratch, ";grid-column-end:span ") + s_add_num(scratch, slot_w(i)) + attr_ref("style", scratch) + + def view_home_slot(i: i32) -> None: row: i32 if hkind[i] == WIDGET: @@ -645,12 +697,7 @@ def view_home_slot(i: i32) -> None: cls_if("resizing", i == resizing) attr_num("data-slot", i) # Its height spans whole rows of app tiles (72px rows, 12px gaps; style.css). - s_set(scratch, "grid-row:span ") - s_add_num(scratch, widget_rows(hh[i])) - if hw[i] > 0: - s_add(scratch, ";grid-column:span ") - s_add_num(scratch, hw[i]) - attr_ref("style", scratch) + view_cell(i) # The native widget view is laid over slots marked live, so only mark # them while nothing covers the home screen. While one is resized the # others step aside, so nothing native covers its handles. @@ -675,6 +722,8 @@ def view_home_slot(i: i32) -> None: cls("tile") cls_if("lifted", i == lifted) attr_num("data-slot", i) + if hx[i] >= 0: + view_cell(i) on_click(OPEN_HOME, i) on_longpress(LIFT, i) view_icon(icons[row]) @@ -721,6 +770,12 @@ def view_home() -> None: el("ul") cls("slots") cls_if("dragging", lifted >= 0) + if nrows > 0 and nh > 0: + # Rows down to the bottom of the screen, so empty cells there take apps too. + s_set(scratch, "grid-template-rows:repeat(") + s_add_num(scratch, nrows) + s_add(scratch, ",4.5rem)") + attr_ref("style", scratch) for i in range(nh): view_home_slot(i) end() @@ -836,10 +891,6 @@ def view_sheet() -> None: if row >= 0: text_ref(labels[row]) end() - if item > 0: - sheet_btn("Move earlier", MOVE, item) - if item < nh - 1: - sheet_btn("Move later", MOVE, item + 1) if hkind[item] == APP: sheet_btn("App info", INFO, item) sheet_btn("Remove from home", REMOVE, item) diff --git a/web/app.wasm b/web/app.wasm index 099ad36..4c23ea3 100644 Binary files a/web/app.wasm and b/web/app.wasm differ diff --git a/web/index.html b/web/index.html index dfc91d7..385fda4 100644 --- a/web/index.html +++ b/web/index.html @@ -116,8 +116,9 @@ }; 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(); watchDrawer(); }, + onRender: () => { fillIcons(); place(); watchDrawer(); if (ready) measureGrid(); }, imports: ({ strs }) => ({ launch: (h) => host.launch(strs[h]), app_info: (h) => host.appInfo(strs[h]), @@ -131,34 +132,66 @@ }), }); + 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; + WIDGET_ADDED = 7, WIDGET_MENU = 8, MOVE_CELL = 35, DROP = 36, RESIZE = 40, GRAB = 42, WIDGET_COLS = 44, + GRID = 45, PIN = 46; + + // 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) || 72, rgap: parseFloat(cs.rowGap) || 0 }; + }; + 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 then slides it to the slot under the finger, and letting go drops it there. + // finger then carries it over the grid to the cell under it (by where it was held); + // letting go drops it there, and whatever it covers moves to the first free cells. Before the first move every slot is + // pinned where it shows, so the rest stay put and its old cell is left empty. // 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 - const slotAt = (x, y) => { - let best = -1, bestD = Infinity; + let carry = null; // { dx, dy, cell }: where the lifted slot was held, the cell sent last + const freeze = (g) => { 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; } + const r = li.getBoundingClientRect(), k = li.classList.contains("lifted") ? 1.12 : 1; // style.css + const [col, row] = cellAt(g, r.left + r.width / 2 * (1 - 1 / k), r.top + r.height / 2 * (1 - 1 / k)); + app.dispatch(PIN, +li.dataset.slot * 65536 + row * 64 + Math.min(col, 63)); } - 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 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 }; + freeze(g); + } + 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; @@ -175,7 +208,7 @@ 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); + 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; }); @@ -266,7 +299,8 @@ for (const img of document.querySelectorAll("img.icon")) img.__icon = undefined; }, }; addEventListener("scroll", () => requestAnimationFrame(place), { passive: true }); - addEventListener("resize", () => requestAnimationFrame(place)); + 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).