Skip to content

Commit

Permalink
Fix cancelling nested scroll across Pager axis
Browse files Browse the repository at this point in the history
Upstreaming #1154 fix

Bug: 327687953
Test: run `nestedScrollContent_shouldPropagateCrossAxisUnconsumedFlings` test
(cherry picked from https://android-review.googlesource.com/q/commit:9083dbe9cb097ef39adaa5ff23533b526fd70309)
Merged-In: I39287f09161f9775665fb45ad4075954cfac5032
Change-Id: I39287f09161f9775665fb45ad4075954cfac5032
  • Loading branch information
MatkovIvan authored and Android Build Coastguard Worker committed Mar 5, 2024
1 parent 22b329d commit 8d23611
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.platform.LocalViewConfiguration
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performTouchInput
Expand Down Expand Up @@ -163,8 +164,18 @@ class PagerNestedScrollContentTest(
@Test
fun nestedScrollContent_shouldPropagateCrossAxisUnconsumedFlings() {
// Arrange
var scrollAvailable = Offset.Zero
var postFlingVelocity = Velocity.Zero
val dataCapturingConnection = object : NestedScrollConnection {
override fun onPostScroll(
consumed: Offset,
available: Offset,
source: NestedScrollSource
): Offset {
scrollAvailable += available
return Offset.Zero
}

override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
postFlingVelocity = available
return Velocity.Zero
Expand Down Expand Up @@ -207,6 +218,11 @@ class PagerNestedScrollContentTest(
val crossAxisVelocity = if (vertical) postFlingVelocity.x else postFlingVelocity.y
assertThat(mainAxisVelocity.absoluteValue).isEqualTo(0f)
assertThat(crossAxisVelocity.absoluteValue).isNotEqualTo(0f)

val mainAxisScrollAvailable = if (vertical) scrollAvailable.y else scrollAvailable.x
val crossAxisScrollAvailable = if (vertical) scrollAvailable.x else scrollAvailable.y
assertThat(crossAxisScrollAvailable.absoluteValue).isNotEqualTo(0f)
assertThat(mainAxisScrollAvailable.absoluteValue).isEqualTo(0f)
}

@OptIn(ExperimentalFoundationApi::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ private class DefaultPagerNestedScrollConnection(
available: Offset,
source: NestedScrollSource
): Offset {
if (source == NestedScrollSource.Fling && available != Offset.Zero) {
if (source == NestedScrollSource.Fling && available.mainAxis() != 0f) {
throw CancellationException()
}
return Offset.Zero
Expand All @@ -904,6 +904,9 @@ private class DefaultPagerNestedScrollConnection(
override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
return available.consumeOnOrientation(orientation)
}

private fun Offset.mainAxis(): Float =
if (orientation == Orientation.Horizontal) this.x else this.y
}

@OptIn(ExperimentalFoundationApi::class)
Expand Down

0 comments on commit 8d23611

Please sign in to comment.