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

Fix awaitDragStartOnSlop to detect slop-passing on both axes. #534

Merged
merged 1 commit into from
Apr 28, 2023
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 @@ -179,7 +179,8 @@ private suspend fun AwaitPointerEventScope.awaitDragStartOnSlop(initialDown: Poi
do {
drag = awaitPointerSlopOrCancellation(
initialDown.changes[0].id,
initialDown.changes[0].type
initialDown.changes[0].type,
triggerOnMainAxisSlop = false
) { change, over ->
change.consume()
overSlop = over
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import androidx.compose.ui.use
import kotlin.math.ceil
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlin.test.*
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.UnconfinedTestDispatcher
Expand Down Expand Up @@ -180,6 +176,69 @@ class DragGestureTest {
}
}

private fun assertDragSucceeds(
density: Density,
startOffset: Offset,
endOffset: Offset
){
ImageComposeScene(
width = 100,
height = 100,
density = density
).use { scene ->

var dragStarted = false
var dragged = false
var dragEnded = false

scene.setContent {
Box(
modifier = Modifier
.size(40.dp, 40.dp)
.onDrag(
enabled = true,
onDragStart = { dragStarted = true },
onDragEnd = { dragEnded = true },
onDrag = { dragged = true }
)
)
}

scene.sendPointerEvent(PointerEventType.Move, startOffset)
scene.sendPointerEvent(PointerEventType.Press, startOffset, button = PointerButton.Primary)
scene.sendPointerEvent(PointerEventType.Move, endOffset)
scene.sendPointerEvent(PointerEventType.Release,endOffset, button = PointerButton.Primary)

assertTrue(dragStarted)
assertTrue(dragged)
assertTrue(dragEnded)
}
}

@Test
fun vertical_drag_passes_slop() {
val density = Density(1f)
val viewConfiguration = DefaultViewConfiguration(density)
val startOffset = Offset(5f, 5f)
assertDragSucceeds(
density = density,
startOffset = startOffset,
endOffset = startOffset + Offset(0f, viewConfiguration.touchSlop + 1f)
)
}

@Test
fun horizontal_drag_passes_slop() {
val density = Density(1f)
val viewConfiguration = DefaultViewConfiguration(density)
val startOffset = Offset(5f, 5f)
assertDragSucceeds(
density = density,
startOffset = startOffset,
endOffset = startOffset + Offset(viewConfiguration.touchSlop + 1f, 0f)
)
}

@Test
fun draggable_by_touch() {
val density = Density(1f)
Expand Down