From a387ff9708a8e891130171dd44689560214d32b7 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 21 Oct 2026 22:30:58 +0000 Subject: [PATCH] Press and hold a home app or widget to drag it to a new spot 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) Claude-Session: https://claude.ai/code/session_01JfvcrzLMTeE68CpNffNDvE --- .../java/dev/ultra/launcher/MainActivity.java | 53 +++++++++++++++- tests/e2e.cjs | 22 ++++++- web/app.py | 58 ++++++++++++++---- web/app.wasm | Bin 11442 -> 11803 bytes web/index.html | 47 +++++++++++++- web/style.css | 7 +++ 6 files changed, 167 insertions(+), 20 deletions(-) diff --git a/android/app/src/main/java/dev/ultra/launcher/MainActivity.java b/android/app/src/main/java/dev/ultra/launcher/MainActivity.java index 59d318a..349424b 100644 --- a/android/app/src/main/java/dev/ultra/launcher/MainActivity.java +++ b/android/app/src/main/java/dev/ultra/launcher/MainActivity.java @@ -503,7 +503,11 @@ public class MainActivity extends Activity { } } - /** Widget views that report a long press to the page instead of to the widget. */ + /** + * Widget views that report a long press to the page instead of to the widget. The press + * lifts the widget's slot; the moves and the release that follow go to the page as + * widgetDrag(x, y) in CSS px and widgetDrop(moved), so it can be dragged to a new place. + */ static final class Host extends AppWidgetHost { Host(MainActivity a) { super(a, HOST_ID); } @@ -517,11 +521,14 @@ public class MainActivity extends Activity { private final MainActivity home; private final int id; private float downX, downY; - private boolean longPressed; + private boolean longPressed, moved; + private final int[] webAt = new int[2]; private final Runnable fire = new Runnable() { @Override public void run() { longPressed = true; + moved = false; performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); + setAlpha(.85f); home.js("widgetMenu(" + id + ")"); } }; @@ -550,14 +557,54 @@ public class MainActivity extends Activity { case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: removeCallbacks(fire); + if (longPressed) { + drop(); // released before any move reached onTouchEvent + return true; + } break; } return longPressed; // once fired, take the gesture away from the widget } + private void drop() { + longPressed = false; + setAlpha(1f); + home.js("widgetDrop(" + moved + ")"); + } + @Override public boolean onTouchEvent(MotionEvent e) { - return longPressed || super.onTouchEvent(e); + if (!longPressed) { + // Where no child took the press, its moves come here instead of through + // onInterceptTouchEvent; keep the gesture so they do. + int a = e.getActionMasked(); + if (a == MotionEvent.ACTION_UP || a == MotionEvent.ACTION_CANCEL + || (a == MotionEvent.ACTION_MOVE && Math.hypot(e.getX() - downX, e.getY() - downY) + > ViewConfiguration.get(getContext()).getScaledTouchSlop())) { + removeCallbacks(fire); + } + return super.onTouchEvent(e) || a == MotionEvent.ACTION_DOWN; + } + switch (e.getActionMasked()) { + case MotionEvent.ACTION_MOVE: + if (!moved && Math.hypot(e.getX() - downX, e.getY() - downY) + > ViewConfiguration.get(getContext()).getScaledTouchSlop()) { + moved = true; + } + if (moved) { + // Raw screen coordinates: this view moves as the page reorders slots. + float d = getResources().getDisplayMetrics().density; + home.web.getLocationOnScreen(webAt); + home.js("widgetDrag(" + Math.round((e.getRawX() - webAt[0]) / d) + "," + + Math.round((e.getRawY() - webAt[1]) / d) + ")"); + } + break; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + drop(); + break; + } + return true; } } diff --git a/tests/e2e.cjs b/tests/e2e.cjs index 89fcca7..8c465d9 100644 --- a/tests/e2e.cjs +++ b/tests/e2e.cjs @@ -37,9 +37,27 @@ const BASE = process.env.BASE || "http://localhost:8000"; await p.click(".widget-list li:first-child"); await settle(); assert.equal(await p.getAttribute(".widget-slot", "data-wid"), "100"); assert.equal(await p.textContent(".widget-label"), "Clock"); - // long press a home app: reorder - await longpress(".slots .tile:nth-child(2)"); + // 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(); + 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.up(); await settle(); + }; + await drag(".slots .tile:nth-child(2)", ".slots .tile:nth-child(1)"); + 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(); assert.deepEqual(await homeApps(), ["Gmail", "Chrome", "Phone"]); // swipe down opens the drawer (and takes the widget off screen), search, long press adds const swipe = (y0, y1) => p.evaluate(([y0, y1]) => { diff --git a/web/app.py b/web/app.py index 7e2dce1..d297d05 100644 --- a/web/app.py +++ b/web/app.py @@ -9,6 +9,8 @@ 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). # # The native shell (android/) serves this folder in a WebView, answers # apps.json and widgets.json, hosts the widgets as native views over the @@ -68,7 +70,7 @@ BACK: Const[i32] = 4 SWIPE_DOWN: Const[i32] = 5 SWIPE_UP: Const[i32] = 6 WIDGET_ADDED: Const[i32] = 7 -WIDGET_MENU: Const[i32] = 8 +WIDGET_MENU: Const[i32] = 8 # arg: widget id; a long press on a native widget lifts it LOADED: Const[i32] = 10 WIDGETS_LOADED: Const[i32] = 11 QUERY: Const[i32] = 12 @@ -81,7 +83,6 @@ OPEN_HOME: Const[i32] = 18 # arg: home slot ADD_HOME: Const[i32] = 19 # arg: row in the app list (long press in the drawer) TOGGLE_HOME: Const[i32] = 20 # arg: row in the app list (drawer in pick mode) MENU: Const[i32] = 21 # long press on the empty home -ITEM_MENU: Const[i32] = 22 # arg: home slot CLOSE_SHEET: Const[i32] = 23 PICK_APPS: Const[i32] = 24 PICK_WIDGET: Const[i32] = 25 @@ -93,6 +94,9 @@ 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 +DROP: Const[i32] = 36 # arg: 1 if it was dragged, 0 if just held (opens its menu) CAP: Const[i32] = 512 HOME_CAP: Const[i32] = 40 @@ -147,6 +151,7 @@ drawer: i32 = 0 picking: i32 = 0 # drawer is in "add apps" mode: taps toggle home slots sheet: i32 = 0 item: i32 = -1 # home slot the item sheet is about +lifted: i32 = -1 # home slot picked up by a long press, being dragged pending: i32 = 0 # JSON handle of a request in flight @@ -365,6 +370,17 @@ def swap_slots(a: i32, b: i32) -> None: 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): @@ -374,16 +390,17 @@ def slot_of_widget(wid: i32) -> i32: def close_all() -> None: - global drawer, picking, sheet, item + global drawer, picking, sheet, item, lifted s_clear(query) drawer = 0 picking = 0 sheet = NONE item = -1 + lifted = -1 # ---- Update def update(msg: i32, arg: i32) -> None: - global loading, drawer, picking, sheet, item, pending, wloading, chosen + global loading, drawer, picking, sheet, item, pending, wloading, chosen, lifted i: i32 if msg == LOADED: load_apps(arg) @@ -423,15 +440,23 @@ def update(msg: i32, arg: i32) -> None: elif msg == MENU: haptic() sheet = SHEET_MENU - elif msg == ITEM_MENU: - haptic() - item = arg - sheet = SHEET_ITEM + elif msg == LIFT: + if drawer == 0 and sheet == NONE: + haptic() + lifted = arg elif msg == WIDGET_MENU: i = slot_of_widget(arg) - if i >= 0 and drawer == 0: - item = i + if i >= 0 and drawer == 0 and sheet == NONE: + lifted = i + elif msg == DRAG_TO: + if lifted >= 0 and arg >= 0 and arg < nh and arg != lifted: + move_slot(lifted, arg) + lifted = arg + elif msg == DROP: + if lifted >= 0 and arg == 0: + item = lifted sheet = SHEET_ITEM + lifted = -1 elif msg == CLOSE_SHEET: sheet = NONE item = -1 @@ -487,7 +512,9 @@ def update(msg: i32, arg: i32) -> None: elif msg == HOME: close_all() elif msg == BACK: - if sheet != NONE: + if lifted >= 0: + lifted = -1 + elif sheet != NONE: sheet = NONE item = -1 elif drawer == 1: @@ -511,6 +538,8 @@ def view_home_slot(i: i32) -> None: el("li") key(hkey[i]) cls("widget-slot") + cls_if("lifted", i == lifted) + attr_num("data-slot", i) s_set(scratch, "height:") s_add_num(scratch, hh[i]) s_add(scratch, "px") @@ -519,7 +548,7 @@ def view_home_slot(i: i32) -> None: # them while nothing covers the home screen. if drawer == 0 and sheet == NONE: attr_num("data-wid", hwid[i]) - on_longpress(ITEM_MENU, i) + on_longpress(LIFT, i) el("span") cls("widget-label") text_ref(hstr[i]) @@ -532,8 +561,10 @@ def view_home_slot(i: i32) -> None: el("li") key(hkey[i]) cls("tile") + cls_if("lifted", i == lifted) + attr_num("data-slot", i) on_click(OPEN_HOME, i) - on_longpress(ITEM_MENU, i) + on_longpress(LIFT, i) view_icon(icons[row]) el("span") cls("name") @@ -585,6 +616,7 @@ def view_home() -> None: el("ul") cls("slots") + cls_if("dragging", lifted >= 0) for i in range(nh): view_home_slot(i) end() diff --git a/web/app.wasm b/web/app.wasm index 7ce1cf1c2efc8756aaa1c87cc0292b8fcbcf09d0..92af1e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ezinv>K2sWqeQd1{weM3kebK6Lmn#ZeV8sbLAVIvQ)=l`CJ%`qB4_B< zp*zPnV6Hk>DiFykm@hVZ{`;3+Wf%O{z;-!*X|!3V6I>fg%e6p-HFl%+enD=p@czow z*ZvIhr|GO!7Z%7fc!YsdFE23lMv*92*D$yRLAhF3pwI`&Ri`xm0pa(n3~oYY?lLv| z8R2deLCZx9gXz{mAa#`OSmGQD)Tv$O#lt6*)N!E)}xCU;Y|R%YZz^yQ2;tn z5#4A;X=>rOo~Y7~$`IsQl6(BqvqwHxbOrSDNCB@WOznqWQS>8DK|H$9sgd@;qfM#b z!;)Ir4NKBocIyKjZBs3{Y#hvx@YSZ6wC@K1RnlCQ!6>Y?sJTUN$Z2mISNi7){0jfb zBW~ThrErM~vrpZ0WdV9Vd{QS`s1`jL-f7&*%Yd=6^7@=VHHRDNk+q;cybQ!6tyuT2 z@88Y;cz=G~)shW<1Apq_Cye_iVpAzYp%%G06E%!IT9Wz1x!R)As4Z^Z1ea^>O^{{o z>uwvp{bDIp9XIa)u|^~A5IGTzjZdVyAw}77^FE|ST6!O(f95p{5|ko0A3_q)l7~bl zQ?JLP7D-W)+)HpXU=Ma|(q35^Z^MppA=DB$Th;nyB4%=FnMhif|z;r`; zho(gmu}IrQY-}t#1wWucaB~R=H%XR2`csK>QA0f-$sH5dy-^xZ5E86CET$kK*RBG(_&s7iqFd({sBk->kjU?hRd|RUS z#m##;Bktu8aeR+Tq{#|2FT^po#3Lfr%#0PAIx~$p61fbi&`w?kDW22c#jG=l=}Aj} zwxV8#FivEX>jKCuD!W~*gqFw67NA^nuPqLHE{S+fF{tx#a}OX@dbwL5Ylf#IW7w~U rNq#;18!{Vxkee?8S*wxv7oD%#nk_U#galkStji#?Jt`hcrmX(}ZNreN diff --git a/web/index.html b/web/index.html index 734846b..4a797e5 100644 --- a/web/index.html +++ b/web/index.html @@ -113,7 +113,48 @@ // 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; + 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), @@ -121,6 +162,8 @@ 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; }, @@ -137,7 +180,7 @@ start = e.touches.length === 1 ? { x: t.clientX, y: t.clientY, at: Date.now() } : null; }, { passive: true }); addEventListener("touchend", (e) => { - if (!start) return; + 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; diff --git a/web/style.css b/web/style.css index 0e86f30..d9670e0 100644 --- a/web/style.css +++ b/web/style.css @@ -58,6 +58,13 @@ body { margin: 0; min-height: 100vh; color: #fff; -webkit-tap-highlight-color: t border: 1px dashed rgb(255 255 255 / .3); } .widget-label { font-size: .875rem; opacity: .75; text-shadow: var(--shadow-text); } +/* A slot picked up by a long press, while it is dragged (index.html moves it). */ +.slots > li { transition: transform .15s ease-out; } +.slots > .lifted { z-index: 1; transform: scale(1.12); background: rgb(255 255 255 / .16); + box-shadow: 0 8px 24px rgb(0 0 0 / .35); } +.slots.dragging > li:not(.lifted) { opacity: .8; } +.slots.dragging .tile:active { background: none; } + .hint, .empty { text-align: center; opacity: .85; text-shadow: var(--shadow-text); } .hint { margin-top: 30vh; padding: 0 2rem; font-size: .9rem; line-height: 1.5; }