|
| 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 com.facebook.common.logging.FLog |
| 13 | +import com.facebook.react.common.ReactConstants |
| 14 | + |
| 15 | +/** Helper to handle implementing ViewGroups with custom drawing order based on z-index. */ |
| 16 | +public class ViewGroupDrawingOrderHelper(private val viewGroup: ViewGroup) { |
| 17 | + private var numberOfChildrenWithZIndex = 0 |
| 18 | + private var drawingOrderIndices: IntArray? = null |
| 19 | + |
| 20 | + /** |
| 21 | + * This should be called every time a view is added to the ViewGroup in [ViewGroup.addView]. |
| 22 | + * |
| 23 | + * @param view The view that is being added |
| 24 | + */ |
| 25 | + public fun handleAddView(view: View) { |
| 26 | + if (ViewGroupManager.getViewZIndex(view) != null) { |
| 27 | + numberOfChildrenWithZIndex++ |
| 28 | + } |
| 29 | + |
| 30 | + drawingOrderIndices = null |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * This should be called every time a view is removed from the ViewGroup in [ViewGroup.removeView] |
| 35 | + * and [ViewGroup.removeViewAt]. |
| 36 | + * |
| 37 | + * @param view The view that is being removed. |
| 38 | + */ |
| 39 | + public fun handleRemoveView(view: View?) { |
| 40 | + if (ViewGroupManager.getViewZIndex(view) != null) { |
| 41 | + numberOfChildrenWithZIndex-- |
| 42 | + } |
| 43 | + |
| 44 | + drawingOrderIndices = null |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * If the ViewGroup should enable drawing order. ViewGroups should call |
| 49 | + * [ViewGroup.setChildrenDrawingOrderEnabled] with the value returned from this method when a view |
| 50 | + * is added or removed. |
| 51 | + */ |
| 52 | + public fun shouldEnableCustomDrawingOrder(): Boolean = numberOfChildrenWithZIndex > 0 |
| 53 | + |
| 54 | + /** |
| 55 | + * The index of the child view that should be drawn. This should be used in |
| 56 | + * [ViewGroup.getChildDrawingOrder]. |
| 57 | + */ |
| 58 | + public fun getChildDrawingOrder(childCount: Int, index: Int): Int { |
| 59 | + var currentDrawingOrderIndices = this.drawingOrderIndices |
| 60 | + if (currentDrawingOrderIndices != null && |
| 61 | + (index >= currentDrawingOrderIndices.size || |
| 62 | + currentDrawingOrderIndices[index] >= childCount)) { |
| 63 | + FLog.w( |
| 64 | + ReactConstants.TAG, |
| 65 | + "getChildDrawingOrder index out of bounds! Please check any custom view manipulations you" + |
| 66 | + " may have done. childCount = %d, index = %d", |
| 67 | + childCount, |
| 68 | + index) |
| 69 | + update() |
| 70 | + } |
| 71 | + |
| 72 | + if (currentDrawingOrderIndices == null) { |
| 73 | + val viewsToSort = ArrayList<View>() |
| 74 | + for (i in 0 until childCount) { |
| 75 | + viewsToSort.add(viewGroup.getChildAt(i)) |
| 76 | + } |
| 77 | + |
| 78 | + // Sort the views by zIndex |
| 79 | + viewsToSort.sortWith( |
| 80 | + Comparator { view1, view2 -> |
| 81 | + val view1ZIndex = ViewGroupManager.getViewZIndex(view1) ?: 0 |
| 82 | + val view2ZIndex = ViewGroupManager.getViewZIndex(view2) ?: 0 |
| 83 | + view1ZIndex - view2ZIndex |
| 84 | + }) |
| 85 | + |
| 86 | + currentDrawingOrderIndices = IntArray(childCount) |
| 87 | + for (i in 0 until childCount) { |
| 88 | + val child = viewsToSort[i] |
| 89 | + currentDrawingOrderIndices[i] = viewGroup.indexOfChild(child) |
| 90 | + } |
| 91 | + |
| 92 | + this.drawingOrderIndices = currentDrawingOrderIndices |
| 93 | + } |
| 94 | + |
| 95 | + return currentDrawingOrderIndices[index] |
| 96 | + } |
| 97 | + |
| 98 | + /** Recheck all children for z-index changes. */ |
| 99 | + public fun update() { |
| 100 | + numberOfChildrenWithZIndex = 0 |
| 101 | + for (i in 0 until viewGroup.childCount) { |
| 102 | + val child = viewGroup.getChildAt(i) |
| 103 | + if (ViewGroupManager.getViewZIndex(child) != null) { |
| 104 | + numberOfChildrenWithZIndex++ |
| 105 | + } |
| 106 | + } |
| 107 | + drawingOrderIndices = null |
| 108 | + } |
| 109 | +} |
0 commit comments