• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.permissioncontroller.permission.ui.wear.elements
18 
19 import androidx.annotation.DrawableRes
20 import androidx.compose.foundation.layout.size
21 import androidx.compose.runtime.Composable
22 import androidx.compose.ui.Alignment
23 import androidx.compose.ui.Modifier
24 import androidx.compose.ui.graphics.vector.ImageVector
25 import androidx.compose.ui.unit.Dp
26 import androidx.wear.compose.material.Button
27 import androidx.wear.compose.material.ButtonColors
28 import androidx.wear.compose.material.ButtonDefaults
29 import androidx.wear.compose.material.ButtonDefaults.DefaultButtonSize
30 import androidx.wear.compose.material.ButtonDefaults.DefaultIconSize
31 import androidx.wear.compose.material.ButtonDefaults.LargeButtonSize
32 import androidx.wear.compose.material.ButtonDefaults.LargeIconSize
33 import androidx.wear.compose.material.ButtonDefaults.SmallButtonSize
34 import androidx.wear.compose.material.ButtonDefaults.SmallIconSize
35 
36 /**
37  * This component is an alternative to [Button], providing the following:
38  * - a convenient way of providing an icon and choosing its size from a range of sizes recommended
39  *   by the Wear guidelines;
40  */
41 @Composable
Buttonnull42 public fun Button(
43     imageVector: ImageVector,
44     contentDescription: String,
45     onClick: () -> Unit,
46     modifier: Modifier = Modifier,
47     colors: ButtonColors = ButtonDefaults.primaryButtonColors(),
48     buttonSize: ButtonSize = ButtonSize.Default,
49     iconRtlMode: IconRtlMode = IconRtlMode.Default,
50     enabled: Boolean = true
51 ) {
52     Button(
53         icon = imageVector,
54         contentDescription = contentDescription,
55         onClick = onClick,
56         modifier = modifier,
57         colors = colors,
58         buttonSize = buttonSize,
59         iconRtlMode = iconRtlMode,
60         enabled = enabled
61     )
62 }
63 
64 /**
65  * This component is an alternative to [Button], providing the following:
66  * - a convenient way of providing an icon and choosing its size from a range of sizes recommended
67  *   by the Wear guidelines;
68  */
69 @Composable
Buttonnull70 public fun Button(
71     @DrawableRes id: Int,
72     contentDescription: String,
73     onClick: () -> Unit,
74     modifier: Modifier = Modifier,
75     colors: ButtonColors = ButtonDefaults.primaryButtonColors(),
76     buttonSize: ButtonSize = ButtonSize.Default,
77     iconRtlMode: IconRtlMode = IconRtlMode.Default,
78     enabled: Boolean = true
79 ) {
80     Button(
81         icon = id,
82         contentDescription = contentDescription,
83         onClick = onClick,
84         modifier = modifier,
85         colors = colors,
86         buttonSize = buttonSize,
87         iconRtlMode = iconRtlMode,
88         enabled = enabled
89     )
90 }
91 
92 @Composable
Buttonnull93 internal fun Button(
94     icon: Any,
95     contentDescription: String,
96     onClick: () -> Unit,
97     modifier: Modifier = Modifier,
98     colors: ButtonColors = ButtonDefaults.primaryButtonColors(),
99     buttonSize: ButtonSize = ButtonSize.Default,
100     iconRtlMode: IconRtlMode = IconRtlMode.Default,
101     enabled: Boolean = true
102 ) {
103     Button(
104         onClick = onClick,
105         modifier = modifier.size(buttonSize.tapTargetSize),
106         enabled = enabled,
107         colors = colors
108     ) {
109         val iconModifier = Modifier.size(buttonSize.iconSize).align(Alignment.Center)
110 
111         Icon(
112             icon = icon,
113             contentDescription = contentDescription,
114             modifier = iconModifier,
115             rtlMode = iconRtlMode
116         )
117     }
118 }
119 
120 public sealed class ButtonSize(public val iconSize: Dp, public val tapTargetSize: Dp) {
121     public object Default :
122         ButtonSize(iconSize = DefaultIconSize, tapTargetSize = DefaultButtonSize)
123 
124     public object Large : ButtonSize(iconSize = LargeIconSize, tapTargetSize = LargeButtonSize)
125     public object Small : ButtonSize(iconSize = SmallIconSize, tapTargetSize = SmallButtonSize)
126 
127     /**
128      * Custom sizes should follow the
129      * [accessibility principles and guidance for touch targets](https://developer.android.com/training/wearables/accessibility#set-minimum).
130      */
131     public data class Custom(val customIconSize: Dp, val customTapTargetSize: Dp) :
132         ButtonSize(iconSize = customIconSize, tapTargetSize = customTapTargetSize)
133 }
134