Skip to content

Commit bc9a162

Browse files
committed
Update Glance weather widget to use Scaffold.
- The Scaffold component in glance handles the background color, widget radius for you. - For the inner content, calculate widget radius based on the padding and system radius.
1 parent 66fde1c commit bc9a162

File tree

2 files changed

+49
-44
lines changed

2 files changed

+49
-44
lines changed

samples/user-interface/appwidgets/src/main/java/com/example/platform/ui/appwidgets/glance/GlanceKtx.kt

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@
1717
package com.example.platform.ui.appwidgets.glance
1818

1919

20-
import android.content.res.Resources
2120
import android.os.Build
2221
import androidx.annotation.StringRes
2322
import androidx.compose.runtime.Composable
23+
import androidx.compose.ui.unit.Dp
2424
import androidx.compose.ui.unit.dp
2525
import androidx.glance.GlanceModifier
26-
import androidx.glance.GlanceTheme
2726
import androidx.glance.LocalContext
28-
import androidx.glance.appwidget.appWidgetBackground
27+
import androidx.glance.appwidget.components.Scaffold
2928
import androidx.glance.appwidget.cornerRadius
30-
import androidx.glance.background
3129
import androidx.glance.layout.Alignment
3230
import androidx.glance.layout.Box
3331
import androidx.glance.layout.Column
@@ -36,25 +34,35 @@ import androidx.glance.layout.fillMaxSize
3634
import 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
4343
fun 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
6068
fun 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
9092
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
93102
}
94-
return cornerRadius(8.dp)
103+
return this.cornerRadius(Dp(widgetBackgroundRadiusDpValue - widgetPadding.value))
95104
}
96105

97106
@Composable
98107
fun 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

samples/user-interface/appwidgets/src/main/java/com/example/platform/ui/appwidgets/glance/weather/WeatherGlanceWidget.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,15 +335,11 @@ fun HourForecast(
335335
}
336336

337337
@Composable
338-
fun DailyForecast(
339-
weatherInfo: WeatherInfo.Available,
340-
modifier: GlanceModifier = GlanceModifier,
341-
) {
338+
fun DailyForecast(weatherInfo: WeatherInfo.Available) {
342339
LazyColumn(
343340
modifier = GlanceModifier
344341
.background(GlanceTheme.colors.surfaceVariant)
345-
.appWidgetInnerCornerRadius()
346-
.then(modifier),
342+
.appWidgetInnerCornerRadius(),
347343
) {
348344
items(weatherInfo.dailyForecast) { dayForecast ->
349345
Row(

0 commit comments

Comments
 (0)