17
17
package com.example.platform.ui.appwidgets.glance
18
18
19
19
20
- import android.content.res.Resources
21
20
import android.os.Build
22
21
import androidx.annotation.StringRes
23
22
import androidx.compose.runtime.Composable
23
+ import androidx.compose.ui.unit.Dp
24
24
import androidx.compose.ui.unit.dp
25
25
import androidx.glance.GlanceModifier
26
- import androidx.glance.GlanceTheme
27
26
import androidx.glance.LocalContext
28
- import androidx.glance.appwidget.appWidgetBackground
27
+ import androidx.glance.appwidget.components.Scaffold
29
28
import androidx.glance.appwidget.cornerRadius
30
- import androidx.glance.background
31
29
import androidx.glance.layout.Alignment
32
30
import androidx.glance.layout.Box
33
31
import androidx.glance.layout.Column
@@ -36,25 +34,35 @@ import androidx.glance.layout.fillMaxSize
36
34
import androidx.glance.layout.padding
37
35
38
36
/* *
39
- * Provide a Box composable using the system parameters for app widgets background with rounded
40
- * corners and background color.
37
+ * Provide a Box composable that be used as app widget's background
38
+ *
39
+ * Uses the Scaffold component to achieve the recommended background color and rounded corners for
40
+ * the widget.
41
41
*/
42
42
@Composable
43
43
fun AppWidgetBox (
44
44
modifier : GlanceModifier = GlanceModifier ,
45
45
contentAlignment : Alignment = Alignment .TopStart ,
46
46
content : @Composable () -> Unit ,
47
47
) {
48
- Box (
49
- modifier = GlanceModifier .appWidgetBackgroundModifier().then(modifier),
50
- contentAlignment = contentAlignment,
51
- content = content,
52
- )
48
+ Scaffold (
49
+ modifier = GlanceModifier
50
+ .padding(vertical = widgetPadding)
51
+ ) {
52
+ Box (
53
+ modifier = modifier.fillMaxSize(),
54
+ contentAlignment = contentAlignment,
55
+ ) {
56
+ content()
57
+ }
58
+ }
53
59
}
54
60
55
61
/* *
56
- * Provide a Column composable using the system parameters for app widgets background with rounded
57
- * corners and background color.
62
+ * Provide a Column composable that be used as app widget's background
63
+ *
64
+ * Uses the Scaffold component to achieve the recommended background color and rounded corners for
65
+ * the widget.
58
66
*/
59
67
@Composable
60
68
fun AppWidgetColumn (
@@ -63,40 +71,41 @@ fun AppWidgetColumn(
63
71
horizontalAlignment : Alignment .Horizontal = Alignment .Start ,
64
72
content : @Composable ColumnScope .() -> Unit ,
65
73
) {
66
- Column (
67
- modifier = GlanceModifier .appWidgetBackgroundModifier().then(modifier),
68
- verticalAlignment = verticalAlignment,
69
- horizontalAlignment = horizontalAlignment,
70
- content = content,
71
- )
72
- }
73
-
74
- @Composable
75
- fun GlanceModifier.appWidgetBackgroundModifier (): GlanceModifier {
76
- return this .fillMaxSize()
77
- .padding(16 .dp)
78
- .appWidgetBackground()
79
- .background(GlanceTheme .colors.background)
80
- .appWidgetBackgroundCornerRadius()
81
- }
82
-
83
- fun GlanceModifier.appWidgetBackgroundCornerRadius (): GlanceModifier {
84
- if (Build .VERSION .SDK_INT >= 31 ) {
85
- cornerRadius(android.R .dimen.system_app_widget_background_radius)
74
+ Scaffold (
75
+ modifier = GlanceModifier
76
+ .padding(vertical = widgetPadding)
77
+ ) {
78
+ Column (
79
+ modifier = modifier,
80
+ verticalAlignment = verticalAlignment,
81
+ horizontalAlignment = horizontalAlignment,
82
+ content = content,
83
+ )
86
84
}
87
- return cornerRadius(16 .dp)
88
85
}
89
86
87
+ /* *
88
+ * Applies corner radius for views that are visually positioned [widgetPadding]dp inside of the
89
+ * widget background.
90
+ */
91
+ @Composable
90
92
fun GlanceModifier.appWidgetInnerCornerRadius (): GlanceModifier {
91
- if (Build .VERSION .SDK_INT >= 31 ) {
92
- return cornerRadius(android.R .dimen.system_app_widget_inner_radius)
93
+ if (Build .VERSION .SDK_INT < 31 ) {
94
+ return this
95
+ }
96
+ val resources = LocalContext .current.resources
97
+ // get dimension in float (without rounding).
98
+ val px = resources.getDimension(android.R .dimen.system_app_widget_background_radius)
99
+ val widgetBackgroundRadiusDpValue = px / resources.displayMetrics.density
100
+ if (widgetBackgroundRadiusDpValue < widgetPadding.value) {
101
+ return this
93
102
}
94
- return cornerRadius(8 .dp )
103
+ return this . cornerRadius(Dp (widgetBackgroundRadiusDpValue - widgetPadding.value) )
95
104
}
96
105
97
106
@Composable
98
107
fun stringResource (@StringRes id : Int , vararg args : Any ): String {
99
108
return LocalContext .current.getString(id, args)
100
109
}
101
110
102
- val Float .toPx get() = this * Resources .getSystem().displayMetrics.density
111
+ val widgetPadding = 12 .dp
0 commit comments