|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.uimanager; |
| 9 | + |
| 10 | +import android.view.View; |
| 11 | +import android.view.ViewGroup; |
| 12 | +import androidx.annotation.Nullable; |
| 13 | +import com.facebook.infer.annotation.Nullsafe; |
| 14 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 15 | +import com.facebook.react.bridge.UiThreadUtil; |
| 16 | +import java.util.List; |
| 17 | +import java.util.WeakHashMap; |
| 18 | + |
| 19 | +/** Class providing children management API for view managers of classes extending ViewGroup. */ |
| 20 | +@Nullsafe(Nullsafe.Mode.LOCAL) |
| 21 | +public abstract class ViewGroupManager<T extends ViewGroup> |
| 22 | + extends BaseViewManager<T, LayoutShadowNode> implements IViewGroupManager<T> { |
| 23 | + |
| 24 | + private static WeakHashMap<View, Integer> mZIndexHash = new WeakHashMap<>(); |
| 25 | + |
| 26 | + public ViewGroupManager() { |
| 27 | + super(null); |
| 28 | + } |
| 29 | + |
| 30 | + public ViewGroupManager(@Nullable ReactApplicationContext reactContext) { |
| 31 | + super(reactContext); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public LayoutShadowNode createShadowNodeInstance() { |
| 36 | + return new LayoutShadowNode(); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Class<? extends LayoutShadowNode> getShadowNodeClass() { |
| 41 | + return LayoutShadowNode.class; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void updateExtraData(T root, Object extraData) {} |
| 46 | + |
| 47 | + @Override |
| 48 | + public void addView(T parent, View child, int index) { |
| 49 | + parent.addView(child, index); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Convenience method for batching a set of addView calls Note that this adds the views to the |
| 54 | + * beginning of the ViewGroup |
| 55 | + * |
| 56 | + * @param parent the parent ViewGroup |
| 57 | + * @param views the set of views to add |
| 58 | + */ |
| 59 | + public void addViews(T parent, List<View> views) { |
| 60 | + UiThreadUtil.assertOnUiThread(); |
| 61 | + |
| 62 | + for (int i = 0, size = views.size(); i < size; i++) { |
| 63 | + addView(parent, views.get(i), i); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public static void setViewZIndex(View view, int zIndex) { |
| 68 | + mZIndexHash.put(view, zIndex); |
| 69 | + } |
| 70 | + |
| 71 | + public static @Nullable Integer getViewZIndex(@Nullable View view) { |
| 72 | + return mZIndexHash.get(view); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public int getChildCount(T parent) { |
| 77 | + return parent.getChildCount(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + @Nullable |
| 82 | + public View getChildAt(T parent, int index) { |
| 83 | + return parent.getChildAt(index); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void removeViewAt(T parent, int index) { |
| 88 | + UiThreadUtil.assertOnUiThread(); |
| 89 | + |
| 90 | + parent.removeViewAt(index); |
| 91 | + } |
| 92 | + |
| 93 | + public void removeView(T parent, View view) { |
| 94 | + UiThreadUtil.assertOnUiThread(); |
| 95 | + |
| 96 | + for (int i = 0; i < getChildCount(parent); i++) { |
| 97 | + if (getChildAt(parent, i) == view) { |
| 98 | + removeViewAt(parent, i); |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns whether this View type needs to handle laying out its own children instead of deferring |
| 106 | + * to the standard css-layout algorithm. Returns true for the layout to *not* be automatically |
| 107 | + * invoked. Instead onLayout will be invoked as normal and it is the View instance's |
| 108 | + * responsibility to properly call layout on its children. Returns false for the default behavior |
| 109 | + * of automatically laying out children without going through the ViewGroup's onLayout method. In |
| 110 | + * that case, onLayout for this View type must *not* call layout on its children. |
| 111 | + */ |
| 112 | + @Override |
| 113 | + public boolean needsCustomLayoutForChildren() { |
| 114 | + return false; |
| 115 | + } |
| 116 | +} |
0 commit comments