1 /*
<lambda>null2 * 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.systemui.keyboard.stickykeys.ui.view
18
19 import android.content.Context
20 import android.view.View
21 import androidx.compose.foundation.layout.Arrangement
22 import androidx.compose.foundation.layout.Column
23 import androidx.compose.foundation.layout.heightIn
24 import androidx.compose.foundation.layout.padding
25 import androidx.compose.foundation.layout.width
26 import androidx.compose.material3.LocalContentColor
27 import androidx.compose.material3.MaterialTheme
28 import androidx.compose.material3.Surface
29 import androidx.compose.material3.Text
30 import androidx.compose.runtime.Composable
31 import androidx.compose.runtime.CompositionLocalProvider
32 import androidx.compose.runtime.getValue
33 import androidx.compose.runtime.key
34 import androidx.compose.ui.Alignment
35 import androidx.compose.ui.Modifier
36 import androidx.compose.ui.platform.ComposeView
37 import androidx.compose.ui.text.font.FontWeight
38 import androidx.compose.ui.unit.dp
39 import androidx.lifecycle.compose.collectAsStateWithLifecycle
40 import com.android.compose.theme.PlatformTheme
41 import com.android.systemui.keyboard.stickykeys.shared.model.Locked
42 import com.android.systemui.keyboard.stickykeys.shared.model.ModifierKey
43 import com.android.systemui.keyboard.stickykeys.ui.viewmodel.StickyKeysIndicatorViewModel
44
45 fun createStickyKeyIndicatorView(context: Context, viewModel: StickyKeysIndicatorViewModel): View {
46 return ComposeView(context).apply {
47 setContent {
48 PlatformTheme {
49 val defaultContentColor = MaterialTheme.colorScheme.onSurfaceVariant
50 CompositionLocalProvider(LocalContentColor provides defaultContentColor) {
51 StickyKeysIndicator(viewModel)
52 }
53 }
54 }
55 }
56 }
57
58 @Composable
StickyKeysIndicatornull59 fun StickyKeysIndicator(viewModel: StickyKeysIndicatorViewModel) {
60 val stickyKeys by viewModel.indicatorContent.collectAsStateWithLifecycle(emptyMap())
61 StickyKeysIndicator(stickyKeys)
62 }
63
64 @Composable
StickyKeysIndicatornull65 fun StickyKeysIndicator(stickyKeys: Map<ModifierKey, Locked>, modifier: Modifier = Modifier) {
66 Surface(
67 color = MaterialTheme.colorScheme.inverseSurface,
68 shape = MaterialTheme.shapes.medium,
69 modifier = modifier.heightIn(min = 84.dp).width(96.dp)
70 ) {
71 Column(
72 horizontalAlignment = Alignment.CenterHorizontally,
73 verticalArrangement = Arrangement.Center,
74 modifier = Modifier.padding(16.dp)
75 ) {
76 stickyKeys.forEach { (key, isLocked) -> key(key) { StickyKeyText(key, isLocked) } }
77 }
78 }
79 }
80
81 @Composable
StickyKeyTextnull82 private fun StickyKeyText(key: ModifierKey, isLocked: Locked, modifier: Modifier = Modifier) {
83 Text(
84 text = key.displayedText,
85 fontWeight = if (isLocked.locked) FontWeight.Bold else FontWeight.Normal,
86 style = MaterialTheme.typography.bodyMedium,
87 color =
88 if (isLocked.locked) {
89 MaterialTheme.colorScheme.inverseOnSurface
90 } else {
91 MaterialTheme.colorScheme.outlineVariant
92 },
93 modifier = modifier
94 )
95 }
96