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

@@ -98,6 +98,7 @@ public class MainActivity extends Activity {
private final Map<Integer, AppWidgetHostView> widgetViews = new HashMap<>(); private final Map<Integer, AppWidgetHostView> widgetViews = new HashMap<>();
private final Map<Integer, Long> widgetSizes = new HashMap<>(); // last size told to each widget private final Map<Integer, Long> widgetSizes = new HashMap<>(); // last size told to each widget
private int pendingWidget = -1; private int pendingWidget = -1;
private int editingWidget = -1; // showing resize handles: a press on it moves it
private boolean ready; private boolean ready;
private int insetTop, insetBottom; // CSS px private int insetTop, insetBottom; // CSS px
private LauncherApps launcher; private LauncherApps launcher;
@@ -522,14 +523,19 @@ public class MainActivity extends Activity {
web.setSystemGestureExclusionRects(rects); web.setSystemGestureExclusionRects(rects);
} }
/** Lay widget views over the page's live slots: [[id, x, y, w, h], ...] in CSS px. */ /**
* Lay widget views over the page's live slots: [[id, x, y, w, h, editing], ...] in CSS
* px, where editing is 1 for the one showing resize handles.
*/
private void placeWidgets(String json) { private void placeWidgets(String json) {
float d = getResources().getDisplayMetrics().density; float d = getResources().getDisplayMetrics().density;
Map<Integer, int[]> want = new HashMap<>(); Map<Integer, int[]> want = new HashMap<>();
int editing = -1;
try { try {
JSONArray a = new JSONArray(json); JSONArray a = new JSONArray(json);
for (int i = 0; i < a.length(); i++) { for (int i = 0; i < a.length(); i++) {
JSONArray r = a.getJSONArray(i); JSONArray r = a.getJSONArray(i);
if (r.optInt(5) == 1) editing = r.getInt(0);
want.put(r.getInt(0), new int[] { want.put(r.getInt(0), new int[] {
Math.round(r.getInt(1) * d), Math.round(r.getInt(2) * d), Math.round(r.getInt(1) * d), Math.round(r.getInt(2) * d),
Math.round(r.getInt(3) * d), Math.round(r.getInt(4) * d), Math.round(r.getInt(3) * d), Math.round(r.getInt(4) * d),
@@ -538,6 +544,7 @@ public class MainActivity extends Activity {
} catch (JSONException e) { } catch (JSONException e) {
return; return;
} }
editingWidget = editing;
for (Map.Entry<Integer, AppWidgetHostView> e : widgetViews.entrySet()) { for (Map.Entry<Integer, AppWidgetHostView> e : widgetViews.entrySet()) {
if (!want.containsKey(e.getKey())) e.getValue().setVisibility(View.GONE); if (!want.containsKey(e.getKey())) e.getValue().setVisibility(View.GONE);
} }
@@ -576,6 +583,7 @@ public class MainActivity extends Activity {
* Widget views that report a long press to the page instead of to the widget. The press * 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 * 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. * widgetDrag(x, y) in CSS px and widgetDrop(moved), so it can be dragged to a new place.
* A widget showing resize handles is picked up by any press on it (widgetGrab(id)).
*/ */
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); }
@@ -615,6 +623,14 @@ public class MainActivity extends Activity {
longPressed = false; longPressed = false;
downX = e.getX(); downX = e.getX();
downY = e.getY(); downY = e.getY();
if (home.editingWidget == id) {
// Being resized: the press moves it, without a long press first.
longPressed = true;
moved = false;
setAlpha(.85f);
home.js("widgetGrab(" + id + ")");
return true;
}
postDelayed(fire, ViewConfiguration.getLongPressTimeout()); postDelayed(fire, ViewConfiguration.getLongPressTimeout());
break; break;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
+12
View File
@@ -116,7 +116,19 @@ const BASE = process.env.BASE || "http://localhost:8000";
assert.ok(!(await p.$(".resize-handle"))); assert.ok(!(await p.$(".resize-handle")));
await p.reload(); await p.waitForSelector(".widget-slot"); await p.reload(); await p.waitForSelector(".widget-slot");
assert.deepEqual(await size(), [w1, h1], "the size is saved"); 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"); 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 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 p.$(".widget-slot.resizing"), "it keeps its handles after the move");
assert.ok(!(await p.$(".sheet")));
await p.click(".resize-bar >> text=Remove"); await settle(); await p.click(".resize-bar >> text=Remove"); await settle();
assert.ok(!(await p.$(".widget-slot"))); assert.ok(!(await p.$(".widget-slot")));
await p.evaluate(() => { window.ultraHome.reload(); }); await settle(); await p.evaluate(() => { window.ultraHome.reload(); }); await settle();
+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 # 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 # 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: # 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 # 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
@@ -93,6 +94,7 @@ WCLEAR: Const[i32] = 38
CHOOSE_FIRST_WIDGET: Const[i32] = 39 CHOOSE_FIRST_WIDGET: Const[i32] = 39
RESIZE: Const[i32] = 40 # arg: columns * 4096 + height in px for the widget being resized RESIZE: Const[i32] = 40 # arg: columns * 4096 + height in px for the widget being resized
RESIZE_DONE: Const[i32] = 41 RESIZE_DONE: Const[i32] = 41
GRAB: Const[i32] = 42 # arg: the widget slot showing handles, picked up by its body
CAP: Const[i32] = 512 CAP: Const[i32] = 512
HOME_CAP: Const[i32] = 40 HOME_CAP: Const[i32] = 40
@@ -151,6 +153,7 @@ 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 lifted: i32 = -1 # home slot picked up by a long press, being dragged
resizing: i32 = -1 # widget slot showing resize handles 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 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: def close_all() -> None:
global drawer, picking, sheet, item, lifted, resizing global drawer, picking, sheet, item, lifted, resizing, regrab
s_clear(query) s_clear(query)
drawer = 0 drawer = 0
picking = 0 picking = 0
@@ -415,10 +418,11 @@ def close_all() -> None:
item = -1 item = -1
lifted = -1 lifted = -1
resizing = -1 resizing = -1
regrab = 0
# ---- 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, lifted, resizing global loading, drawer, picking, sheet, item, pending, wloading, chosen, lifted, resizing, regrab
i: i32 i: i32
if msg == LOADED: if msg == LOADED:
load_apps(arg) load_apps(arg)
@@ -465,27 +469,35 @@ def update(msg: i32, arg: i32) -> None:
haptic() haptic()
sheet = SHEET_MENU sheet = SHEET_MENU
elif msg == LIFT: elif msg == LIFT:
if drawer == 0 and sheet == NONE: if drawer == 0 and sheet == NONE and arg != lifted:
haptic() haptic()
resizing = -1 resizing = -1
regrab = 0
lifted = arg lifted = arg
elif msg == WIDGET_MENU: elif msg == WIDGET_MENU:
i = slot_of_widget(arg) i = slot_of_widget(arg)
if i >= 0 and drawer == 0 and sheet == NONE: if i >= 0 and drawer == 0 and sheet == NONE:
resizing = -1 resizing = -1
regrab = 0
lifted = i 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: elif msg == DRAG_TO:
if lifted >= 0 and arg >= 0 and arg < nh and arg != lifted: if lifted >= 0 and arg >= 0 and arg < nh and arg != lifted:
move_slot(lifted, arg) move_slot(lifted, arg)
lifted = arg lifted = arg
elif msg == DROP: elif msg == DROP:
if lifted >= 0 and arg == 0: if lifted >= 0 and (arg == 0 or regrab == 1):
if hkind[lifted] == WIDGET: if hkind[lifted] == WIDGET:
resizing = lifted resizing = lifted
else: else:
item = lifted item = lifted
sheet = SHEET_ITEM sheet = SHEET_ITEM
lifted = -1 lifted = -1
regrab = 0
elif msg == RESIZE: elif msg == RESIZE:
if resizing >= 0: if resizing >= 0:
hw[resizing] = arg // 4096 hw[resizing] = arg // 4096
@@ -551,6 +563,7 @@ def update(msg: i32, arg: i32) -> None:
elif msg == BACK: elif msg == BACK:
if lifted >= 0: if lifted >= 0:
lifted = -1 lifted = -1
regrab = 0
elif resizing >= 0: elif resizing >= 0:
resizing = -1 resizing = -1
elif sheet != NONE: elif sheet != NONE:
@@ -586,7 +599,7 @@ def view_resize_bar() -> None:
attr("role", "toolbar") attr("role", "toolbar")
el("span") el("span")
cls("resize-hint") cls("resize-hint")
text("Drag the handles to resize") text("Drag to move, or drag the handles to resize")
end() end()
btn_open("ghost", REMOVE, resizing) btn_open("ghost", REMOVE, resizing)
cls("sm") cls("sm")
BIN
View File
Binary file not shown.
+17 -6
View File
@@ -43,7 +43,8 @@
const place = () => { const place = () => {
const slots = [...document.querySelectorAll(".widget-slot[data-wid]")].map((s) => { const slots = [...document.querySelectorAll(".widget-slot[data-wid]")].map((s) => {
const r = s.getBoundingClientRect(); 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); const j = JSON.stringify(slots);
if (j !== placed) { placed = j; host.placeWidgets(j); } 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). // 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, 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 // 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 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 // Letting go without moving opens its menu instead. A widget showing resize handles
// own touches, so the shell reports those moves through widgetDrag/widgetDrop. // 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 drag = null, dragged = false; // drag: { x, y, moved } from pointerdown
const slotAt = (x, y) => { const slotAt = (x, y) => {
let best = -1, bestD = Infinity; let best = -1, bestD = Infinity;
@@ -156,6 +159,12 @@
const drop = (moved) => { const drop = (moved) => {
if (document.querySelector(".slots > .lifted")) app.dispatch(DROP, moved ? 1 : 0); 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) => { addEventListener("pointerdown", (e) => {
drag = e.isPrimary && e.target.closest?.(".slots > li") ? { x: e.clientX, y: e.clientY, moved: false } : null; drag = e.isPrimary && e.target.closest?.(".slots > li") ? { x: e.clientX, y: e.clientY, moved: false } : null;
dragged = false; dragged = false;
@@ -220,10 +229,10 @@
host.haptic(); host.haptic();
app.dispatch(RESIZE, (cols >= n ? 0 : cols) * 4096 + h); app.dispatch(RESIZE, (cols >= n ? 0 : cols) * 4096 + h);
}, true); }, 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("pointerup", endResize, true);
addEventListener("pointercancel", 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(); } if (swallowClick || e.target.closest?.(".resize-handle")) { swallowClick = false; e.stopPropagation(); e.preventDefault(); }
}, true); }, true);
addEventListener("touchmove", (e) => { if (rs && e.cancelable) e.preventDefault(); }, { passive: false }); addEventListener("touchmove", (e) => { if (rs && e.cancelable) e.preventDefault(); }, { passive: false });
@@ -234,6 +243,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),
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); }, widgetDrag: (x, y) => { dragged = true; dragTo(x, y); },
widgetDrop: (moved) => drop(moved), widgetDrop: (moved) => drop(moved),
place: () => { placed = ""; place(); }, place: () => { placed = ""; place(); },