Modern Apps git

← Modern-Apps.git

launcher: size new widgets from their dp minimum, not pixels

Commit
dce16f21b946494b2d246fe4812d5ddb859a0666
Author
Vayun Mathur <[email protected]>
When
2026-11-07 19:59 UTC (28 days ago)
Parents
9e5691a
Symptom: on high-density phones, widgets added from the
picker are placed much smaller than the minimum size their
provider declares - cramped content the user then has to
resize by hand.

Root cause: WidgetBindFlow.spanFor divided
AppWidgetProviderInfo.minWidth/minHeight by the display
density before converting to cells. Those fields are already
in dp (the platform definition), so at density 3 a provider
declaring a 210dp minimum was handed a single 72dp cell
instead of three. The existing test masked this because
Robolectric runs at density 1, where the division is a no-op.

Fix: treat minWidth/minHeight as dp directly and drop the
density division.

Verification: LauncherWidgetHostTest.spanForCoversMinimum now
asserts exact spans (200dp -> 3 cells of 80dp, 100dp -> 2)
instead of only asserting the result was at least one cell.
Full module verification runs at the end of the stack.

Diff

launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherWidgetHost.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherWidgetHost.kt b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherWidgetHost.ktindex b897ef9692..3656d68e71 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherWidgetHost.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherWidgetHost.kt@@ -159,9 +159,13 @@ class WidgetBindFlow(      /** Default span for a provider, in cells, clamped by the caller to the current grid. */     fun spanFor(provider: AppWidgetProviderInfo, cellWidthDp: Int, cellHeightDp: Int): Pair<Int, Int> {-        val density = context.resources.displayMetrics.density-        val widthDp = provider.minWidth.coerceAtLeast(1) / density-        val heightDp = provider.minHeight.coerceAtLeast(1) / density+        // AppWidgetProviderInfo.minWidth/minHeight are already in dp, not+        // px. Dividing by density shrank every widget on a high-density+        // screen (at density 3 a 210dp-wide widget got one 72dp cell+        // instead of three), so widgets were placed far smaller than+        // the size they declare they need.+        val widthDp = provider.minWidth.coerceAtLeast(1).toFloat()+        val heightDp = provider.minHeight.coerceAtLeast(1).toFloat()         val spanX = ceil(widthDp / cellWidthDp).toInt().coerceAtLeast(1)         val spanY = ceil(heightDp / cellHeightDp).toInt().coerceAtLeast(1)         return spanX to spanY
launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherWidgetHostTest.kt
diff --git a/launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherWidgetHostTest.kt b/launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherWidgetHostTest.ktindex 51a44d2939..a664be7966 100644--- a/launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherWidgetHostTest.kt+++ b/launcher/src/test/java/com/vayunmathur/launcher/platform/LauncherWidgetHostTest.kt@@ -5,7 +5,6 @@ import android.content.ComponentName import androidx.test.core.app.ApplicationProvider import kotlin.test.Test import kotlin.test.assertEquals-import kotlin.test.assertTrue import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner @@ -33,10 +32,11 @@ class LauncherWidgetHostTest {     }      @Test fun spanForCoversMinimum() {-        // minWidth in px; density 1 under Robolectric defaults... cover the math.+        // minWidth/minHeight are in dp: 200dp needs three 80dp cells+        // and 100dp needs two, whatever the screen density is.         val (x, y) = flow().spanFor(info(200, 100), cellWidthDp = 80, cellHeightDp = 80)-        assertTrue(x >= 1)-        assertTrue(y >= 1)+        assertEquals(3, x)+        assertEquals(2, y)     }      @Test fun spanForTinyWidgetIsOneCell() {