1
+ /*
2
+ * Copyright 2024 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
+ package com.example.platform.ui.appwidgets.glance.layout.toolbars.layout
17
+
18
+ import androidx.compose.runtime.Composable
19
+ import androidx.compose.ui.unit.Dp
20
+ import androidx.compose.ui.unit.dp
21
+ import androidx.glance.ColorFilter
22
+ import androidx.glance.GlanceModifier
23
+ import androidx.glance.GlanceTheme
24
+ import androidx.glance.Image
25
+ import androidx.glance.ImageProvider
26
+ import androidx.glance.action.Action
27
+ import androidx.glance.action.clickable
28
+ import androidx.glance.appwidget.cornerRadius
29
+ import androidx.glance.background
30
+ import androidx.glance.layout.Alignment
31
+ import androidx.glance.layout.Box
32
+ import androidx.glance.layout.height
33
+ import androidx.glance.layout.size
34
+ import androidx.glance.layout.width
35
+ import androidx.glance.semantics.contentDescription
36
+ import androidx.glance.semantics.semantics
37
+ import androidx.glance.unit.ColorProvider
38
+
39
+ /* *
40
+ * A rectangular button displaying the provided icon on a background of
41
+ * provided corner radius and colors.
42
+ *
43
+ * @param imageProvider icon to be displayed at center of the button
44
+ * @param onClick [Action] to be performed on click of button
45
+ * @param roundedCornerShape type of rounding to be applied to the button
46
+ * @param contentDescription description about the button that can be used by the accessibility
47
+ * services
48
+ * @param iconSize size of the icon displayed at center of the button
49
+ * @param modifier the modifier to be applied to this button.
50
+ * @param backgroundColor background color for the button
51
+ * @param contentColor color of the icon displayed at center of the button
52
+ */
53
+ @Composable
54
+ fun RectangularIconButton (
55
+ imageProvider : ImageProvider ,
56
+ onClick : Action ,
57
+ roundedCornerShape : RoundedCornerShape ,
58
+ contentDescription : String ,
59
+ iconSize : Dp ,
60
+ modifier : GlanceModifier ,
61
+ backgroundColor : ColorProvider = GlanceTheme .colors.primary,
62
+ contentColor : ColorProvider = GlanceTheme .colors.onPrimary,
63
+ ) {
64
+ Box (
65
+ contentAlignment = Alignment .Center ,
66
+ modifier = modifier
67
+ .background(backgroundColor)
68
+ .cornerRadius(roundedCornerShape.cornerRadius)
69
+ .semantics { this .contentDescription = contentDescription }
70
+ .clickable(onClick)
71
+ ) {
72
+ Image (
73
+ provider = imageProvider,
74
+ contentDescription = null ,
75
+ colorFilter = ColorFilter .tint(contentColor),
76
+ modifier = GlanceModifier .size(iconSize)
77
+ )
78
+ }
79
+ }
80
+
81
+ /* *
82
+ * A fixed height pill-shaped button meant to be displayed in a title bar.
83
+ *
84
+ * @param iconImageProvider icon to be displayed in the button
85
+ * @param iconSize size of the icon displayed at center of the button
86
+ * @param backgroundColor background color for the button
87
+ * @param contentColor color of the icon displayed in the button
88
+ * @param contentDescription description about the button that can be used by the accessibility
89
+ * services
90
+ * @param onClick [Action] to be performed on click of button
91
+ * @param modifier the modifier to be applied to this button.
92
+ */
93
+ @Composable
94
+ fun PillShapedButton (
95
+ iconImageProvider : ImageProvider ,
96
+ iconSize : Dp ,
97
+ backgroundColor : ColorProvider ,
98
+ contentColor : ColorProvider ,
99
+ contentDescription : String ,
100
+ onClick : Action ,
101
+ modifier : GlanceModifier ,
102
+ ) {
103
+ Box ( // A clickable transparent outer container
104
+ contentAlignment = Alignment .Center ,
105
+ modifier = modifier
106
+ .semantics { this .contentDescription = contentDescription }
107
+ .height(48 .dp)
108
+ .clickable(onClick),
109
+ ) {
110
+ Box ( // A filled background with smaller height
111
+ contentAlignment = Alignment .Center ,
112
+ modifier = GlanceModifier
113
+ .width(52 .dp)
114
+ .height(32 .dp)
115
+ .background(backgroundColor)
116
+ .cornerRadius(RoundedCornerShape .FULL .cornerRadius)
117
+ ) { // The icon.
118
+ Image (
119
+ provider = iconImageProvider,
120
+ contentDescription = null ,
121
+ colorFilter = ColorFilter .tint(contentColor),
122
+ modifier = GlanceModifier .size(iconSize)
123
+ )
124
+ }
125
+ }
126
+ }
127
+
128
+ /* *
129
+ * Defines the roundness of a shape inline with the tokens used in M3
130
+ * https://m3.material.io/styles/shape/shape-scale-tokens
131
+ */
132
+ enum class RoundedCornerShape (val cornerRadius : Dp ) {
133
+ FULL (100 .dp),
134
+ MEDIUM (16 .dp),
135
+ }
0 commit comments