diff --git a/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[LTR]_both.png b/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[LTR]_both.png new file mode 100644 index 0000000000..6668367b7d --- /dev/null +++ b/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[LTR]_both.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:af4fa85b541988cb9f49de14556e6f32eb932b22719ef4a31916fc78823a2466 +size 5152 diff --git a/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[LTR]_single.png b/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[LTR]_single.png new file mode 100644 index 0000000000..26edd4fec1 --- /dev/null +++ b/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[LTR]_single.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5e4b532debf18b4849d145305dd21d18d77067008e285cb412422c19ff6cf95a +size 4520 diff --git a/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[RTL]_both.png b/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[RTL]_both.png new file mode 100644 index 0000000000..e5b7297064 --- /dev/null +++ b/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[RTL]_both.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:954430a939da223f8043d8c19ecb9d02aab591a0adb88d0d256e9958bc294939 +size 5206 diff --git a/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[RTL]_single.png b/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[RTL]_single.png new file mode 100644 index 0000000000..6214adae77 --- /dev/null +++ b/redwood-layout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiFlexContainerTest_testDynamicContainerSize[RTL]_single.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:34a4fb92183cb3a23276a98b2790deb50c96d3ae46dfb411999efd384af32929 +size 4530 diff --git a/redwood-layout-shared-test/src/commonMain/kotlin/app/cash/redwood/layout/AbstractFlexContainerTest.kt b/redwood-layout-shared-test/src/commonMain/kotlin/app/cash/redwood/layout/AbstractFlexContainerTest.kt index 03fbd0ba1d..c34bd03857 100644 --- a/redwood-layout-shared-test/src/commonMain/kotlin/app/cash/redwood/layout/AbstractFlexContainerTest.kt +++ b/redwood-layout-shared-test/src/commonMain/kotlin/app/cash/redwood/layout/AbstractFlexContainerTest.kt @@ -437,6 +437,35 @@ abstract class AbstractFlexContainerTest { verifySnapshot(container, "BCDE") } + @Test + fun testDynamicContainerSize() { + val parent = flexContainer(FlexDirection.Column).apply { + width(Constraint.Fill) + height(Constraint.Fill) + } + + parent.add(flexContainer(FlexDirection.Column).apply { + modifier = GrowImpl(1.0) + width(Constraint.Fill) + mainAxisAlignment(MainAxisAlignment.SpaceBetween) + add(widget("A")) + add(widget("B")) + }) + + parent.add(flexContainer(FlexDirection.Column).apply { + modifier = GrowImpl(1.0) + width(Constraint.Fill) + mainAxisAlignment(MainAxisAlignment.SpaceBetween) + add(widget("C")) + add(widget("D")) + }) + + verifySnapshot(parent, "both") + + parent.removeAt(1) + verifySnapshot(parent, "single") + } + /** We don't have assume() on kotlin.test. Tests that fail here should be skipped instead. */ private fun assumeTrue(b: Boolean) { assertTrue(b) diff --git a/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/ViewFlexContainer.kt b/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/ViewFlexContainer.kt index e16705cc3f..7e55183bec 100644 --- a/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/ViewFlexContainer.kt +++ b/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/ViewFlexContainer.kt @@ -168,8 +168,7 @@ internal class ViewFlexContainer( } private fun View.asNode(): Node { - val childNode = Node() - childNode.measureCallback = ViewMeasureCallback(this) - applyLayoutParams(childNode, layoutParams) - return childNode + return Node().apply { + measureCallback = ViewMeasureCallback(this@asNode) + } } diff --git a/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/YogaLayout.kt b/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/YogaLayout.kt index 2a5ff354c4..763ba74875 100644 --- a/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/YogaLayout.kt +++ b/redwood-layout-view/src/main/kotlin/app/cash/redwood/layout/view/YogaLayout.kt @@ -21,10 +21,6 @@ internal class YogaLayout( ) : ViewGroup(context) { val rootNode = Node() - init { - applyLayoutParams(rootNode, layoutParams) - } - private fun applyLayout(node: Node, xOffset: Float, yOffset: Float) { val view = node.view if (view != null && view !== this) { @@ -71,6 +67,11 @@ internal class YogaLayout( } private fun calculateLayout(widthSpec: Int, heightSpec: Int) { + rootNode.requestedWidth = Size.Undefined + rootNode.requestedMaxWidth = Size.Undefined + rootNode.requestedHeight = Size.Undefined + rootNode.requestedMaxHeight = Size.Undefined + val widthSize = MeasureSpec.getSize(widthSpec).toFloat() when (MeasureSpec.getMode(widthSpec)) { MeasureSpec.EXACTLY -> rootNode.requestedWidth = widthSize @@ -95,16 +96,3 @@ internal class YogaLayout( private val Node.view: View? get() = (measureCallback as ViewMeasureCallback?)?.view - -internal fun applyLayoutParams(node: Node, layoutParams: ViewGroup.LayoutParams?) { - if (layoutParams != null) { - val width = layoutParams.width - if (width >= 0) { - node.requestedWidth = width.toFloat() - } - val height = layoutParams.height - if (height >= 0) { - node.requestedHeight = height.toFloat() - } - } -} diff --git a/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[LTR]_both.png b/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[LTR]_both.png new file mode 100644 index 0000000000..ffef9cd78a --- /dev/null +++ b/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[LTR]_both.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:004f479da8ccd9ff1c44bbb54c857c1babe40562a210a0470a41c676c725c3b4 +size 5147 diff --git a/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[LTR]_single.png b/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[LTR]_single.png new file mode 100644 index 0000000000..19c7696ece --- /dev/null +++ b/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[LTR]_single.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d4fde88e225c3af519aa13e25b941d8ccdf4ef74b2e5931bcec8fedc675d337c +size 4515 diff --git a/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[RTL]_both.png b/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[RTL]_both.png new file mode 100644 index 0000000000..90e5dbab10 --- /dev/null +++ b/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[RTL]_both.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4f89e4749603066d96af4cf13f5aef7ef65cecb97ae742bfc0b523f960616d38 +size 5187 diff --git a/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[RTL]_single.png b/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[RTL]_single.png new file mode 100644 index 0000000000..bb9aa4dbc5 --- /dev/null +++ b/redwood-layout-view/src/test/snapshots/images/app.cash.redwood.layout.view_ViewFlexContainerTest_testDynamicContainerSize[RTL]_single.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:987460f6c257727a30f80a3ec0fe23a8ccdf5288b649f51c035f28caf381b1c0 +size 4518 diff --git a/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[LTR]_both.png b/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[LTR]_both.png new file mode 100644 index 0000000000..784d7fc152 --- /dev/null +++ b/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[LTR]_both.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:57e6bb80bdab38226d042e9f722c4b0951a83ca348ef237b1b5e7e64d5770def +size 3836 diff --git a/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[LTR]_single.png b/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[LTR]_single.png new file mode 100644 index 0000000000..784d7fc152 --- /dev/null +++ b/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[LTR]_single.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:57e6bb80bdab38226d042e9f722c4b0951a83ca348ef237b1b5e7e64d5770def +size 3836 diff --git a/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[RTL]_both.png b/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[RTL]_both.png new file mode 100644 index 0000000000..784d7fc152 --- /dev/null +++ b/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[RTL]_both.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:57e6bb80bdab38226d042e9f722c4b0951a83ca348ef237b1b5e7e64d5770def +size 3836 diff --git a/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[RTL]_single.png b/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[RTL]_single.png new file mode 100644 index 0000000000..784d7fc152 --- /dev/null +++ b/redwood-lazylayout-composeui/src/test/snapshots/images/app.cash.redwood.layout.composeui_ComposeUiLazyListTest_testDynamicContainerSize[RTL]_single.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:57e6bb80bdab38226d042e9f722c4b0951a83ca348ef237b1b5e7e64d5770def +size 3836 diff --git a/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[LTR]_both.png b/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[LTR]_both.png new file mode 100644 index 0000000000..ebb6a426ef --- /dev/null +++ b/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[LTR]_both.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cdca08973fd5cbf55a12717a65eaa147bbfaff662cc7dca62bcae0f9d63a9859 +size 3837 diff --git a/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[LTR]_single.png b/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[LTR]_single.png new file mode 100644 index 0000000000..ebb6a426ef --- /dev/null +++ b/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[LTR]_single.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cdca08973fd5cbf55a12717a65eaa147bbfaff662cc7dca62bcae0f9d63a9859 +size 3837 diff --git a/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[RTL]_both.png b/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[RTL]_both.png new file mode 100644 index 0000000000..ebb6a426ef --- /dev/null +++ b/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[RTL]_both.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cdca08973fd5cbf55a12717a65eaa147bbfaff662cc7dca62bcae0f9d63a9859 +size 3837 diff --git a/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[RTL]_single.png b/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[RTL]_single.png new file mode 100644 index 0000000000..ebb6a426ef --- /dev/null +++ b/redwood-lazylayout-view/src/test/snapshots/images/app.cash.redwood.lazylayout.view_ViewLazyListTest_testDynamicContainerSize[RTL]_single.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cdca08973fd5cbf55a12717a65eaa147bbfaff662cc7dca62bcae0f9d63a9859 +size 3837