I have a `Box` and I want to use `pointerInput` t...
# compose
d
I have a
Box
and I want to use
pointerInput
to detect drag gestures inside this box, but I want to ignore cases when drag starts in the smaller child in center of that box. For example box is 400x400.dp and I want to ignore drags coming from within 100x100.dp of its centered child. How can I achieve this? Use
onGlobalLayout
to capture the position and size of the child and then in
onDrag
check if
change: PointerInputChange
comes from a child area and do not do what I usually do there in this case?
m
You can observe the
interactionSource
of the child, and do nothing in case it was being dragged:
Copy code
val interactionSource = remember { MutableInteractionSource() }
val isChildDragged by interactionSource.collectIsDraggedAsState()
Box(
   modifier = Modifier.pointerInput{
   // check if isChildDragged or not
}
) {
    Child(
modifier = Modifier.draggable(
    interactionSource = interactionSource,
    ...
)
)
}
z
You don't need
onGloballyPositioned
for this case,
onPlaced
is sufficient and much cheaper since you're only using it to grab the LayoutCoordinates and then read it later.
Then yea you're on the right track, just make sure not to consume the events when you want to ignore them.
d
Thanks for suggestions! By "not consuming" you mean simply not doing the usual action, or do I have to somehow explicitly state that I do not consume a certain event? I'm not very familiar with gesture APIs yet...
z
When you are processing the events for a drag, you need to explicitly consume them (by calling the consume* methods on the event), so the views underneath you don't process them again.
This isn't a compose thing, pointer events work this way in views too the api is just different.
d
right, in the View api I was returning true/false from onTouchEvent. In compose I plan to use
pointerInput
+
detectDragGestures
. I see that
change
parameter of the latter has the
consume
method, so I will play with this! 🙂