Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't restart the drag gesture when the matcher changes. #976

Merged
merged 1 commit into from Jan 3, 2024

Conversation

m-sasha
Copy link

@m-sasha m-sasha commented Jan 3, 2024

We currenty restart the drag gesture detector when the pointer matcher changes:

        Modifier.pointerInput(matcher) {
            detectDragGestures(
                matcher = matcher,
                onDragStart = { onDragStartState.value(it) },
                onDrag = { onDragState.value(it) },
                onDragEnd = { onDragEndState.value() },
                onDragCancel = { onDragCancelState.value() }
            )
        }

This means that, for example,

@OptIn(ExperimentalFoundationApi::class)
fun main() = singleWindowApplication {
    var n by remember { mutableStateOf(0) }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.LightGray)
            .onDrag(matcher = PointerMatcher.mouse(PointerButton.Primary)) {
                println("Dragged: $it")
                n += 1
            }
    ) {
        Text(n.toString())
    }
}

will not work.

Proposed Changes

  • Make PointerMatcher a fun interface (because it's fun!!)
  • Remember the updated matcher and use it instead of restarting the pointer input scope.

Testing

Test: Added a unit test.

@m-sasha m-sasha requested review from igordmn and eymar January 3, 2024 17:14
@m-sasha
Copy link
Author

m-sasha commented Jan 3, 2024

Adding Igor because making PointerMatcher a fun interface is possibly (but unlikely, I think) a source-level breaking change. #kotlin channel confirmed it's not a binary-breaking change.

But I think it's a good change in general, and the API is still experimental, so I put it in.

@m-sasha m-sasha merged commit 72d8841 into jb-main Jan 3, 2024
6 checks passed
@m-sasha m-sasha deleted the m-sasha/fix-restarting-drag branch January 3, 2024 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants