-
Notifications
You must be signed in to change notification settings - Fork 586
/
PagerIndicator.kt
210 lines (196 loc) · 8.92 KB
/
PagerIndicator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.accompanist.pager
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.ContentAlpha
import androidx.compose.material.LocalContentAlpha
import androidx.compose.material.LocalContentColor
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlin.math.absoluteValue
import kotlin.math.sign
/**
* An horizontally laid out indicator for a [HorizontalPager] or [VerticalPager], representing
* the currently active page and total pages drawn using a [Shape].
*
* This element allows the setting of the [indicatorShape], which defines how the
* indicator is visually represented.
*
* @sample com.google.accompanist.sample.pager.HorizontalPagerIndicatorSample
*
* @param pagerState the state object of your [Pager] to be used to observe the list's state.
* @param modifier the modifier to apply to this layout.
* @param pageCount the size of indicators should be displayed, defaults to [PagerState.pageCount].
* If you are implementing a looping pager with a much larger [PagerState.pageCount]
* than indicators should displayed, e.g. [Int.MAX_VALUE], specify you real size in this param.
* @param pageIndexMapping describe how to get the position of active indicator by the giving page
* from [PagerState.currentPage], if [pageCount] is not equals to [PagerState.pageCount].
* @param activeColor the color of the active Page indicator
* @param inactiveColor the color of page indicators that are inactive. This defaults to
* [activeColor] with the alpha component set to the [ContentAlpha.disabled].
* @param indicatorWidth the width of each indicator in [Dp].
* @param indicatorHeight the height of each indicator in [Dp]. Defaults to [indicatorWidth].
* @param spacing the spacing between each indicator in [Dp].
* @param indicatorShape the shape representing each indicator. This defaults to [CircleShape].
*/
@ExperimentalPagerApi
@Composable
fun HorizontalPagerIndicator(
pagerState: PagerState,
modifier: Modifier = Modifier,
pageCount: Int = pagerState.pageCount,
pageIndexMapping: (Int) -> Int = { it },
activeColor: Color = LocalContentColor.current.copy(alpha = LocalContentAlpha.current),
inactiveColor: Color = activeColor.copy(ContentAlpha.disabled),
indicatorWidth: Dp = 8.dp,
indicatorHeight: Dp = indicatorWidth,
spacing: Dp = indicatorWidth,
indicatorShape: Shape = CircleShape,
) {
val indicatorWidthPx = LocalDensity.current.run { indicatorWidth.roundToPx() }
val spacingPx = LocalDensity.current.run { spacing.roundToPx() }
Box(
modifier = modifier,
contentAlignment = Alignment.CenterStart
) {
Row(
horizontalArrangement = Arrangement.spacedBy(spacing),
verticalAlignment = Alignment.CenterVertically,
) {
val indicatorModifier = Modifier
.size(width = indicatorWidth, height = indicatorHeight)
.background(color = inactiveColor, shape = indicatorShape)
repeat(pageCount) {
Box(indicatorModifier)
}
}
Box(
Modifier
.offset {
val position = pageIndexMapping(pagerState.currentPage)
val offset = pagerState.currentPageOffset
val next = pageIndexMapping(pagerState.currentPage + offset.sign.toInt())
val scrollPosition = ((next - position) * offset.absoluteValue + position)
.coerceIn(0f, (pageCount - 1).coerceAtLeast(0).toFloat())
IntOffset(
x = ((spacingPx + indicatorWidthPx) * scrollPosition).toInt(),
y = 0
)
}
.size(width = indicatorWidth, height = indicatorHeight)
.then(
if (pageCount > 0) Modifier.background(
color = activeColor,
shape = indicatorShape,
)
else Modifier
)
)
}
}
/**
* An vertically laid out indicator for a [VerticalPager] or [HorizontalPager], representing
* the currently active page and total pages drawn using a [Shape].
*
* This element allows the setting of the [indicatorShape], which defines how the
* indicator is visually represented.
*
* @sample com.google.accompanist.sample.pager.VerticalPagerIndicatorSample
*
* @param pagerState the state object of your [Pager] to be used to observe the list's state.
* @param modifier the modifier to apply to this layout.
* @param pageCount the size of indicators should be displayed, defaults to [PagerState.pageCount].
* If you are implementing a looping pager with a much larger [PagerState.pageCount]
* than indicators should displayed, e.g. [Int.MAX_VALUE], specify you real size in this param.
* @param pageIndexMapping describe how to get the position of active indicator by the giving page
* from [PagerState.currentPage], if [pageCount] is not equals to [PagerState.pageCount].
* @param activeColor the color of the active Page indicator
* @param inactiveColor the color of page indicators that are inactive. This defaults to
* [activeColor] with the alpha component set to the [ContentAlpha.disabled].
* @param indicatorHeight the height of each indicator in [Dp].
* @param indicatorWidth the width of each indicator in [Dp]. Defaults to [indicatorHeight].
* @param spacing the spacing between each indicator in [Dp].
* @param indicatorShape the shape representing each indicator. This defaults to [CircleShape].
*/
@ExperimentalPagerApi
@Composable
fun VerticalPagerIndicator(
pagerState: PagerState,
modifier: Modifier = Modifier,
pageCount: Int = pagerState.pageCount,
pageIndexMapping: (Int) -> Int = { it },
activeColor: Color = LocalContentColor.current.copy(alpha = LocalContentAlpha.current),
inactiveColor: Color = activeColor.copy(ContentAlpha.disabled),
indicatorHeight: Dp = 8.dp,
indicatorWidth: Dp = indicatorHeight,
spacing: Dp = indicatorHeight,
indicatorShape: Shape = CircleShape,
) {
val indicatorHeightPx = LocalDensity.current.run { indicatorHeight.roundToPx() }
val spacingPx = LocalDensity.current.run { spacing.roundToPx() }
Box(
modifier = modifier,
contentAlignment = Alignment.TopCenter
) {
Column(
verticalArrangement = Arrangement.spacedBy(spacing),
horizontalAlignment = Alignment.CenterHorizontally,
) {
val indicatorModifier = Modifier
.size(width = indicatorWidth, height = indicatorHeight)
.background(color = inactiveColor, shape = indicatorShape)
repeat(pageCount) {
Box(indicatorModifier)
}
}
Box(
Modifier
.offset {
val position = pageIndexMapping(pagerState.currentPage)
val offset = pagerState.currentPageOffset
val next = pageIndexMapping(pagerState.currentPage + offset.sign.toInt())
val scrollPosition = ((next - position) * offset.absoluteValue + position)
.coerceIn(0f, (pageCount - 1).coerceAtLeast(0).toFloat())
IntOffset(
x = 0,
y = ((spacingPx + indicatorHeightPx) * scrollPosition).toInt(),
)
}
.size(width = indicatorWidth, height = indicatorHeight)
.then(
if (pageCount > 0) Modifier.background(
color = activeColor,
shape = indicatorShape,
)
else Modifier
)
)
}
}