• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.compose.animation.scene
18 
19 import androidx.compose.animation.core.Animatable
20 import androidx.compose.animation.core.AnimationVector1D
21 import androidx.compose.animation.core.SpringSpec
22 import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
23 import androidx.compose.runtime.withFrameNanos
24 import com.android.compose.animation.scene.content.state.TransitionState
25 import kotlinx.coroutines.CoroutineScope
26 import kotlinx.coroutines.Job
27 
animateContentnull28 internal fun CoroutineScope.animateContent(
29     layoutState: MutableSceneTransitionLayoutStateImpl,
30     transition: TransitionState.Transition,
31     oneOffAnimation: OneOffAnimation,
32     targetProgress: Float,
33     chain: Boolean = true,
34 ): Job {
35     oneOffAnimation.onRun = {
36         // Animate the progress to its target value.
37         @OptIn(ExperimentalMaterial3ExpressiveApi::class)
38         val animationSpec =
39             transition.transformationSpec.progressSpec
40                 ?: layoutState.motionScheme.defaultSpatialSpec()
41         val visibilityThreshold =
42             (animationSpec as? SpringSpec)?.visibilityThreshold ?: ProgressVisibilityThreshold
43         val replacedTransition = transition.replacedTransition
44         val initialProgress = replacedTransition?.progress ?: 0f
45         val initialVelocity = replacedTransition?.progressVelocity ?: 0f
46         val animatable =
47             Animatable(initialProgress, visibilityThreshold = visibilityThreshold).also {
48                 oneOffAnimation.animatable = it
49             }
50 
51         if (layoutState.deferTransitionProgress) {
52             // Defer the animation by one frame so that the transition progress is changed only when
53             // the expensive first composition frame is done.
54             withFrameNanos {}
55         }
56         animatable.animateTo(targetProgress, animationSpec, initialVelocity)
57     }
58 
59     return layoutState.startTransitionImmediately(animationScope = this, transition, chain)
60 }
61 
62 internal class OneOffAnimation {
63     /**
64      * The animatable used to animate this transition.
65      *
66      * Note: This is lateinit because we need to first create this object so that
67      * [SceneTransitionLayoutState] can compute the transformations and animation spec associated to
68      * the transition, which is needed to initialize this Animatable.
69      */
70     lateinit var animatable: Animatable<Float, AnimationVector1D>
71 
72     /** The runnable to run for this animation. */
73     lateinit var onRun: suspend () -> Unit
74 
75     val progress: Float
76         get() = animatable.value
77 
78     val progressVelocity: Float
79         get() = animatable.velocity
80 
runnull81     suspend fun run() {
82         onRun()
83     }
84 
freezeAndAnimateToCurrentStatenull85     fun freezeAndAnimateToCurrentState() {
86         // Do nothing, the state of one-off animations never change and we directly animate to it.
87     }
88 }
89 
90 // TODO(b/290184746): Compute a good default visibility threshold that depends on the layout size
91 // and screen density.
92 internal const val ProgressVisibilityThreshold = 1e-3f
93