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

Commit 34caab7a authored by Shawn Lee's avatar Shawn Lee Committed by Android (Google) Code Review
Browse files

Merge "[flexiglass] Added isUserInputOngoing to transition model" into main

parents 4cb17881 43814fef
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -161,7 +161,8 @@ private fun SceneTransitionObservableTransitionState.toModel(): ObservableTransi
                fromScene = fromScene.toModel().key,
                toScene = toScene.toModel().key,
                progress = progress,
                isUserInputDriven = isUserInputDriven,
                isInitiatedByUserInput = isInitiatedByUserInput,
                isUserInputOngoing = isUserInputOngoing,
            )
    }
}
+19 −4
Original line number Diff line number Diff line
@@ -108,7 +108,7 @@ private fun CoroutineScope.animate(
) {
    val fromScene = layoutImpl.state.transitionState.currentScene
    val isUserInput =
        (layoutImpl.state.transitionState as? TransitionState.Transition)?.isUserInputDriven
        (layoutImpl.state.transitionState as? TransitionState.Transition)?.isInitiatedByUserInput
            ?: false

    val animationSpec = layoutImpl.transitions.transitionSpec(fromScene, target).spec
@@ -119,9 +119,23 @@ private fun CoroutineScope.animate(
    val targetProgress = if (reversed) 0f else 1f
    val transition =
        if (reversed) {
            OneOffTransition(target, fromScene, currentScene = target, isUserInput, animatable)
            OneOffTransition(
                fromScene = target,
                toScene = fromScene,
                currentScene = target,
                isInitiatedByUserInput = isUserInput,
                isUserInputOngoing = false,
                animatable = animatable,
            )
        } else {
            OneOffTransition(fromScene, target, currentScene = target, isUserInput, animatable)
            OneOffTransition(
                fromScene = fromScene,
                toScene = target,
                currentScene = target,
                isInitiatedByUserInput = isUserInput,
                isUserInputOngoing = false,
                animatable = animatable,
            )
        }

    // Change the current layout state to use this new transition.
@@ -142,7 +156,8 @@ private class OneOffTransition(
    override val fromScene: SceneKey,
    override val toScene: SceneKey,
    override val currentScene: SceneKey,
    override val isUserInputDriven: Boolean,
    override val isInitiatedByUserInput: Boolean,
    override val isUserInputOngoing: Boolean,
    private val animatable: Animatable<Float, AnimationVector1D>,
) : TransitionState.Transition {
    override val progress: Float
+10 −2
Original line number Diff line number Diff line
@@ -52,7 +52,14 @@ sealed class ObservableTransitionState {
         * scene, this value will remain true after the pointer is no longer touching the screen and
         * will be true in any transition created to animate back to the original position.
         */
        val isUserInputDriven: Boolean,
        val isInitiatedByUserInput: Boolean,

        /**
         * Whether user input is currently driving the transition. For example, if a user is
         * dragging a pointer, this emits true. Once they lift their finger, this emits false while
         * the transition completes/settles.
         */
        val isUserInputOngoing: Flow<Boolean>,
    ) : ObservableTransitionState()
}

@@ -73,7 +80,8 @@ fun SceneTransitionLayoutState.observableTransitionState(): Flow<ObservableTrans
                            fromScene = state.fromScene,
                            toScene = state.toScene,
                            progress = snapshotFlow { state.progress },
                            isUserInputDriven = state.isUserInputDriven,
                            isInitiatedByUserInput = state.isInitiatedByUserInput,
                            isUserInputOngoing = snapshotFlow { state.isUserInputOngoing },
                        )
                    }
                }
+4 −1
Original line number Diff line number Diff line
@@ -70,6 +70,9 @@ sealed interface TransitionState {
        val progress: Float

        /** Whether the transition was triggered by user input rather than being programmatic. */
        val isUserInputDriven: Boolean
        val isInitiatedByUserInput: Boolean

        /** Whether user input is currently driving the transition. */
        val isUserInputOngoing: Boolean
    }
}
+5 −1
Original line number Diff line number Diff line
@@ -508,7 +508,7 @@ class SceneGestureHandler(
                return offset / distance
            }

        override val isUserInputDriven = true
        override val isInitiatedByUserInput = true

        /** The current offset caused by the drag gesture. */
        var dragOffset by mutableFloatStateOf(0f)
@@ -519,6 +519,10 @@ class SceneGestureHandler(
         */
        var isAnimatingOffset by mutableStateOf(false)

        // If we are not animating offset, it means the offset is being driven by the user's finger.
        override val isUserInputOngoing: Boolean
            get() = !isAnimatingOffset

        /** The animatable used to animate the offset once the user lifted its finger. */
        val offsetAnimatable = Animatable(0f, OffsetVisibilityThreshold)

Loading