1 /*
2  * 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.foundation.lazy
18 
19 import androidx.compose.foundation.gestures.Orientation
20 import androidx.compose.foundation.internal.JvmDefaultWithCompatibility
21 import androidx.compose.ui.unit.IntSize
22 import androidx.compose.ui.util.fastSumBy
23 
24 /**
25  * Contains useful information about the currently displayed layout state of lazy lists like
26  * [LazyColumn] or [LazyRow]. For example you can get the list of currently displayed item.
27  *
28  * Use [LazyListState.layoutInfo] to retrieve this
29  */
30 @JvmDefaultWithCompatibility
31 interface LazyListLayoutInfo {
32     /** The list of [LazyListItemInfo] representing all the currently visible items. */
33     val visibleItemsInfo: List<LazyListItemInfo>
34 
35     /**
36      * The start offset of the layout's viewport in pixels. You can think of it as a minimum offset
37      * which would be visible. Usually it is 0, but it can be negative if non-zero
38      * [beforeContentPadding] was applied as the content displayed in the content padding area is
39      * still visible.
40      *
41      * You can use it to understand what items from [visibleItemsInfo] are fully visible.
42      */
43     val viewportStartOffset: Int
44 
45     /**
46      * The end offset of the layout's viewport in pixels. You can think of it as a maximum offset
47      * which would be visible. It is the size of the lazy list layout minus [beforeContentPadding].
48      *
49      * You can use it to understand what items from [visibleItemsInfo] are fully visible.
50      */
51     val viewportEndOffset: Int
52 
53     /** The total count of items passed to [LazyColumn] or [LazyRow]. */
54     val totalItemsCount: Int
55 
56     /**
57      * The size of the viewport in pixels. It is the lazy list layout size including all the content
58      * paddings.
59      */
60     val viewportSize: IntSize
61         get() = IntSize.Zero
62 
63     /** The orientation of the lazy list. */
64     val orientation: Orientation
65         get() = Orientation.Vertical
66 
67     /** True if the direction of scrolling and layout is reversed. */
68     val reverseLayout: Boolean
69         get() = false
70 
71     /**
72      * The content padding in pixels applied before the first item in the direction of scrolling.
73      * For example it is a top content padding for LazyColumn with reverseLayout set to false.
74      */
75     val beforeContentPadding: Int
76         get() = 0
77 
78     /**
79      * The content padding in pixels applied after the last item in the direction of scrolling. For
80      * example it is a bottom content padding for LazyColumn with reverseLayout set to false.
81      */
82     val afterContentPadding: Int
83         get() = 0
84 
85     /** The spacing between items in the direction of scrolling. */
86     val mainAxisItemSpacing: Int
87         get() = 0
88 }
89 
visibleItemsAverageSizenull90 internal fun LazyListLayoutInfo.visibleItemsAverageSize(): Int {
91     val visibleItems = visibleItemsInfo
92     val itemsSum = visibleItems.fastSumBy { it.size }
93     return itemsSum / visibleItems.size + mainAxisItemSpacing
94 }
95