From 6e88f7c81b8d896f3b08c018f7cb2dc8ce39c49d Mon Sep 17 00:00:00 2001 From: TMC Date: Wed, 18 Nov 2026 14:44:33 +0100 Subject: [PATCH] Grid and margins: draw the margins in the preview; fit them to the rows The owner reported that the preview did not show the top and bottom margins. On the emulator it did, but little: the rows share the height the margins leave, so a first row moves by less than the margin, and at about 40% of real size a 16 dp margin moves it some 6 dp. On a page with few icons that is hard to see. At large values it also went wrong the other way: a phone's grid (not scalable) does not shrink its icons to fit shorter rows, so with labels on, 120 dp top and bottom cut off the last row's labels (cell height 201 px, rows got 179 px). Now: - The preview draws the margins: tinted bands along the sides and above and below the rows, as wide as they are laid out, and a dashed outline around the rows (HomeMargins.Guides, through a new overlay slot in GridOverridesPreview). They show however empty the page is. - Top and bottom margins take only the height the rows can spare beyond their icons and labels, cut in proportion (HomeMargins.fitTopBottom, in DeviceProfile.updateWorkspacePadding); DeviceProfile keeps what it laid out (flexeraTopMarginPx, flexeraBottomMarginPx) for the guides. - When the grid has room for less than the sliders ask, a note under them says so and that fewer rows or columns leave more room. - The class comment no longer claims that rows too short get smaller icons; that was wrong for these grids. Tested on the emulator: guides on a 5x5 and a 4x9 grid line up with the rows and columns; on 5x5 with labels 120/120 fits whole and no label is cut; on 4x9 with labels the bands shrink to what fits and the note shows; 8, 0, 0 leaves the layout as before. Co-Authored-By: Claude Opus 5.5 --- flexera/res/values/flexera_strings.xml | 2 + flexera/src/app/flexera/home/HomeMargins.kt | 127 +++++++++++++++++- .../components/GridOverridesPreview.kt | 4 + .../destinations/HomeScreenGridPreferences.kt | 16 +++ src/com/android/launcher3/DeviceProfile.java | 21 ++- 5 files changed, 162 insertions(+), 8 deletions(-) diff --git a/flexera/res/values/flexera_strings.xml b/flexera/res/values/flexera_strings.xml index d8baae00fd..9e8298846a 100644 --- a/flexera/res/values/flexera_strings.xml +++ b/flexera/res/values/flexera_strings.xml @@ -126,6 +126,8 @@ Side margins Top margin Bottom margin + + The grid leaves no more room: larger margins would cut off labels or make icons overlap. Fewer rows or columns leave more room. Grid and margins Quick launch diff --git a/flexera/src/app/flexera/home/HomeMargins.kt b/flexera/src/app/flexera/home/HomeMargins.kt index ca1afbadf3..0c31249b82 100644 --- a/flexera/src/app/flexera/home/HomeMargins.kt +++ b/flexera/src/app/flexera/home/HomeMargins.kt @@ -1,10 +1,27 @@ package app.flexera.home +import android.content.Context import android.util.DisplayMetrics +import androidx.compose.foundation.Canvas +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.geometry.Size +import androidx.compose.ui.graphics.PathEffect +import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp import app.lawnchair.preferences.PreferenceAdapter import app.lawnchair.ui.preferences.components.controls.SliderPreference +import app.lawnchair.ui.preferences.components.wallpaperSupportsDarkText +import app.lawnchair.ui.theme.LawnchairTheme +import com.android.launcher3.InvariantDeviceProfile import com.android.launcher3.R import com.android.launcher3.icons.IconNormalizer.ICON_VISIBLE_AREA_FACTOR import com.android.launcher3.testing.shared.ResourceUtils.pxFromDp @@ -20,10 +37,14 @@ import com.android.launcher3.testing.shared.ResourceUtils.pxFromDp * looked as if the settings did nothing. * * The rows and columns share what room the margins leave, so larger margins also bring the - * icons closer together; that is how the spacing between icons is set. Rows that become too - * short for their icons get smaller icons, as Launcher3 does by itself (DeviceProfile - * .updateAvailableDimensions). Columns get no such help, so the side margins are kept narrow - * enough that every column stays wider than what is seen of its icon ([sidePx]). + * icons closer together; that is how the spacing between icons is set. A phone's grid does not + * shrink its icons to fit smaller cells (Launcher3 does that only for scalable grids), so the + * margins stop growing where the cells would get too small: the side margins where a column + * would be narrower than what is seen of its icon ([sidePx]), the top and bottom margins where + * a row would be shorter than its icon and label ([fitTopBottom]). + * + * The grid page's preview draws the margins over the home screen ([Guides]), so that they can + * be seen however full or empty the page is. */ object HomeMargins { @@ -50,17 +71,105 @@ object HomeMargins { return chosen.coerceAtMost(most).coerceAtLeast(0) } + /** + * What the rows can spare of a top margin of [topPx] and a bottom margin of [bottomPx]: + * [spare] is the height the rows have beyond what their icons and labels need. Both are cut + * in proportion until they fit. The top margin comes first, then the bottom one. + */ + @JvmStatic + fun fitTopBottom(topPx: Int, bottomPx: Int, spare: Int): IntArray { + val wanted = topPx + bottomPx + if (wanted <= 0 || spare <= 0) return intArrayOf(0, 0) + if (wanted <= spare) return intArrayOf(topPx, bottomPx) + val top = (topPx.toLong() * spare / wanted).toInt() + return intArrayOf(top, spare - top) + } + + /** + * The margins over the grid page's preview of [idp]: tinted bands along the screen's sides + * and above and below the rows, as wide as the margins are laid out, and a dashed outline + * around the rows. Drawn in the colours of the preview's own background, which follows the + * wallpaper, so that they stand out from it. + */ + @Composable + fun Guides(idp: InvariantDeviceProfile) { + val context = LocalContext.current + val profile = remember(idp) { idp.getDeviceProfile(context) } + LawnchairTheme(darkTheme = !wallpaperSupportsDarkText()) { + val tint = MaterialTheme.colorScheme.primary + Canvas(modifier = Modifier.fillMaxSize()) { + val screen = profile.deviceProperties + val scaleX = size.width / screen.widthPx + val scaleY = size.height / screen.heightPx + val insets = profile.insets + val padding = profile.workspacePadding + val cells = profile.cellLayoutPaddingPx + // The rows, as the workspace lays them out: inside the system bars, the + // workspace's padding and the page's own. + val left = (insets.left + padding.left + cells.left) * scaleX + val right = (screen.widthPx - insets.right - padding.right - cells.right) * scaleX + val top = (insets.top + padding.top + cells.top) * scaleY + val bottom = (screen.heightPx - insets.bottom - padding.bottom - cells.bottom) * scaleY + if (right <= left || bottom <= top) return@Canvas + + val band = tint.copy(alpha = BAND_ALPHA) + val side = profile.desiredWorkspaceHorizontalMarginPx * scaleX + val above = profile.flexeraTopMarginPx * scaleY + val below = profile.flexeraBottomMarginPx * scaleY + if (side > 0f) { + drawRect(band, Offset(left - side, top), Size(side, bottom - top)) + drawRect(band, Offset(right, top), Size(side, bottom - top)) + } + if (above > 0f) drawRect(band, Offset(left, top - above), Size(right - left, above)) + if (below > 0f) drawRect(band, Offset(left, bottom), Size(right - left, below)) + drawRect( + color = tint, + topLeft = Offset(left, top), + size = Size(right - left, bottom - top), + style = Stroke( + width = OUTLINE_DP.dp.toPx(), + pathEffect = PathEffect.dashPathEffect(floatArrayOf(DASH_DP.dp.toPx(), GAP_DP.dp.toPx())), + ), + ) + } + } + } + + private const val BAND_ALPHA = 0.32f + private const val OUTLINE_DP = 1.5f + private const val DASH_DP = 5f + private const val GAP_DP = 4f + /** A top or bottom margin of [chosenDp], in pixels. */ @JvmStatic fun topBottomPx(chosenDp: Int, metrics: DisplayMetrics): Int = pxFromDp(chosenDp.coerceIn(0, MAX_TOP_BOTTOM_DP).toFloat(), metrics) + /** + * Whether the grid of [idp] lays out less of the margins than [sideDp], [topDp] and + * [bottomDp]: its rows or columns leave no more room (see [sidePx] and [fitTopBottom]). + */ + fun isLimited(context: Context, idp: InvariantDeviceProfile, sideDp: Int, topDp: Int, bottomDp: Int): Boolean { + val profile = idp.getDeviceProfile(context) + val metrics = context.resources.displayMetrics + val side = profile.desiredWorkspaceHorizontalMarginPx < pxFromDp(sideDp.toFloat(), metrics) - 1 + val wanted = topBottomPx(topDp, metrics) + topBottomPx(bottomDp, metrics) + val laidOut = profile.flexeraTopMarginPx + profile.flexeraBottomMarginPx + return side || laidOut < wanted - 1 + } + /** * The three sliders, on the grid page in place of Lawnchair's two padding sliders on the home * screen page. They change [side], [top] and [bottom], which the grid page keeps on Apply. + * When the grid cannot lay out as much as they ask ([limited]), a note under them says why. */ @Composable - fun Preferences(side: PreferenceAdapter, top: PreferenceAdapter, bottom: PreferenceAdapter) { + fun Preferences( + side: PreferenceAdapter, + top: PreferenceAdapter, + bottom: PreferenceAdapter, + limited: Boolean, + ) { SliderPreference( label = stringResource(id = R.string.flexera_home_side_margins), adapter = side, @@ -82,5 +191,13 @@ object HomeMargins { step = 4, showUnit = "dp", ) + if (limited) { + Text( + text = stringResource(id = R.string.flexera_home_margins_limited), + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.padding(horizontal = 16.dp, vertical = 12.dp), + ) + } } } diff --git a/lawnchair/src/app/lawnchair/ui/preferences/components/GridOverridesPreview.kt b/lawnchair/src/app/lawnchair/ui/preferences/components/GridOverridesPreview.kt index 40916a0248..7220deaf86 100644 --- a/lawnchair/src/app/lawnchair/ui/preferences/components/GridOverridesPreview.kt +++ b/lawnchair/src/app/lawnchair/ui/preferences/components/GridOverridesPreview.kt @@ -1,6 +1,7 @@ package app.lawnchair.ui.preferences.components import android.content.res.Configuration +import androidx.compose.foundation.layout.BoxScope import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.ColumnScope import androidx.compose.foundation.layout.fillMaxSize @@ -26,6 +27,8 @@ fun ColumnScope.GridOverridesPreview( modifier: Modifier = Modifier, expandToAvailableSpace: Boolean = true, previewOverrides: DeviceProfileOverrides.PreviewOverrides = DeviceProfileOverrides.PreviewOverrides(), + // Flexera Launcher: drawn over the preview, such as the margins (app.flexera.home.HomeMargins). + overlay: @Composable BoxScope.(InvariantDeviceProfile) -> Unit = {}, updateGridOptions: DeviceProfileOverrides.DBGridInfo.() -> DeviceProfileOverrides.DBGridInfo, ) { val previewIdp = createPreviewIdp(updateGridOptions, previewOverrides) @@ -81,6 +84,7 @@ fun ColumnScope.GridOverridesPreview( idp = previewIdp, modifier = Modifier.fillMaxSize(), ) + overlay(previewIdp) } } } diff --git a/lawnchair/src/app/lawnchair/ui/preferences/destinations/HomeScreenGridPreferences.kt b/lawnchair/src/app/lawnchair/ui/preferences/destinations/HomeScreenGridPreferences.kt index c6708f109f..7d554da744 100644 --- a/lawnchair/src/app/lawnchair/ui/preferences/destinations/HomeScreenGridPreferences.kt +++ b/lawnchair/src/app/lawnchair/ui/preferences/destinations/HomeScreenGridPreferences.kt @@ -22,9 +22,12 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalConfiguration @@ -91,6 +94,8 @@ fun HomeScreenGridPreferences( val sideMargin = rememberSaveable { mutableIntStateOf(originalSideMargin) } val topMargin = rememberSaveable { mutableIntStateOf(originalTopMargin) } val bottomMargin = rememberSaveable { mutableIntStateOf(originalBottomMargin) } + // Whether the previewed grid has room for less than these margins. + var marginsLimited by remember { mutableStateOf(false) } LaunchedEffect(hotseatColumns.intValue) { if (hotseatColumnsUnfolded.intValue < hotseatColumns.intValue) { @@ -146,6 +151,16 @@ fun HomeScreenGridPreferences( homeTopMarginDp = topMargin.intValue, homeBottomMarginDp = bottomMargin.intValue, ), + // Flexera Launcher: the margins drawn over the preview, and whether they fit. + overlay = { idp -> + app.flexera.home.HomeMargins.Guides(idp) + val context = LocalContext.current + LaunchedEffect(idp) { + marginsLimited = app.flexera.home.HomeMargins.isLimited( + context, idp, sideMargin.intValue, topMargin.intValue, bottomMargin.intValue, + ) + } + }, ) Column( @@ -228,6 +243,7 @@ fun HomeScreenGridPreferences( side = sideMargin.asPreferenceAdapter(), top = topMargin.asPreferenceAdapter(), bottom = bottomMargin.asPreferenceAdapter(), + limited = marginsLimited, ) } } diff --git a/src/com/android/launcher3/DeviceProfile.java b/src/com/android/launcher3/DeviceProfile.java index aa032c4763..f15fa12242 100644 --- a/src/com/android/launcher3/DeviceProfile.java +++ b/src/com/android/launcher3/DeviceProfile.java @@ -352,6 +352,9 @@ public class DeviceProfile { private int homeSideMarginDp; private int homeTopMarginDp; private int homeBottomMarginDp; + /** Flexera Launcher: the top and bottom margins as laid out, after fitting the rows. */ + public int flexeraTopMarginPx; + public int flexeraBottomMarginPx; private float widgetPaddingFactor; // Flexera Launcher: widgets lined up with the icons (app.flexera.widget.FlexeraWidgets). public boolean alignWidgetsWithIcons; @@ -1827,9 +1830,21 @@ public class DeviceProfile { int bottomHotseatReserve = Math.min(hotseatBarSizePx, paddingBottom); paddingBottom = bottomHotseatReserve + Math.round((paddingBottom - bottomHotseatReserve) * workspacePaddingVerticalFactor); - // Flexera Launcher: the owner's top and bottom margins (app.flexera.home.HomeMargins). - paddingTop += app.flexera.home.HomeMargins.topBottomPx(homeTopMarginDp, mMetrics); - paddingBottom += app.flexera.home.HomeMargins.topBottomPx(homeBottomMarginDp, mMetrics); + // Flexera Launcher: the owner's top and bottom margins (app.flexera.home.HomeMargins), + // as far as the rows can spare the room. This grid does not shrink its icons to fit + // shorter rows, so taller margins would cut off the labels of the last row. + int rowsNeed = cellHeightPx * inv.numRows + + cellLayoutBorderSpacePx.y * (inv.numRows - 1); + int spare = mDeviceProperties.getAvailableHeightPx() + - Math.max(paddingTop, cellLayoutPaddingPx.top) + - Math.max(paddingBottom, cellLayoutPaddingPx.bottom) - rowsNeed; + int[] margins = app.flexera.home.HomeMargins.fitTopBottom( + app.flexera.home.HomeMargins.topBottomPx(homeTopMarginDp, mMetrics), + app.flexera.home.HomeMargins.topBottomPx(homeBottomMarginDp, mMetrics), spare); + flexeraTopMarginPx = margins[0]; + flexeraBottomMarginPx = margins[1]; + paddingTop += margins[0]; + paddingBottom += margins[1]; padding.set(paddingLeft, paddingTop, paddingRight, paddingBottom); }