From 244e242897cd8a3d288626b6a5e326a52a63ecf0 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Sat, 27 Jul 2024 11:39:27 -0700 Subject: [PATCH] Expose more in CSSBackgroundDrawable (#45691) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45691 1. Add some accessors, so we can keep accessors and setters symetric 2. Use the shared BorderStyle enum added in last diff 3. Fix some missing invalidation on setting style Changelog: [Internal] Reviewed By: rshest Differential Revision: D60252276 fbshipit-source-id: 3dde6ad5926f109cefc7247da4ba1894694b1867 --- .../drawable/CSSBackgroundDrawable.java | 59 +++++++++++-------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/CSSBackgroundDrawable.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/CSSBackgroundDrawable.java index 90e501ba112..644dc95cb56 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/CSSBackgroundDrawable.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/CSSBackgroundDrawable.java @@ -36,6 +36,7 @@ import com.facebook.react.uimanager.LengthPercentageType; import com.facebook.react.uimanager.Spacing; import com.facebook.react.uimanager.style.BorderRadiusProp; import com.facebook.react.uimanager.style.BorderRadiusStyle; +import com.facebook.react.uimanager.style.BorderStyle; import com.facebook.react.uimanager.style.ComputedBorderRadius; import java.util.Locale; import java.util.Objects; @@ -64,29 +65,23 @@ public class CSSBackgroundDrawable extends Drawable { // 0 == 0x00000000, all bits set to 0. private static final int ALL_BITS_UNSET = 0; - private enum BorderStyle { - SOLID, - DASHED, - DOTTED; + private static @Nullable PathEffect getPathEffect(BorderStyle style, float borderWidth) { + switch (style) { + case SOLID: + return null; - public static @Nullable PathEffect getPathEffect(BorderStyle style, float borderWidth) { - switch (style) { - case SOLID: - return null; + case DASHED: + return new DashPathEffect( + new float[] {borderWidth * 3, borderWidth * 3, borderWidth * 3, borderWidth * 3}, 0); - case DASHED: - return new DashPathEffect( - new float[] {borderWidth * 3, borderWidth * 3, borderWidth * 3, borderWidth * 3}, 0); + case DOTTED: + return new DashPathEffect( + new float[] {borderWidth, borderWidth, borderWidth, borderWidth}, 0); - case DOTTED: - return new DashPathEffect( - new float[] {borderWidth, borderWidth, borderWidth, borderWidth}, 0); - - default: - return null; - } + default: + return null; } - }; + } /* Value at Spacing.ALL index used for rounded borders, whole array used by rectangular borders */ private @Nullable Spacing mBorderWidth; @@ -255,6 +250,10 @@ public class CSSBackgroundDrawable extends Drawable { public void setBorderStyle(@Nullable String style) { BorderStyle borderStyle = style == null ? null : BorderStyle.valueOf(style.toUpperCase(Locale.US)); + setBorderStyle(borderStyle); + } + + public void setBorderStyle(@Nullable BorderStyle borderStyle) { if (mBorderStyle != borderStyle) { mBorderStyle = borderStyle; mNeedUpdatePathForBorderRadius = true; @@ -262,6 +261,10 @@ public class CSSBackgroundDrawable extends Drawable { } } + public @Nullable BorderStyle getBorderStyle() { + return mBorderStyle; + } + /** * @deprecated Use {@link #setBorderRadius(BorderRadiusProp, LengthPercentage)} instead. */ @@ -286,6 +289,7 @@ public class CSSBackgroundDrawable extends Drawable { if (boxedRadius == null) { mBorderRadius.set(BorderRadiusProp.values()[position], null); + invalidateSelf(); } else { setBorderRadius( BorderRadiusProp.values()[position], @@ -1020,14 +1024,23 @@ public class CSSBackgroundDrawable extends Drawable { } public float getBorderWidthOrDefaultTo(final float defaultValue, final int spacingType) { - if (mBorderWidth == null) { + @Nullable Float width = getBorderWidth(spacingType); + if (width == null) { return defaultValue; } + return width; + } + + public @Nullable Float getBorderWidth(int spacingType) { + if (mBorderWidth == null) { + return null; + } + final float width = mBorderWidth.getRaw(spacingType); if (Float.isNaN(width)) { - return defaultValue; + return null; } return width; @@ -1037,7 +1050,7 @@ public class CSSBackgroundDrawable extends Drawable { private void updatePathEffect() { // Used for rounded border and rounded background PathEffect mPathEffectForBorderStyle = - mBorderStyle != null ? BorderStyle.getPathEffect(mBorderStyle, getFullBorderWidth()) : null; + mBorderStyle != null ? getPathEffect(mBorderStyle, getFullBorderWidth()) : null; mPaint.setPathEffect(mPathEffectForBorderStyle); } @@ -1045,7 +1058,7 @@ public class CSSBackgroundDrawable extends Drawable { private void updatePathEffect(int borderWidth) { PathEffect pathEffectForBorderStyle = null; if (mBorderStyle != null) { - pathEffectForBorderStyle = BorderStyle.getPathEffect(mBorderStyle, borderWidth); + pathEffectForBorderStyle = getPathEffect(mBorderStyle, borderWidth); } mPaint.setPathEffect(pathEffectForBorderStyle); }