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.lazy.grid
18 
19 import androidx.compose.foundation.gestures.Orientation
20 import androidx.compose.ui.unit.IntOffset
21 import androidx.compose.ui.unit.IntSize
22 
23 /**
24  * Contains useful information about an individual item in lazy grids like [LazyVerticalGrid].
25  *
26  * @see LazyGridLayoutInfo
27  */
28 sealed interface LazyGridItemInfo {
29     /** The index of the item in the grid. */
30     val index: Int
31 
32     /** The key of the item which was passed to the item() or items() function. */
33     val key: Any
34 
35     /**
36      * The offset of the item in pixels. It is relative to the top start of the lazy grid container.
37      */
38     val offset: IntOffset
39 
40     /**
41      * The row occupied by the top start point of the item. If this is unknown, for example while
42      * this item is animating to exit the viewport and is still visible, the value will be
43      * [UnknownRow].
44      */
45     val row: Int
46 
47     /**
48      * The column occupied by the top start point of the item. If this is unknown, for example while
49      * this item is animating to exit the viewport and is still visible, the value will be
50      * [UnknownColumn].
51      */
52     val column: Int
53 
54     /**
55      * The pixel size of the item. Note that if you emit multiple layouts in the composable slot for
56      * the item then this size will be calculated as the max of their sizes.
57      */
58     val size: IntSize
59 
60     /** The content type of the item which was passed to the item() or items() function. */
61     val contentType: Any?
62 
63     /**
64      * The horizontal span of the item if it's in a [LazyVerticalGrid] or the vertical span if the
65      * item is in a [LazyHorizontalGrid].
66      *
67      * Note, [LazyGridLayoutInfo.maxSpan] can be used to get the maximum number of spans in a line,
68      * e.g., to check if the item is filling the whole line.
69      */
70     val span: Int
71 
72     companion object {
73         /**
74          * Possible value for [row], when they are unknown. This can happen when the item is visible
75          * while animating to exit the viewport.
76          */
77         const val UnknownRow = -1
78         /**
79          * Possible value for [column], when they are unknown. This can happen when the item is
80          * visible while animating to exit the viewport.
81          */
82         const val UnknownColumn = -1
83     }
84 }
85 
lineIndexnull86 internal fun LazyGridItemInfo.lineIndex(orientation: Orientation): Int =
87     if (orientation == Orientation.Vertical) {
88         row
89     } else {
90         column
91     }
92