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 60b2135..c1f6ece 100644 --- a/android/app/src/main/java/dev/ultra/launcher/MainActivity.java +++ b/android/app/src/main/java/dev/ultra/launcher/MainActivity.java @@ -98,6 +98,7 @@ public class MainActivity extends Activity { private final Map widgetViews = new HashMap<>(); private final Map widgetSizes = new HashMap<>(); // last size told to each widget private int pendingWidget = -1; + private int editingWidget = -1; // showing resize handles: a press on it moves it private boolean ready; private int insetTop, insetBottom; // CSS px private LauncherApps launcher; @@ -522,14 +523,19 @@ public class MainActivity extends Activity { 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) { float d = getResources().getDisplayMetrics().density; Map want = new HashMap<>(); + int editing = -1; try { JSONArray a = new JSONArray(json); for (int i = 0; i < a.length(); i++) { JSONArray r = a.getJSONArray(i); + if (r.optInt(5) == 1) editing = r.getInt(0); want.put(r.getInt(0), new int[] { Math.round(r.getInt(1) * d), Math.round(r.getInt(2) * 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) { return; } + editingWidget = editing; for (Map.Entry e : widgetViews.entrySet()) { 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 * 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. + * A widget showing resize handles is picked up by any press on it (widgetGrab(id)). */ static final class Host extends AppWidgetHost { Host(MainActivity a) { super(a, HOST_ID); } @@ -615,6 +623,14 @@ public class MainActivity extends Activity { longPressed = false; downX = e.getX(); 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()); break; case MotionEvent.ACTION_MOVE: diff --git a/tests/e2e.cjs b/tests/e2e.cjs index 135b957..6b7bcb0 100644 --- a/tests/e2e.cjs +++ b/tests/e2e.cjs @@ -116,7 +116,19 @@ const BASE = process.env.BASE || "http://localhost:8000"; assert.ok(!(await p.$(".resize-handle"))); await p.reload(); await p.waitForSelector(".widget-slot"); 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 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(); assert.ok(!(await p.$(".widget-slot"))); await p.evaluate(() => { window.ultraHome.reload(); }); await settle(); diff --git a/web/app.py b/web/app.py index e45624c..9332c1f 100644 --- a/web/app.py +++ b/web/app.py @@ -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") diff --git a/web/app.wasm b/web/app.wasm index 028dc46..010cdaf 100644 Binary files a/web/app.wasm and b/web/app.wasm differ diff --git a/web/index.html b/web/index.html index a302c33..f5afffd 100644 --- a/web/index.html +++ b/web/index.html @@ -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(); },