Skip to content

Commit

Permalink
Adding support for the Width Modifier in ComposeUiBox
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-cortes committed Dec 9, 2024
1 parent 8c03b03 commit 7908aca
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Changed:
Fixed:
- Fix inconsistency in margin application between `ComposeUiBox` and `ViewBox`.
- Add support for the Height modifier in `ComposeUiBox`.

- Add support for the Width modifier in `ComposeUiBox`.

## [0.16.0] - 2024-11-19
[0.16.0]: https://github.com/cashapp/redwood/releases/tag/0.16.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ internal data class BoxChildLayoutInfo(
val alignment: Alignment,
val matchParentWidth: Boolean,
val matchParentHeight: Boolean,
val requestedHeight: Dp?
val requestedHeight: Dp?,
val requestedWidth: Dp?
)

@PublishedApi
Expand Down Expand Up @@ -105,7 +106,13 @@ internal data class BoxMeasurePolicy(
minWidth = if (boxWidth != Constraints.Infinity) boxWidth else 0,
maxWidth = boxWidth,
)
} else if (layoutInfo.requestedWidth != null) {
childConstraints = childConstraints.copy(
minWidth = layoutInfo.requestedWidth.toPx().toInt(),
maxWidth = layoutInfo.requestedWidth.toPx().toInt(),
)
}

if (layoutInfo.matchParentHeight) {
childConstraints = childConstraints.copy(
minHeight = if (boxHeight != Constraints.Infinity) boxHeight else 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ internal class ComposeUiBox(
var matchParentWidth = matchParentWidth
var matchParentHeight = matchParentHeight
var requestedHeight: Dp? = null
var requestedWidth: Dp? = null

forEachScoped { childModifier ->
when (childModifier) {
Expand All @@ -107,7 +108,7 @@ internal class ComposeUiBox(
}

is Width -> {
// TODO
requestedWidth = childModifier.width.toDp()
}

is Height -> {
Expand Down Expand Up @@ -137,6 +138,7 @@ internal class ComposeUiBox(
matchParentWidth = matchParentWidth,
matchParentHeight = matchParentHeight,
requestedHeight = requestedHeight,
requestedWidth = requestedWidth,
)
}

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.
Original file line number Diff line number Diff line change
Expand Up @@ -629,4 +629,36 @@ abstract class AbstractBoxTest<T : Any> {

snapshotter.snapshot()
}

@Test
fun testChildExplicitWidth() {
val container = box()
.apply {
width(Constraint.Fill)
height(Constraint.Fill)
horizontalAlignment(CrossAxisAlignment.Center)
verticalAlignment(CrossAxisAlignment.Stretch)
}
val snapshotter = snapshotter(container.value)

val box = box()
.apply {
modifier = MarginImpl(all = 24.dp)
width(Constraint.Wrap)
height(Constraint.Fill)
horizontalAlignment(CrossAxisAlignment.Center)
verticalAlignment(CrossAxisAlignment.Stretch)
}
.also { container.children.insert(0, it) }

widgetFactory.color().apply {
modifier = WidthImpl(200.dp)
}.also { box.children.insert(0, it) }

widgetFactory.text(
text = "foreground",
).also { box.children.insert(1, it) }

snapshotter.snapshot()
}
}
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 7908aca

Please sign in to comment.