1 /*
2 * 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 package com.android.permissioncontroller.wear.permission.components.material2
17
18 import androidx.compose.foundation.background
19 import androidx.compose.foundation.layout.Arrangement
20 import androidx.compose.foundation.layout.Box
21 import androidx.compose.foundation.layout.BoxScope
22 import androidx.compose.foundation.layout.IntrinsicSize
23 import androidx.compose.foundation.layout.Row
24 import androidx.compose.foundation.layout.RowScope
25 import androidx.compose.foundation.layout.Spacer
26 import androidx.compose.foundation.layout.fillMaxWidth
27 import androidx.compose.foundation.layout.height
28 import androidx.compose.foundation.layout.width
29 import androidx.compose.foundation.layout.wrapContentSize
30 import androidx.compose.runtime.Composable
31 import androidx.compose.runtime.CompositionLocalProvider
32 import androidx.compose.ui.Alignment
33 import androidx.compose.ui.Modifier
34 import androidx.compose.ui.graphics.Color
35 import androidx.compose.ui.semantics.heading
36 import androidx.compose.ui.semantics.semantics
37 import androidx.compose.ui.text.style.Hyphens
38 import androidx.compose.ui.unit.dp
39 import androidx.wear.compose.material.LocalContentColor
40 import androidx.wear.compose.material.LocalTextStyle
41 import androidx.wear.compose.material.MaterialTheme
42
43 /**
44 * A slot based composable for creating a list header item. [ListHeader]s are typically expected to
45 * be a few words of text on a single line. The contents will be start and end padded.
46 *
47 * @param modifier The modifier for the [ListHeader].
48 * @param backgroundColor The background color to apply - typically Color.Transparent
49 * @param contentColor The color to apply to content.
50 * @param content Slot for [ListHeader] content, expected to be a single line of text.
51 */
52
53 // Styling updated to match with wear material title
54 // Ref:
55 // https://source.corp.google.com/h/googleplex-android/platform/superproject/main/+/main:vendor/google_clockwork_partners/libs/ClockworkCommonLibs/common/wearable/wearmaterial/preference/res/layout/wear_title_preference.xml;l=1;drc=8ebd53cbba588e8e9aa964522fb05f4f5224609e;bpv=1;bpt=0
56 @Composable
ListHeadernull57 fun ListHeader(
58 modifier: Modifier = Modifier,
59 backgroundColor: Color = Color.Transparent,
60 contentColor: Color = MaterialTheme.colors.onBackground,
61 content: @Composable RowScope.() -> Unit,
62 ) {
63 Row(
64 horizontalArrangement = Arrangement.Center,
65 modifier =
66 modifier.wrapContentSize().background(backgroundColor).semantics(
67 mergeDescendants = true
68 ) {
69 heading()
70 },
71 ) {
72 CompositionLocalProvider(
73 LocalContentColor provides contentColor,
74 LocalTextStyle provides MaterialTheme.typography.title3.copy(hyphens = Hyphens.Auto),
75 ) {
76 content()
77 }
78 }
79 }
80
81 /**
82 * A two slot based composable for creating a list subheader item. [ListSubheader]s offer slots for
83 * an icon and for a text label. The contents will be start and end padded.
84 *
85 * @param modifier The modifier for the [ListSubheader].
86 * @param backgroundColor The background color to apply - typically Color.Transparent
87 * @param contentColor The color to apply to content.
88 * @param icon A slot for providing icon to the [ListSubheader].
89 * @param label A slot for providing label to the [ListSubheader].
90 */
91 @Composable
ListSubheadernull92 fun ListSubheader(
93 modifier: Modifier = Modifier,
94 backgroundColor: Color = Color.Transparent,
95 contentColor: Color = MaterialTheme.colors.onBackground,
96 icon: (@Composable BoxScope.() -> Unit)? = null,
97 label: @Composable RowScope.() -> Unit,
98 ) {
99 Row(
100 verticalAlignment = Alignment.CenterVertically,
101 horizontalArrangement = Arrangement.Start,
102 modifier =
103 modifier
104 .height(IntrinsicSize.Min)
105 .fillMaxWidth()
106 .wrapContentSize(align = Alignment.CenterStart)
107 .background(backgroundColor)
108 .semantics(mergeDescendants = true) { heading() },
109 ) {
110 CompositionLocalProvider(
111 LocalContentColor provides contentColor,
112 LocalTextStyle provides MaterialTheme.typography.caption1,
113 ) {
114 if (icon != null) {
115 Box(
116 modifier = Modifier.wrapContentSize(align = Alignment.CenterStart),
117 content = icon,
118 )
119 Spacer(modifier = Modifier.width(6.dp))
120 }
121 label()
122 }
123 }
124 }
125