From a96bdb7154b0d8c7f43977d8a583e8d2cbdcb795 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Mon, 22 Nov 2021 03:41:06 -0800 Subject: [PATCH] Support setting hitSlop with single value Summary: The native side supports this in https://github.com/facebook/react-native/blob/main/ReactCommon/react/renderer/graphics/conversions.h#L156 but it fails on the Java side. Changelog: [Android][Fixed] Enable hitSlop to be set using a single number. Reviewed By: yungsters Differential Revision: D32138347 fbshipit-source-id: 266ec061c6849d845b592cf245cc0599055b8522 --- .../react/views/view/ReactViewManager.java | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java index 1192b50f2e4cc7..ba4a1d5cb9567a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java @@ -13,6 +13,7 @@ import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import com.facebook.react.bridge.Dynamic; import com.facebook.react.bridge.JSApplicationIllegalArgumentException; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReadableArray; @@ -126,22 +127,35 @@ public void setBorderStyle(ReactViewGroup view, @Nullable String borderStyle) { } @ReactProp(name = "hitSlop") - public void setHitSlop(final ReactViewGroup view, @Nullable ReadableMap hitSlop) { - if (hitSlop == null) { - view.setHitSlopRect(null); - } else { - view.setHitSlopRect( - new Rect( - hitSlop.hasKey("left") - ? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("left")) - : 0, - hitSlop.hasKey("top") ? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("top")) : 0, - hitSlop.hasKey("right") - ? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("right")) - : 0, - hitSlop.hasKey("bottom") - ? (int) PixelUtil.toPixelFromDIP(hitSlop.getDouble("bottom")) - : 0)); + public void setHitSlop(final ReactViewGroup view, Dynamic hitSlop) { + switch (hitSlop.getType()) { + case Null: + view.setHitSlopRect(null); + break; + case Map: + ReadableMap hitSlopMap = hitSlop.asMap(); + view.setHitSlopRect( + new Rect( + hitSlopMap.hasKey("left") + ? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("left")) + : 0, + hitSlopMap.hasKey("top") + ? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("top")) + : 0, + hitSlopMap.hasKey("right") + ? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("right")) + : 0, + hitSlopMap.hasKey("bottom") + ? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("bottom")) + : 0)); + break; + case Number: + int hitSlopValue = (int) PixelUtil.toPixelFromDIP(hitSlop.asDouble()); + view.setHitSlopRect(new Rect(hitSlopValue, hitSlopValue, hitSlopValue, hitSlopValue)); + break; + default: + throw new JSApplicationIllegalArgumentException( + "Invalid type for 'hitSlop' value " + hitSlop.getType()); } }