Skip to content

Commit

Permalink
implement BorderRadiiDrawableUtils on 0.76
Browse files Browse the repository at this point in the history
  • Loading branch information
jakex7 committed Sep 19, 2024
1 parent e3136da commit 3428f75
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,37 @@ public BorderRadii(
}

public static BorderRadii getBorderRadii(View view) {
// if (view.getBackground() != null) {
// return BorderRadiiDrawableUtils.getBorderRadii(view);
// } else if (view instanceof ReactImageView) {
// try {
// if (mBorderRadiusField == null) {
// mBorderRadiusField = ReactImageView.class.getDeclaredField("mBorderRadius");
// mBorderRadiusField.setAccessible(true);
// }
// float fullBorderRadius = mBorderRadiusField.getFloat(view);
// if (getCornerRadiiMethod == null) {
// getCornerRadiiMethod =
// ReactImageView.class.getDeclaredMethod("getCornerRadii", float[].class);
// getCornerRadiiMethod.setAccessible(true);
// }
// if (Float.isNaN(fullBorderRadius)) {
// fullBorderRadius = 0;
// }
// float[] cornerRadii = new float[4];
// getCornerRadiiMethod.invoke(view, (Object) cornerRadii);
// return new BorderRadii(
// fullBorderRadius, cornerRadii[0], cornerRadii[1], cornerRadii[2], cornerRadii[3]);
// } catch (NullPointerException
// | NoSuchFieldException
// | NoSuchMethodException
// | IllegalAccessException
// | InvocationTargetException ignored) {
// // In case of non-standard view is better to not support the border animation
// // instead of throwing exception
// }
// }
if (view.getBackground() != null) {
return BorderRadiiDrawableUtils.getBorderRadii(view);
// The rest can be safely removed once support for version 0.75 is dropped.
} else if (view instanceof ReactImageView) {
try {
if (mBorderRadiusField == null) {
mBorderRadiusField = ReactImageView.class.getDeclaredField("mBorderRadius");
mBorderRadiusField.setAccessible(true);
}
float fullBorderRadius = mBorderRadiusField.getFloat(view);
if (getCornerRadiiMethod == null) {
getCornerRadiiMethod =
ReactImageView.class.getDeclaredMethod("getCornerRadii", float[].class);
getCornerRadiiMethod.setAccessible(true);
}
if (Float.isNaN(fullBorderRadius)) {
fullBorderRadius = 0;
}
float[] cornerRadii = new float[4];
getCornerRadiiMethod.invoke(view, (Object) cornerRadii);
return new BorderRadii(
fullBorderRadius, cornerRadii[0], cornerRadii[1], cornerRadii[2], cornerRadii[3]);
} catch (NullPointerException
| NoSuchFieldException
| NoSuchMethodException
| IllegalAccessException
| InvocationTargetException ignored) {
// In case of non-standard view is better to not support the border animation
// instead of throwing exception
}
}
return new BorderRadii(0, 0, 0, 0, 0);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,49 @@
package com.swmansion.reanimated;

import android.graphics.drawable.Drawable;
import android.graphics.Rect;
import android.view.View;
import com.facebook.react.uimanager.BackgroundStyleApplicator;
import com.facebook.react.uimanager.LengthPercentage;
import com.facebook.react.uimanager.drawable.CSSBackgroundDrawable;
import com.facebook.react.uimanager.style.ComputedBorderRadius;
import com.facebook.react.uimanager.style.BorderRadiusProp;

public class BorderRadiiDrawableUtils {
public static ReactNativeUtils.BorderRadii getBorderRadii(View view) {
// Drawable background = view.getBackground();
// if (background instanceof CSSBackgroundDrawable) {
// CSSBackgroundDrawable drawable = (CSSBackgroundDrawable) background;
// LengthPercentage uniform = drawable.getBorderRadius().getUniform();
// float full = uniform != null ? uniform.resolve(view.getWidth(), view.getHeight()) : Float.NaN;
// ComputedBorderRadius computedBorderRadius = drawable.getComputedBorderRadius();
// return new ReactNativeUtils.BorderRadii(
// full,
// computedBorderRadius.getTopLeft(),
// computedBorderRadius.getTopRight(),
// computedBorderRadius.getBottomLeft(),
// computedBorderRadius.getBottomRight());
// } else {
return new ReactNativeUtils.BorderRadii(0, 0, 0, 0, 0);
// }
Rect bounds = view.getBackground().getBounds();
LengthPercentage full =
BackgroundStyleApplicator.getBorderRadius(view, BorderRadiusProp.BORDER_RADIUS);
LengthPercentage topLeft =
BackgroundStyleApplicator.getBorderRadius(view, BorderRadiusProp.BORDER_TOP_LEFT_RADIUS);
LengthPercentage topRight =
BackgroundStyleApplicator.getBorderRadius(view, BorderRadiusProp.BORDER_TOP_RIGHT_RADIUS);
LengthPercentage bottomLeft =
BackgroundStyleApplicator.getBorderRadius(view, BorderRadiusProp.BORDER_BOTTOM_LEFT_RADIUS);
LengthPercentage bottomRight =
BackgroundStyleApplicator.getBorderRadius(
view, BorderRadiusProp.BORDER_BOTTOM_RIGHT_RADIUS);

// This logic is valid only when `LengthPercentage.getType()` equals
// `LengthPercentageType.POINT`. However, since the `SET` logic is only applicable on paper,
// which only supports the `POINT` type, it's safe to assume that the type will always be
// `POINT`.
// https://github.com/facebook/react-native/blob/6d51c5cd8ef817fe30f01a10f2af682fc02a5fb7/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java#L164
return new ReactNativeUtils.BorderRadii(
full == null
? 0
: full.resolve(bounds.width(), bounds.height()).toPixelFromDIP().getHorizontal(),
topLeft == null
? Float.NaN
: topLeft.resolve(bounds.width(), bounds.height()).toPixelFromDIP().getHorizontal(),
topRight == null
? Float.NaN
: topRight.resolve(bounds.width(), bounds.height()).toPixelFromDIP().getHorizontal(),
bottomLeft == null
? Float.NaN
: bottomLeft.resolve(bounds.width(), bounds.height()).toPixelFromDIP().getHorizontal(),
bottomRight == null
? Float.NaN
: bottomRight
.resolve(bounds.width(), bounds.height())
.toPixelFromDIP()
.getHorizontal());
}
}

0 comments on commit 3428f75

Please sign in to comment.