• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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
18 
19 import androidx.compose.foundation.clickable
20 import androidx.compose.foundation.layout.BoxWithConstraints
21 import androidx.compose.foundation.layout.Column
22 import androidx.compose.foundation.layout.Row
23 import androidx.compose.foundation.layout.Spacer
24 import androidx.compose.foundation.layout.padding
25 import androidx.compose.foundation.layout.size
26 import androidx.compose.foundation.shape.RoundedCornerShape
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.getValue
32 import androidx.compose.runtime.mutableStateOf
33 import androidx.compose.runtime.remember
34 import androidx.compose.runtime.setValue
35 import androidx.compose.ui.Modifier
36 import androidx.compose.ui.unit.Dp
37 import androidx.compose.ui.unit.dp
38 import kotlin.math.roundToInt
39 
40 /**
41  * This is an example Compose feature, which shows a text and a count that is incremented when
42  * clicked. We also show the max width available to this component, which is displayed either next
43  * to or below the text depending on that max width.
44  */
45 @Composable
ExampleFeaturenull46 fun ExampleFeature(text: String, modifier: Modifier = Modifier) {
47     BoxWithConstraints(modifier) {
48         val maxWidth = maxWidth
49         if (maxWidth < 600.dp) {
50             Column {
51                 CounterTile(text)
52                 Spacer(Modifier.size(16.dp))
53                 MaxWidthTile(maxWidth)
54             }
55         } else {
56             Row {
57                 CounterTile(text)
58                 Spacer(Modifier.size(16.dp))
59                 MaxWidthTile(maxWidth)
60             }
61         }
62     }
63 }
64 
65 @Composable
CounterTilenull66 private fun CounterTile(text: String, modifier: Modifier = Modifier) {
67     Surface(
68         modifier,
69         color = MaterialTheme.colorScheme.primaryContainer,
70         shape = RoundedCornerShape(28.dp),
71     ) {
72         var count by remember { mutableStateOf(0) }
73         Column(
74             Modifier.clickable { count++ }.padding(16.dp),
75         ) {
76             Text(text)
77             Text("I was clicked $count times.")
78         }
79     }
80 }
81 
82 @Composable
MaxWidthTilenull83 private fun MaxWidthTile(maxWidth: Dp, modifier: Modifier = Modifier) {
84     Surface(
85         modifier,
86         color = MaterialTheme.colorScheme.tertiaryContainer,
87         shape = RoundedCornerShape(28.dp),
88     ) {
89         Text(
90             "The max available width to me is: ${maxWidth.value.roundToInt()}dp",
91             Modifier.padding(16.dp)
92         )
93     }
94 }
95