Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid modifying constraints when a scale factor can't be computed #5264

Merged
merged 2 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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