• 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.height
25 import androidx.compose.foundation.layout.padding
26 import androidx.compose.material3.ButtonColors
27 import androidx.compose.material3.ButtonDefaults
28 import androidx.compose.material3.Icon
29 import androidx.compose.material3.IconButton
30 import androidx.compose.material3.IconButtonColors
31 import androidx.compose.material3.IconButtonDefaults
32 import androidx.compose.runtime.Composable
33 import androidx.compose.ui.Modifier
34 import androidx.compose.ui.res.painterResource
35 import androidx.compose.ui.unit.Dp
36 import androidx.compose.ui.unit.dp
37 import com.android.compose.theme.LocalAndroidColorScheme
38 
39 @Composable
PlatformButtonnull40 fun PlatformButton(
41     onClick: () -> Unit,
42     modifier: Modifier = Modifier,
43     enabled: Boolean = true,
44     colors: ButtonColors = filledButtonColors(),
45     verticalPadding: Dp = DefaultPlatformButtonVerticalPadding,
46     content: @Composable RowScope.() -> Unit,
47 ) {
48     androidx.compose.material3.Button(
49         modifier = modifier.padding(vertical = verticalPadding).height(36.dp),
50         colors = colors,
51         contentPadding = ButtonPaddings,
52         onClick = onClick,
53         enabled = enabled,
54     ) {
55         content()
56     }
57 }
58 
59 @Composable
PlatformOutlinedButtonnull60 fun PlatformOutlinedButton(
61     onClick: () -> Unit,
62     modifier: Modifier = Modifier,
63     enabled: Boolean = true,
64     colors: ButtonColors = outlineButtonColors(),
65     border: BorderStroke? = outlineButtonBorder(),
66     verticalPadding: Dp = DefaultPlatformButtonVerticalPadding,
67     content: @Composable RowScope.() -> Unit,
68 ) {
69     androidx.compose.material3.OutlinedButton(
70         modifier = modifier.padding(vertical = verticalPadding).height(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     @DrawableRes iconResource: Int,
105     contentDescription: String?,
106 ) {
107     IconButton(
108         modifier = modifier,
109         onClick = onClick,
110         enabled = enabled,
111         colors = colors,
112     ) {
113         Icon(
114             painter = painterResource(id = iconResource),
115             contentDescription = contentDescription,
116             tint = colors.contentColor,
117         )
118     }
119 }
120 
121 private val DefaultPlatformButtonVerticalPadding = 6.dp
122 private val ButtonPaddings = PaddingValues(horizontal = 16.dp, vertical = 8.dp)
123 
124 @Composable
filledButtonColorsnull125 private fun filledButtonColors(): ButtonColors {
126     val colors = LocalAndroidColorScheme.current
127     return ButtonDefaults.buttonColors(
128         containerColor = colors.primary,
129         contentColor = colors.onPrimary,
130     )
131 }
132 
133 @Composable
outlineButtonColorsnull134 private fun outlineButtonColors(): ButtonColors {
135     return ButtonDefaults.outlinedButtonColors(
136         contentColor = LocalAndroidColorScheme.current.onSurface,
137     )
138 }
139 
140 @Composable
iconButtonColorsnull141 private fun iconButtonColors(): IconButtonColors {
142     return IconButtonDefaults.filledIconButtonColors(
143         contentColor = LocalAndroidColorScheme.current.onSurface,
144     )
145 }
146 
147 @Composable
outlineButtonBordernull148 private fun outlineButtonBorder(): BorderStroke {
149     return BorderStroke(
150         width = 1.dp,
151         color = LocalAndroidColorScheme.current.primary,
152     )
153 }
154 
155 @Composable
textButtonColorsnull156 private fun textButtonColors(): ButtonColors {
157     return ButtonDefaults.textButtonColors(contentColor = LocalAndroidColorScheme.current.primary)
158 }
159