-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
ComposeGestureTargetLocator.java
106 lines (92 loc) · 3.74 KB
/
ComposeGestureTargetLocator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package io.sentry.compose.gestures;
import androidx.compose.ui.layout.LayoutCoordinatesKt;
import androidx.compose.ui.layout.ModifierInfo;
import androidx.compose.ui.node.LayoutNode;
import androidx.compose.ui.node.Owner;
import androidx.compose.ui.semantics.SemanticsConfiguration;
import androidx.compose.ui.semantics.SemanticsModifier;
import androidx.compose.ui.semantics.SemanticsPropertyKey;
import io.sentry.internal.gestures.GestureTargetLocator;
import io.sentry.internal.gestures.UiElement;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@SuppressWarnings("KotlinInternalInJava")
public final class ComposeGestureTargetLocator implements GestureTargetLocator {
@Override
public @Nullable UiElement locate(
@NotNull Object root, float x, float y, UiElement.Type targetType) {
@Nullable String targetTag = null;
if (!(root instanceof Owner)) {
return null;
}
final @NotNull Queue<LayoutNode> queue = new LinkedList<>();
queue.add(((Owner) root).getRoot());
while (!queue.isEmpty()) {
final @Nullable LayoutNode node = queue.poll();
if (node == null) {
continue;
}
final boolean isPlaced = node.isPlaced();
final boolean inBounds = layoutNodeBoundsContain(node, x, y);
if (node.isPlaced() && layoutNodeBoundsContain(node, x, y)) {
boolean isClickable = false;
boolean isScrollable = false;
@Nullable String testTag = null;
final List<ModifierInfo> modifiers = node.getModifierInfo();
for (ModifierInfo modifierInfo : modifiers) {
if (modifierInfo.getModifier() instanceof SemanticsModifier) {
final SemanticsModifier semanticsModifierCore =
(SemanticsModifier) modifierInfo.getModifier();
final SemanticsConfiguration semanticsConfiguration =
semanticsModifierCore.getSemanticsConfiguration();
for (Map.Entry<? extends SemanticsPropertyKey<?>, ?> entry : semanticsConfiguration) {
final @Nullable String key = entry.getKey().getName();
switch (key) {
case "ScrollBy":
isScrollable = true;
break;
case "OnClick":
isClickable = true;
break;
case "TestTag":
if (entry.getValue() instanceof String) {
testTag = (String) entry.getValue();
}
break;
}
}
}
}
if (isClickable && targetType == UiElement.Type.CLICKABLE) {
targetTag = testTag;
}
if (isScrollable && targetType == UiElement.Type.SCROLLABLE) {
targetTag = testTag;
// skip any children for scrollable targets
break;
}
}
queue.addAll(node.getZSortedChildren().asMutableList());
}
if (targetTag == null) {
return null;
} else {
return new UiElement(null, null, null, targetTag);
}
}
private static boolean layoutNodeBoundsContain(
@NotNull LayoutNode node, final float x, final float y) {
final int nodeHeight = node.getHeight();
final int nodeWidth = node.getWidth();
// Offset is a Kotlin value class, packing x/y into a long
// TODO find a way to use the existing APIs
final long nodePosition = LayoutCoordinatesKt.positionInWindow(node.getCoordinates());
final int nodeX = (int) Float.intBitsToFloat((int) (nodePosition >> 32));
final int nodeY = (int) Float.intBitsToFloat((int) (nodePosition));
return x >= nodeX && x <= (nodeX + nodeWidth) && y >= nodeY && y <= (nodeY + nodeHeight);
}
}