-
Notifications
You must be signed in to change notification settings - Fork 605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Pager] Migrate to Modifier.scrollable #236
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b99abf7
Try out using Modifier.scrollable
chrisbanes 628efd3
Add reverseLayout parameter
chrisbanes 6bea8ef
Improve semantics implementation
chrisbanes aa149c4
Update to Compose SNAPSHOT 7212271
chrisbanes 95cc0e9
Improve scroll snapping
chrisbanes 97d01f8
Expose animation specs via HorizontalPager
chrisbanes d42994e
Tweak Pager tests for new snap thresholds
chrisbanes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,9 +20,15 @@ package com.google.accompanist.pager | |
|
||
import android.util.Log | ||
import androidx.annotation.IntRange | ||
import androidx.compose.animation.splineBasedDecay | ||
import androidx.compose.animation.core.AnimationSpec | ||
import androidx.compose.animation.core.DecayAnimationSpec | ||
import androidx.compose.animation.core.spring | ||
import androidx.compose.animation.defaultDecayAnimationSpec | ||
import androidx.compose.foundation.gestures.FlingBehavior | ||
import androidx.compose.foundation.gestures.Orientation | ||
import androidx.compose.foundation.gestures.draggable | ||
import androidx.compose.foundation.gestures.ScrollScope | ||
import androidx.compose.foundation.gestures.scrollBy | ||
import androidx.compose.foundation.gestures.scrollable | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.BoxScope | ||
import androidx.compose.runtime.Composable | ||
|
@@ -33,10 +39,10 @@ import androidx.compose.runtime.remember | |
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clipToBounds | ||
import androidx.compose.ui.layout.Layout | ||
import androidx.compose.ui.layout.Measurable | ||
import androidx.compose.ui.layout.ParentDataModifier | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.platform.LocalLayoutDirection | ||
import androidx.compose.ui.semantics.ScrollAxisRange | ||
import androidx.compose.ui.semantics.horizontalScrollAxisRange | ||
|
@@ -49,19 +55,19 @@ import androidx.compose.ui.unit.LayoutDirection | |
import kotlinx.coroutines.launch | ||
import kotlin.math.roundToInt | ||
|
||
/** | ||
* The scroll threshold for moving to the next page. The value is used in both directions | ||
* (so both negative and positive). | ||
*/ | ||
internal const val ScrollThreshold = 0.35f | ||
|
||
/** | ||
* Library-wide switch to turn on debug logging. | ||
*/ | ||
internal const val DebugLog = false | ||
|
||
private const val LogTag = "Pager" | ||
|
||
/** | ||
* This attempts to mimic ViewPager's custom scroll interpolator. It's not a perfect match | ||
* (and we may not want it to be), but this seem to match in terms of scroll duration and 'feel' | ||
*/ | ||
private const val SnapSpringStiffness = 2750f | ||
|
||
@RequiresOptIn(message = "Accompanist Pager is experimental. The API may be changed in the future.") | ||
@Retention(AnnotationRetention.BINARY) | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) | ||
|
@@ -87,8 +93,13 @@ private val Measurable.page: Int | |
* | ||
* @param state the state object to be used to control or observe the list's state. | ||
* @param modifier the modifier to apply to this layout. | ||
* @param reverseLayout reverse the direction of scrolling and layout, when `true` items will be | ||
* composed from the end to the start and [PagerState.currentPage] == 0 will mean | ||
* the first item is located at the end. | ||
* @param offscreenLimit the number of pages that should be retained on either side of the | ||
* current page. This value is required to be `1` or greater. | ||
* @param decayAnimationSpec The decay animation spec to use for decayed flings. | ||
* @param snapAnimationSpec The animation spec to use when snapping. | ||
* @param content a block which describes the content. Inside this block you can reference | ||
* [PagerScope.currentPage] and other properties in [PagerScope]. | ||
*/ | ||
|
@@ -97,51 +108,62 @@ private val Measurable.page: Int | |
fun HorizontalPager( | ||
state: PagerState, | ||
modifier: Modifier = Modifier, | ||
reverseLayout: Boolean = false, | ||
@IntRange(from = 1) offscreenLimit: Int = 1, | ||
content: @Composable PagerScope.(page: Int) -> Unit | ||
decayAnimationSpec: DecayAnimationSpec<Float> = defaultDecayAnimationSpec(), | ||
snapAnimationSpec: AnimationSpec<Float> = spring(stiffness = SnapSpringStiffness), | ||
content: @Composable PagerScope.(page: Int) -> Unit, | ||
) { | ||
require(offscreenLimit >= 1) { "offscreenLimit is required to be >= 1" } | ||
|
||
val reverseScroll = LocalLayoutDirection.current == LayoutDirection.Rtl | ||
|
||
val density = LocalDensity.current | ||
val decay = remember(density) { splineBasedDecay<Float>(density) } | ||
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl | ||
val reverseDirection = if (isRtl) !reverseLayout else reverseLayout | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you do |
||
|
||
val coroutineScope = rememberCoroutineScope() | ||
|
||
val semanticsAxisRange = remember(state, reverseDirection) { | ||
ScrollAxisRange( | ||
value = { state.currentPage + state.currentPageOffset }, | ||
maxValue = { state.lastPageIndex.toFloat() }, | ||
) | ||
} | ||
val semantics = Modifier.semantics { | ||
if (state.pageCount > 0) { | ||
horizontalScrollAxisRange = ScrollAxisRange( | ||
value = { (state.currentPage + state.currentPageOffset) * state.pageSize }, | ||
maxValue = { state.lastPageIndex.toFloat() * state.pageSize }, | ||
reverseScrolling = reverseScroll | ||
) | ||
// Hook up scroll actions to our state | ||
scrollBy { x: Float, _ -> | ||
coroutineScope.launch { | ||
state.draggableState.drag { dragBy(x) } | ||
} | ||
true | ||
horizontalScrollAxisRange = semanticsAxisRange | ||
// Hook up scroll actions to our state | ||
scrollBy { x: Float, _ -> | ||
coroutineScope.launch { | ||
state.scrollBy(if (reverseDirection) x else -x) | ||
} | ||
// Treat this as a selectable group | ||
selectableGroup() | ||
true | ||
} | ||
// Treat this as a selectable group | ||
selectableGroup() | ||
} | ||
|
||
val draggable = Modifier.draggable( | ||
state = state.draggableState, | ||
startDragImmediately = true, | ||
onDragStopped = { velocity -> | ||
launch { state.performFling(velocity, decay) } | ||
}, | ||
val flingBehavior = remember(state, decayAnimationSpec, snapAnimationSpec) { | ||
object : FlingBehavior { | ||
override suspend fun ScrollScope.performFling(initialVelocity: Float): Float { | ||
return state.fling( | ||
initialVelocity = -initialVelocity, | ||
decayAnimationSpec = decayAnimationSpec, | ||
snapAnimationSpec = snapAnimationSpec, | ||
scrollBy = { deltaPixels -> -scrollBy(-deltaPixels) }, | ||
) | ||
} | ||
} | ||
} | ||
|
||
val scrollable = Modifier.scrollable( | ||
orientation = Orientation.Horizontal, | ||
reverseDirection = reverseScroll, | ||
flingBehavior = flingBehavior, | ||
reverseDirection = reverseDirection, | ||
state = state, | ||
) | ||
|
||
Layout( | ||
modifier = modifier | ||
.then(semantics) | ||
.then(draggable), | ||
.then(scrollable) | ||
.clipToBounds(), | ||
content = { | ||
val firstPage = (state.currentPage - offscreenLimit).coerceAtLeast(0) | ||
val lastPage = (state.currentPage + offscreenLimit).coerceAtMost(state.lastPageIndex) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's is an idea: since your internal PagerState.fling uses only public API available (if not, it's still a good idea :) ) what you can do here is to replace two animation specs with one FlingBehaviour, e.g.
Or similar, then you can just open the whole flingbehaviour for overriding (make FlingBehaviour a param in HorizontalPager), so people can:
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I like it, I'll follow up after #254