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.settingslib.spa.widget.ui
18 
19 import androidx.compose.foundation.layout.Box
20 import androidx.compose.foundation.layout.Column
21 import androidx.compose.foundation.layout.fillMaxSize
22 import androidx.compose.foundation.layout.padding
23 import androidx.compose.foundation.layout.width
24 import androidx.compose.material3.MaterialTheme
25 import androidx.compose.material3.Text
26 import androidx.compose.runtime.Composable
27 import androidx.compose.ui.Alignment
28 import androidx.compose.ui.Modifier
29 import androidx.compose.ui.text.TextStyle
30 import androidx.compose.ui.text.style.TextOverflow
31 import androidx.compose.ui.tooling.preview.Preview
32 import androidx.compose.ui.unit.dp
33 import com.android.settingslib.spa.framework.compose.contentDescription
34 import com.android.settingslib.spa.framework.theme.SettingsDimension
35 import com.android.settingslib.spa.framework.theme.SettingsOpacity.alphaForEnabled
36 import com.android.settingslib.spa.framework.theme.SettingsTheme
37 import com.android.settingslib.spa.framework.theme.toMediumWeight
38 
39 @Composable
SettingsTitlenull40 fun SettingsTitle(
41     title: String,
42     contentDescription: String? = null,
43     useMediumWeight: Boolean = false,
44 ) {
45     Text(
46         text = title,
47         modifier = Modifier
48             .padding(vertical = SettingsDimension.paddingTiny)
49             .contentDescription(contentDescription),
50         style = MaterialTheme.typography.titleMedium.withWeight(useMediumWeight),
51     )
52 }
53 
54 @Composable
SettingsTitleSmallnull55 fun SettingsTitleSmall(title: String, useMediumWeight: Boolean = false) {
56     Text(
57         text = title,
58         color = MaterialTheme.colorScheme.onSurface,
59         style = MaterialTheme.typography.titleSmall.withWeight(useMediumWeight),
60     )
61 }
62 
63 @Composable
SettingsDialogItemnull64 fun SettingsDialogItem(text: String, enabled: Boolean = true) {
65     Text(
66         text = text,
67         modifier = Modifier.alphaForEnabled(enabled),
68         color = MaterialTheme.colorScheme.onSurface,
69         style = MaterialTheme.typography.bodyLarge,
70         overflow = TextOverflow.Ellipsis,
71     )
72 }
73 
74 @Composable
SettingsListItemnull75 fun SettingsListItem(text: String, enabled: Boolean = true) {
76     Text(
77         text = text,
78         modifier = Modifier
79             .alphaForEnabled(enabled)
80             .padding(vertical = SettingsDimension.paddingTiny),
81         color = MaterialTheme.colorScheme.onSurface,
82         style = MaterialTheme.typography.titleMedium,
83         overflow = TextOverflow.Ellipsis,
84     )
85 }
86 
87 @Composable
SettingsBodynull88 fun SettingsBody(
89     body: String,
90     contentDescription: String? = null,
91     maxLines: Int = Int.MAX_VALUE,
92 ) {
93     if (body.isNotEmpty()) {
94         Text(
95             text = body,
96             modifier = Modifier.contentDescription(contentDescription),
97             color = MaterialTheme.colorScheme.onSurfaceVariant,
98             style = MaterialTheme.typography.bodyMedium,
99             overflow = TextOverflow.Ellipsis,
100             maxLines = maxLines,
101         )
102     }
103 }
104 
105 @Composable
PlaceholderTitlenull106 fun PlaceholderTitle(title: String) {
107     Box(
108         modifier = Modifier
109             .fillMaxSize()
110             .padding(SettingsDimension.itemPadding),
111         contentAlignment = Alignment.Center,
112     ) {
113         Text(
114             text = title,
115             color = MaterialTheme.colorScheme.onSurface,
116             style = MaterialTheme.typography.titleLarge,
117         )
118     }
119 }
120 
TextStylenull121 private fun TextStyle.withWeight(useMediumWeight: Boolean = false) = when (useMediumWeight) {
122     true -> toMediumWeight()
123     else -> this
124 }
125 
126 @Preview
127 @Composable
BasePreferencePreviewnull128 private fun BasePreferencePreview() {
129     SettingsTheme {
130         Column(Modifier.width(100.dp)) {
131             SettingsTitle(
132                 title = "Title",
133             )
134             SettingsBody(
135                 body = "Long long long long long long text",
136             )
137             SettingsBody(
138                 body = "Long long long long long long text",
139                 maxLines = 1,
140             )
141         }
142     }
143 }
144