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

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

Merge changes I16f37ace,I61376765 into main

* changes:
  Revert "Simplify PriorityNestedScrollConnection"
  Revert "PriorityNestedScrollConnection: make onStop suspendable and add onCancel"
parents 695fddd9 7f60633a
Loading
Loading
Loading
Loading
+12 −16
Original line number Diff line number Diff line
@@ -18,8 +18,6 @@ package com.android.systemui.notifications.ui.composable

import androidx.compose.foundation.gestures.Orientation
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.util.fastCoerceAtLeast
import androidx.compose.ui.util.fastCoerceAtMost
import com.android.compose.nestedscroll.PriorityNestedScrollConnection

/**
@@ -46,7 +44,7 @@ fun NotificationScrimNestedScrollConnection(
        orientation = Orientation.Vertical,
        // scrolling up and inner content is taller than the scrim, so scrim needs to
        // expand; content can scroll once scrim is at the minScrimOffset.
        canStartPreScroll = { offsetAvailable, offsetBeforeStart, _ ->
        canStartPreScroll = { offsetAvailable, offsetBeforeStart ->
            offsetAvailable < 0 &&
                offsetBeforeStart == 0f &&
                contentHeight() > minVisibleScrimHeight() &&
@@ -54,38 +52,36 @@ fun NotificationScrimNestedScrollConnection(
        },
        // scrolling down and content is done scrolling to top. After that, the scrim
        // needs to collapse; collapse the scrim until it is at the maxScrimOffset.
        canStartPostScroll = { offsetAvailable, _, _ ->
        canStartPostScroll = { offsetAvailable, _ ->
            offsetAvailable > 0 && (scrimOffset() < maxScrimOffset || isCurrentGestureOverscroll())
        },
        canStartPostFling = { false },
        canStopOnPreFling = { false },
        canContinueScroll = {
            val currentHeight = scrimOffset()
            minScrimOffset() < currentHeight && currentHeight < maxScrimOffset
        },
        canScrollOnFling = true,
        onStart = { offsetAvailable -> onStart(offsetAvailable) },
        onScroll = { offsetAvailable, _ ->
        onScroll = { offsetAvailable ->
            val currentHeight = scrimOffset()
            val amountConsumed =
                if (offsetAvailable > 0) {
                    val amountLeft = maxScrimOffset - currentHeight
                    offsetAvailable.fastCoerceAtMost(amountLeft)
                    offsetAvailable.coerceAtMost(amountLeft)
                } else {
                    val amountLeft = minScrimOffset() - currentHeight
                    offsetAvailable.fastCoerceAtLeast(amountLeft)
                    offsetAvailable.coerceAtLeast(amountLeft)
                }
            snapScrimOffset(currentHeight + amountConsumed)
            amountConsumed
        },
        // Don't consume the velocity on pre/post fling
        onStop = { velocityAvailable ->
            onStop(velocityAvailable)
            if (scrimOffset() < minScrimOffset()) {
                animateScrimOffset(minScrimOffset())
            }
            // Don't consume the velocity on pre/post fling
            0f
        },
        onCancel = {
            onStop(0f)
            if (scrimOffset() < minScrimOffset()) {
                animateScrimOffset(minScrimOffset())
            }
            { 0f }
        },
    )
}
+8 −13
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastCoerceAtLeast
import com.android.compose.nestedscroll.PriorityNestedScrollConnection
import kotlin.math.max
import kotlin.math.roundToInt
@@ -87,25 +86,21 @@ fun NotificationStackNestedScrollConnection(
): PriorityNestedScrollConnection {
    return PriorityNestedScrollConnection(
        orientation = Orientation.Vertical,
        canStartPreScroll = { _, _, _ -> false },
        canStartPostScroll = { offsetAvailable, offsetBeforeStart, _ ->
        canStartPreScroll = { _, _ -> false },
        canStartPostScroll = { offsetAvailable, offsetBeforeStart ->
            offsetAvailable < 0f && offsetBeforeStart < 0f && !canScrollForward()
        },
        canStartPostFling = { velocityAvailable -> velocityAvailable < 0f && !canScrollForward() },
        canStopOnPreFling = { false },
        canContinueScroll = { stackOffset() > 0f },
        canScrollOnFling = true,
        onStart = { offsetAvailable -> onStart(offsetAvailable) },
        onScroll = { offsetAvailable, _ ->
            val minOffset = 0f
            val consumed = offsetAvailable.fastCoerceAtLeast(minOffset - stackOffset())
            if (consumed != 0f) {
                onScroll(consumed)
            }
            consumed
        onScroll = { offsetAvailable ->
            onScroll(offsetAvailable)
            offsetAvailable
        },
        onStop = { velocityAvailable ->
            onStop(velocityAvailable)
            velocityAvailable
            suspend { velocityAvailable }
        },
        onCancel = { onStop(0f) },
    )
}
+10 −18
Original line number Diff line number Diff line
@@ -27,10 +27,9 @@ import com.android.compose.animation.scene.content.Content
import com.android.compose.animation.scene.content.state.TransitionState
import com.android.compose.animation.scene.content.state.TransitionState.HasOverscrollProperties.Companion.DistanceUnspecified
import com.android.compose.nestedscroll.PriorityNestedScrollConnection
import com.android.compose.nestedscroll.SuspendedValue
import kotlin.math.absoluteValue

internal typealias SuspendedValue<T> = suspend () -> T

internal interface DraggableHandler {
    /**
     * Start a drag in the given [startedPosition], with the given [overSlop] and number of
@@ -613,7 +612,7 @@ internal class NestedScrollHandlerImpl(

        return PriorityNestedScrollConnection(
            orientation = orientation,
            canStartPreScroll = { offsetAvailable, offsetBeforeStart, _ ->
            canStartPreScroll = { offsetAvailable, offsetBeforeStart ->
                canChangeScene =
                    if (isExternalOverscrollGesture()) false else offsetBeforeStart == 0f

@@ -645,7 +644,7 @@ internal class NestedScrollHandlerImpl(
                isIntercepting = true
                true
            },
            canStartPostScroll = { offsetAvailable, offsetBeforeStart, _ ->
            canStartPostScroll = { offsetAvailable, offsetBeforeStart ->
                val behavior: NestedScrollBehavior =
                    when {
                        offsetAvailable > 0f -> topOrLeftBehavior
@@ -710,7 +709,8 @@ internal class NestedScrollHandlerImpl(

                canStart
            },
            canStopOnPreFling = { true },
            canContinueScroll = { true },
            canScrollOnFling = false,
            onStart = { offsetAvailable ->
                val pointersInfo = pointersInfo()
                dragController =
@@ -720,7 +720,7 @@ internal class NestedScrollHandlerImpl(
                        overSlop = if (isIntercepting) 0f else offsetAvailable,
                    )
            },
            onScroll = { offsetAvailable, _ ->
            onScroll = { offsetAvailable ->
                val controller = dragController ?: error("Should be called after onStart")

                val pointersInfo = pointersInfoOwner.pointersInfo()
@@ -735,18 +735,10 @@ internal class NestedScrollHandlerImpl(
            },
            onStop = { velocityAvailable ->
                val controller = dragController ?: error("Should be called after onStart")
                try {

                controller
                    .onStop(velocity = velocityAvailable, canChangeContent = canChangeScene)
                        .invoke()
                } finally {
                    dragController = null
                }
            },
            onCancel = {
                val controller = dragController ?: error("Should be called after onStart")
                controller.onStop(velocity = 0f, canChangeContent = canChangeScene)
                dragController = null
                    .also { dragController = null }
            },
        )
    }
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.unit.IntSize
import com.android.compose.animation.scene.content.state.TransitionState
import com.android.compose.animation.scene.content.state.TransitionState.HasOverscrollProperties.Companion.DistanceUnspecified
import com.android.compose.nestedscroll.SuspendedValue
import kotlin.math.absoluteValue
import kotlinx.coroutines.CompletableDeferred

+11 −10
Original line number Diff line number Diff line
@@ -18,8 +18,6 @@ package com.android.compose.nestedscroll

import androidx.compose.foundation.gestures.Orientation
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.util.fastCoerceAtLeast
import androidx.compose.ui.util.fastCoerceAtMost

/**
 * A [NestedScrollConnection] that listens for all vertical scroll events and responds in the
@@ -45,32 +43,35 @@ fun LargeTopAppBarNestedScrollConnection(
        orientation = Orientation.Vertical,
        // When swiping up, the LargeTopAppBar will shrink (to [minHeight]) and the content will
        // expand. Then, you can then scroll down the content.
        canStartPreScroll = { offsetAvailable, offsetBeforeStart, _ ->
        canStartPreScroll = { offsetAvailable, offsetBeforeStart ->
            offsetAvailable < 0 && offsetBeforeStart == 0f && height() > minHeight()
        },
        // When swiping down, the content will scroll up until it reaches the top. Then, the
        // LargeTopAppBar will expand until it reaches its [maxHeight].
        canStartPostScroll = { offsetAvailable, _, _ ->
        canStartPostScroll = { offsetAvailable, _ ->
            offsetAvailable > 0 && height() < maxHeight()
        },
        canStartPostFling = { false },
        canStopOnPreFling = { false },
        canContinueScroll = {
            val currentHeight = height()
            minHeight() < currentHeight && currentHeight < maxHeight()
        },
        canScrollOnFling = true,
        onStart = { /* do nothing */ },
        onScroll = { offsetAvailable, _ ->
        onScroll = { offsetAvailable ->
            val currentHeight = height()
            val amountConsumed =
                if (offsetAvailable > 0) {
                    val amountLeft = maxHeight() - currentHeight
                    offsetAvailable.fastCoerceAtMost(amountLeft)
                    offsetAvailable.coerceAtMost(amountLeft)
                } else {
                    val amountLeft = minHeight() - currentHeight
                    offsetAvailable.fastCoerceAtLeast(amountLeft)
                    offsetAvailable.coerceAtLeast(amountLeft)
                }
            onHeightChanged(currentHeight + amountConsumed)
            amountConsumed
        },
        // Don't consume the velocity on pre/post fling
        onStop = { 0f },
        onCancel = { /* do nothing */ },
        onStop = { { 0f } },
    )
}
Loading