From 2834825b8ba01119b4ae3a01ec641defaea1a1f2 Mon Sep 17 00:00:00 2001 From: "priyanka.raghuvanshi" Date: Mon, 14 Apr 2025 18:18:36 -0700 Subject: [PATCH] Migrate ReactClippingViewGroupHelper.java to Kotlin (#50679) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Migrated ReactClippingViewGroupHelper.java to Kotlin as part of the React Native Kotlin migration initiative. This change helps modernize the React Native Android codebase and improve maintainability. No functional changes were introduced—only a language conversion while preserving the original logic and behavior. ## Changelog: [ANDROID] [CHANGED] - Migrated ReactClippingViewGroupHelper.java to Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/50679 Test Plan: ✅ Ran yarn && yarn android in the root directory and verified RNTester builds successfully. ✅ Opened RNTester on an Android emulator/device and navigated through various screens to ensure the app works as expected. ✅ Specifically tested views with removeClippedSubviews enabled to confirm runtime behavior remains unchanged. ✅ Used [KtFmt](https://github.com/facebook/ktfmt) to format the Kotlin file properly. Reviewed By: arushikesarwani94 Differential Revision: D72973621 Pulled By: cortinico fbshipit-source-id: e6f426a0cb7eb583935a560660900b29786df4c3 --- .../ReactAndroid/api/ReactAndroid.api | 6 +- .../ReactClippingViewGroupHelper.java | 65 ------------------- .../uimanager/ReactClippingViewGroupHelper.kt | 63 ++++++++++++++++++ 3 files changed, 66 insertions(+), 68 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactClippingViewGroupHelper.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactClippingViewGroupHelper.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index f0b6d0b3a3e..f3f9d5ce64a 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -3987,10 +3987,10 @@ public abstract interface class com/facebook/react/uimanager/ReactClippingViewGr public abstract fun updateClippingRect (Ljava/util/Set;)V } -public class com/facebook/react/uimanager/ReactClippingViewGroupHelper { +public final class com/facebook/react/uimanager/ReactClippingViewGroupHelper { + public static final field INSTANCE Lcom/facebook/react/uimanager/ReactClippingViewGroupHelper; public static final field PROP_REMOVE_CLIPPED_SUBVIEWS Ljava/lang/String; - public fun ()V - public static fun calculateClippingRect (Landroid/view/View;Landroid/graphics/Rect;)V + public static final fun calculateClippingRect (Landroid/view/View;Landroid/graphics/Rect;)V } public abstract interface class com/facebook/react/uimanager/ReactCompoundView { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactClippingViewGroupHelper.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactClippingViewGroupHelper.java deleted file mode 100644 index ff88e2471da..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactClippingViewGroupHelper.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.uimanager; - -import android.graphics.Rect; -import android.view.View; -import android.view.ViewParent; -import com.facebook.infer.annotation.Nullsafe; -import javax.annotation.concurrent.NotThreadSafe; - -/** - * Provides implementation of common tasks for view and it's view manager supporting property {@code - * removeClippedSubviews}. - */ -@Nullsafe(Nullsafe.Mode.LOCAL) -@NotThreadSafe -public class ReactClippingViewGroupHelper { - - public static final String PROP_REMOVE_CLIPPED_SUBVIEWS = "removeClippedSubviews"; - - private static final Rect sHelperRect = new Rect(); - - /** - * Can be used by view that support {@code removeClippedSubviews} property to calculate area that - * given {@param view} should be clipped to based on the clipping rectangle of it's parent in case - * when parent is also set to clip it's children. - * - * @param view view that we want to calculate clipping rect for - * @param outputRect where the calculated rectangle will be written - */ - public static void calculateClippingRect(View view, Rect outputRect) { - ViewParent parent = view.getParent(); - if (parent == null) { - outputRect.setEmpty(); - return; - } else if (parent instanceof ReactClippingViewGroup) { - ReactClippingViewGroup clippingViewGroup = (ReactClippingViewGroup) parent; - if (clippingViewGroup.getRemoveClippedSubviews()) { - clippingViewGroup.getClippingRect(sHelperRect); - // Intersect the view with the parent's rectangle - // This will result in the overlap with coordinates in the parent space - if (!sHelperRect.intersect( - view.getLeft(), - view.getTop() + (int) view.getTranslationY(), - view.getRight(), - view.getBottom() + (int) view.getTranslationY())) { - outputRect.setEmpty(); - return; - } - // Now we move the coordinates to the View's coordinate space - sHelperRect.offset(-view.getLeft(), -view.getTop()); - sHelperRect.offset(-(int) view.getTranslationX(), -(int) view.getTranslationY()); - sHelperRect.offset(view.getScrollX(), view.getScrollY()); - outputRect.set(sHelperRect); - return; - } - } - view.getDrawingRect(outputRect); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactClippingViewGroupHelper.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactClippingViewGroupHelper.kt new file mode 100644 index 00000000000..d9f35082cdc --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactClippingViewGroupHelper.kt @@ -0,0 +1,63 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.uimanager + +import android.graphics.Rect +import android.view.View +import javax.annotation.concurrent.NotThreadSafe + +/** + * Provides implementation of common tasks for view and it's view manager supporting property + * [removeClippedSubviews]. + */ +@NotThreadSafe +public object ReactClippingViewGroupHelper { + + public const val PROP_REMOVE_CLIPPED_SUBVIEWS: String = "removeClippedSubviews" + + private val helperRect: Rect = Rect() + + /** + * Can be used by view that support [removeClippedSubviews] property to calculate area that given + * [view] should be clipped to based on the clipping rectangle of it's parent in case when parent + * is also set to clip it's children. + * + * @param view view that we want to calculate clipping rect for + * @param outputRect where the calculated rectangle will be written + */ + @JvmStatic + public fun calculateClippingRect(view: View, outputRect: Rect): Unit { + when (val parent = view.parent) { + null -> { + outputRect.setEmpty() + return + } + is ReactClippingViewGroup -> { + if (parent.removeClippedSubviews) { + parent.getClippingRect(helperRect) + // Intersect the view with the parent's rectangle + if (!helperRect.intersect( + view.left, + view.top + view.translationY.toInt(), + view.right, + view.bottom + view.translationY.toInt())) { + outputRect.setEmpty() + return + } + // Move coordinates to View's coordinate space + helperRect.offset(-view.left, -view.top) + helperRect.offset(-view.translationX.toInt(), -view.translationY.toInt()) + helperRect.offset(view.scrollX, view.scrollY) + outputRect.set(helperRect) + return + } + } + } + view.getDrawingRect(outputRect) + } +}