1 /*
<lambda>null2 * Copyright (C) 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.systemui.keyguard.ui.composable.modifier
18
19 import androidx.compose.runtime.Composable
20 import androidx.compose.runtime.getValue
21 import androidx.compose.runtime.mutableStateOf
22 import androidx.compose.runtime.remember
23 import androidx.compose.ui.Modifier
24 import androidx.compose.ui.graphics.graphicsLayer
25 import androidx.compose.ui.layout.boundsInWindow
26 import androidx.compose.ui.layout.onPlaced
27 import androidx.lifecycle.compose.collectAsStateWithLifecycle
28 import com.android.systemui.keyguard.ui.viewmodel.AodBurnInViewModel
29 import com.android.systemui.keyguard.ui.viewmodel.BurnInParameters
30 import com.android.systemui.keyguard.ui.viewmodel.BurnInScaleViewModel
31 import kotlinx.coroutines.flow.map
32
33 /**
34 * Modifies the composable to account for anti-burn in translation, alpha, and scaling.
35 *
36 * Please override [isClock] as `true` if the composable is an element that's part of a clock.
37 */
38 @Composable
39 fun Modifier.burnInAware(
40 viewModel: AodBurnInViewModel,
41 params: BurnInParameters,
42 isClock: Boolean = false,
43 ): Modifier {
44 val translationYState = remember { mutableStateOf(0F) }
45 val copiedParams = params.copy(translationY = { translationYState.value })
46 val burnIn = viewModel.movement(copiedParams)
47 val translationX by
48 burnIn.map { it.translationX.toFloat() }.collectAsStateWithLifecycle(initialValue = 0f)
49 val translationY by
50 burnIn.map { it.translationY.toFloat() }.collectAsStateWithLifecycle(initialValue = 0f)
51 translationYState.value = translationY
52 val scaleViewModel by
53 burnIn
54 .map {
55 BurnInScaleViewModel(
56 scale = it.scale,
57 scaleClockOnly = it.scaleClockOnly,
58 )
59 }
60 .collectAsStateWithLifecycle(initialValue = BurnInScaleViewModel())
61
62 return this.graphicsLayer {
63 this.translationX = if (isClock) 0F else translationX
64 this.translationY = translationY
65 this.alpha = alpha
66
67 val scale = if (scaleViewModel.scaleClockOnly) scaleViewModel.scale else 1f
68 this.scaleX = scale
69 this.scaleY = scale
70 }
71 }
72
73 /** Reports the "top" coordinate of the modified composable to the given [consumer]. */
74 @Composable
onTopPlacementChangednull75 fun Modifier.onTopPlacementChanged(
76 consumer: (Float) -> Unit,
77 ): Modifier {
78 return onPlaced { coordinates -> consumer(coordinates.boundsInWindow().top) }
79 }
80