Skip to content
Open
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 @@ -77,6 +77,19 @@ internal fun calculateBackgroundImageSize(
finalWidth = finalHeight * ratio
}
}
} else if (
keepsRatio &&
(backgroundSize is BackgroundSize.Cover || backgroundSize is BackgroundSize.Contain)
) {
xAuto = false
yAuto = false
val widthScale = containerWidth / imageWidth
val heightScale = containerHeight / imageHeight
val scale =
if (backgroundSize is BackgroundSize.Cover) maxOf(widthScale, heightScale)
else minOf(widthScale, heightScale)
finalWidth = imageWidth * scale
finalHeight = imageHeight * scale
}

if (repeat?.x == BackgroundRepeatKeyword.Round && finalWidth > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import com.facebook.common.logging.FLog
import com.facebook.common.references.CloseableReference
import com.facebook.datasource.BaseDataSubscriber
import com.facebook.datasource.DataSource
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.components.DeferredReleaser
import com.facebook.fresco.middleware.HasExtraData
import com.facebook.imagepipeline.common.ImageDecodeOptions
import com.facebook.imagepipeline.core.ImagePipelineFactory
import com.facebook.imagepipeline.image.CloseableImage
import com.facebook.imagepipeline.image.CloseableStaticBitmap
import com.facebook.imagepipeline.request.ImageRequestBuilder
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.common.ReactConstants
import com.facebook.react.uimanager.style.BackgroundImageLayer
import com.facebook.react.views.imagehelper.ImageSource
Expand Down Expand Up @@ -115,8 +115,18 @@ internal class BackgroundImageURLLoader(
ImageDecodeOptions.newBuilder().setForceStaticImage(true).build()
)
}
val imageRequest =
try {
requestBuilder.build()
} catch (e: RuntimeException) {
// `build()` validates the uri, e.g. a `res:` uri must name a resource id.
FLog.w(ReactConstants.TAG, e, "Invalid background image uri: %s", uri)
return
}
val dataSource =
Fresco.getImagePipeline().fetchDecodedImage(requestBuilder.build(), CALLER_CONTEXT)
ImagePipelineFactory.getInstance()
.imagePipeline
.fetchDecodedImage(imageRequest, CALLER_CONTEXT)
this.dataSource = dataSource
dataSource.subscribe(
object : BaseDataSubscriber<CloseableReference<CloseableImage>>() {
Expand Down Expand Up @@ -181,9 +191,7 @@ internal class BackgroundImageURLLoader(
this.imageReference = null
// The last frame drawn can still use the bitmap, so release it after that frame, like
// Fresco's drawee.
DeferredReleaser.getInstance().scheduleDeferredRelease {
CloseableReference.closeSafely(imageReference)
}
UiThreadUtil.runOnUiThread { CloseableReference.closeSafely(imageReference) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ internal class BackgroundSizeLengthPercentage(
/**
* Sealed class representing CSS background-size property values.
*
* This class models the different ways a background size can be specified in CSS, currently
* supporting length/percentage/auto values for both dimensions.
* This class models the different ways a background size can be specified in CSS: length,
* percentage, or auto values for both dimensions, or the `cover` and `contain` keywords.
*
* @see BackgroundSizeLengthPercentage
*/
Expand All @@ -124,11 +124,17 @@ internal sealed class BackgroundSize {
class LengthPercentageAuto(val lengthPercentage: BackgroundSizeLengthPercentage) :
BackgroundSize()

/** Scales an image that has an aspect ratio to cover the whole background area. */
object Cover : BackgroundSize()

/** Scales an image that has an aspect ratio to fit inside the background area. */
object Contain : BackgroundSize()

companion object {
/**
* Parses a Dynamic value into a BackgroundSize.
*
* Currently supports map values containing x/y dimensions.
* Supports map values containing x/y dimensions, and the `cover` and `contain` keywords.
*
* @param backgroundSizeValue The dynamic value to parse
* @return A BackgroundSize instance, or null if parsing fails
Expand All @@ -146,6 +152,12 @@ internal sealed class BackgroundSize {
null
}
}
ReadableType.String ->
when (backgroundSizeValue.asString()) {
"cover" -> Cover
"contain" -> Contain
else -> null
}
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ package com.facebook.react.uimanager.drawable

import com.facebook.react.uimanager.LengthPercentage
import com.facebook.react.uimanager.LengthPercentageType
import com.facebook.react.uimanager.style.BackgroundRepeat
import com.facebook.react.uimanager.style.BackgroundRepeatKeyword
import com.facebook.react.uimanager.style.BackgroundSize
import com.facebook.react.uimanager.style.BackgroundSizeLengthPercentage
import org.assertj.core.api.Assertions.assertThat
Expand Down Expand Up @@ -44,4 +46,59 @@ class BackgroundImageDrawableTest {

assertThat(result).isEqualTo(50f to 12f)
}

@Test
fun coverFillsTheAreaKeepingTheRatio() {
val result =
calculateBackgroundImageSize(
300f,
200f,
100f,
100f,
BackgroundSize.Cover,
null,
hasIntrinsicRatio = true,
)

assertThat(result).isEqualTo(300f to 300f)
}

@Test
fun containFitsInsideTheAreaKeepingTheRatio() {
val result =
calculateBackgroundImageSize(
300f,
200f,
100f,
100f,
BackgroundSize.Contain,
null,
hasIntrinsicRatio = true,
)

assertThat(result).isEqualTo(200f to 200f)
}

@Test
fun coverWithoutARatioKeepsTheGivenSize() {
val result = calculateBackgroundImageSize(300f, 200f, 300f, 200f, BackgroundSize.Cover, null)

assertThat(result).isEqualTo(300f to 200f)
}

@Test
fun roundOnOneAxisDoesNotRescaleContainOnTheOther() {
val result =
calculateBackgroundImageSize(
300f,
200f,
100f,
100f,
BackgroundSize.Contain,
BackgroundRepeat(BackgroundRepeatKeyword.Round, BackgroundRepeatKeyword.NoRepeat),
hasIntrinsicRatio = true,
)

assertThat(result).isEqualTo(150f to 200f)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
package com.facebook.react.uimanager.drawable

import android.graphics.Bitmap
import com.facebook.imagepipeline.core.ImagePipelineFactory
import com.facebook.react.bridge.JavaOnlyMap
import com.facebook.react.uimanager.style.BackgroundImageLayer
import org.assertj.core.api.Assertions.*
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.GraphicsMode

/** Tests for [BackgroundImageURLLoader.LoadedImage] */
/** Tests for [BackgroundImageURLLoader] */
@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class BackgroundImageURLLoaderTest {
Expand Down Expand Up @@ -50,4 +55,21 @@ class BackgroundImageURLLoaderTest {
assertThat(image.height).isEqualTo(40f)
}
}

@After
fun tearDown() {
ImagePipelineFactory.shutDown()
}

@Test
fun testTreatsInvalidResourceUriAsFailedLoad() {
val context = RuntimeEnvironment.getApplication()
ImagePipelineFactory.initialize(context)
val loader = BackgroundImageURLLoader(context) {}
for (uri in listOf("res:///icon", "res:icon")) {
val layer = BackgroundImageLayer.parse(JavaOnlyMap.of("type", "url", "uri", uri), context)
loader.loadBackgroundImages(listOfNotNull(layer))
assertThat(loader.imageForUri(uri)).isNull()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class BackgroundImageLayerTest {

@Test
fun testRejectsUnknownType() {
assertThat(BackgroundImageLayer.parse(JavaOnlyMap.of("type", "conic-gradient"), context))
assertThat(BackgroundImageLayer.parse(JavaOnlyMap.of("type", "unknown-gradient"), context))
.isNull()
assertThat(BackgroundImageLayer.parse(null, context)).isNull()
}
Expand Down
Loading