1717package com.example.platform.ui.appwidgets.glance
1818
1919
20- import android.content.res.Resources
2120import android.os.Build
2221import androidx.annotation.StringRes
2322import androidx.compose.runtime.Composable
23+ import androidx.compose.ui.unit.Dp
2424import androidx.compose.ui.unit.dp
2525import androidx.glance.GlanceModifier
26- import androidx.glance.GlanceTheme
2726import androidx.glance.LocalContext
28- import androidx.glance.appwidget.appWidgetBackground
27+ import androidx.glance.appwidget.components.Scaffold
2928import androidx.glance.appwidget.cornerRadius
30- import androidx.glance.background
3129import androidx.glance.layout.Alignment
3230import androidx.glance.layout.Box
3331import androidx.glance.layout.Column
@@ -36,25 +34,35 @@ import androidx.glance.layout.fillMaxSize
3634import androidx.glance.layout.padding
3735
3836/* *
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.
4141 */
4242@Composable
4343fun AppWidgetBox (
4444 modifier : GlanceModifier = GlanceModifier ,
4545 contentAlignment : Alignment = Alignment .TopStart ,
4646 content : @Composable () -> Unit ,
4747) {
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+ }
5359}
5460
5561/* *
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.
5866 */
5967@Composable
6068fun AppWidgetColumn (
@@ -63,40 +71,41 @@ fun AppWidgetColumn(
6371 horizontalAlignment : Alignment .Horizontal = Alignment .Start ,
6472 content : @Composable ColumnScope .() -> Unit ,
6573) {
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+ )
8684 }
87- return cornerRadius(16 .dp)
8885}
8986
87+ /* *
88+ * Applies corner radius for views that are visually positioned [widgetPadding]dp inside of the
89+ * widget background.
90+ */
91+ @Composable
9092fun 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
93102 }
94- return cornerRadius(8 .dp )
103+ return this . cornerRadius(Dp (widgetBackgroundRadiusDpValue - widgetPadding.value) )
95104}
96105
97106@Composable
98107fun stringResource (@StringRes id : Int , vararg args : Any ): String {
99108 return LocalContext .current.getString(id, args)
100109}
101110
102- val Float .toPx get() = this * Resources .getSystem().displayMetrics.density
111+ val widgetPadding = 12 .dp
0 commit comments