1 /*
2  * Copyright 2023 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.pager
18 
19 import androidx.compose.foundation.gestures.Orientation
20 import androidx.compose.foundation.gestures.snapping.SnapPosition
21 import androidx.compose.ui.unit.IntSize
22 
23 /**
24  * Contains useful information about the currently displayed layout state of a [Pager]. This
25  * information is available after the first measure pass.
26  *
27  * Use [PagerState.layoutInfo] to retrieve this
28  */
29 sealed interface PagerLayoutInfo {
30     /** A list of all pages that are currently visible in the [Pager] */
31     val visiblePagesInfo: List<PageInfo>
32 
33     /**
34      * The main axis size of the Pages in this [Pager] provided by the [PageSize] API in the Pager
35      * definition. This is provided in pixels.
36      */
37     val pageSize: Int
38 
39     /** The spacing in pixels provided in the [Pager] creation. */
40     val pageSpacing: Int
41 
42     /**
43      * The start offset of the layout's viewport in pixels. You can think of it as a minimum offset
44      * which would be visible. Usually it is 0, but it can be negative if non-zero
45      * beforeContentPadding was applied as the content displayed in the content padding area is
46      * still visible.
47      *
48      * You can use it to understand what items from [visiblePagesInfo] are fully visible.
49      */
50     val viewportStartOffset: Int
51 
52     /**
53      * The end offset of the layout's viewport in pixels. You can think of it as a maximum offset
54      * which would be visible. It is the size of the lazy list layout minus [beforeContentPadding].
55      *
56      * You can use it to understand what items from [visiblePagesInfo] are fully visible.
57      */
58     val viewportEndOffset: Int
59 
60     /**
61      * The content padding in pixels applied before the first page in the direction of scrolling.
62      * For example it is a top content padding for [VerticalPager] with reverseLayout set to false.
63      */
64     val beforeContentPadding: Int
65 
66     /**
67      * The content padding in pixels applied after the last page in the direction of scrolling. For
68      * example it is a bottom content padding for [VerticalPager] with reverseLayout set to false.
69      */
70     val afterContentPadding: Int
71 
72     /**
73      * The size of the viewport in pixels. It is the [Pager] layout size including all the content
74      * paddings.
75      */
76     val viewportSize: IntSize
77 
78     /** The [Pager] orientation. */
79     val orientation: Orientation
80 
81     /** True if the direction of scrolling and layout is reversed. */
82     @Suppress("GetterSetterNames") @get:Suppress("GetterSetterNames") val reverseLayout: Boolean
83 
84     /**
85      * Pages to compose and layout before and after the list of visible pages. This does not include
86      * the pages automatically composed and laid out by the pre-fetcher in the direction of the
87      * scroll during scroll events.
88      */
89     val beyondViewportPageCount: Int
90 
91     /** The calculation of how this Pager performs snapping of pages. */
92     val snapPosition: SnapPosition
93 }
94 
95 internal val PagerLayoutInfo.mainAxisViewportSize: Int
96     get() = if (orientation == Orientation.Vertical) viewportSize.height else viewportSize.width
97