1 /*
2  * Copyright 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 androidx.wear.compose.material3.samples
18 
19 import androidx.annotation.Sampled
20 import androidx.compose.foundation.layout.Column
21 import androidx.compose.foundation.layout.Spacer
22 import androidx.compose.foundation.layout.fillMaxWidth
23 import androidx.compose.foundation.layout.height
24 import androidx.compose.foundation.layout.size
25 import androidx.compose.foundation.layout.width
26 import androidx.compose.material.icons.Icons
27 import androidx.compose.material.icons.filled.KeyboardArrowDown
28 import androidx.compose.material.icons.filled.KeyboardArrowUp
29 import androidx.compose.runtime.Composable
30 import androidx.compose.runtime.getValue
31 import androidx.compose.runtime.mutableStateOf
32 import androidx.compose.runtime.remember
33 import androidx.compose.runtime.setValue
34 import androidx.compose.ui.Alignment
35 import androidx.compose.ui.Modifier
36 import androidx.compose.ui.res.painterResource
37 import androidx.compose.ui.text.style.TextAlign
38 import androidx.compose.ui.text.style.TextOverflow
39 import androidx.wear.compose.material3.Button
40 import androidx.wear.compose.material3.ButtonDefaults
41 import androidx.wear.compose.material3.ChildButton
42 import androidx.wear.compose.material3.CompactButton
43 import androidx.wear.compose.material3.FilledTonalButton
44 import androidx.wear.compose.material3.Icon
45 import androidx.wear.compose.material3.ImageButton
46 import androidx.wear.compose.material3.OutlinedButton
47 import androidx.wear.compose.material3.Text
48 
49 @Sampled
50 @Composable
SimpleButtonSamplenull51 fun SimpleButtonSample(modifier: Modifier = Modifier) {
52     Button(onClick = { /* Do something */ }, label = { Text("Simple Button") }, modifier = modifier)
53 }
54 
55 @Sampled
56 @Composable
ButtonSamplenull57 fun ButtonSample(modifier: Modifier = Modifier.fillMaxWidth()) {
58     Button(
59         onClick = { /* Do something */ },
60         label = { Text("Button") },
61         secondaryLabel = { Text("Secondary label") },
62         icon = {
63             Icon(
64                 painter = painterResource(R.drawable.ic_favorite_rounded),
65                 contentDescription = "Favorite icon",
66                 modifier = Modifier.size(ButtonDefaults.IconSize)
67             )
68         },
69         modifier = modifier
70     )
71 }
72 
73 @Sampled
74 @Composable
ButtonLargeIconSamplenull75 fun ButtonLargeIconSample(modifier: Modifier = Modifier.fillMaxWidth(), enabled: Boolean = true) {
76     // When customising the icon size, it is recommended to also specify
77     // the associated content padding
78     Button(
79         onClick = { /* Do something */ },
80         enabled = enabled,
81         label = { Text("Button") },
82         secondaryLabel = { Text("Secondary label") },
83         icon = {
84             Icon(
85                 painter = painterResource(R.drawable.ic_favorite_rounded),
86                 contentDescription = "Favorite icon",
87                 modifier = Modifier.size(ButtonDefaults.LargeIconSize)
88             )
89         },
90         contentPadding = ButtonDefaults.ButtonWithLargeIconContentPadding,
91         modifier = modifier
92     )
93 }
94 
95 @Sampled
96 @Composable
ButtonExtraLargeIconSamplenull97 fun ButtonExtraLargeIconSample(
98     modifier: Modifier = Modifier.fillMaxWidth(),
99     enabled: Boolean = true
100 ) {
101     // When customising the icon size, it is recommended to also specify
102     // the associated content padding
103     Button(
104         onClick = { /* Do something */ },
105         enabled = enabled,
106         label = { Text("Button") },
107         secondaryLabel = { Text("Secondary label") },
108         icon = {
109             Icon(
110                 painter = painterResource(R.drawable.ic_favorite_rounded),
111                 contentDescription = "Favorite icon",
112                 modifier = Modifier.size(ButtonDefaults.ExtraLargeIconSize)
113             )
114         },
115         contentPadding = ButtonDefaults.ButtonWithExtraLargeIconContentPadding,
116         modifier = modifier
117     )
118 }
119 
120 @Sampled
121 @Composable
ButtonWithImageSamplenull122 fun ButtonWithImageSample(modifier: Modifier = Modifier.fillMaxWidth(), enabled: Boolean = true) {
123     ImageButton(
124         onClick = { /* Do something */ },
125         containerPainter =
126             ButtonDefaults.containerPainter(
127                 image = painterResource(id = R.drawable.backgroundimage)
128             ),
129         enabled = enabled,
130         label = { Text("Button") },
131         secondaryLabel = { Text("Secondary label") },
132         icon = {
133             Icon(
134                 painter = painterResource(R.drawable.ic_favorite_rounded),
135                 contentDescription = "Favorite icon",
136             )
137         },
138         modifier = modifier
139     )
140 }
141 
142 @Sampled
143 @Composable
SimpleFilledTonalButtonSamplenull144 fun SimpleFilledTonalButtonSample(modifier: Modifier = Modifier.fillMaxWidth()) {
145     FilledTonalButton(
146         onClick = { /* Do something */ },
147         label = { Text("Filled Tonal Button") },
148         modifier = modifier,
149     )
150 }
151 
152 @Sampled
153 @Composable
FilledTonalButtonSamplenull154 fun FilledTonalButtonSample(modifier: Modifier = Modifier.fillMaxWidth()) {
155     FilledTonalButton(
156         onClick = { /* Do something */ },
157         label = { Text("Filled Tonal Button") },
158         secondaryLabel = { Text("Secondary label") },
159         icon = {
160             Icon(
161                 painter = painterResource(R.drawable.ic_favorite_rounded),
162                 contentDescription = "Favorite icon",
163                 modifier = Modifier.size(ButtonDefaults.IconSize)
164             )
165         },
166         modifier = modifier
167     )
168 }
169 
170 @Sampled
171 @Composable
SimpleFilledVariantButtonSamplenull172 fun SimpleFilledVariantButtonSample(modifier: Modifier = Modifier.fillMaxWidth()) {
173     Button(
174         onClick = { /* Do something */ },
175         colors = ButtonDefaults.filledVariantButtonColors(),
176         label = { Text("Filled Variant Button") },
177         modifier = modifier,
178     )
179 }
180 
181 @Sampled
182 @Composable
FilledVariantButtonSamplenull183 fun FilledVariantButtonSample(modifier: Modifier = Modifier.fillMaxWidth()) {
184     Button(
185         onClick = { /* Do something */ },
186         colors = ButtonDefaults.filledVariantButtonColors(),
187         label = { Text("Filled Variant Button") },
188         secondaryLabel = { Text("Secondary label") },
189         icon = {
190             Icon(
191                 painter = painterResource(R.drawable.ic_favorite_rounded),
192                 contentDescription = "Favorite icon",
193                 modifier = Modifier.size(ButtonDefaults.IconSize)
194             )
195         },
196         modifier = modifier
197     )
198 }
199 
200 @Sampled
201 @Composable
SimpleOutlinedButtonSamplenull202 fun SimpleOutlinedButtonSample(modifier: Modifier = Modifier.fillMaxWidth()) {
203     OutlinedButton(
204         onClick = { /* Do something */ },
205         label = { Text("Outlined Button") },
206         modifier = modifier
207     )
208 }
209 
210 @Sampled
211 @Composable
OutlinedButtonSamplenull212 fun OutlinedButtonSample(modifier: Modifier = Modifier.fillMaxWidth()) {
213     OutlinedButton(
214         onClick = { /* Do something */ },
215         label = { Text("Outlined Button") },
216         secondaryLabel = { Text("Secondary label") },
217         icon = {
218             Icon(
219                 painter = painterResource(R.drawable.ic_favorite_rounded),
220                 contentDescription = "Favorite icon",
221                 modifier = Modifier.size(ButtonDefaults.IconSize)
222             )
223         },
224         modifier = modifier,
225     )
226 }
227 
228 @Sampled
229 @Composable
SimpleChildButtonSamplenull230 fun SimpleChildButtonSample(modifier: Modifier = Modifier.fillMaxWidth()) {
231     ChildButton(
232         onClick = { /* Do something */ },
233         label = {
234             Text("Child Button", textAlign = TextAlign.Center, modifier = Modifier.fillMaxWidth())
235         },
236         modifier = modifier,
237     )
238 }
239 
240 @Sampled
241 @Composable
ChildButtonSamplenull242 fun ChildButtonSample(modifier: Modifier = Modifier.fillMaxWidth()) {
243     ChildButton(
244         onClick = { /* Do something */ },
245         label = { Text("Child Button") },
246         secondaryLabel = { Text("Secondary label") },
247         icon = {
248             Icon(
249                 painter = painterResource(R.drawable.ic_favorite_rounded),
250                 contentDescription = "Favorite icon",
251                 modifier = Modifier.size(ButtonDefaults.IconSize)
252             )
253         },
254         modifier = modifier
255     )
256 }
257 
258 @Sampled
259 @Composable
CompactButtonSamplenull260 fun CompactButtonSample(modifier: Modifier = Modifier) {
261     CompactButton(
262         onClick = { /* Do something */ },
263         icon = {
264             Icon(
265                 painter = painterResource(R.drawable.ic_favorite_rounded),
266                 contentDescription = "Favorite icon",
267                 modifier = Modifier.size(ButtonDefaults.ExtraSmallIconSize)
268             )
269         },
270         modifier = modifier,
271     ) {
272         Text("Compact Button", maxLines = 1, overflow = TextOverflow.Ellipsis)
273     }
274 }
275 
276 @Sampled
277 @Composable
CompactButtonWithOnLongClickSamplenull278 fun CompactButtonWithOnLongClickSample(
279     onClickHandler: () -> Unit,
280     onLongClickHandler: () -> Unit,
281     modifier: Modifier = Modifier
282 ) {
283     CompactButton(
284         onClick = onClickHandler,
285         onLongClick = onLongClickHandler,
286         onLongClickLabel = "Long click",
287         label = { Text("Long clickable") },
288         modifier = modifier,
289     )
290 }
291 
292 @Sampled
293 @Composable
FilledTonalCompactButtonSamplenull294 fun FilledTonalCompactButtonSample(modifier: Modifier = Modifier) {
295     CompactButton(
296         onClick = { /* Do something */ },
297         icon = {
298             Icon(
299                 painter = painterResource(R.drawable.ic_favorite_rounded),
300                 contentDescription = "Favorite icon",
301                 modifier = Modifier.size(ButtonDefaults.ExtraSmallIconSize)
302             )
303         },
304         colors = ButtonDefaults.filledTonalButtonColors(),
305         modifier = modifier,
306     ) {
307         Text("Filled Tonal Compact Button", maxLines = 1, overflow = TextOverflow.Ellipsis)
308     }
309 }
310 
311 @Sampled
312 @Composable
OutlinedCompactButtonSamplenull313 fun OutlinedCompactButtonSample(modifier: Modifier = Modifier) {
314     Column(horizontalAlignment = Alignment.CenterHorizontally) {
315         var expanded by remember { mutableStateOf(false) }
316         if (expanded) {
317             Text("A multiline string showing two lines")
318         } else {
319             Text("One line text")
320         }
321         Spacer(Modifier.height(ButtonDefaults.IconSpacing))
322         CompactButton(
323             onClick = { expanded = !expanded },
324             colors = ButtonDefaults.outlinedButtonColors(),
325             border = ButtonDefaults.outlinedButtonBorder(enabled = true),
326             modifier = modifier,
327         ) {
328             if (expanded) {
329                 Text("Show Less", maxLines = 1, overflow = TextOverflow.Ellipsis)
330             } else {
331                 Text("Show More", maxLines = 1, overflow = TextOverflow.Ellipsis)
332             }
333             Spacer(Modifier.width(ButtonDefaults.IconSpacing))
334             if (expanded) {
335                 Icon(
336                     Icons.Filled.KeyboardArrowUp,
337                     contentDescription = "Collapse",
338                     modifier = Modifier.size(ButtonDefaults.ExtraSmallIconSize)
339                 )
340             } else {
341                 Icon(
342                     Icons.Filled.KeyboardArrowDown,
343                     contentDescription = "Expand",
344                     modifier = Modifier.size(ButtonDefaults.ExtraSmallIconSize)
345                 )
346             }
347         }
348     }
349 }
350