Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8c035ed8 authored by Jordan Demeulenaere's avatar Jordan Demeulenaere
Browse files

Return correct consumed scroll in ContentOverscrollEffect

Bug: 428690181
Fixes: 428690181
Test: atest OffsetOverscrollEffectTest
Flag: com.android.systemui.scene_container
Change-Id: I5f8faf0538be012970f618146efb25734145badc
parent 8265ce74
Loading
Loading
Loading
Loading
+10 −5
Original line number Original line Diff line number Diff line
@@ -84,7 +84,7 @@ open class BaseContentOverscrollEffect(
        val currentOffset = animatable.value
        val currentOffset = animatable.value
        val sameDirection = deltaForAxis.sign == currentOffset.sign
        val sameDirection = deltaForAxis.sign == currentOffset.sign
        val consumedByPreScroll =
        val consumedByPreScroll =
            if (abs(currentOffset) > 0.5 && !sameDirection) {
            if (abs(currentOffset) > 0f && !sameDirection) {
                    // The user has scrolled in the opposite direction.
                    // The user has scrolled in the opposite direction.
                    val prevOverscrollValue = currentOffset
                    val prevOverscrollValue = currentOffset
                    val newOverscrollValue = currentOffset + deltaForAxis
                    val newOverscrollValue = currentOffset + deltaForAxis
@@ -112,11 +112,16 @@ open class BaseContentOverscrollEffect(


        // If the user is dragging (not flinging), and there's any remaining scroll delta after the
        // If the user is dragging (not flinging), and there's any remaining scroll delta after the
        // standard scrolling logic has been applied, we add it to the overscroll.
        // standard scrolling logic has been applied, we add it to the overscroll.
        if (abs(overscrollDelta.toFloat()) > 0.5 && source == NestedScrollSource.UserInput) {
        val overscroll = overscrollDelta.toFloat()
            animationScope.launch { animatable.snapTo(currentOffset + overscrollDelta.toFloat()) }
        val consumedByPostScroll =
            if (abs(overscroll) > 0f && source == NestedScrollSource.UserInput) {
                animationScope.launch { animatable.snapTo(currentOffset + overscroll) }
                overscroll.toOffset()
            } else {
                Offset.Zero
            }
            }


        return delta
        return consumedByPreScroll + consumedByScroll + consumedByPostScroll
    }
    }


    override suspend fun applyToFling(
    override suspend fun applyToFling(
+18 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package com.android.compose.gesture.effect
package com.android.compose.gesture.effect


import androidx.compose.animation.core.spring
import androidx.compose.foundation.OverscrollEffect
import androidx.compose.foundation.OverscrollEffect
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.ScrollableState
import androidx.compose.foundation.gestures.ScrollableState
@@ -29,6 +30,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalViewConfiguration
import androidx.compose.ui.platform.LocalViewConfiguration
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.platform.testTag
@@ -328,4 +330,20 @@ class OffsetOverscrollEffectTest {
        assertThat(info.scrollableState.isScrollInProgress).isFalse()
        assertThat(info.scrollableState.isScrollInProgress).isFalse()
        assertThat(info.overscrollEffect.isInProgress).isFalse()
        assertThat(info.overscrollEffect.isInProgress).isFalse()
    }
    }

    @Test
    fun applyToScroll_doesNotConsumeFlingScrolls() = runTest {
        val effect = OffsetOverscrollEffect(animationScope = this, animationSpec = spring())
        assertThat(
                effect.applyToScroll(Offset(0f, 20f), source = NestedScrollSource.SideEffect) {
                    scroll ->
                    assertThat(scroll).isEqualTo(Offset(0f, 20f))

                    // Consume some of the delta during scroll.
                    Offset(0f, 15f)
                }
            )
            // The remaining offset was not consumed to overscroll given that the source is a fling.
            .isEqualTo(Offset(0f, 15f))
    }
}
}