Move a widget by dragging it while it shows resize handles

Holding a widget and letting go puts it in edit mode (resize handles). Now
pressing the widget's body in that mode picks it up right away, without
another long press: drag it to a new spot and it drops back into edit mode
so it can be resized or moved again. The handles still resize. Natively, the
page marks the widget in edit mode in placeWidgets, and its WidgetView takes
the press as a drag (widgetGrab) instead of passing it to the widget.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0126xtn5CLB47ph1Njd7frgW
This commit is contained in:
Claude committed 2026-10-22 00:26:34 +00:00
1 parent 53a23a6a0e
commit 9278de14aa
5 files changed
+65 -13

No files matched your search

+19 -6
View File
@@ -12,7 +12,8 @@ from ui import btn_open, input_open, alert
# 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
# widget that way shows resize handles on its right and bottom edges instead:
# dragging them snaps the widget to the grid, within the sizes it allows.
# dragging them snaps the widget to the grid, within the sizes it allows, and
# dragging the widget itself moves it, staying in that mode once let go.
#
# The native shell (android/) serves this folder in a WebView, answers
# apps.json and widgets.json, hosts the widgets as native views over the
@@ -93,6 +94,7 @@ WCLEAR: Const[i32] = 38
CHOOSE_FIRST_WIDGET: Const[i32] = 39
RESIZE: Const[i32] = 40 # arg: columns * 4096 + height in px for the widget being resized
RESIZE_DONE: Const[i32] = 41
GRAB: Const[i32] = 42 # arg: the widget slot showing handles, picked up by its body
CAP: Const[i32] = 512
HOME_CAP: Const[i32] = 40
@@ -151,6 +153,7 @@ 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
resizing: i32 = -1 # widget slot showing resize handles
regrab: i32 = 0 # the lifted slot was grabbed while showing handles: drop it back into them
pending: i32 = 0 # JSON handle of a request in flight
@@ -407,7 +410,7 @@ def slot_of_widget(wid: i32) -> i32:
def close_all() -> None:
global drawer, picking, sheet, item, lifted, resizing
global drawer, picking, sheet, item, lifted, resizing, regrab
s_clear(query)
drawer = 0
picking = 0
@@ -415,10 +418,11 @@ def close_all() -> None:
item = -1
lifted = -1
resizing = -1
regrab = 0
# ---- Update
def update(msg: i32, arg: i32) -> None:
global loading, drawer, picking, sheet, item, pending, wloading, chosen, lifted, resizing
global loading, drawer, picking, sheet, item, pending, wloading, chosen, lifted, resizing, regrab
i: i32
if msg == LOADED:
load_apps(arg)
@@ -465,27 +469,35 @@ def update(msg: i32, arg: i32) -> None:
haptic()
sheet = SHEET_MENU
elif msg == LIFT:
if drawer == 0 and sheet == NONE:
if drawer == 0 and sheet == NONE and arg != lifted:
haptic()
resizing = -1
regrab = 0
lifted = arg
elif msg == WIDGET_MENU:
i = slot_of_widget(arg)
if i >= 0 and drawer == 0 and sheet == NONE:
resizing = -1
regrab = 0
lifted = i
elif msg == GRAB:
if arg >= 0 and arg == resizing and drawer == 0 and sheet == 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 == DROP:
if lifted >= 0 and arg == 0:
if lifted >= 0 and (arg == 0 or regrab == 1):
if hkind[lifted] == WIDGET:
resizing = lifted
else:
item = lifted
sheet = SHEET_ITEM
lifted = -1
regrab = 0
elif msg == RESIZE:
if resizing >= 0:
hw[resizing] = arg // 4096
@@ -551,6 +563,7 @@ def update(msg: i32, arg: i32) -> None:
elif msg == BACK:
if lifted >= 0:
lifted = -1
regrab = 0
elif resizing >= 0:
resizing = -1
elif sheet != NONE:
@@ -586,7 +599,7 @@ def view_resize_bar() -> None:
attr("role", "toolbar")
el("span")
cls("resize-hint")
text("Drag the handles to resize")
text("Drag to move, or drag the handles to resize")
end()
btn_open("ghost", REMOVE, resizing)
cls("sm")
BIN
View File
Binary file not shown.
+17 -6
View File
@@ -43,7 +43,8 @@
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)];
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);
if (j !== placed) { placed = j; host.placeWidgets(j); }
@@ -130,12 +131,14 @@
// 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;
WIDGET_ADDED = 7, WIDGET_MENU = 8, DRAG_TO = 35, DROP = 36, RESIZE = 40, GRAB = 42;
// 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.
// 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;
@@ -156,6 +159,12 @@
const drop = (moved) => {
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;
@@ -220,10 +229,10 @@
host.haptic();
app.dispatch(RESIZE, (cols >= n ? 0 : cols) * 4096 + h);
}, true);
const endResize = () => { if (rs) { rs = null; swallowClick = true; setTimeout(() => swallowClick = false, 0); } };
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 isn't a tap beside the widget
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 });
@@ -234,6 +243,8 @@
back: () => app.dispatch(BACK, 0),
widgetAdded: (id) => app.dispatch(WIDGET_ADDED, 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(); },