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

Daniel

03/01/2021, 7:05 PM
I'm planning on filing a bug suggesting that
PointerInputScope.awaitFirstDown
should have an inverse, but I wanted to check if anyone saw anything obvious I was missing first.
Copy code
private suspend fun PointerInputScope.awaitChangedToUp(id: PointerId) {
    awaitPointerEventScope {
        do {
            val event = awaitPointerEvent().changes.fastFirstOrNull { it.id == id }
            event?.consumeAllChanges()
        } while (event?.changedToUp() != true)
    }
}
Not sure if it does exactly what your snippet does 🤔
d

Daniel

03/01/2021, 7:20 PM
Thanks, I didn't notice that's there. Unfortunately it behaves differently from what I want
But it made me realize that large issue, which is that most of the PointerInputScope functions aren't composable.
tryAwaitRelease
works in a
detectTapGestures
, which loops infinitely looking for tap gestures, making it hard to combine with other detectors
t

Tash

03/01/2021, 7:24 PM
Yeah I'm currently trying to work on something where I need to detect taps vs drags, and perform different things. Haven't spent much time on it, but yeah something like
awaitTapUp
or something will be useful to combine with
await..
+
drag { }
Not sure if there's a clean way to do it with current APIs
d

Daniel

03/01/2021, 7:29 PM
Yeah, I have a bunch of states the interaction can be in and my code was getting into a mess. Once I realized I was getting a bunch of concurrency bugs and needed mutexes I decided to rethink
1
So I refactored to something like while(true) { await event; when { isStartOfTurn(event) -> awaitTurnOrCancel(event) ...} }
With standard branching on custom functions on PointerInputScope
@Tash
t

Tash

03/01/2021, 7:43 PM
Oh I see, that might work! Might have to try out something like this for my use case as well. Wish there was a sample regarding this on cs.android.com 😅
👍 1
I ended up submitting a feature request https://issuetracker.google.com/issues/181577176
1
👍🏼 1
t

Tash

03/01/2021, 8:16 PM
Great, thanks for sharing @Daniel ! I see now what you mean with the
when
statement.
👍 1
Just curious, I see inside
awaitChangedToUp
you are doing
event?.consumeAllChanges()
. Is that being done because you need to consume the up + minute position changes as well?
d

Daniel

03/01/2021, 8:21 PM
This code is a modifier that adds selectability to a page
Pages also handle other gestures in other places
The intent of that bit @Tash is that in general gestures I don't handle should be propagated to the page, but gestures that start with a press within the area of the selection should be swallowed
I'm not sure what you mean by minute
Hope that makes sense 🙂
t

Tash

03/01/2021, 8:24 PM
I see. Right, by minute position changes I mean the slight pointer slop that happens even as you tap. Looks like we’d want to swallow those as well, which is why it’s doing
consumeAllChanges()
instead of just
consumeDownChange()
d

Daniel

03/01/2021, 8:25 PM
Oh, I didn't even think of that @Tash. I just use consumeAllChanges by default 🙂
👍🏼 1
4 Views