https://kotlinlang.org logo
#compose
Title
# compose
m

Mark

03/08/2024, 5:02 AM
I’m using M3
ModalNavigationDrawer
with
gesturesEnabled = true
. I noticed that when the
Scaffold
content is something like
HorziontalPager
the open drawer gesture doesn’t work, with the pager seeming taking priority. This is good. However, when the scaffold content contains my custom composable where the user can draw strokes (I use
Modifier.pointerInteropFilter
for this), then the drawer gestures take priority and so no strokes can be drawn. My current workaround is to explicitly disable drawer gestures when that composable is visible. But is there a better way perhaps involving priority with which pointer events are consumed?
a

Albert Chang

03/08/2024, 1:25 PM
There is a
requestDisallowInterceptTouchEvent
parameter. You should invoke the lambda if you don’t want pointer input to be stolen by parent, same as in the view world.
m

Mark

03/08/2024, 2:32 PM
Thank Albert. So I see I can pass an instance of
RequestDisallowInterceptTouchEvent()
to
pointerInteropFilter
and the documentation says “you can later call it” but when should it be called?
a

Albert Chang

03/08/2024, 3:01 PM
When you detected that a gesture has started and you want to handle the following events of the gesture.
m

Mark

03/09/2024, 2:38 AM
Do you mean at the
ACTION_DOWN
here? Seems weird though seems to work!
Copy code
val requestDisallowInterceptTouchEvent = remember { RequestDisallowInterceptTouchEvent() }
modifier = Modifier
    .pointerInteropFilter(
        requestDisallowInterceptTouchEvent = requestDisallowInterceptTouchEvent,
    ) { event ->
        val offset = Offset(event.x, event.y)
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                requestDisallowInterceptTouchEvent(true)
                onStartStroke(offset)
            }
            MotionEvent.ACTION_MOVE -> onPushPoint(offset)
            MotionEvent.ACTION_UP -> onFinishStroke(offset)
        }
        true
    }
a

Albert Chang

03/09/2024, 3:55 AM
Yeah that's how it works.
thank you color 1
m

Mark

03/09/2024, 5:59 AM
Seems I don’t need to
requestDisallowInterceptTouchEvent(false)
in
ACTION_UP
a

Albert Chang

03/09/2024, 6:00 AM
Yes, it will be reset for each gesture.
👍 1
4 Views