Skip to content

Commit 1853957

Browse files
fabriziocuccifacebook-github-bot
authored andcommitted
Kotlinify ViewGroupDrawingOrderHelper
Summary: As per title. Changelog: [Internal] Reviewed By: javache Differential Revision: D72566036 fbshipit-source-id: ac3156b247df3818073e31aa44d5c96f62f7b881
1 parent 91acacc commit 1853957

File tree

3 files changed

+115
-133
lines changed

3 files changed

+115
-133
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

+6-6
Original file line numberDiff line numberDiff line change
@@ -4763,13 +4763,13 @@ public final class com/facebook/react/uimanager/ViewDefaults {
47634763
public static final field NUMBER_OF_LINES I
47644764
}
47654765

4766-
public class com/facebook/react/uimanager/ViewGroupDrawingOrderHelper {
4766+
public final class com/facebook/react/uimanager/ViewGroupDrawingOrderHelper {
47674767
public fun <init> (Landroid/view/ViewGroup;)V
4768-
public fun getChildDrawingOrder (II)I
4769-
public fun handleAddView (Landroid/view/View;)V
4770-
public fun handleRemoveView (Landroid/view/View;)V
4771-
public fun shouldEnableCustomDrawingOrder ()Z
4772-
public fun update ()V
4768+
public final fun getChildDrawingOrder (II)I
4769+
public final fun handleAddView (Landroid/view/View;)V
4770+
public final fun handleRemoveView (Landroid/view/View;)V
4771+
public final fun shouldEnableCustomDrawingOrder ()Z
4772+
public final fun update ()V
47734773
}
47744774

47754775
public abstract class com/facebook/react/uimanager/ViewGroupManager : com/facebook/react/uimanager/BaseViewManager, com/facebook/react/uimanager/IViewGroupManager {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupDrawingOrderHelper.java

-127
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)