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

Commit 8a88fe39 authored by omarmt's avatar omarmt
Browse files

STL Added support for pointerType in Swipe definition

Allow filtering swipe actions by input type (mouse, touch, or any)

Test: atest SwipeToSceneTest
Bug: 371984715
Flag: com.android.systemui.scene_container
Change-Id: I753e38593a907229371acf8989588d412bae91e7
parent f2825433
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -227,6 +227,8 @@ private fun resolveSwipe(
            },
        // If the number of pointers is not specified, 1 is assumed.
        pointerCount = pointersInfo?.pointersDown ?: 1,
        // Resolves the pointer type only if all pointers are of the same type.
        pointersType = pointersInfo?.pointersDownByType?.keys?.singleOrNull(),
        fromSource = fromSource,
    )
}
+13 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.PointerId
import androidx.compose.ui.input.pointer.PointerInputChange
import androidx.compose.ui.input.pointer.PointerInputScope
import androidx.compose.ui.input.pointer.PointerType
import androidx.compose.ui.input.pointer.SuspendingPointerInputModifierNode
import androidx.compose.ui.input.pointer.changedToDown
import androidx.compose.ui.input.pointer.changedToUpIgnoreConsumed
@@ -52,6 +53,7 @@ import androidx.compose.ui.util.fastAll
import androidx.compose.ui.util.fastAny
import androidx.compose.ui.util.fastFilter
import androidx.compose.ui.util.fastFirstOrNull
import androidx.compose.ui.util.fastForEach
import androidx.compose.ui.util.fastSumBy
import com.android.compose.ui.util.SpaceVectorConverter
import kotlin.coroutines.cancellation.CancellationException
@@ -676,11 +678,14 @@ internal fun interface PointersInfoOwner {
 *   this one.
 * @param pointersDown The number of pointers currently down.
 * @param isMouseWheel Indicates whether the last pointer event was a mouse wheel scroll.
 * @param pointersDownByType Provide a map of pointer types to the count of pointers of that type
 *   currently down/pressed.
 */
internal data class PointersInfo(
    val startedPosition: Offset,
    val pointersDown: Int,
    val isMouseWheel: Boolean,
    val pointersDownByType: Map<PointerType, Int>,
) {
    init {
        check(pointersDown > 0) { "We should have at least 1 pointer down, $pointersDown instead" }
@@ -696,5 +701,13 @@ private fun PointersInfo(
        startedPosition = startedPosition,
        pointersDown = pointersDown,
        isMouseWheel = lastPointerEvent.type == PointerEventType.Scroll,
        pointersDownByType =
            buildMap {
                lastPointerEvent.changes.fastForEach { change ->
                    if (!change.pressed) return@fastForEach
                    val newValue = (get(change.type) ?: 0) + 1
                    put(change.type, newValue)
                }
            },
    )
}
+4 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.pointer.PointerType
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.Density
@@ -407,6 +408,7 @@ data object Back : UserAction() {
data class Swipe(
    val direction: SwipeDirection,
    val pointerCount: Int = 1,
    val pointersType: PointerType? = null,
    val fromSource: SwipeSource? = null,
) : UserAction() {
    companion object {
@@ -422,6 +424,7 @@ data class Swipe(
        return Resolved(
            direction = direction.resolve(layoutDirection),
            pointerCount = pointerCount,
            pointersType = pointersType,
            fromSource = fromSource?.resolve(layoutDirection),
        )
    }
@@ -431,6 +434,7 @@ data class Swipe(
        val direction: SwipeDirection.Resolved,
        val pointerCount: Int,
        val fromSource: SwipeSource.Resolved?,
        val pointersType: PointerType?,
    ) : UserAction.Resolved()
}

+16 −4
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ private fun Content.shouldEnableSwipes(orientation: Orientation): Boolean {
 * @return The best matching [UserActionResult], or `null` if no match is found.
 */
internal fun Content.findActionResultBestMatch(swipe: Swipe.Resolved): UserActionResult? {
    var bestPoints = Int.MIN_VALUE
    var bestMatch: UserActionResult? = null
    userActions.forEach { (actionSwipe, actionResult) ->
        if (
@@ -81,20 +82,31 @@ internal fun Content.findActionResultBestMatch(swipe: Swipe.Resolved): UserActio
                // The number of pointers down must match.
                actionSwipe.pointerCount != swipe.pointerCount ||
                // The action requires a specific fromSource.
                (actionSwipe.fromSource != null && actionSwipe.fromSource != swipe.fromSource)
                (actionSwipe.fromSource != null && actionSwipe.fromSource != swipe.fromSource) ||
                // The action requires a specific pointerType.
                (actionSwipe.pointersType != null && actionSwipe.pointersType != swipe.pointersType)
        ) {
            // This action is not eligible.
            return@forEach
        }

        // Prioritize actions with a matching fromSource.
        if (actionSwipe.fromSource == swipe.fromSource) {
        val sameFromSource = actionSwipe.fromSource == swipe.fromSource
        val samePointerType = actionSwipe.pointersType == swipe.pointersType
        // Prioritize actions with a perfect match.
        if (sameFromSource && samePointerType) {
            return actionResult
        }

        var points = 0
        if (sameFromSource) points++
        if (samePointerType) points++

        // Otherwise, keep track of the best eligible action.
        if (points > bestPoints) {
            bestPoints = points
            bestMatch = actionResult
        }
    }
    return bestMatch
}

+2 −0
Original line number Diff line number Diff line
@@ -56,11 +56,13 @@ private fun pointersInfo(
    startedPosition: Offset = Offset.Zero,
    pointersDown: Int = 1,
    isMouseWheel: Boolean = false,
    pointersDownByType: Map<PointerType, Int> = mapOf(PointerType.Touch to pointersDown),
): PointersInfo {
    return PointersInfo(
        startedPosition = startedPosition,
        pointersDown = pointersDown,
        isMouseWheel = isMouseWheel,
        pointersDownByType = pointersDownByType,
    )
}

Loading