launcher: shrinking a widget back restores the neighbour it displaced
- Commit
afd4f61506b182bf485ac430a7916186c9b20a4b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 12:28 UTC (5 days ago)
- Parents
62e4c95
Resize preview state accumulated across steps: grow a widget over a neighbour and the neighbour's pushed cell was remembered; step back to the saved rect and the shrunk candidate (clear of the pushed neighbour) displaced nobody, so the accumulated empty result never restored anything โ release then committed the neighbour one cell over anyway, widget unchanged, icon moved, hole left behind. That contradicted WidgetResize's own 'a shrink pushes nothing'. Displacement is now a pure function of the saved layout computed fresh on every step (resizeDisplaced), so stepping back displaces nobody; monotonic growth and wall-refusal behave as before. Five regression tests cover grow, shrink- back restore, two-cell growth, shrink-alone and the crowded refusal chain. Drawer search, folders, hotseat and gesture paths were audited and cleared. Verified: targeted tests pass; the full suite's 3 failures are pre-existing in untouched areas (reported, not chased); the 1 lint error is pre-existing on an untouched file.
Diff
launcher/src/main/java/com/vayunmathur/launcher/ui/HomeDropLogic.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/ui/HomeDropLogic.kt b/launcher/src/main/java/com/vayunmathur/launcher/ui/HomeDropLogic.ktindex 8659dcd673..b700863e00 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/ui/HomeDropLogic.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/ui/HomeDropLogic.kt@@ -10,6 +10,7 @@ import com.vayunmathur.launcher.domain.FolderRules import com.vayunmathur.launcher.domain.GridPreview import com.vayunmathur.launcher.domain.GridReorder import com.vayunmathur.launcher.domain.GridSpec+import com.vayunmathur.launcher.domain.WidgetResize import com.vayunmathur.launcher.platform.HomeActions import com.vayunmathur.launcher.platform.WorkspaceItem import com.vayunmathur.launcher.ui.components.CellLayout@@ -75,6 +76,31 @@ internal fun planDrop( return GridPreview.plan(spec, placed, payload.itemId, wanted, direction) } +/**+ * The neighbours a widget resized to [candidate] displaces, or null when that step cannot be+ * taken.+ *+ * Computed from the saved layout ([placed]) on every step, never accumulated across steps.+ * Accumulating made a resize one-way: growing into a neighbour pushed it aside, and shrinking+ * back to the saved rect then found the candidate already clear (the neighbour was measured at+ * its pushed cell), so it reported nothing to restore โ and releasing committed the neighbour's+ * displacement even though the widget ended at exactly the size it started. Recomputing from+ * the saved layout makes the preview a pure function of the candidate: the same candidate the+ * drag started from displaces nobody, however the finger got there.+ */+internal fun resizeDisplaced(+ spec: GridSpec,+ placed: Map<Long, CellRect>,+ widgetId: Long,+ candidate: CellRect,+ edge: WidgetResize.Edge,+): Map<Long, CellRect>? = WidgetResize.resizeWithPush(+ spec = spec,+ candidate = candidate,+ others = placed.filterKeys { it != widgetId },+ direction = WidgetResize.pushDirection(edge),+)+ internal fun mergeTargetAt( payload: DragPayload, at: Offset,
launcher/src/main/java/com/vayunmathur/launcher/ui/WorkspacePage.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/ui/WorkspacePage.kt b/launcher/src/main/java/com/vayunmathur/launcher/ui/WorkspacePage.ktindex af6e1808a3..3141d3c4fa 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/ui/WorkspacePage.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/ui/WorkspacePage.kt@@ -25,7 +25,6 @@ import androidx.compose.ui.unit.dp import com.vayunmathur.launcher.domain.CellRect import com.vayunmathur.launcher.domain.DropPlan import com.vayunmathur.launcher.domain.LauncherItemType-import com.vayunmathur.launcher.domain.WidgetResize import com.vayunmathur.launcher.platform.HomeActions import com.vayunmathur.launcher.platform.HomeUiState import com.vayunmathur.launcher.platform.WorkspaceItem@@ -195,19 +194,21 @@ internal fun WorkspacePage( draggable = draggable, onOpenFolder = onOpenFolder, onResizeStep = { candidateRect, edge ->- val pushed = WidgetResize.resizeWithPush(+ // From the saved layout, not from the last previewed step: see+ // resizeDisplaced. Replacing rather than adding to the pushed map is+ // what lets a step back undo a step out.+ val pushed = resizeDisplaced( spec = state.grid,+ placed = placed,+ widgetId = item.id, candidate = candidateRect,- others = placed- .filterKeys { it != item.id }- .mapValues { (id, at) -> resizePushed[id] ?: at },- direction = WidgetResize.pushDirection(edge),+ edge = edge, ) if (pushed == null) { false } else { resizeRect = candidateRect- resizePushed = resizePushed + pushed+ resizePushed = pushed true } },
launcher/src/test/java/com/vayunmathur/launcher/ui/WorkspaceResizePreviewTest.kt
diff --git a/launcher/src/test/java/com/vayunmathur/launcher/ui/WorkspaceResizePreviewTest.kt b/launcher/src/test/java/com/vayunmathur/launcher/ui/WorkspaceResizePreviewTest.ktnew file mode 100644index 0000000000..94662133a1--- /dev/null+++ b/launcher/src/test/java/com/vayunmathur/launcher/ui/WorkspaceResizePreviewTest.kt@@ -0,0 +1,80 @@+package com.vayunmathur.launcher.ui++import com.vayunmathur.launcher.domain.CellRect+import com.vayunmathur.launcher.domain.GridSpec+import com.vayunmathur.launcher.domain.WidgetResize+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull++/**+ * Regression tests for [resizeDisplaced], the per-step widget-resize preview in+ * [HomeDropLogic].+ *+ * The preview used to be accumulated in `WorkspacePage` across the steps of one+ * resize: each step measured its neighbours at their already-pushed cells and+ * added that step's pushes to the running map. Growing a widget one cell right+ * and then shrinking it back to its saved size therefore still committed the+ * neighbour's displacement โ the widget ended exactly where it started, but+ * the icon beside it had been moved a cell away and a hole left behind it.+ * Every step must instead be a pure function of the candidate against the+ * saved layout, so the tests below drive [resizeDisplaced] with the one saved+ * [placed] map for a whole grow-and-shrink-back sequence.+ */+class WorkspaceResizePreviewTest {++ private val spec = GridSpec(columns = 5, rows = 4, hotseatSlots = 5)++ /** Widget 1 at (0,0) spanning 2x1, with icon 9 in the cell to its right. */+ private val placed = mapOf(+ 1L to CellRect(0, 0, 2, 1),+ 9L to CellRect(2, 0),+ )++ @Test fun growingIntoANeighbourPushesIt() {+ assertEquals(+ mapOf(9L to CellRect(3, 0)),+ resizeDisplaced(spec, placed, 1L, CellRect(0, 0, 3, 1), WidgetResize.Edge.Right),+ )+ }++ @Test fun shrinkingBackToTheSavedSizeDisplacesNobody() {+ // Step out, then step back: both steps are judged against the saved+ // layout, so the second one restores the neighbour instead of+ // leaving the first step's push in place.+ assertEquals(+ mapOf(9L to CellRect(3, 0)),+ resizeDisplaced(spec, placed, 1L, CellRect(0, 0, 3, 1), WidgetResize.Edge.Right),+ )+ assertEquals(+ emptyMap(),+ resizeDisplaced(spec, placed, 1L, CellRect(0, 0, 2, 1), WidgetResize.Edge.Right),+ )+ }++ @Test fun growingTwoCellsPushesTheNeighbourTwoCellsFromTheSavedLayout() {+ assertEquals(+ mapOf(9L to CellRect(4, 0)),+ resizeDisplaced(spec, placed, 1L, CellRect(0, 0, 4, 1), WidgetResize.Edge.Right),+ )+ }++ @Test fun shrinkingAloneDisplacesNobody() {+ assertEquals(+ emptyMap(),+ resizeDisplaced(spec, placed, 1L, CellRect(0, 0, 1, 1), WidgetResize.Edge.Right),+ )+ }++ @Test fun growthWithNowhereToPushTheChainIsRefused() {+ val crowded = mapOf(+ 1L to CellRect(0, 0, 2, 1),+ 9L to CellRect(2, 0),+ 8L to CellRect(3, 0),+ 7L to CellRect(4, 0),+ )+ assertNull(+ resizeDisplaced(spec, crowded, 1L, CellRect(0, 0, 3, 1), WidgetResize.Edge.Right),+ )+ }+}