Skip to content

Commit

Permalink
Don't fill the available space in AsyncImage when the painter has no …
Browse files Browse the repository at this point in the history
…child painter. (#2578)

* Don't fill the available space in AsyncImage when the painter has no intrinsic size.

* Fix and add regression test.
  • Loading branch information
colinrtwhite authored Oct 28, 2024
1 parent 1d4b9a8 commit b2c1aa3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,33 @@ class AsyncImageTest {
assertEquals(1, requestTracker.finishedRequests)
}

@Test
fun zeroHeightWhenAsyncImageIsEmpty() {
composeTestRule.setContent {
Column(Modifier.fillMaxSize()) {
AsyncImage(
model = ImageRequest.Builder(context)
.data(Unit)
// Skip waiting for the constraints which will always be empty.
.size(100)
.build(),
contentDescription = null,
imageLoader = imageLoader,
contentScale = ContentScale.FillWidth,
modifier = Modifier
.fillMaxWidth()
.testTag(Image),
)
}
}

waitForRequestComplete()

composeTestRule.onNodeWithTag(Image)
.assertWidthIsEqualTo(context.resources.displayMetrics.widthPixels.toDp())
.assertHeightIsEqualTo(0.dp)
}

private fun waitForRequestComplete(finishedRequests: Int = 1) = waitUntil {
requestTracker.finishedRequests >= finishedRequests
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.constrainHeight
import androidx.compose.ui.unit.constrainWidth
import coil3.compose.AsyncImage
import coil3.compose.AsyncImagePainter
import coil3.compose.SubcomposeAsyncImage
import kotlin.math.max
import kotlin.math.roundToInt
Expand Down Expand Up @@ -183,16 +184,21 @@ internal class ContentPainterNode(
}

// Fill the available space if the painter has no intrinsic size.
val painter = painter
val hasBoundedSize = constraints.hasBoundedWidth && constraints.hasBoundedHeight
val intrinsicSize = painter.intrinsicSize
if (intrinsicSize.isUnspecified) {
if (hasBoundedSize) {
// Changed from `PainterModifier`:
// If AsyncImagePainter has no child painter, do not occupy the max constraints.
if (!hasBoundedSize ||
(painter is AsyncImagePainter && painter.state.value.painter == null)
) {
return constraints
} else {
return constraints.copy(
minWidth = constraints.maxWidth,
minHeight = constraints.maxHeight,
)
} else {
return constraints
}
}

Expand Down

0 comments on commit b2c1aa3

Please sign in to comment.