diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.java deleted file mode 100644 index 49457129f44838..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.java +++ /dev/null @@ -1,116 +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.view.View; -import android.view.ViewGroup; -import androidx.annotation.Nullable; -import com.facebook.infer.annotation.Nullsafe; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.UiThreadUtil; -import java.util.List; -import java.util.WeakHashMap; - -/** Class providing children management API for view managers of classes extending ViewGroup. */ -@Nullsafe(Nullsafe.Mode.LOCAL) -public abstract class ViewGroupManager - extends BaseViewManager implements IViewGroupManager { - - private static WeakHashMap mZIndexHash = new WeakHashMap<>(); - - public ViewGroupManager() { - super(null); - } - - public ViewGroupManager(@Nullable ReactApplicationContext reactContext) { - super(reactContext); - } - - @Override - public LayoutShadowNode createShadowNodeInstance() { - return new LayoutShadowNode(); - } - - @Override - public Class getShadowNodeClass() { - return LayoutShadowNode.class; - } - - @Override - public void updateExtraData(T root, Object extraData) {} - - @Override - public void addView(T parent, View child, int index) { - parent.addView(child, index); - } - - /** - * Convenience method for batching a set of addView calls Note that this adds the views to the - * beginning of the ViewGroup - * - * @param parent the parent ViewGroup - * @param views the set of views to add - */ - public void addViews(T parent, List views) { - UiThreadUtil.assertOnUiThread(); - - for (int i = 0, size = views.size(); i < size; i++) { - addView(parent, views.get(i), i); - } - } - - public static void setViewZIndex(View view, int zIndex) { - mZIndexHash.put(view, zIndex); - } - - public static @Nullable Integer getViewZIndex(@Nullable View view) { - return mZIndexHash.get(view); - } - - @Override - public int getChildCount(T parent) { - return parent.getChildCount(); - } - - @Override - @Nullable - public View getChildAt(T parent, int index) { - return parent.getChildAt(index); - } - - @Override - public void removeViewAt(T parent, int index) { - UiThreadUtil.assertOnUiThread(); - - parent.removeViewAt(index); - } - - public void removeView(T parent, View view) { - UiThreadUtil.assertOnUiThread(); - - for (int i = 0; i < getChildCount(parent); i++) { - if (getChildAt(parent, i) == view) { - removeViewAt(parent, i); - break; - } - } - } - - /** - * Returns whether this View type needs to handle laying out its own children instead of deferring - * to the standard css-layout algorithm. Returns true for the layout to *not* be automatically - * invoked. Instead onLayout will be invoked as normal and it is the View instance's - * responsibility to properly call layout on its children. Returns false for the default behavior - * of automatically laying out children without going through the ViewGroup's onLayout method. In - * that case, onLayout for this View type must *not* call layout on its children. - */ - @Override - public boolean needsCustomLayoutForChildren() { - return false; - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.kt new file mode 100644 index 00000000000000..e2afeb09c0b388 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.kt @@ -0,0 +1,83 @@ +/* + * 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.view.View +import android.view.ViewGroup +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.UiThreadUtil +import java.util.WeakHashMap + + +public abstract class ViewGroupManager +@JvmOverloads +constructor(reactContext: ReactApplicationContext? = null) : + BaseViewManager(reactContext), IViewGroupManager { + + public override fun createShadowNodeInstance(): LayoutShadowNode = LayoutShadowNode() + + public override fun getShadowNodeClass(): Class = + LayoutShadowNode::class.java + + public override fun updateExtraData(root: T, extraData: Any) {} + + public override fun addView(parent: T, child: View, index: Int): Unit = + parent.addView(child, index) + + /** + * Convenience method for batching a set of addView calls Note that this adds the views to the + * beginning of the ViewGroup + * + * @param parent the parent ViewGroup + * @param views the set of views to add + */ + public fun addViews(parent: T, views: List) { + UiThreadUtil.assertOnUiThread() + views.forEachIndexed { i, view -> addView(parent, view, i) } + } + + public override fun getChildCount(parent: T): Int = parent.childCount + + public override fun getChildAt(parent: T, index: Int): View? = parent.getChildAt(index) + + public override fun removeViewAt(parent: T, index: Int) { + UiThreadUtil.assertOnUiThread() + parent.removeViewAt(index) + } + + public fun removeView(parent: T, view: View) { + UiThreadUtil.assertOnUiThread() + + for (i in 0 until getChildCount(parent)) { + if (getChildAt(parent, i) === view) { + + removeViewAt(parent, i) + break + } + } + } + + /** + * Returns whether this View type needs to handle laying out its own children instead of deferring + * to the standard css-layout algorithm. Returns true for the layout to *not* be automatically + * invoked. Instead onLayout will be invoked as normal and it is the View instance's + * responsibility to properly call layout on its children. Returns false for the default behavior + * of automatically laying out children without going through the ViewGroup's onLayout method. In + * that case, onLayout for this View type must *not* call layout on its children. + */ + public override fun needsCustomLayoutForChildren(): Boolean = false + + public companion object { + private val zIndexHash: WeakHashMap = WeakHashMap() + + @JvmStatic + public fun setViewZIndex(view: View, zIndex: Int): Unit = zIndexHash.set(view, zIndex) + + @JvmStatic public fun getViewZIndex(view: View?): Int? = zIndexHash[view] + } +}