Skip to content

Commit 7859c67

Browse files
committed
Use 16.dp widget padding for text layouts
For textual layouts that don't have an icon or container use larger padding so that text isn't near the corners.
1 parent 7d5906f commit 7859c67

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

samples/user-interface/appwidgets/src/main/java/com/example/platform/ui/appwidgets/glance/layout/text/data/FakeLongTextRespository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class FakeLongTextRepository {
7777
),
7878
LongTextLayoutData(
7979
key = "item 3",
80-
text = "Tellus orci ac auctor augue mauris augue neque gravida in fermentum et sollicitudin ac orci...",
80+
text = "Tellus orci ac auctor augue mauris augue neque gravida in fermentum et sollicitudin ac orci",
8181
caption = "Amet cursus"
8282
),
8383
LongTextLayoutData(

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import androidx.glance.layout.padding
4040
import androidx.glance.text.Text
4141
import androidx.glance.text.TextStyle
4242
import com.example.platform.ui.appwidgets.glance.layout.text.layout.LongTextLayoutDimensions.captionFontSizeAndMaxLines
43-
import com.example.platform.ui.appwidgets.glance.layout.text.layout.LongTextLayoutDimensions.contentPadding
43+
import com.example.platform.ui.appwidgets.glance.layout.text.layout.LongTextLayoutDimensions.widgetPadding
4444
import com.example.platform.ui.appwidgets.glance.layout.text.layout.LongTextLayoutDimensions.primaryTextFontSizeAndMaxLines
4545
import com.example.platform.ui.appwidgets.glance.layout.utils.FontUtils.calculateFontSizeAndMaxLines
4646

@@ -78,15 +78,16 @@ fun LongTextLayout(
7878
val scaffoldTopPadding = if (showTitleBar) {
7979
0.dp
8080
} else {
81-
contentPadding
81+
widgetPadding
8282
}
8383

8484
Scaffold(
8585
backgroundColor = GlanceTheme.colors.widgetBackground,
86+
horizontalPadding = widgetPadding,
8687
modifier = GlanceModifier
8788
.maybeClickable(action)
8889
.padding(
89-
bottom = contentPadding,
90+
bottom = widgetPadding,
9091
top = scaffoldTopPadding
9192
),
9293
titleBar = {
@@ -217,7 +218,7 @@ data class LongTextLayoutData(
217218
)
218219

219220
private object LongTextLayoutDimensions {
220-
val contentPadding = 12.dp
221+
val widgetPadding = 16.dp
221222
private val titleBarHeight: Dp
222223
@Composable get() = if (LongTextLayoutSize.fromLocalSize() == LongTextLayoutSize.XSmall) {
223224
0.dp
@@ -231,8 +232,8 @@ private object LongTextLayoutDimensions {
231232
val size = LocalSize.current
232233

233234
return DpSize(
234-
width = size.width - (2 * contentPadding),
235-
height = size.height - contentPadding - titleBarHeight
235+
width = size.width - (2 * widgetPadding),
236+
height = size.height - widgetPadding - titleBarHeight
236237
)
237238
}
238239

@@ -252,7 +253,7 @@ private object LongTextLayoutDimensions {
252253
// Primary text and caption share 70:30 height within the area available for texts.
253254
val availableHeightForPrimaryText = Dp(0.70f * contentSize.height.value)
254255
// In this layout, texts take up entire horizontal space except the paddings on the sides.
255-
val availableWidthForPrimaryText = size.width - (contentPadding * 2)
256+
val availableWidthForPrimaryText = size.width - (widgetPadding * 2)
256257

257258
return calculateFontSizeAndMaxLines(
258259
context = LocalContext.current,

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ fun TextWithImageLayout(
121121

122122
Scaffold(
123123
titleBar = titleBar,
124+
horizontalPadding = widgetPadding,
124125
backgroundColor = GlanceTheme.colors.widgetBackground,
125126
modifier = GlanceModifier
126127
.maybeClickable(action)
@@ -134,14 +135,28 @@ fun TextWithImageLayout(
134135
else -> {
135136
when (layoutSize) {
136137
VerticalSmall -> VerticalContent(
138+
data = data,
139+
showImage = true,
140+
showSecondaryText = false
141+
)
142+
143+
VerticalLarge -> VerticalContent(
144+
data = data,
145+
showImage = true,
146+
showSecondaryText = true
147+
)
148+
149+
HorizontalSmall -> HorizontalContent(
137150
data = data,
138151
showImage = false,
139152
showSecondaryText = false
140153
)
141154

142-
VerticalLarge -> VerticalContent(data = data, showImage = true, showSecondaryText = true)
143-
HorizontalSmall -> HorizontalContent(data = data, showImage = false)
144-
HorizontalLarge -> HorizontalContent(data = data, showImage = true)
155+
HorizontalLarge -> HorizontalContent(
156+
data = data,
157+
showImage = true,
158+
showSecondaryText = true
159+
)
145160
}
146161
}
147162
}
@@ -164,22 +179,27 @@ private fun NoDataContent() {
164179
}
165180

166181
@Composable
167-
private fun HorizontalContent(data: TextWithImageData, showImage: Boolean) {
182+
private fun HorizontalContent(
183+
data: TextWithImageData,
184+
showImage: Boolean,
185+
showSecondaryText: Boolean,
186+
) {
168187
val contentWidth = contentSize.width - contentSpacing
169-
188+
val contentHeight = contentSize.height - (2 * verticalTextsSpacing)
170189
Row(
171190
verticalAlignment = Alignment.Vertical.Bottom,
172191
horizontalAlignment = Alignment.Horizontal.Start,
173192
modifier = GlanceModifier.fillMaxSize()
174193
) {
175194
TextStack(
176195
data = data.textData,
196+
showSecondaryText = showSecondaryText,
177197
modifier = GlanceModifier.fillMaxHeight().defaultWeight(),
178198
availableSize = DpSize(
179199
// Use 40% of available width for text area if showing image on side.
180200
width = (0.4 * contentWidth).takeIf { showImage } ?: contentWidth,
181201
// Use 80% of vertical space for text area.
182-
height = 0.80 * (contentSize.height - (2 * verticalTextsSpacing))
202+
height = (0.80 * contentHeight).takeIf { showImage } ?: contentHeight
183203
)
184204
)
185205
if (showImage) {
@@ -231,7 +251,7 @@ private fun TextStack(
231251
data: TextData,
232252
modifier: GlanceModifier,
233253
availableSize: DpSize,
234-
showSecondaryText: Boolean = true,
254+
showSecondaryText: Boolean,
235255
) {
236256
val (primaryTextFontSize, primaryTextMaxLines) = primaryTextFontValues(
237257
text = data.primary,
@@ -343,10 +363,10 @@ data class ImageData(
343363
)
344364

345365
private enum class TextWithImageLayoutSize {
346-
// No title bar
366+
// No title bar or secondary text
347367
HorizontalSmall,
348368

349-
// No title bar
369+
// No title bar or secondary text
350370
VerticalSmall,
351371

352372
// Text with Image on the side
@@ -361,7 +381,7 @@ private enum class TextWithImageLayoutSize {
361381
val size = LocalSize.current
362382
val isTall = size.height >= size.width
363383

364-
return if (isTall && size.height <= 165.dp) {
384+
return if (isTall && size.height <= 300.dp) {
365385
VerticalSmall
366386
} else if (isTall) {
367387
VerticalLarge
@@ -401,8 +421,8 @@ private object TextWithImageLayoutTextStyles {
401421
showSecondaryText: Boolean,
402422
): Pair<TextUnit, Int> {
403423
val availableHeight = if (showSecondaryText) {
404-
// Within the text area, 35% space is used by primary text.
405-
0.35 * availableSize.height
424+
// Within the text area, 30% space is used by primary text.
425+
0.30 * availableSize.height
406426
} else {
407427
0.60 * availableSize.height
408428
}
@@ -423,8 +443,8 @@ private object TextWithImageLayoutTextStyles {
423443
context = LocalContext.current,
424444
text = text,
425445
availableWidth = availableSize.width,
426-
// Within the text area, 35% space is used by secondary text.
427-
availableHeight = 0.35 * availableSize.height,
446+
// Within the text area, 25% space is used by secondary text.
447+
availableHeight = 0.25 * availableSize.height,
428448
maxFontSize = 24.sp,
429449
minFontSize = 12.sp
430450
)
@@ -433,7 +453,7 @@ private object TextWithImageLayoutTextStyles {
433453

434454
private object TextWithImageLayoutDimensions {
435455
/** Padding that visually appears between the widget outline and anything inside. */
436-
val widgetPadding = 12.dp
456+
val widgetPadding = 16.dp
437457

438458
/** Corner radius to be applied to an image. */
439459
val pictureRadius = 16.dp

0 commit comments

Comments
 (0)