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.demo
18
19 import androidx.compose.foundation.background
20 import androidx.compose.foundation.clickable
21 import androidx.compose.foundation.layout.Box
22 import androidx.compose.foundation.layout.fillMaxSize
23 import androidx.compose.foundation.layout.padding
24 import androidx.compose.foundation.layout.size
25 import androidx.compose.foundation.shape.CircleShape
26 import androidx.compose.material.icons.Icons
27 import androidx.compose.material.icons.filled.PhotoCamera
28 import androidx.compose.material3.Icon
29 import androidx.compose.runtime.Composable
30 import androidx.compose.runtime.getValue
31 import androidx.compose.ui.Alignment
32 import androidx.compose.ui.Modifier
33 import androidx.compose.ui.draw.clip
34 import androidx.compose.ui.draw.drawBehind
35 import androidx.compose.ui.graphics.Color
36 import androidx.compose.ui.unit.dp
37 import com.android.compose.animation.scene.Back
38 import com.android.compose.animation.scene.ContentScope
39 import com.android.compose.animation.scene.ElementKey
40 import com.android.compose.animation.scene.SceneKey
41 import com.android.compose.animation.scene.UserAction
42 import com.android.compose.animation.scene.UserActionResult
43 import com.android.compose.animation.scene.ValueKey
44 import com.android.compose.animation.scene.animateElementColorAsState
45
46 object Camera {
userActionsnull47 fun userActions(lockscreenScene: SceneKey): Map<UserAction, UserActionResult> =
48 mapOf(Back to lockscreenScene)
49
50 object Elements {
51 val Background = ElementKey("CameraBackground")
52 val Button = ElementKey("CameraButton")
53 val ButtonIcon = ElementKey("CameraButtonBackground")
54 }
55
56 object Values {
57 val ButtonColor = ValueKey("CameraButtonColor")
58 val ButtonIconColor = ValueKey("CameraButtonIconColor")
59 }
60 }
61
62 @Composable
ContentScopenull63 fun ContentScope.Camera(modifier: Modifier = Modifier) {
64 Box(modifier) {
65 Box(Modifier.element(Camera.Elements.Background).fillMaxSize().background(Color.Black))
66 CameraButton(
67 backgroundColor = Color.White,
68 iconColor = Color.Black,
69 onClick = {},
70 Modifier.align(Alignment.BottomCenter).padding(bottom = 100.dp).size(70.dp),
71 )
72 }
73 }
74
75 @Composable
CameraButtonnull76 fun ContentScope.CameraButton(
77 backgroundColor: Color,
78 iconColor: Color,
79 onClick: () -> Unit,
80 modifier: Modifier = Modifier,
81 ) {
82 ElementWithValues(Camera.Elements.Button, modifier) {
83 val backgroundColor by
84 animateElementColorAsState(backgroundColor, Camera.Values.ButtonColor)
85 val iconColor by
86 animateElementColorAsState(iconColor, Camera.Values.ButtonIconColor)
87 // TODO(b/231674463): We should not read iconColor during composition.
88 .unsafeCompositionState(initialValue = iconColor)
89
90 content {
91 Box(
92 Modifier.fillMaxSize().clip(CircleShape).clickable(onClick = onClick).drawBehind {
93 drawRect(backgroundColor)
94 }
95 ) {
96 CameraButtonIcon(
97 iconColor = iconColor,
98 Modifier.element(Camera.Elements.ButtonIcon).size(24.dp).align(Alignment.Center),
99 )
100 }
101 }
102 }
103 }
104
105 @Composable
CameraButtonIconnull106 private fun CameraButtonIcon(iconColor: Color, modifier: Modifier = Modifier) {
107 // Note: In practice, we should somehow defer the icon color read to the drawing phase but
108 // there is no overload of Icon taking a `tint: () -> Color`.
109 Icon(
110 Icons.Default.PhotoCamera,
111 contentDescription = null,
112 tint = iconColor,
113 modifier = modifier,
114 )
115 }
116