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

Commit c3513a5f authored by Jordan Demeulenaere's avatar Jordan Demeulenaere
Browse files

Introduce disableSwipesWhenScrolling()

This CL introduces Modifier.disableSwipesWhenScrolling() in
ContentScope to allow contents to temporarily disable their swipe user
actions while a scrollable consumes some scroll events. This allows to
make sure that a scrollable is being overscrolled when reaching its
bounds rather than directly starting a scene transition.

Bug: 383288923
Test: atest ContentTest
Flag: EXEMPT this is not used anywhere yet
Change-Id: I589861fd27a91bdbac2db7054618574918089a26
parent d590a3dd
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import com.android.compose.gesture.NestedScrollableBound
import com.android.compose.gesture.effect.ContentOverscrollEffect

/**
@@ -237,6 +238,18 @@ interface BaseContentScope : ElementStateScope {
     */
    fun Modifier.noResizeDuringTransitions(): Modifier

    /**
     * Temporarily disable this content swipe actions when any scrollable below this modifier has
     * consumed any amount of scroll delta, until the scroll gesture is finished.
     *
     * This can for instance be used to ensure that a scrollable list is overscrolled once it
     * reached its bounds instead of directly starting a scene transition from the same scroll
     * gesture.
     */
    fun Modifier.disableSwipesWhenScrolling(
        bounds: NestedScrollableBound = NestedScrollableBound.Any
    ): Modifier

    /**
     * A [NestedSceneTransitionLayout] will share its elements with its ancestor STLs therefore
     * enabling sharedElement transitions between them.
+5 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ private fun DraggableHandlerImpl.contentForSwipes(): Content {

/** Whether swipe should be enabled in the given [orientation]. */
internal fun Content.shouldEnableSwipes(orientation: Orientation): Boolean {
    if (userActions.isEmpty()) {
    if (userActions.isEmpty() || !areSwipesAllowed()) {
        return false
    }

@@ -69,6 +69,10 @@ internal fun Content.shouldEnableSwipes(orientation: Orientation): Boolean {
 * @return The best matching [UserActionResult], or `null` if no match is found.
 */
internal fun Content.findActionResultBestMatch(swipe: Swipe.Resolved): UserActionResult? {
    if (!areSwipesAllowed()) {
        return null
    }

    var bestPoints = Int.MIN_VALUE
    var bestMatch: UserActionResult? = null
    userActions.forEach { (actionSwipe, actionResult) ->
+12 −1
Original line number Diff line number Diff line
@@ -56,7 +56,10 @@ import com.android.compose.animation.scene.effect.GestureEffect
import com.android.compose.animation.scene.effect.VisualEffect
import com.android.compose.animation.scene.element
import com.android.compose.animation.scene.modifiers.noResizeDuringTransitions
import com.android.compose.gesture.NestedScrollControlState
import com.android.compose.gesture.NestedScrollableBound
import com.android.compose.gesture.effect.OffsetOverscrollEffect
import com.android.compose.gesture.nestedScrollController
import com.android.compose.modifiers.thenIf
import com.android.compose.ui.graphics.ContainerState
import com.android.compose.ui.graphics.container
@@ -70,7 +73,8 @@ internal sealed class Content(
    actions: Map<UserAction.Resolved, UserActionResult>,
    zIndex: Float,
) {
    internal val scope = ContentScopeImpl(layoutImpl, content = this)
    private val nestedScrollControlState = NestedScrollControlState()
    internal val scope = ContentScopeImpl(layoutImpl, content = this, nestedScrollControlState)
    val containerState = ContainerState()

    var content by mutableStateOf(content)
@@ -101,11 +105,14 @@ internal sealed class Content(
            scope.content()
        }
    }

    fun areSwipesAllowed(): Boolean = nestedScrollControlState.isOuterScrollAllowed
}

internal class ContentScopeImpl(
    private val layoutImpl: SceneTransitionLayoutImpl,
    private val content: Content,
    private val nestedScrollControlState: NestedScrollControlState,
) : ContentScope, ElementStateScope by layoutImpl.elementStateScope {
    override val contentKey: ContentKey
        get() = content.key
@@ -176,6 +183,10 @@ internal class ContentScopeImpl(
        return noResizeDuringTransitions(layoutState = layoutImpl.state)
    }

    override fun Modifier.disableSwipesWhenScrolling(bounds: NestedScrollableBound): Modifier {
        return nestedScrollController(nestedScrollControlState, bounds)
    }

    @Composable
    override fun NestedSceneTransitionLayout(
        state: SceneTransitionLayoutState,
+69 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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
 *
 *      http://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.android.compose.animation.scene

import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.rememberScrollableState
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.performTouchInput
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.compose.animation.scene.TestScenes.SceneA
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class ContentTest {
    @get:Rule val rule = createComposeRule()

    @Test
    fun disableSwipesWhenScrolling() {
        lateinit var layoutImpl: SceneTransitionLayoutImpl
        rule.setContent {
            SceneTransitionLayoutForTesting(
                remember { MutableSceneTransitionLayoutState(SceneA) },
                onLayoutImpl = { layoutImpl = it },
            ) {
                scene(SceneA) {
                    Box(
                        Modifier.fillMaxSize()
                            .disableSwipesWhenScrolling()
                            .scrollable(rememberScrollableState { it }, Orientation.Vertical)
                    )
                }
            }
        }

        val content = layoutImpl.content(SceneA)
        assertThat(content.areSwipesAllowed()).isTrue()
        rule.onRoot().performTouchInput {
            down(topLeft)
            moveBy(bottomLeft)
        }

        assertThat(content.areSwipesAllowed()).isFalse()
        rule.onRoot().performTouchInput { up() }
        assertThat(content.areSwipesAllowed()).isTrue()
    }
}