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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import androidx.compose.ui.util.fastAll
*/
@ExperimentalFoundationApi
@OptIn(ExperimentalComposeUiApi::class)
interface PointerMatcher {
fun interface PointerMatcher {

@ExperimentalFoundationApi
fun matches(event: PointerEvent): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package androidx.compose.foundation.gestures

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.PointerMatcher
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
Expand Down Expand Up @@ -153,18 +154,19 @@ fun Modifier.onDrag(
factory = {
if (!enabled) return@composed Modifier

val onDragState = rememberUpdatedState(onDrag)
val onDragStartState = rememberUpdatedState(onDragStart)
val onDragEndState = rememberUpdatedState(onDragEnd)
val onDragCancelState = rememberUpdatedState(onDragCancel)
val matcherState by rememberUpdatedState(matcher)
val onDragState by rememberUpdatedState(onDrag)
val onDragStartState by rememberUpdatedState(onDragStart)
val onDragEndState by rememberUpdatedState(onDragEnd)
val onDragCancelState by rememberUpdatedState(onDragCancel)

Modifier.pointerInput(matcher) {
Modifier.pointerInput(Unit) {
detectDragGestures(
matcher = matcher,
onDragStart = { onDragStartState.value(it) },
onDrag = { onDragState.value(it) },
onDragEnd = { onDragEndState.value() },
onDragCancel = { onDragCancelState.value() }
matcher = { matcherState.matches(it) },
onDragStart = { onDragStartState(it) },
onDrag = { onDragState(it) },
onDragEnd = { onDragEndState() },
onDragCancel = { onDragCancelState() }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.PointerMatcher
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.ImageComposeScene
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.ui.Modifier
Expand All @@ -30,6 +33,12 @@ import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.PointerType
import androidx.compose.ui.platform.PlatformContext
import androidx.compose.ui.platform.ViewConfiguration
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.MouseButton
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performMouseInput
import androidx.compose.ui.test.runComposeUiTest
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import androidx.compose.ui.use
Expand Down Expand Up @@ -462,6 +471,37 @@ class DragGestureTest {
assertEquals(3, onDragCounter)
}
}

@OptIn(ExperimentalTestApi::class)
@Test
fun dragGestureDoesNotRestartOnMatcherChange() = runComposeUiTest {
var matcher by mutableStateOf(PointerMatcher.Primary)
var dragCount = 0
setContent {
Box(Modifier
.testTag("box")
.size(100.dp)
.onDrag(matcher = matcher) {
dragCount += 1
}
)
}

onNodeWithTag("box").performMouseInput {
moveTo(Offset(10f, 10f))
press(MouseButton.Primary)
moveTo(Offset(20f, 20f))
}
assertEquals(expected = 1, actual = dragCount)

dragCount = 0
matcher = PointerMatcher.Primary + PointerMatcher.mouse(PointerButton.Secondary)
onNodeWithTag("box").performMouseInput {
moveTo(Offset(30f, 30f))
release(MouseButton.Primary)
}
assertEquals(expected = 1, actual = dragCount)
}
}

@OptIn(InternalComposeUiApi::class)
Expand Down