Skip to content

Commit 94aee19

Browse files
joyhsiaintergalacticspacehighwayclaude
authored
fix(android): size background images with one auto axis (fix #60346) (#60353)
* fix(android): size background images with one auto axis * Format BackgroundImageDrawable.kt with ktfmt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: intergalacticspacehighway <nishanbende@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 32260dd commit 94aee19

2 files changed

Lines changed: 97 additions & 48 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt‎

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,56 @@ import kotlin.math.ceil
3232
import kotlin.math.floor
3333
import kotlin.math.round
3434

35+
private fun positionToPixels(lengthPercentage: LengthPercentage, availableSpace: Float): Float =
36+
if (lengthPercentage.type == LengthPercentageType.PERCENT) {
37+
lengthPercentage.resolve(availableSpace)
38+
} else {
39+
lengthPercentage.resolve(availableSpace).dpToPx()
40+
}
41+
42+
internal fun calculateBackgroundImageSize(
43+
containerWidth: Float,
44+
containerHeight: Float,
45+
imageWidth: Float,
46+
imageHeight: Float,
47+
backgroundSize: BackgroundSize?,
48+
repeat: BackgroundRepeat?,
49+
): Pair<Float, Float> {
50+
var finalWidth = imageWidth
51+
var finalHeight = imageHeight
52+
53+
if (backgroundSize is BackgroundSize.LengthPercentageAuto) {
54+
val w = backgroundSize.lengthPercentage.x
55+
val h = backgroundSize.lengthPercentage.y
56+
if (w != null) {
57+
finalWidth = positionToPixels(w, containerWidth)
58+
}
59+
if (h != null) {
60+
finalHeight = positionToPixels(h, containerHeight)
61+
}
62+
}
63+
64+
if (repeat?.x == BackgroundRepeatKeyword.Round && finalWidth > 0) {
65+
if (!FloatUtil.floatsEqual(containerWidth.rem(finalWidth), 0f)) {
66+
val numRepeats = round(containerWidth / finalWidth)
67+
if (numRepeats > 0) {
68+
finalWidth = containerWidth / numRepeats
69+
}
70+
}
71+
}
72+
73+
if (repeat?.y == BackgroundRepeatKeyword.Round && finalHeight > 0) {
74+
if (!FloatUtil.floatsEqual(containerHeight.rem(finalHeight), 0f)) {
75+
val numRepeats = round(containerHeight / finalHeight)
76+
if (numRepeats > 0) {
77+
finalHeight = containerHeight / numRepeats
78+
}
79+
}
80+
}
81+
82+
return finalWidth to finalHeight
83+
}
84+
3585
internal class BackgroundImageDrawable(
3686
private val context: Context,
3787
/*
@@ -343,54 +393,6 @@ internal class BackgroundImageDrawable(
343393
}
344394
}
345395

346-
private fun positionToPixels(lengthPercentage: LengthPercentage, availableSpace: Float): Float =
347-
if (lengthPercentage.type == LengthPercentageType.PERCENT) {
348-
lengthPercentage.resolve(availableSpace)
349-
} else {
350-
lengthPercentage.resolve(availableSpace).dpToPx()
351-
}
352-
353-
private fun calculateBackgroundImageSize(
354-
containerWidth: Float,
355-
containerHeight: Float,
356-
imageWidth: Float,
357-
imageHeight: Float,
358-
backgroundSize: BackgroundSize?,
359-
repeat: BackgroundRepeat?,
360-
): Pair<Float, Float> {
361-
var finalWidth = imageWidth
362-
var finalHeight = imageHeight
363-
364-
if (backgroundSize is BackgroundSize.LengthPercentageAuto) {
365-
val w = backgroundSize.lengthPercentage.x
366-
val h = backgroundSize.lengthPercentage.y
367-
if (w != null && h != null) {
368-
finalWidth = positionToPixels(w, containerWidth)
369-
finalHeight = positionToPixels(h, containerHeight)
370-
}
371-
}
372-
373-
if (repeat?.x == BackgroundRepeatKeyword.Round && finalWidth > 0) {
374-
if (!FloatUtil.floatsEqual(containerWidth.rem(finalWidth), 0f)) {
375-
val numRepeats = round(containerWidth / finalWidth)
376-
if (numRepeats > 0) {
377-
finalWidth = containerWidth / numRepeats
378-
}
379-
}
380-
}
381-
382-
if (repeat?.y == BackgroundRepeatKeyword.Round && finalHeight > 0) {
383-
if (!FloatUtil.floatsEqual(containerHeight.rem(finalHeight), 0f)) {
384-
val numRepeats = round(containerHeight / finalHeight)
385-
if (numRepeats > 0) {
386-
finalHeight = containerHeight / numRepeats
387-
}
388-
}
389-
}
390-
391-
return finalWidth to finalHeight
392-
}
393-
394396
private fun calculateBackgroundPosition(
395397
tileWidth: Float,
396398
tileHeight: Float,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.uimanager.drawable
9+
10+
import com.facebook.react.uimanager.LengthPercentage
11+
import com.facebook.react.uimanager.LengthPercentageType
12+
import com.facebook.react.uimanager.style.BackgroundSize
13+
import com.facebook.react.uimanager.style.BackgroundSizeLengthPercentage
14+
import org.assertj.core.api.Assertions.assertThat
15+
import org.junit.Test
16+
17+
class BackgroundImageDrawableTest {
18+
@Test
19+
fun autoYAxisKeepsContainerHeight() {
20+
val size =
21+
BackgroundSize.LengthPercentageAuto(
22+
BackgroundSizeLengthPercentage(
23+
LengthPercentage(10f, LengthPercentageType.PERCENT),
24+
null,
25+
),
26+
)
27+
28+
val result = calculateBackgroundImageSize(200f, 100f, 200f, 100f, size, null)
29+
30+
assertThat(result).isEqualTo(20f to 100f)
31+
}
32+
33+
@Test
34+
fun explicitAxesAreResolvedIndependently() {
35+
val size =
36+
BackgroundSize.LengthPercentageAuto(
37+
BackgroundSizeLengthPercentage(
38+
LengthPercentage(25f, LengthPercentageType.PERCENT),
39+
LengthPercentage(12f, LengthPercentageType.PERCENT),
40+
),
41+
)
42+
43+
val result = calculateBackgroundImageSize(200f, 100f, 200f, 100f, size, null)
44+
45+
assertThat(result).isEqualTo(50f to 12f)
46+
}
47+
}

0 commit comments

Comments
 (0)