Summary:Using TouchableNativeFeedback has been a problem for me because the ripples it makes don't follow the child view's border radii so the ripples stick out of the child view's rounded corners. This PR should fix this problem with a minor caveat: this only works for TouchableNativeFeedback.Ripple and not TouchableNativeFeedback.SelectableBackground. I searched how I could apply corner radius to selectableItemBackground and it doesn't seem to be possible (the prevalent advice is to create the ripple manually which is equivalent to using TNF.Ripple in our case), though I could be wrong.

I added [an example to UIExplorer (TouchableExample)](http://i.imgur.com/CHY9xjW.png). This is my first PR to this repo so let me know if something's wrong. Cheers!
Closes https://github.com/facebook/react-native/pull/6515

Differential Revision: D3126513

Pulled By: AaaChiuuu

fb-gh-sync-id: 4a00e7177ee4ffd8dffeca143f4f43f08c99b5a1
fbshipit-source-id: 4a00e7177ee4ffd8dffeca143f4f43f08c99b5a1
This commit is contained in:
Jeff Chien 2016-04-24 15:44:17 -07:00 коммит произвёл Facebook Github Bot 5
Родитель 02578df4f7
Коммит 475f1b5796
6 изменённых файлов: 31 добавлений и 103 удалений

Просмотреть файл

@ -110,14 +110,6 @@ exports.examples = [
render: function(): ReactElement { render: function(): ReactElement {
return <TouchableDisabled />; return <TouchableDisabled />;
}, },
}, {
title: 'TouchableNativeFeedback with Border Radius',
description: 'The ripples from <TouchableNativeFeedback> should follow borderRadius. ' +
'This only works with a non-borderless background from TouchableNativeFeedback.Ripple',
platform: 'android',
render: function(): ReactElement {
return <TouchableNativeFeedbackBorderRadius />;
},
}]; }];
var TextOnPressBox = React.createClass({ var TextOnPressBox = React.createClass({
@ -376,25 +368,6 @@ var TouchableDisabled = React.createClass({
} }
}); });
var TouchableNativeFeedbackBorderRadius = React.createClass({
render: function() {
return (
<View>
<TouchableNativeFeedback
style={[styles.row, styles.block]}
onPress={() => console.log('TouchableNativeFeedback Ripple with Border Radius has been clicked')}
background={TouchableNativeFeedback.Ripple('#ccc')}>
<View style={[styles.tnfBorderRadiusView]}>
<Text style={[styles.button, styles.nativeFeedbackButton]}>
TouchableNativeFeedback.Ripple with Border Radius
</Text>
</View>
</TouchableNativeFeedback>
</View>
);
}
});
var heartImage = {uri: 'https://pbs.twimg.com/media/BlXBfT3CQAA6cVZ.png:small'}; var heartImage = {uri: 'https://pbs.twimg.com/media/BlXBfT3CQAA6cVZ.png:small'};
var styles = StyleSheet.create({ var styles = StyleSheet.create({
@ -468,10 +441,4 @@ var styles = StyleSheet.create({
fontWeight: '500', fontWeight: '500',
color: 'blue', color: 'blue',
}, },
tnfBorderRadiusView: {
borderTopLeftRadius: 10,
borderTopRightRadius: 5,
borderBottomLeftRadius: 0,
borderBottomRightRadius: 20
}
}); });

Просмотреть файл

