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