Skip to content

Commit

Permalink
expand: add option for different vertical & horizontal paddings
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 16, 2022
1 parent c319841 commit 0d33f8b
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions ui/src/main/java/kiwi/orbit/compose/ui/layout/ExpandModifier.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,52 @@ import androidx.compose.ui.unit.constrainWidth
import androidx.compose.ui.unit.offset

/**
* Expands drawing beyond the assigned rect on all sides by provided [shift] size.
* Expands drawing beyond the assigned rect on all sides by provided [all] size.
*
* This modifier may be utilized to workaround (shadow)cropping issues. Apply the same
* [shift] to the inner content as a padding.
* [all] shift to the inner content as a padding.
*
* ```
* AnimatedContent(Modifier.expand(16.dp)) {
* CustomContent(Modifier.padding(16.dp))
* }
* ```
*/
public fun Modifier.expand(shift: Dp): Modifier =
then(ExpandModifier(shift))
public fun Modifier.expand(all: Dp): Modifier =
then(ExpandModifier(all, all))

/**
* Expands drawing beyond the assigned rect on all sides by provided [horizontal] and [vertical] size.
*
* This modifier may be utilized to workaround (shadow)cropping issues. Apply the same
* [horizontal] and [vertical] shift to the inner content as a padding.
*
* ```
* AnimatedContent(Modifier.expand(horizontal = 16.dp, vertical = 8.dp)) {
* CustomContent(Modifier.padding(horizontal = 16.dp, vertical = 8.dp))
* }
* ```
*/
public fun Modifier.expand(horizontal: Dp, vertical: Dp): Modifier =
then(ExpandModifier(horizontal, vertical))

private data class ExpandModifier(
private val shift: Dp,
private val horizontalShift: Dp,
private val verticalShift: Dp,
) : LayoutModifier {
override fun MeasureScope.measure(measurable: Measurable, constraints: Constraints): MeasureResult {
val shiftPx = shift.roundToPx()
val shiftXPx = horizontalShift.roundToPx()
val shiftYPx = verticalShift.roundToPx()
val expandedConstraints = constraints.offset(
horizontal = shiftPx * 2,
vertical = shiftPx * 2,
horizontal = shiftXPx * 2,
vertical = shiftYPx * 2,
)
val placeable = measurable.measure(expandedConstraints)
val width = constraints.constrainWidth(placeable.width - shiftPx * 2)
val height = constraints.constrainHeight(placeable.height - shiftPx * 2)
val width = constraints.constrainWidth(placeable.width - shiftXPx * 2)
val height = constraints.constrainHeight(placeable.height - shiftYPx * 2)

return layout(width, height) {
placeable.place(-shiftPx, -shiftPx)
placeable.place(-shiftXPx, -shiftYPx)
}
}
}

0 comments on commit 0d33f8b

Please sign in to comment.