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) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JfvcrzLMTeE68CpNffNDvE
This commit is contained in:
Claude committed 2026-10-21 22:30:58 +00:00
1 parent 7aabe57afa
commit a387ff9708
6 files changed
+166 -19

No files matched your search

@@ -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 { static final class Host extends AppWidgetHost {
Host(MainActivity a) { super(a, HOST_ID); } Host(MainActivity a) { super(a, HOST_ID); }
@@ -517,11 +521,14 @@ public class MainActivity extends Activity {
private final MainActivity home; private final MainActivity home;
private final int id; private final int id;
private float downX, downY; private float downX, downY;
private boolean longPressed; private boolean longPressed, moved;
private final int[] webAt = new int[2];
private final Runnable fire = new Runnable() { private final Runnable fire = new Runnable() {
@Override public void run() { @Override public void run() {
longPressed = true; longPressed = true;
moved = false;
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
setAlpha(.85f);
home.js("widgetMenu(" + id + ")"); home.js("widgetMenu(" + id + ")");
} }
}; };
@@ -550,14 +557,54 @@ public class MainActivity extends Activity {
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
removeCallbacks(fire); removeCallbacks(fire);
if (longPressed) {
drop(); // released before any move reached onTouchEvent
return true;
}
break; break;
} }
return longPressed; // once fired, take the gesture away from the widget return longPressed; // once fired, take the gesture away from the widget
} }
private void drop() {
longPressed = false;
setAlpha(1f);
home.js("widgetDrop(" + moved + ")");
}
@Override @Override
public boolean onTouchEvent(MotionEvent e) { 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;
} }
} }
+20 -2
View File
@@ -37,9 +37,27 @@ const BASE = process.env.BASE || "http://localhost:8000";
await p.click(".widget-list li:first-child"); await settle(); await p.click(".widget-list li:first-child"); await settle();
assert.equal(await p.getAttribute(".widget-slot", "data-wid"), "100"); assert.equal(await p.getAttribute(".widget-slot", "data-wid"), "100");
assert.equal(await p.textContent(".widget-label"), "Clock"); assert.equal(await p.textContent(".widget-label"), "Clock");
// long press a home app: reorder // hold a home app and drag it: reorder
await longpress(".slots .tile:nth-child(2)"); 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(); 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"]); assert.deepEqual(await homeApps(), ["Gmail", "Chrome", "Phone"]);
// swipe down opens the drawer (and takes the widget off screen), search, long press adds // swipe down opens the drawer (and takes the widget off screen), search, long press adds
const swipe = (y0, y1) => p.evaluate(([y0, y1]) => { const swipe = (y0, y1) => p.evaluate(([y0, y1]) => {
+44 -12
View File
@@ -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 # 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. # 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 # The native shell (android/) serves this folder in a WebView, answers
# apps.json and widgets.json, hosts the widgets as native views over the # 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_DOWN: Const[i32] = 5
SWIPE_UP: Const[i32] = 6 SWIPE_UP: Const[i32] = 6
WIDGET_ADDED: Const[i32] = 7 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 LOADED: Const[i32] = 10
WIDGETS_LOADED: Const[i32] = 11 WIDGETS_LOADED: Const[i32] = 11
QUERY: Const[i32] = 12 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) 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) 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 MENU: Const[i32] = 21 # long press on the empty home
ITEM_MENU: Const[i32] = 22 # arg: home slot
CLOSE_SHEET: Const[i32] = 23 CLOSE_SHEET: Const[i32] = 23
PICK_APPS: Const[i32] = 24 PICK_APPS: Const[i32] = 24
PICK_WIDGET: Const[i32] = 25 PICK_WIDGET: Const[i32] = 25
@@ -93,6 +94,9 @@ SETTINGS: Const[i32] = 30
WALLPAPER: Const[i32] = 31 WALLPAPER: Const[i32] = 31
NOTICE_DONE: Const[i32] = 32 NOTICE_DONE: Const[i32] = 32
RECENTS: Const[i32] = 33 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 CAP: Const[i32] = 512
HOME_CAP: Const[i32] = 40 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 picking: i32 = 0 # drawer is in "add apps" mode: taps toggle home slots
sheet: i32 = 0 sheet: i32 = 0
item: i32 = -1 # home slot the item sheet is about 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 pending: i32 = 0 # JSON handle of a request in flight
@@ -365,6 +370,17 @@ def swap_slots(a: i32, b: i32) -> None:
save_home() 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: def slot_of_widget(wid: i32) -> i32:
i: i32 i: i32
for i in range(nh): for i in range(nh):
@@ -374,16 +390,17 @@ def slot_of_widget(wid: i32) -> i32:
def close_all() -> None: def close_all() -> None:
global drawer, picking, sheet, item global drawer, picking, sheet, item, lifted
s_clear(query) s_clear(query)
drawer = 0 drawer = 0
picking = 0 picking = 0
sheet = NONE sheet = NONE
item = -1 item = -1
lifted = -1
# ---- Update # ---- Update
def update(msg: i32, arg: i32) -> None: 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 i: i32
if msg == LOADED: if msg == LOADED:
load_apps(arg) load_apps(arg)
@@ -423,15 +440,23 @@ def update(msg: i32, arg: i32) -> None:
elif msg == MENU: elif msg == MENU:
haptic() haptic()
sheet = SHEET_MENU sheet = SHEET_MENU
elif msg == ITEM_MENU: elif msg == LIFT:
if drawer == 0 and sheet == NONE:
haptic() haptic()
item = arg lifted = arg
sheet = SHEET_ITEM
elif msg == WIDGET_MENU: elif msg == WIDGET_MENU:
i = slot_of_widget(arg) i = slot_of_widget(arg)
if i >= 0 and drawer == 0: if i >= 0 and drawer == 0 and sheet == NONE:
item = i 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 sheet = SHEET_ITEM
lifted = -1
elif msg == CLOSE_SHEET: elif msg == CLOSE_SHEET:
sheet = NONE sheet = NONE
item = -1 item = -1
@@ -487,7 +512,9 @@ def update(msg: i32, arg: i32) -> None:
elif msg == HOME: elif msg == HOME:
close_all() close_all()
elif msg == BACK: elif msg == BACK:
if sheet != NONE: if lifted >= 0:
lifted = -1
elif sheet != NONE:
sheet = NONE sheet = NONE
item = -1 item = -1
elif drawer == 1: elif drawer == 1:
@@ -511,6 +538,8 @@ def view_home_slot(i: i32) -> None:
el("li") el("li")
key(hkey[i]) key(hkey[i])
cls("widget-slot") cls("widget-slot")
cls_if("lifted", i == lifted)
attr_num("data-slot", i)
s_set(scratch, "height:") s_set(scratch, "height:")
s_add_num(scratch, hh[i]) s_add_num(scratch, hh[i])
s_add(scratch, "px") s_add(scratch, "px")
@@ -519,7 +548,7 @@ def view_home_slot(i: i32) -> None:
# them while nothing covers the home screen. # them while nothing covers the home screen.
if drawer == 0 and sheet == NONE: if drawer == 0 and sheet == NONE:
attr_num("data-wid", hwid[i]) attr_num("data-wid", hwid[i])
on_longpress(ITEM_MENU, i) on_longpress(LIFT, i)
el("span") el("span")
cls("widget-label") cls("widget-label")
text_ref(hstr[i]) text_ref(hstr[i])
@@ -532,8 +561,10 @@ def view_home_slot(i: i32) -> None:
el("li") el("li")
key(hkey[i]) key(hkey[i])
cls("tile") cls("tile")
cls_if("lifted", i == lifted)
attr_num("data-slot", i)
on_click(OPEN_HOME, i) on_click(OPEN_HOME, i)
on_longpress(ITEM_MENU, i) on_longpress(LIFT, i)
view_icon(icons[row]) view_icon(icons[row])
el("span") el("span")
cls("name") cls("name")
@@ -585,6 +616,7 @@ def view_home() -> None:
el("ul") el("ul")
cls("slots") cls("slots")
cls_if("dragging", lifted >= 0)
for i in range(nh): for i in range(nh):
view_home_slot(i) view_home_slot(i)
end() end()
BIN
View File
Binary file not shown.
+45 -2
View File
@@ -113,7 +113,48 @@
// Messages the shell and the gestures below send (keep in sync with app.py). // 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, 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 = { window.ultraHome = {
tick: () => { formats(); app.dispatch(TICK, 1); }, tick: () => { formats(); app.dispatch(TICK, 1); },
reload: () => app.dispatch(RELOAD, 0), reload: () => app.dispatch(RELOAD, 0),
@@ -121,6 +162,8 @@
back: () => app.dispatch(BACK, 0), back: () => app.dispatch(BACK, 0),
widgetAdded: (id) => app.dispatch(WIDGET_ADDED, id), widgetAdded: (id) => app.dispatch(WIDGET_ADDED, id),
widgetMenu: (id) => app.dispatch(WIDGET_MENU, id), widgetMenu: (id) => app.dispatch(WIDGET_MENU, id),
widgetDrag: (x, y) => { dragged = true; dragTo(x, y); },
widgetDrop: (moved) => drop(moved),
place: () => { placed = ""; place(); }, place: () => { placed = ""; place(); },
resetIcons: () => { iconSrc.clear(); asked.clear(); tries.clear(); resetIcons: () => { iconSrc.clear(); asked.clear(); tries.clear();
for (const img of document.querySelectorAll("img.icon")) img.__icon = undefined; }, 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; start = e.touches.length === 1 ? { x: t.clientX, y: t.clientY, at: Date.now() } : null;
}, { passive: true }); }, { passive: true });
addEventListener("touchend", (e) => { 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 t = e.changedTouches[0], dx = t.clientX - start.x, dy = t.clientY - start.y;
const quick = Date.now() - start.at < 700; const quick = Date.now() - start.at < 700;
start = null; start = null;
+7
View File
@@ -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); } border: 1px dashed rgb(255 255 255 / .3); }
.widget-label { font-size: .875rem; opacity: .75; text-shadow: var(--shadow-text); } .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, .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; } .hint { margin-top: 30vh; padding: 0 2rem; font-size: .9rem; line-height: 1.5; }