1 /*
<lambda>null2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.ui.layout
18 
19 import androidx.compose.ui.node.LayoutNode
20 import androidx.compose.ui.unit.Constraints
21 import androidx.compose.ui.unit.constrainHeight
22 import androidx.compose.ui.unit.constrainWidth
23 import androidx.compose.ui.util.fastForEach
24 import androidx.compose.ui.util.fastMap
25 
26 internal object RootMeasurePolicy :
27     LayoutNode.NoIntrinsicsMeasurePolicy("Undefined intrinsics block and it is required") {
28     override fun MeasureScope.measure(
29         measurables: List<Measurable>,
30         constraints: Constraints
31     ): MeasureResult {
32         return when (measurables.size) {
33             0 -> {
34                 layout(constraints.minWidth, constraints.minHeight) {}
35             }
36             1 -> {
37                 val placeable = measurables[0].measure(constraints)
38                 layout(
39                     constraints.constrainWidth(placeable.width),
40                     constraints.constrainHeight(placeable.height)
41                 ) {
42                     placeable.placeRelativeWithLayer(0, 0)
43                 }
44             }
45             else -> {
46                 var maxWidth = 0
47                 var maxHeight = 0
48                 val placeables =
49                     measurables.fastMap {
50                         it.measure(constraints).apply {
51                             maxWidth = maxOf(width, maxWidth)
52                             maxHeight = maxOf(height, maxHeight)
53                         }
54                     }
55                 layout(
56                     constraints.constrainWidth(maxWidth),
57                     constraints.constrainHeight(maxHeight)
58                 ) {
59                     placeables.fastForEach { placeable -> placeable.placeRelativeWithLayer(0, 0) }
60                 }
61             }
62         }
63     }
64 }
65