Skip to content

Commit

Permalink
Fix: Use ceil(px) to Dp for padding calculations (#2476)
Browse files Browse the repository at this point in the history
This change updates the padding calculations in `TransformingLazyColumnDefaults` and `ScalingLazyColumnDefaults` to use a new function, `ceilPx`, to ensure that padding values are rounded up to the nearest pixel. This ensures that padding is consistent across different screen densities.

---------

Co-authored-by: yschimke <[email protected]>
  • Loading branch information
yschimke and yschimke authored Dec 9, 2024
1 parent 4cceb8d commit 1c590bc
Show file tree
Hide file tree
Showing 28 changed files with 86 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.wear.compose.foundation.lazy.ScalingParams
import androidx.wear.compose.material.ChipDefaults
import com.google.android.horologist.annotations.ExperimentalHorologistApi
import com.google.android.horologist.compose.layout.ScalingLazyColumnState.RotaryMode
import kotlin.math.ceil
import kotlin.math.sqrt

/**
Expand Down Expand Up @@ -253,34 +254,38 @@ public object ScalingLazyColumnDefaults {
val screenWidthDp = configuration.screenWidthDp.dp
val screenHeightDp = configuration.screenHeightDp.dp

return if (this != Unspecified) {
topPaddingPct * screenHeightDp + paddingCorrection
} else {
if (configuration.isScreenRound) {
calculateVerticalOffsetForChip(screenWidthDp.value, horizontalPercent)
return (
if (this != Unspecified) {
topPaddingPct * screenHeightDp + paddingCorrection
} else {
32.dp
if (configuration.isScreenRound) {
calculateVerticalOffsetForChip(screenWidthDp.value, horizontalPercent)
} else {
32.dp
}
}
}
).ceilPx()
}

@Composable
override fun bottomPadding(horizontalPercent: Float): Dp {
val configuration = LocalConfiguration.current
val screenWidthDp = configuration.screenWidthDp.dp
val screenHeightDp = configuration.screenHeightDp.dp
return if (this != Unspecified) {
bottomPaddingPct * screenHeightDp + paddingCorrection
} else {
if (configuration.isScreenRound) {
calculateVerticalOffsetForChip(
screenWidthDp.value,
horizontalPercent,
) + 10.dp
return (
if (this != Unspecified) {
bottomPaddingPct * screenHeightDp + paddingCorrection
} else {
0.dp
if (configuration.isScreenRound) {
calculateVerticalOffsetForChip(
screenWidthDp.value,
horizontalPercent,
) + 10.dp
} else {
0.dp
}
}
}
).ceilPx()
}
}

Expand Down Expand Up @@ -333,3 +338,12 @@ public object ScalingLazyColumnDefaults {
@Composable
fun Modifier.listTextPadding() = this.padding(horizontal = 0.052f * LocalConfiguration.current.screenWidthDp.dp)
}

@Composable
internal fun Dp.ceilPx(): Dp {
val density = LocalDensity.current

return with(density) {
ceil(this@ceilPx.toPx()).toDp()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ public fun rememberResponsiveColumnPadding(
val configuration = LocalConfiguration.current
val screenWidthDp = configuration.screenWidthDp.dp

val horizontalPadding = screenWidthDp * horizontalPercent
val horizontalPadding = (screenWidthDp * horizontalPercent).ceilPx()

return PaddingValues(
top = first.topPadding(horizontalPercent),
bottom = last.bottomPadding(horizontalPercent),
top = first.topPadding(horizontalPercent).ceilPx(),
bottom = last.bottomPadding(horizontalPercent).ceilPx(),
start = horizontalPadding,
end = horizontalPadding,
)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1c590bc

Please sign in to comment.