1 /*
2  * Copyright 2023 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.foundation.layout.Box
20 import androidx.compose.runtime.Composable
21 import androidx.compose.runtime.State
22 import androidx.compose.runtime.getValue
23 import androidx.compose.runtime.mutableFloatStateOf
24 import androidx.compose.runtime.mutableStateOf
25 import androidx.compose.runtime.setValue
26 import androidx.compose.ui.Modifier
27 import androidx.compose.ui.layout.onPlaced
28 import androidx.compose.ui.unit.IntSize
29 import androidx.compose.ui.zIndex
30 
31 /** A scene in a [SceneTransitionLayout]. */
32 internal class Scene(
33     val key: SceneKey,
34     layoutImpl: SceneTransitionLayoutImpl,
35     content: @Composable SceneScope.() -> Unit,
36     actions: Map<UserAction, SceneKey>,
37     zIndex: Float,
38 ) {
39     private val scope = SceneScopeImpl(layoutImpl, this)
40 
41     var content by mutableStateOf(content)
42     var userActions by mutableStateOf(actions)
43     var zIndex by mutableFloatStateOf(zIndex)
44     var size by mutableStateOf(IntSize.Zero)
45 
46     @Composable
47     fun Content(modifier: Modifier = Modifier) {
48         Box(modifier.zIndex(zIndex).onPlaced { size = it.size }) { scope.content() }
49     }
50 
51     override fun toString(): String {
52         return "Scene(key=$key)"
53     }
54 }
55 
56 private class SceneScopeImpl(
57     private val layoutImpl: SceneTransitionLayoutImpl,
58     private val scene: Scene,
59 ) : SceneScope {
60     @Composable
61     override fun Modifier.element(key: ElementKey): Modifier {
62         return element(layoutImpl, scene, key)
63     }
64 
65     @Composable
66     override fun <T> animateSharedValueAsState(
67         value: T,
68         key: ValueKey,
69         element: ElementKey,
70         lerp: (T, T, Float) -> T,
71         canOverflow: Boolean
72     ): State<T> {
73         val element =
74             layoutImpl.elements[element]
75                 ?: error(
76                     "Element $element is not composed. Make sure to call animateSharedXAsState " +
77                         "*after* Modifier.element(key)."
78                 )
79 
80         return animateSharedValueAsState(
81             layoutImpl,
82             scene,
83             element,
84             key,
85             value,
86             lerp,
87             canOverflow,
88         )
89     }
90 }
91