• 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 
18 package com.android.compose
19 
20 import androidx.annotation.DrawableRes
21 import androidx.compose.foundation.BorderStroke
22 import androidx.compose.foundation.layout.PaddingValues
23 import androidx.compose.foundation.layout.RowScope
24 import androidx.compose.foundation.layout.heightIn
25 import androidx.compose.material3.ButtonColors
26 import androidx.compose.material3.ButtonDefaults
27 import androidx.compose.material3.Icon
28 import androidx.compose.material3.IconButton
29 import androidx.compose.material3.IconButtonColors
30 import androidx.compose.material3.IconButtonDefaults
31 import androidx.compose.material3.MaterialTheme
32 import androidx.compose.runtime.Composable
33 import androidx.compose.ui.Modifier
34 import androidx.compose.ui.graphics.Shape
35 import androidx.compose.ui.res.painterResource
36 import androidx.compose.ui.unit.dp
37 
38 @Composable
PlatformButtonnull39 fun PlatformButton(
40     onClick: () -> Unit,
41     modifier: Modifier = Modifier,
42     enabled: Boolean = true,
43     colors: ButtonColors = filledButtonColors(),
44     contentPadding: PaddingValues = ButtonPaddings,
45     shape: Shape = ButtonDefaults.shape,
46     content: @Composable RowScope.() -> Unit,
47 ) {
48     androidx.compose.material3.Button(
49         modifier = modifier.heightIn(min = 36.dp),
50         colors = colors,
51         contentPadding = contentPadding,
52         shape = shape,
53         onClick = onClick,
54         enabled = enabled,
55     ) {
56         content()
57     }
58 }
59 
60 @Composable
PlatformOutlinedButtonnull61 fun PlatformOutlinedButton(
62     onClick: () -> Unit,
63     modifier: Modifier = Modifier,
64     enabled: Boolean = true,
65     colors: ButtonColors = outlineButtonColors(),
66     border: BorderStroke? = outlineButtonBorder(),
67     content: @Composable RowScope.() -> Unit,
68 ) {
69     androidx.compose.material3.OutlinedButton(
70         modifier = modifier.heightIn(min = 36.dp),
71         enabled = enabled,
72         colors = colors,
73         border = border,
74         contentPadding = ButtonPaddings,
75         onClick = onClick,
76     ) {
77         content()
78     }
79 }
80 
81 @Composable
PlatformTextButtonnull82 fun PlatformTextButton(
83     onClick: () -> Unit,
84     modifier: Modifier = Modifier,
85     enabled: Boolean = true,
86     colors: ButtonColors = textButtonColors(),
87     content: @Composable RowScope.() -> Unit,
88 ) {
89     androidx.compose.material3.TextButton(
90         onClick = onClick,
91         modifier = modifier,
92         enabled = enabled,
93         content = content,
94         colors = colors,
95     )
96 }
97 
98 @Composable
PlatformIconButtonnull99 fun PlatformIconButton(
100     onClick: () -> Unit,
101     modifier: Modifier = Modifier,
102     enabled: Boolean = true,
103     colors: IconButtonColors = iconButtonColors(),
104     shape: Shape = IconButtonDefaults.standardShape,
105     @DrawableRes iconResource: Int,
106     contentDescription: String?,
107 ) {
108     IconButton(
109         modifier = modifier,
110         onClick = onClick,
111         enabled = enabled,
112         colors = colors,
113         shape = shape,
114     ) {
115         Icon(
116             painter = painterResource(id = iconResource),
117             contentDescription = contentDescription,
118             tint = colors.contentColor,
119         )
120     }
121 }
122 
123 private val ButtonPaddings = PaddingValues(horizontal = 16.dp, vertical = 8.dp)
124 
125 @Composable
filledButtonColorsnull126 private fun filledButtonColors(): ButtonColors {
127     val colors = MaterialTheme.colorScheme
128     return ButtonDefaults.buttonColors(
129         containerColor = colors.primary,
130         contentColor = colors.onPrimary,
131     )
132 }
133 
134 @Composable
outlineButtonColorsnull135 private fun outlineButtonColors(): ButtonColors {
136     return ButtonDefaults.outlinedButtonColors(contentColor = MaterialTheme.colorScheme.onSurface)
137 }
138 
139 @Composable
iconButtonColorsnull140 private fun iconButtonColors(): IconButtonColors {
141     return IconButtonDefaults.filledIconButtonColors(
142         contentColor = MaterialTheme.colorScheme.onSurface
143     )
144 }
145 
146 @Composable
outlineButtonBordernull147 private fun outlineButtonBorder(): BorderStroke {
148     return BorderStroke(width = 1.dp, color = MaterialTheme.colorScheme.primary)
149 }
150 
151 @Composable
textButtonColorsnull152 private fun textButtonColors(): ButtonColors {
153     return ButtonDefaults.textButtonColors(contentColor = MaterialTheme.colorScheme.primary)
154 }
155