@ -92,9 +92,6 @@ var TouchableNativeFeedback = React.createClass({
/** /**
* Creates an object that represents android theme's default background for * Creates an object that represents android theme's default background for
* selectable elements (?android:attr/selectableItemBackground). * selectable elements (?android:attr/selectableItemBackground).
*
* The backgrounds generated by this method DO NOT follow the child view's
* border radii styles. To clip the background with border radii, use Ripple.
*/ */
SelectableBackground: function() { SelectableBackground: function() {
return {type: 'ThemeAttrAndroid', attribute: 'selectableItemBackground'}; return {type: 'ThemeAttrAndroid', attribute: 'selectableItemBackground'};
@ -114,9 +111,6 @@ var TouchableNativeFeedback = React.createClass({
* example of that behavior). This background type is available on Android * example of that behavior). This background type is available on Android
* API level 21+. * API level 21+.
* *
* The non-borderless backgrounds generated by this method follows the child
* view's border radii styles (e.g. the ripples are clipped by the border radii).
*
* @param color The ripple color * @param color The ripple color
* @param borderless If the ripple can render outside it's bounds * @param borderless If the ripple can render outside it's bounds
*/ */

Просмотреть файл

@ -9,12 +9,10 @@
package com.facebook.react.views.view; package com.facebook.react.views.view;
import javax.annotation.Nullable;
import android.content.Context; import android.content.Context;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.PaintDrawable; import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable; import android.graphics.drawable.RippleDrawable;
import android.os.Build; import android.os.Build;
@ -36,13 +34,6 @@ public class ReactDrawableHelper {
public static Drawable createDrawableFromJSDescription( public static Drawable createDrawableFromJSDescription(
Context context, Context context,
ReadableMap drawableDescriptionDict) { ReadableMap drawableDescriptionDict) {
return createDrawableFromJSDescription(context, drawableDescriptionDict, null);
}
public static Drawable createDrawableFromJSDescription(
Context context,
ReadableMap drawableDescriptionDict,
@Nullable float[] cornerRadii) {
String type = drawableDescriptionDict.getString("type"); String type = drawableDescriptionDict.getString("type");
if ("ThemeAttrAndroid".equals(type)) { if ("ThemeAttrAndroid".equals(type)) {
String attr = drawableDescriptionDict.getString("attribute"); String attr = drawableDescriptionDict.getString("attribute");
@ -84,14 +75,11 @@ public class ReactDrawableHelper {
"couldn't be resolved into a drawable"); "couldn't be resolved into a drawable");
} }
} }
PaintDrawable mask = null; Drawable mask = null;
if (!drawableDescriptionDict.hasKey("borderless") || if (!drawableDescriptionDict.hasKey("borderless") ||
drawableDescriptionDict.isNull("borderless") || drawableDescriptionDict.isNull("borderless") ||
!drawableDescriptionDict.getBoolean("borderless")) { !drawableDescriptionDict.getBoolean("borderless")) {
mask = new PaintDrawable(Color.WHITE); mask = new ColorDrawable(Color.WHITE);
if (cornerRadii != null) {
mask.setCornerRadii(cornerRadii);
}
} }
ColorStateList colorStateList = new ColorStateList( ColorStateList colorStateList = new ColorStateList(
new int[][] {new int[]{}}, new int[][] {new int[]{}},

Просмотреть файл

@ -236,25 +236,6 @@ import com.facebook.csslayout.Spacing;
} }
} }
/* package */ float[] getBorderRadii() {
float defaultBorderRadius = !CSSConstants.isUndefined(mBorderRadius) ? mBorderRadius : 0;
float topLeftRadius = mBorderCornerRadii != null && !CSSConstants.isUndefined(mBorderCornerRadii[0]) ? mBorderCornerRadii[0] : defaultBorderRadius;
float topRightRadius = mBorderCornerRadii != null && !CSSConstants.isUndefined(mBorderCornerRadii[1]) ? mBorderCornerRadii[1] : defaultBorderRadius;
float bottomRightRadius = mBorderCornerRadii != null && !CSSConstants.isUndefined(mBorderCornerRadii[2]) ? mBorderCornerRadii[2] : defaultBorderRadius;
float bottomLeftRadius = mBorderCornerRadii != null && !CSSConstants.isUndefined(mBorderCornerRadii[3]) ? mBorderCornerRadii[3] : defaultBorderRadius;
return new float[] {
topLeftRadius,
topLeftRadius,
topRightRadius,
topRightRadius,
bottomRightRadius,
bottomRightRadius,
bottomLeftRadius,
bottomLeftRadius
};
}
private void updatePath() { private void updatePath() {
if (!mNeedUpdatePathForBorderRadius) { if (!mNeedUpdatePathForBorderRadius) {
return; return;
@ -277,10 +258,25 @@ import com.facebook.csslayout.Spacing;
mTempRectForBorderRadius.inset(fullBorderWidth * 0.5f, fullBorderWidth * 0.5f); mTempRectForBorderRadius.inset(fullBorderWidth * 0.5f, fullBorderWidth * 0.5f);
} }
float[] borderRadii = getBorderRadii(); float defaultBorderRadius = !CSSConstants.isUndefined(mBorderRadius) ? mBorderRadius : 0;
float topLeftRadius = mBorderCornerRadii != null && !CSSConstants.isUndefined(mBorderCornerRadii[0]) ? mBorderCornerRadii[0] : defaultBorderRadius;
float topRightRadius = mBorderCornerRadii != null && !CSSConstants.isUndefined(mBorderCornerRadii[1]) ? mBorderCornerRadii[1] : defaultBorderRadius;
float bottomRightRadius = mBorderCornerRadii != null && !CSSConstants.isUndefined(mBorderCornerRadii[2]) ? mBorderCornerRadii[2] : defaultBorderRadius;
float bottomLeftRadius = mBorderCornerRadii != null && !CSSConstants.isUndefined(mBorderCornerRadii[3]) ? mBorderCornerRadii[3] : defaultBorderRadius;
mPathForBorderRadius.addRoundRect( mPathForBorderRadius.addRoundRect(
mTempRectForBorderRadius, mTempRectForBorderRadius,
borderRadii, new float[] {
topLeftRadius,
topLeftRadius,
topRightRadius,
topRightRadius,
bottomRightRadius,
bottomRightRadius,
bottomLeftRadius,
bottomLeftRadius
},
Path.Direction.CW); Path.Direction.CW);
float extraRadiusForOutline = 0; float extraRadiusForOutline = 0;
@ -292,14 +288,14 @@ import com.facebook.csslayout.Spacing;
mPathForBorderRadiusOutline.addRoundRect( mPathForBorderRadiusOutline.addRoundRect(
mTempRectForBorderRadiusOutline, mTempRectForBorderRadiusOutline,
new float[] { new float[] {
borderRadii[0] + extraRadiusForOutline, topLeftRadius + extraRadiusForOutline,
borderRadii[1] + extraRadiusForOutline, topLeftRadius + extraRadiusForOutline,
borderRadii[2] + extraRadiusForOutline, topRightRadius + extraRadiusForOutline,
borderRadii[3] + extraRadiusForOutline, topRightRadius + extraRadiusForOutline,
borderRadii[4] + extraRadiusForOutline, bottomRightRadius + extraRadiusForOutline,
borderRadii[5] + extraRadiusForOutline, bottomRightRadius + extraRadiusForOutline,
borderRadii[6] + extraRadiusForOutline, bottomLeftRadius + extraRadiusForOutline,
borderRadii[7] + extraRadiusForOutline bottomLeftRadius + extraRadiusForOutline
}, },
Path.Direction.CW); Path.Direction.CW);

Просмотреть файл

@ -22,7 +22,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import com.facebook.infer.annotation.Assertions; import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.annotations.VisibleForTesting; import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.touch.ReactHitSlopView; import com.facebook.react.touch.ReactHitSlopView;
import com.facebook.react.touch.ReactInterceptingViewGroup; import com.facebook.react.touch.ReactInterceptingViewGroup;
@ -95,7 +94,6 @@ public class ReactViewGroup extends ViewGroup implements
private @Nullable ReactViewBackgroundDrawable mReactBackgroundDrawable; private @Nullable ReactViewBackgroundDrawable mReactBackgroundDrawable;
private @Nullable OnInterceptTouchEventListener mOnInterceptTouchEventListener; private @Nullable OnInterceptTouchEventListener mOnInterceptTouchEventListener;
private boolean mNeedsOffscreenAlphaCompositing = false; private boolean mNeedsOffscreenAlphaCompositing = false;
private @Nullable ReadableMap mNativeBackground;
public ReactViewGroup(Context context) { public ReactViewGroup(Context context) {
super(context); super(context);
@ -136,21 +134,7 @@ public class ReactViewGroup extends ViewGroup implements
"This method is not supported for ReactViewGroup instances"); "This method is not supported for ReactViewGroup instances");
} }
public void setNativeBackground(@Nullable ReadableMap nativeBackground) { public void setTranslucentBackgroundDrawable(@Nullable Drawable background) {
mNativeBackground = nativeBackground;
refreshTranslucentBackgroundDrawable();
}
private void refreshTranslucentBackgroundDrawable() {
Drawable background = null;
if (mNativeBackground != null) {
float[] cornerRadii = null;
if (mReactBackgroundDrawable != null) {
cornerRadii = mReactBackgroundDrawable.getBorderRadii();
}
background = ReactDrawableHelper.createDrawableFromJSDescription(getContext(), mNativeBackground, cornerRadii);
}
// it's required to call setBackground to null, as in some of the cases we may set new // it's required to call setBackground to null, as in some of the cases we may set new
// background to be a layer drawable that contains a drawable that has been previously setup // background to be a layer drawable that contains a drawable that has been previously setup
// as a background previously. This will not work correctly as the drawable callback logic is // as a background previously. This will not work correctly as the drawable callback logic is
@ -223,12 +207,10 @@ public class ReactViewGroup extends ViewGroup implements
public void setBorderRadius(float borderRadius) { public void setBorderRadius(float borderRadius) {
getOrCreateReactViewBackground().setRadius(borderRadius); getOrCreateReactViewBackground().setRadius(borderRadius);
refreshTranslucentBackgroundDrawable();
} }
public void setBorderRadius(float borderRadius, int position) { public void setBorderRadius(float borderRadius, int position) {
getOrCreateReactViewBackground().setRadius(borderRadius, position); getOrCreateReactViewBackground().setRadius(borderRadius, position);
refreshTranslucentBackgroundDrawable();
} }
public void setBorderStyle(@Nullable String style) { public void setBorderStyle(@Nullable String style) {

Просмотреть файл

@ -101,7 +101,8 @@ public class ReactViewManager extends ViewGroupManager<ReactViewGroup> {
@ReactProp(name = "nativeBackgroundAndroid") @ReactProp(name = "nativeBackgroundAndroid")
public void setNativeBackground(ReactViewGroup view, @Nullable ReadableMap bg) { public void setNativeBackground(ReactViewGroup view, @Nullable ReadableMap bg) {
view.setNativeBackground(bg); view.setTranslucentBackgroundDrawable(bg == null ?
null : ReactDrawableHelper.createDrawableFromJSDescription(view.getContext(), bg));
} }
@ReactProp(name = ReactClippingViewGroupHelper.PROP_REMOVE_CLIPPED_SUBVIEWS) @ReactProp(name = ReactClippingViewGroupHelper.PROP_REMOVE_CLIPPED_SUBVIEWS)