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

Commit f65b369f authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Make OverscrollScope extend Density" into main

parents 3a287975 7bb08aca
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -521,6 +521,7 @@ private fun SwipeTransition(
        }

    return SwipeTransition(
        layoutImpl = layoutImpl,
        layoutState = layoutState,
        coroutineScope = coroutineScope,
        key = result.transitionKey,
@@ -534,6 +535,7 @@ private fun SwipeTransition(

private fun SwipeTransition(old: SwipeTransition): SwipeTransition {
    return SwipeTransition(
            layoutImpl = old.layoutImpl,
            layoutState = old.layoutState,
            coroutineScope = old.coroutineScope,
            key = old.key,
@@ -550,6 +552,7 @@ private fun SwipeTransition(old: SwipeTransition): SwipeTransition {
}

private class SwipeTransition(
    val layoutImpl: SceneTransitionLayoutImpl,
    val layoutState: BaseSceneTransitionLayoutState,
    val coroutineScope: CoroutineScope,
    val key: TransitionKey?,
@@ -607,6 +610,12 @@ private class SwipeTransition(

    override val overscrollScope: OverscrollScope =
        object : OverscrollScope {
            override val density: Float
                get() = layoutImpl.density.density

            override val fontScale: Float
                get() = layoutImpl.density.fontScale

            override val absoluteDistance: Float
                get() = distance().absoluteValue
        }
+2 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.SpringSpec
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.android.compose.animation.scene.TransitionState.HasOverscrollProperties.Companion.DistanceUnspecified
@@ -192,7 +193,7 @@ interface OverscrollBuilder : BaseTransitionBuilder {
    )
}

interface OverscrollScope {
interface OverscrollScope : Density {
    /**
     * Return the absolute distance between fromScene and toScene, if available, otherwise
     * [DistanceUnspecified].
+40 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.platform.LocalViewConfiguration
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.assertPositionInRootIsEqualTo
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.performTouchInput
@@ -594,4 +595,43 @@ class SwipeToSceneTest {
        assertThat(transition).hasToScene(SceneB)
        assertThat(transition).hasProgress(0.5f, tolerance = 0.01f)
    }

    @Test
    fun overscrollScopeExtendsDensity() {
        val swipeDistance = 100.dp
        val state =
            rule.runOnUiThread {
                MutableSceneTransitionLayoutState(
                    SceneA,
                    transitions {
                        from(SceneA, to = SceneB) { distance = FixedDistance(swipeDistance) }

                        overscroll(SceneB, Orientation.Vertical) {
                            translate(TestElements.Foo, x = { 20.dp.toPx() }, y = { 30.dp.toPx() })
                        }
                    }
                )
            }
        val layoutSize = 200.dp
        var touchSlop = 0f
        rule.setContent {
            touchSlop = LocalViewConfiguration.current.touchSlop
            SceneTransitionLayout(state, Modifier.size(layoutSize)) {
                scene(SceneA, userActions = mapOf(Swipe.Down to SceneB)) {
                    Box(Modifier.fillMaxSize())
                }
                scene(SceneB) { Box(Modifier.element(TestElements.Foo).fillMaxSize()) }
            }
        }

        // Swipe down by twice the swipe distance so that we are at 100% overscrolling on scene B.
        rule.onRoot().performTouchInput {
            val middle = (layoutSize / 2).toPx()
            down(Offset(middle, middle))
            moveBy(Offset(0f, touchSlop + (swipeDistance * 2).toPx()), delayMillis = 1_000)
        }

        // Foo should be translated by (20dp, 30dp).
        rule.onNode(isElement(TestElements.Foo)).assertPositionInRootIsEqualTo(20.dp, 30.dp)
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -41,8 +41,10 @@ fun transition(
    return object : TransitionState.Transition(from, to), TransitionState.HasOverscrollProperties {
        override val currentScene: SceneKey
            get() = current()

        override val progress: Float
            get() = progress()

        override val progressVelocity: Float
            get() = progressVelocity()

@@ -53,6 +55,8 @@ fun transition(
        override val orientation: Orientation = orientation
        override val overscrollScope: OverscrollScope =
            object : OverscrollScope {
                override val density: Float = 1f
                override val fontScale: Float = 1f
                override val absoluteDistance = 0f
            }