# SubcomposeLayout

Common/AllAndroid/JVM

## Functions summary

|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/SubcomposeLayout.composable#SubcomposeLayout(androidx.compose.ui.Modifier,kotlin.Function2)( modifier: https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier, measurePolicy: https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/SubcomposeMeasureScope.(https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints) -> https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/MeasureResult )` Analogue of `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` which allows to subcompose the actual content during the measuring stage for example to use the values calculated during the measurement as params for the composition of the children. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable @https://developer.android.com/reference/kotlin/androidx/compose/ui/UiComposable https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/SubcomposeLayout.composable#SubcomposeLayout(androidx.compose.ui.layout.SubcomposeLayoutState,androidx.compose.ui.Modifier,kotlin.Function2)( state: https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/SubcomposeLayoutState, modifier: https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier, measurePolicy: https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/SubcomposeMeasureScope.(https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints) -> https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/MeasureResult )` Analogue of `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` which allows to subcompose the actual content during the measuring stage for example to use the values calculated during the measurement as params for the composition of the children. | Cmn |

## Functions

### SubcomposeLayout

Cmn Artifact: [androidx.compose.ui:ui](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/layout/SubcomposeLayout.kt+function:SubcomposeLayout)

```
@Composable
fun SubcomposeLayout(
    modifier: Modifier = Modifier,
    measurePolicy: SubcomposeMeasureScope.(Constraints) -> MeasureResult
): Unit
```

Analogue of `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` which allows to subcompose the actual content during the measuring stage for example to use the values calculated during the measurement as params for the composition of the children.

Possible use cases:

- You need to know the constraints passed by the parent during the composition and can't solve your use case with just custom `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` or `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/LayoutModifier`. See `https://developer.android.com/reference/kotlin/androidx/compose/foundation/layout/BoxWithConstraints.composable#BoxWithConstraints(androidx.compose.ui.Modifier,androidx.compose.ui.Alignment,kotlin.Boolean,kotlin.Function1)`.

- You want to use the size of one child during the composition of the second child.

- You want to compose your items lazily based on the available size. For example you have a list of 100 items and instead of composing all of them you only compose the ones which are currently visible(say 5 of them) and compose next items when the component is scrolled.

```kotlin
import androidx.compose.ui.layout.SubcomposeLayout
import androidx.compose.ui.unit.IntSize

// enum class SlotsEnum { Main, Dependent }
SubcomposeLayout { constraints ->
    val mainPlaceables = subcompose(SlotsEnum.Main, mainContent).map { it.measure(constraints) }
    val maxSize =
        mainPlaceables.fold(IntSize.Zero) { currentMax, placeable ->
            IntSize(
                width = maxOf(currentMax.width, placeable.width),
                height = maxOf(currentMax.height, placeable.height),
            )
        }
    layout(maxSize.width, maxSize.height) {
        mainPlaceables.forEach { it.placeRelative(0, 0) }
        subcompose(SlotsEnum.Dependent) { dependentContent(maxSize) }
            .forEach { it.measure(constraints).placeRelative(0, 0) }
    }
}
```

| Parameters |
|---|---|
| `modifier: https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier = Modifier` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` to apply for the layout. |
| `measurePolicy: https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/SubcomposeMeasureScope.(https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints) -> https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/MeasureResult` | Measure policy which provides ability to subcompose during the measuring. |

### SubcomposeLayout

Cmn Artifact: [androidx.compose.ui:ui](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/layout/SubcomposeLayout.kt+function:SubcomposeLayout)

```
@Composable
@UiComposable
fun SubcomposeLayout(
    state: SubcomposeLayoutState,
    modifier: Modifier = Modifier,
    measurePolicy: SubcomposeMeasureScope.(Constraints) -> MeasureResult
): Unit
```

Analogue of `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` which allows to subcompose the actual content during the measuring stage for example to use the values calculated during the measurement as params for the composition of the children.

Possible use cases:

- You need to know the constraints passed by the parent during the composition and can't solve your use case with just custom `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` or `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/LayoutModifier`. See `https://developer.android.com/reference/kotlin/androidx/compose/foundation/layout/BoxWithConstraints.composable#BoxWithConstraints(androidx.compose.ui.Modifier,androidx.compose.ui.Alignment,kotlin.Boolean,kotlin.Function1)`.

- You want to use the size of one child during the composition of the second child.

- You want to compose your items lazily based on the available size. For example you have a list of 100 items and instead of composing all of them you only compose the ones which are currently visible(say 5 of them) and compose next items when the component is scrolled.

```kotlin
import androidx.compose.ui.layout.SubcomposeLayout
import androidx.compose.ui.unit.IntSize

// enum class SlotsEnum { Main, Dependent }
SubcomposeLayout { constraints ->
    val mainPlaceables = subcompose(SlotsEnum.Main, mainContent).map { it.measure(constraints) }
    val maxSize =
        mainPlaceables.fold(IntSize.Zero) { currentMax, placeable ->
            IntSize(
                width = maxOf(currentMax.width, placeable.width),
                height = maxOf(currentMax.height, placeable.height),
            )
        }
    layout(maxSize.width, maxSize.height) {
        mainPlaceables.forEach { it.placeRelative(0, 0) }
        subcompose(SlotsEnum.Dependent) { dependentContent(maxSize) }
            .forEach { it.measure(constraints).placeRelative(0, 0) }
    }
}
```

| Parameters |
|---|---|
| `state: https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/SubcomposeLayoutState` | the state object to be used by the layout. |
| `modifier: https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier = Modifier` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` to apply for the layout. |
| `measurePolicy: https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/SubcomposeMeasureScope.(https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Constraints) -> https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/MeasureResult` | Measure policy which provides ability to subcompose during the measuring. |