Skip to content

Commit

Permalink
Avoid modifying constraints when a scale factor can't be computed
Browse files Browse the repository at this point in the history
This will happen if either width or height (or both) are constrained to
have a 0 size.

We already avoid loading the image via AsyncImageSize /
inferredGlideSize. This only impacts layout.

Fixes #5256
  • Loading branch information
sjudd committed Aug 24, 2023
1 parent 4db0b30 commit af0d631
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import android.graphics.drawable.Drawable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.assert
Expand Down Expand Up @@ -372,4 +377,18 @@ class GlideImageTest {
.assert(expectDisplayedDrawable(drawable))
}
}

// See #5256
@Test
fun glideImage_withZeroSize_doesNotCrash() {
glideComposeRule.setContent {
GlideImage(
model = android.R.drawable.star_big_on,
contentDescription = null,
modifier = Modifier.width(IntrinsicSize.Min),
contentScale = ContentScale.Crop
)
}
glideComposeRule.waitForIdle()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasureResult
import androidx.compose.ui.layout.MeasureScope
import androidx.compose.ui.layout.ScaleFactor
import androidx.compose.ui.layout.times
import androidx.compose.ui.node.DrawModifierNode
import androidx.compose.ui.node.LayoutModifierNode
Expand Down Expand Up @@ -477,10 +478,13 @@ internal class GlideNode : DrawModifierNode, LayoutModifierNode, SemanticsModifi
val constrainedHeight = constraints.constrainHeight(intrinsicHeight)

val srcSize = Size(intrinsicWidth.toFloat(), intrinsicHeight.toFloat())
val scaledSize =
srcSize * contentScale.computeScaleFactor(
val scaleFactor = contentScale.computeScaleFactor(
srcSize, Size(constrainedWidth.toFloat(), constrainedHeight.toFloat())
)
if (scaleFactor == ScaleFactor.Unspecified) {
return constraints
}
val scaledSize = srcSize * scaleFactor

val minWidth = constraints.constrainWidth(scaledSize.width.roundToInt())
val minHeight = constraints.constrainHeight(scaledSize.height.roundToInt())
Expand Down

0 comments on commit af0d631

Please sign in to comment.