Skip to content

Commit

Permalink
Use BackgroundStyleApplicator when setting background color in BaseVi…
Browse files Browse the repository at this point in the history
…ewManager (#46160)

Summary:
Pull Request resolved: #46160

We use this for all built-in components now, and rely on going through `BackgroundStyleApplicator` to be able to set additional background effects like borders, rounded corners, box shadow, outlines.

Let's use this in `BaseViewManager`, to allow exposing those other effects to all views, letting RN manage the background drawable for views using `BaseViewManager` and exposing the props for the managed styles.

This is technically breaking, in the case where a custom component could previously inherit directly from `BaseViewManager`/`ReactViewGroupManager`, (vs `SimpleViewManager`/`ReactViewManager`), then override `setBackgroundColor` on the Android View, instead of the view manager, but this effected zero of the [276 top libraries](https://github.com/elicwhite/react-native-libraries), so I don't think it would be common to hit.

Changelog:
[Android][Breaking]  - Use BackgroundStyleApplicator when setting background color in BaseViewManager

Reviewed By: tdn120

Differential Revision: D61658739

fbshipit-source-id: 15af649328ea18fda3cd68e56d481ed1c0fd43c4
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Oct 3, 2024
1 parent 211534b commit 309cdea
Show file tree
Hide file tree
Showing 9 changed files with 3 additions and 63 deletions.
11 changes: 0 additions & 11 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -6680,8 +6680,6 @@ public final class com/facebook/react/views/image/ReactImageManager : com/facebo
public fun getName ()Ljava/lang/String;
public synthetic fun onAfterUpdateTransaction (Landroid/view/View;)V
public final fun setAccessible (Lcom/facebook/react/views/image/ReactImageView;Z)V
public synthetic fun setBackgroundColor (Landroid/view/View;I)V
public fun setBackgroundColor (Lcom/facebook/react/views/image/ReactImageView;I)V
public final fun setBlurRadius (Lcom/facebook/react/views/image/ReactImageView;F)V
public final fun setBorderColor (Lcom/facebook/react/views/image/ReactImageView;Ljava/lang/Integer;)V
public final fun setBorderRadius (Lcom/facebook/react/views/image/ReactImageView;IF)V
Expand Down Expand Up @@ -7108,8 +7106,6 @@ public class com/facebook/react/views/scroll/ReactHorizontalScrollViewManager :
public synthetic fun scrollTo (Ljava/lang/Object;Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollToCommandData;)V
public fun scrollToEnd (Lcom/facebook/react/views/scroll/ReactHorizontalScrollView;Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollToEndCommandData;)V
public synthetic fun scrollToEnd (Ljava/lang/Object;Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollToEndCommandData;)V
public synthetic fun setBackgroundColor (Landroid/view/View;I)V
public fun setBackgroundColor (Lcom/facebook/react/views/scroll/ReactHorizontalScrollView;I)V
public fun setBorderColor (Lcom/facebook/react/views/scroll/ReactHorizontalScrollView;ILjava/lang/Integer;)V
public fun setBorderRadius (Lcom/facebook/react/views/scroll/ReactHorizontalScrollView;IF)V
public fun setBorderStyle (Lcom/facebook/react/views/scroll/ReactHorizontalScrollView;Ljava/lang/String;)V
Expand Down Expand Up @@ -7351,8 +7347,6 @@ public class com/facebook/react/views/scroll/ReactScrollViewManager : com/facebo
public synthetic fun scrollTo (Ljava/lang/Object;Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollToCommandData;)V
public fun scrollToEnd (Lcom/facebook/react/views/scroll/ReactScrollView;Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollToEndCommandData;)V
public synthetic fun scrollToEnd (Ljava/lang/Object;Lcom/facebook/react/views/scroll/ReactScrollViewCommandHelper$ScrollToEndCommandData;)V
public synthetic fun setBackgroundColor (Landroid/view/View;I)V
public fun setBackgroundColor (Lcom/facebook/react/views/scroll/ReactScrollView;I)V
public fun setBorderColor (Lcom/facebook/react/views/scroll/ReactScrollView;ILjava/lang/Integer;)V
public fun setBorderRadius (Lcom/facebook/react/views/scroll/ReactScrollView;IF)V
public fun setBorderStyle (Lcom/facebook/react/views/scroll/ReactScrollView;Ljava/lang/String;)V
Expand Down Expand Up @@ -7652,7 +7646,6 @@ public abstract class com/facebook/react/views/text/ReactTextAnchorViewManager :
public fun setAccessible (Lcom/facebook/react/views/text/ReactTextView;Z)V
public fun setAdjustFontSizeToFit (Lcom/facebook/react/views/text/ReactTextView;Z)V
public fun setAndroidHyphenationFrequency (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
public fun setBackgroundColor (Landroid/view/View;I)V
public fun setBorderColor (Lcom/facebook/react/views/text/ReactTextView;ILjava/lang/Integer;)V
public fun setBorderRadius (Lcom/facebook/react/views/text/ReactTextView;IF)V
public fun setBorderStyle (Lcom/facebook/react/views/text/ReactTextView;Ljava/lang/String;)V
Expand Down Expand Up @@ -8136,8 +8129,6 @@ public class com/facebook/react/views/textinput/ReactTextInputManager : com/face
public fun setAutoCapitalize (Lcom/facebook/react/views/textinput/ReactEditText;Lcom/facebook/react/bridge/Dynamic;)V
public fun setAutoCorrect (Lcom/facebook/react/views/textinput/ReactEditText;Ljava/lang/Boolean;)V
public fun setAutoFocus (Lcom/facebook/react/views/textinput/ReactEditText;Z)V
public synthetic fun setBackgroundColor (Landroid/view/View;I)V
public fun setBackgroundColor (Lcom/facebook/react/views/textinput/ReactEditText;I)V
public fun setBorderColor (Lcom/facebook/react/views/textinput/ReactEditText;ILjava/lang/Integer;)V
public fun setBorderRadius (Lcom/facebook/react/views/textinput/ReactEditText;IF)V
public fun setBorderStyle (Lcom/facebook/react/views/textinput/ReactEditText;Ljava/lang/String;)V
Expand Down Expand Up @@ -8343,8 +8334,6 @@ public class com/facebook/react/views/view/ReactViewManager : com/facebook/react
public fun receiveCommand (Lcom/facebook/react/views/view/ReactViewGroup;Ljava/lang/String;Lcom/facebook/react/bridge/ReadableArray;)V
public fun setAccessible (Lcom/facebook/react/views/view/ReactViewGroup;Z)V
public fun setBackfaceVisibility (Lcom/facebook/react/views/view/ReactViewGroup;Ljava/lang/String;)V
public synthetic fun setBackgroundColor (Landroid/view/View;I)V
public fun setBackgroundColor (Lcom/facebook/react/views/view/ReactViewGroup;I)V
public fun setBackgroundImage (Lcom/facebook/react/views/view/ReactViewGroup;Lcom/facebook/react/bridge/ReadableArray;)V
public fun setBorderColor (Lcom/facebook/react/views/view/ReactViewGroup;ILjava/lang/Integer;)V
public fun setBorderRadius (Lcom/facebook/react/views/view/ReactViewGroup;IF)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.view.View;
import android.view.ViewParent;
import android.view.accessibility.AccessibilityEvent;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
Expand Down Expand Up @@ -195,8 +196,8 @@ public void onLayoutChange(
name = ViewProps.BACKGROUND_COLOR,
defaultInt = Color.TRANSPARENT,
customType = "Color")
public void setBackgroundColor(@NonNull T view, int backgroundColor) {
view.setBackgroundColor(backgroundColor);
public void setBackgroundColor(@NonNull T view, @ColorInt int backgroundColor) {
BackgroundStyleApplicator.setBackgroundColor(view, backgroundColor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package com.facebook.react.views.image

import android.graphics.Color
import android.graphics.PorterDuff
import androidx.annotation.ColorInt
import com.facebook.common.logging.FLog
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.controller.AbstractDraweeControllerBuilder
Expand Down Expand Up @@ -237,13 +236,6 @@ public constructor(
BackgroundStyleApplicator.setBoxShadow(view, shadows)
}

public override fun setBackgroundColor(
view: ReactImageView,
@ColorInt backgroundColor: Int
): Unit {
BackgroundStyleApplicator.setBackgroundColor(view, backgroundColor)
}

public override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any> =
(super.getExportedCustomDirectEventTypeConstants() ?: mutableMapOf<String, Any>()).apply {
put(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import android.graphics.Color;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import com.facebook.infer.annotation.Nullsafe;
Expand Down Expand Up @@ -366,9 +365,4 @@ public void setHorizontal(ReactHorizontalScrollView view, boolean horizontal) {
public void setBoxShadow(ReactHorizontalScrollView view, @Nullable ReadableArray shadows) {
BackgroundStyleApplicator.setBoxShadow(view, shadows);
}

@Override
public void setBackgroundColor(ReactHorizontalScrollView view, @ColorInt int backgroundColor) {
BackgroundStyleApplicator.setBackgroundColor(view, backgroundColor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import android.graphics.Color;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import com.facebook.infer.annotation.Nullsafe;
Expand Down Expand Up @@ -348,11 +347,6 @@ public void setBoxShadow(ReactScrollView view, @Nullable ReadableArray shadows)
BackgroundStyleApplicator.setBoxShadow(view, shadows);
}

@Override
public void setBackgroundColor(ReactScrollView view, @ColorInt int backgroundColor) {
BackgroundStyleApplicator.setBackgroundColor(view, backgroundColor);
}

@Override
public @Nullable Object updateState(
ReactScrollView view, ReactStylesDiffMap props, StateWrapper stateWrapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.text.util.Linkify;
import android.view.Gravity;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.ReadableArray;
Expand Down Expand Up @@ -237,9 +236,4 @@ public void setNotifyOnInlineViewLayout(ReactTextView view, boolean notifyOnInli
public void setBoxShadow(ReactTextView view, @Nullable ReadableArray shadows) {
BackgroundStyleApplicator.setBoxShadow(view, shadows);
}

@Override
public void setBackgroundColor(T view, @ColorInt int backgroundColor) {
BackgroundStyleApplicator.setBackgroundColor(view, backgroundColor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.autofill.HintConstants;
import androidx.core.content.ContextCompat;
Expand Down Expand Up @@ -1035,11 +1034,6 @@ public void setBoxShadow(ReactEditText view, @Nullable ReadableArray shadows) {
BackgroundStyleApplicator.setBoxShadow(view, shadows);
}

@Override
public void setBackgroundColor(ReactEditText view, @ColorInt int backgroundColor) {
BackgroundStyleApplicator.setBackgroundColor(view, backgroundColor);
}

@Override
protected void onAfterUpdateTransaction(ReactEditText view) {
super.onAfterUpdateTransaction(view);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Nullsafe;
Expand Down Expand Up @@ -356,11 +355,6 @@ public void setBoxShadow(ReactViewGroup view, @Nullable ReadableArray shadows) {
BackgroundStyleApplicator.setBoxShadow(view, shadows);
}

@Override
public void setBackgroundColor(ReactViewGroup view, @ColorInt int backgroundColor) {
BackgroundStyleApplicator.setBackgroundColor(view, backgroundColor);
}

@Override
public String getName() {
return REACT_CLASS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package com.facebook.react.uimanager

import android.graphics.drawable.ColorDrawable
import android.view.View
import com.facebook.react.bridge.BridgeReactContext
import com.facebook.react.bridge.CatalystInstance
Expand Down Expand Up @@ -74,17 +73,6 @@ class SimpleViewPropertyTest {
Assertions.assertThat(view.alpha).isEqualTo(1.0f)
}

@Test
fun testBackgroundColor() {
val view = manager.createView(viewTag, themedContext, buildStyles(), null, JSResponderHandler())
manager.updateProperties(view, buildStyles())
Assertions.assertThat(view.background).isEqualTo(null)
manager.updateProperties(view, buildStyles("backgroundColor", 12))
Assertions.assertThat((view.background as ColorDrawable).color).isEqualTo(12)
manager.updateProperties(view, buildStyles("backgroundColor", null))
Assertions.assertThat((view.background as ColorDrawable).color).isEqualTo(0)
}

@Test
fun testGetNativeProps() {
val nativeProps = manager.nativeProps
Expand Down

0 comments on commit 309cdea

Please sign in to comment.