Skip to content

Commit bf59864

Browse files
kmagierafacebook-github-bot-8
authored andcommitted
Convert View to @ReactProp.
Differential Revision: D2479795 committer: Service User <svcscm@fb.com>
1 parent 1ad1f45 commit bf59864

File tree

6 files changed

+237
-97
lines changed

6 files changed

+237
-97
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright 2004-present Facebook. All Rights Reserved.
2+
3+
package com.facebook.react.uimanager;
4+
5+
import android.graphics.Color;
6+
import android.os.Build;
7+
import android.view.View;
8+
9+
import com.facebook.react.bridge.ReadableMap;
10+
11+
/**
12+
* Base class that should be suitable for the majority of subclasses of {@link ViewManager}.
13+
* It provides support for base view properties such as backgroundColor, opacity, etc.
14+
*/
15+
public abstract class BaseViewManager<T extends View, C extends ReactShadowNode>
16+
extends ViewManager<T, C> {
17+
18+
private static final String PROP_BACKGROUND_COLOR = ViewProps.BACKGROUND_COLOR;
19+
private static final String PROP_DECOMPOSED_MATRIX = "decomposedMatrix";
20+
private static final String PROP_DECOMPOSED_MATRIX_ROTATE = "rotate";
21+
private static final String PROP_DECOMPOSED_MATRIX_SCALE_X = "scaleX";
22+
private static final String PROP_DECOMPOSED_MATRIX_SCALE_Y = "scaleY";
23+
private static final String PROP_DECOMPOSED_MATRIX_TRANSLATE_X = "translateX";
24+
private static final String PROP_DECOMPOSED_MATRIX_TRANSLATE_Y = "translateY";
25+
private static final String PROP_OPACITY = "opacity";
26+
private static final String PROP_RENDER_TO_HARDWARE_TEXTURE = "renderToHardwareTextureAndroid";
27+
private static final String PROP_ACCESSIBILITY_LABEL = "accessibilityLabel";
28+
private static final String PROP_ACCESSIBILITY_COMPONENT_TYPE = "accessibilityComponentType";
29+
private static final String PROP_ACCESSIBILITY_LIVE_REGION = "accessibilityLiveRegion";
30+
private static final String PROP_IMPORTANT_FOR_ACCESSIBILITY = "importantForAccessibility";
31+
32+
// DEPRECATED
33+
private static final String PROP_ROTATION = "rotation";
34+
private static final String PROP_SCALE_X = "scaleX";
35+
private static final String PROP_SCALE_Y = "scaleY";
36+
private static final String PROP_TRANSLATE_X = "translateX";
37+
private static final String PROP_TRANSLATE_Y = "translateY";
38+
39+
/**
40+
* Used to locate views in end-to-end (UI) tests.
41+
*/
42+
public static final String PROP_TEST_ID = "testID";
43+
44+
45+
@ReactProp(name = PROP_BACKGROUND_COLOR, defaultInt = Color.TRANSPARENT, customType = "Color")
46+
public void setBackgroundColor(T view, int backgroundColor) {
47+
view.setBackgroundColor(backgroundColor);
48+
}
49+
50+
@ReactProp(name = PROP_DECOMPOSED_MATRIX)
51+
public void setDecomposedMatrix(T view, ReadableMap decomposedMatrix) {
52+
if (decomposedMatrix == null) {
53+
resetTransformMatrix(view);
54+
} else {
55+
setTransformMatrix(view, decomposedMatrix);
56+
}
57+
}
58+
59+
@ReactProp(name = PROP_OPACITY, defaultFloat = 1.f)
60+
public void setOpacity(T view, float opacity) {
61+
view.setAlpha(opacity);
62+
}
63+
64+
@ReactProp(name = PROP_RENDER_TO_HARDWARE_TEXTURE)
65+
public void setRenderToHardwareTexture(T view, boolean useHWTexture) {
66+
view.setLayerType(useHWTexture ? View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE, null);
67+
}
68+
69+
@ReactProp(name = PROP_TEST_ID)
70+
public void setTestId(T view, String testId) {
71+
view.setTag(testId);
72+
}
73+
74+
@ReactProp(name = PROP_ACCESSIBILITY_LABEL)
75+
public void setAccessibilityLabel(T view, String accessibilityLabel) {
76+
view.setContentDescription(accessibilityLabel);
77+
}
78+
79+
@ReactProp(name = PROP_ACCESSIBILITY_COMPONENT_TYPE)
80+
public void setAccessibilityComponentType(T view, String accessibilityComponentType) {
81+
AccessibilityHelper.updateAccessibilityComponentType(view, accessibilityComponentType);
82+
}
83+
84+
@ReactProp(name = PROP_IMPORTANT_FOR_ACCESSIBILITY)
85+
public void setImportantForAccessibility(T view, String importantForAccessibility) {
86+
if (importantForAccessibility == null || importantForAccessibility.equals("auto")) {
87+
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);
88+
} else if (importantForAccessibility.equals("yes")) {
89+
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
90+
} else if (importantForAccessibility.equals("no")) {
91+
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
92+
} else if (importantForAccessibility.equals("no-hide-descendants")) {
93+
view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
94+
}
95+
}
96+
97+
@Deprecated
98+
@ReactProp(name = PROP_ROTATION)
99+
public void setRotation(T view, float rotation) {
100+
view.setRotation(rotation);
101+
}
102+
103+
@Deprecated
104+
@ReactProp(name = PROP_SCALE_X, defaultFloat = 1f)
105+
public void setScaleX(T view, float scaleX) {
106+
view.setScaleX(scaleX);
107+
}
108+
109+
@Deprecated
110+
@ReactProp(name = PROP_SCALE_Y, defaultFloat = 1f)
111+
public void setScaleY(T view, float scaleY) {
112+
view.setScaleY(scaleY);
113+
}
114+
115+
@Deprecated
116+
@ReactProp(name = PROP_TRANSLATE_X, defaultFloat = 1f)
117+
public void setTranslateX(T view, float translateX) {
118+
view.setTranslationX(translateX);
119+
}
120+
121+
@Deprecated
122+
@ReactProp(name = PROP_TRANSLATE_Y, defaultFloat = 1f)
123+
public void setTranslateY(T view, float translateY) {
124+
view.setTranslationY(translateY);
125+
}
126+
127+
@ReactProp(name = PROP_ACCESSIBILITY_LIVE_REGION)
128+
public void setAccessibilityLiveRegion(T view, String liveRegion) {
129+
if (Build.VERSION.SDK_INT >= 19) {
130+
if (liveRegion == null || liveRegion.equals("none")) {
131+
view.setAccessibilityLiveRegion(View.ACCESSIBILITY_LIVE_REGION_NONE);
132+
} else if (liveRegion.equals("polite")) {
133+
view.setAccessibilityLiveRegion(View.ACCESSIBILITY_LIVE_REGION_POLITE);
134+
} else if (liveRegion.equals("assertive")) {
135+
view.setAccessibilityLiveRegion(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
136+
}
137+
}
138+
}
139+
140+
private static void setTransformMatrix(View view, ReadableMap matrix) {
141+
view.setTranslationX(PixelUtil.toPixelFromDIP(
142+
(float) matrix.getDouble(PROP_DECOMPOSED_MATRIX_TRANSLATE_X)));
143+
view.setTranslationY(PixelUtil.toPixelFromDIP(
144+
(float) matrix.getDouble(PROP_DECOMPOSED_MATRIX_TRANSLATE_Y)));
145+
view.setRotation(
146+
(float) matrix.getDouble(PROP_DECOMPOSED_MATRIX_ROTATE));
147+
view.setScaleX(
148+
(float) matrix.getDouble(PROP_DECOMPOSED_MATRIX_SCALE_X));
149+
view.setScaleY(
150+
(float) matrix.getDouble(PROP_DECOMPOSED_MATRIX_SCALE_Y));
151+
}
152+
153+
private static void resetTransformMatrix(View view) {
154+
view.setTranslationX(PixelUtil.toPixelFromDIP(0));
155+
view.setTranslationY(PixelUtil.toPixelFromDIP(0));
156+
view.setRotation(0);
157+
view.setScaleX(1);
158+
view.setScaleY(1);
159+
}
160+
}

ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewPropertyApplicator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
/**
2222
* Takes common view properties from JS and applies them to a given {@link View}.
23+
*
24+
* TODO(krzysztof): Blow away this class once refactoring is complete
2325
*/
2426
public class BaseViewPropertyApplicator {
2527

ReactAndroid/src/main/java/com/facebook/react/uimanager/SimpleViewManager.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,21 @@
1212
import android.view.View;
1313

1414
/**
15-
* A partial implementation of {@link ViewManager} that applies common properties such as background
16-
* color, opacity and CSS layout. Implementations should make sure to call
17-
* {@code super.updateView()} in order for these properties to be applied.
15+
* Common base class for most of the {@link ViewManager}s. It provides support for most common
16+
* properties through extending {@link BaseViewManager}. It also reduces boilerplate by specifying
17+
* the type of shadow node to be {@link ReactShadowNode} and providing default, empty implementation
18+
* for some of the methods of {@link ViewManager} interface.
1819
*
1920
* @param <T> the view handled by this manager
2021
*/
21-
public abstract class SimpleViewManager<T extends View> extends ViewManager<T, ReactShadowNode> {
22+
public abstract class SimpleViewManager<T extends View> extends
23+
BaseViewManager<T, ReactShadowNode> {
2224

2325
@Override
2426
public ReactShadowNode createCSSNodeInstance() {
2527
return new ReactShadowNode();
2628
}
2729

28-
@Override
29-
public void updateView(T root, CatalystStylesDiffMap props) {
30-
BaseViewPropertyApplicator.applyCommonViewProperties(root, props);
31-
}
32-
3330
@Override
3431
public void updateExtraData(T root, Object extraData) {
3532
}

ReactAndroid/src/main/java/com/facebook/react/uimanager/UIProp.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* @UIProp(UIProp.Type.BOOLEAN) public static final String PROP_FOO = "foo";
2626
* @UIProp(UIProp.Type.STRING) public static final String PROP_BAR = "bar";
2727
* }
28+
*
29+
* TODO(krzysztof): Kill this class once @ReactProp refactoring is done
2830
*/
2931
@Target(ElementType.FIELD)
3032
@Retention(RUNTIME)

ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@
1616
* Class providing children management API for view managers of classes extending ViewGroup.
1717
*/
1818
public abstract class ViewGroupManager <T extends ViewGroup>
19-
extends ViewManager<T, ReactShadowNode> {
19+
extends BaseViewManager<T, ReactShadowNode> {
2020

2121
@Override
2222
public ReactShadowNode createCSSNodeInstance() {
2323
return new ReactShadowNode();
2424
}
2525

26-
@Override
27-
public void updateView(T root, CatalystStylesDiffMap props) {
28-
BaseViewPropertyApplicator.applyCommonViewProperties(root, props);
29-
}
30-
3126
@Override
3227
public void updateExtraData(T root, Object extraData) {
3328
}

0 commit comments

Comments
 (0)