Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0c17641b authored by omarmt's avatar omarmt
Browse files

STL MultiPointerDraggable compute direction using the previousPosition

If the user drags in the opposite direction, the delta becomes zero
because we return to the original point.
Therefore, we should use the previous event to calculate the direction.

Test: atest MultiPointerDraggableTest
Bug: 379403509
Flag: com.android.systemui.scene_container
Change-Id: I5065f65ed8995b95245c7b744fb4b5d019f416fd
parent e2778689
Loading
Loading
Loading
Loading
+13 −2
Original line number Diff line number Diff line
@@ -519,8 +519,19 @@ internal class MultiPointerDraggableNode(
                // we intercept an ongoing swipe transition (i.e. startDragImmediately() returned
                // true).
                if (overSlop == 0f) {
                    val delta = (drag.position - consumablePointer.position).toFloat()
                    check(delta != 0f) { "delta is equal to 0" }
                    // If the user drags in the opposite direction, the delta becomes zero because
                    // we return to the original point. Therefore, we should use the previous event
                    // to calculate the direction.
                    val delta = (drag.position - drag.previousPosition).toFloat()
                    check(delta != 0f) {
                        buildString {
                            append("delta is equal to 0 ")
                            append("touchSlop ${currentValueOf(LocalViewConfiguration).touchSlop} ")
                            append("consumablePointer.position ${consumablePointer.position} ")
                            append("drag.position ${drag.position} ")
                            append("drag.previousPosition ${drag.previousPosition}")
                        }
                    }
                    overSlop = delta.sign
                }
                drag
+70 −3
Original line number Diff line number Diff line
@@ -593,7 +593,7 @@ class MultiPointerDraggableTest {
            }
        }

        fun continueDraggingDown() {
        fun dragDown() {
            rule.onRoot().performTouchInput { moveBy(Offset(0f, touchSlop)) }
        }

@@ -603,11 +603,78 @@ class MultiPointerDraggableTest {
        assertThat(started).isFalse()

        swipeConsume = true
        continueDraggingDown()
        // Drag in same direction
        dragDown()
        assertThat(capturedChange).isNotNull()
        capturedChange = null

        continueDraggingDown()
        dragDown()
        assertThat(capturedChange).isNull()

        assertThat(started).isTrue()
    }

    @Test
    fun multiPointerSwipeDetectorInteractionZeroOffsetFromStartPosition() {
        val size = 200f
        val middle = Offset(size / 2f, size / 2f)

        var started = false

        var capturedChange: PointerInputChange? = null
        var swipeConsume = false

        var touchSlop = 0f
        rule.setContent {
            touchSlop = LocalViewConfiguration.current.touchSlop
            Box(
                Modifier.size(with(LocalDensity.current) { Size(size, size).toDpSize() })
                    .nestedScrollDispatcher()
                    .multiPointerDraggable(
                        orientation = Orientation.Vertical,
                        startDragImmediately = { false },
                        swipeDetector =
                            object : SwipeDetector {
                                override fun detectSwipe(change: PointerInputChange): Boolean {
                                    capturedChange = change
                                    return swipeConsume
                                }
                            },
                        onDragStarted = { _, _ ->
                            started = true
                            SimpleDragController(
                                onDrag = { /* do nothing */ },
                                onStop = { /* do nothing */ },
                            )
                        },
                        dispatcher = defaultDispatcher,
                    )
            ) {}
        }

        fun startDraggingDown() {
            rule.onRoot().performTouchInput {
                down(middle)
                moveBy(Offset(0f, touchSlop))
            }
        }

        fun dragUp() {
            rule.onRoot().performTouchInput { moveBy(Offset(0f, -touchSlop)) }
        }

        startDraggingDown()
        assertThat(capturedChange).isNotNull()
        capturedChange = null
        assertThat(started).isFalse()

        swipeConsume = true
        // Drag in the opposite direction
        dragUp()
        assertThat(capturedChange).isNotNull()
        capturedChange = null

        dragUp()
        assertThat(capturedChange).isNull()

        assertThat(started).isTrue()