Skip to content
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 @@ -32,6 +32,56 @@ import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.round

private fun positionToPixels(lengthPercentage: LengthPercentage, availableSpace: Float): Float =
if (lengthPercentage.type == LengthPercentageType.PERCENT) {
lengthPercentage.resolve(availableSpace)
} else {
lengthPercentage.resolve(availableSpace).dpToPx()
}

internal fun calculateBackgroundImageSize(
containerWidth: Float,
containerHeight: Float,
imageWidth: Float,
imageHeight: Float,
backgroundSize: BackgroundSize?,
repeat: BackgroundRepeat?,
): Pair<Float, Float> {
var finalWidth = imageWidth
var finalHeight = imageHeight

if (backgroundSize is BackgroundSize.LengthPercentageAuto) {
val w = backgroundSize.lengthPercentage.x
val h = backgroundSize.lengthPercentage.y
if (w != null) {
finalWidth = positionToPixels(w, containerWidth)
}
if (h != null) {
finalHeight = positionToPixels(h, containerHeight)
}
}

if (repeat?.x == BackgroundRepeatKeyword.Round && finalWidth > 0) {
if (!FloatUtil.floatsEqual(containerWidth.rem(finalWidth), 0f)) {
val numRepeats = round(containerWidth / finalWidth)
if (numRepeats > 0) {
finalWidth = containerWidth / numRepeats
}
}
}

if (repeat?.y == BackgroundRepeatKeyword.Round && finalHeight > 0) {
if (!FloatUtil.floatsEqual(containerHeight.rem(finalHeight), 0f)) {
val numRepeats = round(containerHeight / finalHeight)
if (numRepeats > 0) {
finalHeight = containerHeight / numRepeats
}
}
}

return finalWidth to finalHeight
}

internal class BackgroundImageDrawable(
private val context: Context,
/*
Expand Down Expand Up @@ -343,54 +393,6 @@ internal class BackgroundImageDrawable(
}
}

private fun positionToPixels(lengthPercentage: LengthPercentage, availableSpace: Float): Float =
if (lengthPercentage.type == LengthPercentageType.PERCENT) {
lengthPercentage.resolve(availableSpace)
} else {
lengthPercentage.resolve(availableSpace).dpToPx()
}

private fun calculateBackgroundImageSize(
containerWidth: Float,
containerHeight: Float,
imageWidth: Float,
imageHeight: Float,
backgroundSize: BackgroundSize?,
repeat: BackgroundRepeat?,
): Pair<Float, Float> {
var finalWidth = imageWidth
var finalHeight = imageHeight

if (backgroundSize is BackgroundSize.LengthPercentageAuto) {
val w = backgroundSize.lengthPercentage.x
val h = backgroundSize.lengthPercentage.y
if (w != null && h != null) {
finalWidth = positionToPixels(w, containerWidth)
finalHeight = positionToPixels(h, containerHeight)
}
}

if (repeat?.x == BackgroundRepeatKeyword.Round && finalWidth > 0) {
if (!FloatUtil.floatsEqual(containerWidth.rem(finalWidth), 0f)) {
val numRepeats = round(containerWidth / finalWidth)
if (numRepeats > 0) {
finalWidth = containerWidth / numRepeats
}
}
}

if (repeat?.y == BackgroundRepeatKeyword.Round && finalHeight > 0) {
if (!FloatUtil.floatsEqual(containerHeight.rem(finalHeight), 0f)) {
val numRepeats = round(containerHeight / finalHeight)
if (numRepeats > 0) {
finalHeight = containerHeight / numRepeats
}
}
}

return finalWidth to finalHeight
}

private fun calculateBackgroundPosition(
tileWidth: Float,
tileHeight: Float,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.uimanager.drawable

import com.facebook.react.uimanager.LengthPercentage
import com.facebook.react.uimanager.LengthPercentageType
import com.facebook.react.uimanager.style.BackgroundSize
import com.facebook.react.uimanager.style.BackgroundSizeLengthPercentage
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class BackgroundImageDrawableTest {
@Test
fun autoYAxisKeepsContainerHeight() {
val size =
BackgroundSize.LengthPercentageAuto(
BackgroundSizeLengthPercentage(
LengthPercentage(10f, LengthPercentageType.PERCENT),
null,
),
)

val result = calculateBackgroundImageSize(200f, 100f, 200f, 100f, size, null)

assertThat(result).isEqualTo(20f to 100f)
}

@Test
fun explicitAxesAreResolvedIndependently() {
val size =
BackgroundSize.LengthPercentageAuto(
BackgroundSizeLengthPercentage(
LengthPercentage(25f, LengthPercentageType.PERCENT),
LengthPercentage(12f, LengthPercentageType.PERCENT),
),
)

val result = calculateBackgroundImageSize(200f, 100f, 200f, 100f, size, null)

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