-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ViewManagerDelegate if provided instead of $$PropsSetter to updat…
…e view props Summary: This diff introduces an interface `ViewManagerDelegate` and its base implementation `BaseViewManagerDelegate`, which is used as a parent class for all view manager delegates generated by the JS codegen. Before the changes in this diff, generated delegates didn't support setting the base view properties such as background color, rotation, opacity, etc. Now it's possible to do by using `BaseViewManagerDelegate.setProperty(...)`, and since all generated delegates extend BaseViewManagerDelegate, they can just call `super.setProperty(...)` for properties they don't want to handle. This diff also introduced a new method `ViewManager.getDelegate()`. This will allow view managers to return an instance of the delegate generated by JS and ensure that the view properties are set in a type-safe manner. If this method returns null (it does by default), we fall back to the default implementation of setting view properties using Java-generated `$$PropsSetter` classes. This is an example of an interface class generated by JS: ``` public interface RCTAxialGradientViewViewManagerInterface<T extends View> { void setColors(T view, Nullable ReadableArray value); void setLocations(T view, Nullable ReadableArray value); void setEndX(T view, Float value); void setEndY(T view, Float value); void setStartX(T view, Float value); void setStartY(T view, Float value); } ``` This is an example of a delegate class generated by JS: ``` public class RCTAxialGradientViewManagerDelegate<T extends View, U extends BaseViewManager<T, ? extends LayoutShadowNode> & RCTAxialGradientViewManagerInterface<T>> extends BaseViewManagerDelegate<T, U> { public RCTAxialGradientViewManagerDelegate(U viewManager) { super(viewManager); } Override public void setProperty(T view, String propName, Nullable Object value) { switch (propName) { case "colors": mViewManager.setColors(view, (ReadableArray) value); break; case "locations": mViewManager.setLocations(view, (ReadableArray) value); break; case "endX": mViewManager.setEndX(view, value == null ? Float.NaN : ((Double) value).floatValue()); break; case "endY": mViewManager.setEndY(view, value == null ? Float.NaN : ((Double) value).floatValue()); break; case "startX": mViewManager.setStartX(view, value == null ? Float.NaN : ((Double) value).floatValue()); break; case "startY": mViewManager.setStartY(view, value == null ? Float.NaN : ((Double) value).floatValue()); break; default: super.setProperty(view, propName, value); } } } ``` NOTE: What if a view manager, for instance ReactAxialGradientManager, wanted to add support for the borderRadius prop? In the old Java codegen, it would just need to create a method and annotate it with ReactProp (name = ViewProps.BORDER_RADIUS) and $$PropsSetter would call this method when a property with this name must be set. With the new JS codegen, borderRadius is a part of the basic view props, so setBorderRadius is not generated as a part of the ViewManagerInterface, so it’s not possible to set this value. I see two options: 1) add a method boolean setProperty (String propName, Object value) and let the view manager handle it in a non-type safe way (return true if it’s been handled). 2) Generate BaseViewManagerInterface which will include all basic view props and make BaseViewManager implement this interface, leaving all methods empty so that it stays compatible with the current implementation. Override these methods in a view manager that needs to handle a specific property in a custom way (so we would override setBorderRadius in ReactAxialGradientManager). Reviewed By: mdvacca Differential Revision: D16667686 fbshipit-source-id: 06a15a92f8af55640b7a53c5a34f40366d1be2a8
- Loading branch information
1 parent
bef87b6
commit 50fe811
Showing
39 changed files
with
611 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManagerDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.facebook.react.uimanager; | ||
|
||
import android.view.View; | ||
import androidx.annotation.Nullable; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.bridge.ReadableMap; | ||
|
||
/** | ||
* This is a base implementation of {@link ViewManagerDelegate} which supports setting properties | ||
* that every view should support, such as rotation, background color, etc. | ||
*/ | ||
public abstract class BaseViewManagerDelegate< | ||
T extends View, U extends BaseViewManager<T, ? extends LayoutShadowNode>> | ||
implements ViewManagerDelegate<T> { | ||
protected final U mViewManager; | ||
|
||
public BaseViewManagerDelegate(U viewManager) { | ||
mViewManager = viewManager; | ||
} | ||
|
||
@Override | ||
public void setProperty(T view, String propName, @Nullable Object value) { | ||
switch (propName) { | ||
case ViewProps.ACCESSIBILITY_ACTIONS: | ||
mViewManager.setAccessibilityActions(view, (ReadableArray) value); | ||
break; | ||
case ViewProps.ACCESSIBILITY_HINT: | ||
mViewManager.setAccessibilityHint(view, (String) value); | ||
break; | ||
case ViewProps.ACCESSIBILITY_LABEL: | ||
mViewManager.setAccessibilityLabel(view, (String) value); | ||
break; | ||
case ViewProps.ACCESSIBILITY_LIVE_REGION: | ||
mViewManager.setAccessibilityLiveRegion(view, (String) value); | ||
break; | ||
case ViewProps.ACCESSIBILITY_ROLE: | ||
mViewManager.setAccessibilityRole(view, (String) value); | ||
break; | ||
case ViewProps.ACCESSIBILITY_STATE: | ||
mViewManager.setViewState(view, (ReadableMap) value); | ||
break; | ||
case ViewProps.ACCESSIBILITY_STATES: | ||
mViewManager.setViewStates(view, (ReadableArray) value); | ||
break; | ||
case ViewProps.BACKGROUND_COLOR: | ||
mViewManager.setBackgroundColor(view, value == null ? 0 : ((Double) value).intValue()); | ||
break; | ||
case ViewProps.ELEVATION: | ||
mViewManager.setElevation(view, value == null ? 0.0f : ((Double) value).floatValue()); | ||
break; | ||
case ViewProps.IMPORTANT_FOR_ACCESSIBILITY: | ||
mViewManager.setImportantForAccessibility(view, (String) value); | ||
break; | ||
case ViewProps.NATIVE_ID: | ||
mViewManager.setNativeId(view, (String) value); | ||
break; | ||
case ViewProps.OPACITY: | ||
mViewManager.setOpacity(view, value == null ? 1.0f : ((Double) value).floatValue()); | ||
break; | ||
case ViewProps.RENDER_TO_HARDWARE_TEXTURE: | ||
//noinspection SimplifiableConditionalExpression | ||
mViewManager.setRenderToHardwareTexture(view, value == null ? false : (boolean) value); | ||
break; | ||
case ViewProps.ROTATION: | ||
mViewManager.setRotation(view, value == null ? 0.0f : ((Double) value).floatValue()); | ||
break; | ||
case ViewProps.SCALE_X: | ||
mViewManager.setScaleX(view, value == null ? 1.0f : ((Double) value).floatValue()); | ||
break; | ||
case ViewProps.SCALE_Y: | ||
mViewManager.setScaleY(view, value == null ? 1.0f : ((Double) value).floatValue()); | ||
break; | ||
case ViewProps.TEST_ID: | ||
mViewManager.setTestId(view, (String) value); | ||
break; | ||
case ViewProps.TRANSFORM: | ||
mViewManager.setTransform(view, (ReadableArray) value); | ||
break; | ||
case ViewProps.TRANSLATE_X: | ||
mViewManager.setTranslateX(view, value == null ? 0.0f : ((Double) value).floatValue()); | ||
break; | ||
case ViewProps.TRANSLATE_Y: | ||
mViewManager.setTranslateY(view, value == null ? 0.0f : ((Double) value).floatValue()); | ||
break; | ||
case ViewProps.Z_INDEX: | ||
mViewManager.setZIndex(view, value == null ? 0.0f : ((Double) value).floatValue()); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagerDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.facebook.react.uimanager; | ||
|
||
import android.view.View; | ||
import androidx.annotation.Nullable; | ||
|
||
/** | ||
* This is an interface that must be implemented by classes that wish to take over the | ||
* responsibility of setting properties of all views managed by the view manager. | ||
* | ||
* @param <T> the type of the view supported by this delegate | ||
*/ | ||
public interface ViewManagerDelegate<T extends View> { | ||
void setProperty(T view, String propName, @Nullable Object value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.