1 /*
2  * Copyright 2022 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.foundation
18 
19 import androidx.compose.foundation.gestures.Orientation
20 import androidx.compose.foundation.internal.checkPrecondition
21 import androidx.compose.ui.unit.Constraints
22 
23 /**
24  * @param constraints [Constraints] used to measure the scrollable container
25  * @param orientation orientation of the scrolling
26  * @throws [IllegalStateException] if the container was measured with the infinity constraints in
27  *   the direction of scrolling. This usually means nesting scrollable in the same direction
28  *   containers which is a performance issue and is discouraged.
29  */
checkScrollableContainerConstraintsnull30 fun checkScrollableContainerConstraints(constraints: Constraints, orientation: Orientation) {
31     if (orientation == Orientation.Vertical) {
32         checkPrecondition(constraints.maxHeight != Constraints.Infinity) {
33             "Vertically scrollable component was measured with an infinity maximum height " +
34                 "constraints, which is disallowed. One of the common reasons is nesting layouts " +
35                 "like LazyColumn and Column(Modifier.verticalScroll()). If you want to add a " +
36                 "header before the list of items please add a header as a separate item() before " +
37                 "the main items() inside the LazyColumn scope. There could be other reasons " +
38                 "for this to happen: your ComposeView was added into a LinearLayout with some " +
39                 "weight, you applied Modifier.wrapContentSize(unbounded = true) or wrote a " +
40                 "custom layout. Please try to remove the source of infinite constraints in the " +
41                 "hierarchy above the scrolling container."
42         }
43     } else {
44         checkPrecondition(constraints.maxWidth != Constraints.Infinity) {
45             "Horizontally scrollable component was measured with an infinity maximum width " +
46                 "constraints, which is disallowed. One of the common reasons is nesting layouts " +
47                 "like LazyRow and Row(Modifier.horizontalScroll()). If you want to add a " +
48                 "header before the list of items please add a header as a separate item() before " +
49                 "the main items() inside the LazyRow scope. There could be other reasons " +
50                 "for this to happen: your ComposeView was added into a LinearLayout with some " +
51                 "weight, you applied Modifier.wrapContentSize(unbounded = true) or wrote a " +
52                 "custom layout. Please try to remove the source of infinite constraints in the " +
53                 "hierarchy above the scrolling container."
54         }
55     }
56 }
57