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

Commit 38cb0239 authored by Mike Schneider's avatar Mike Schneider
Browse files

Add @Composable `rememberDistanceGestureContext` helper

Test: Unit tests
Flag: com.android.systemui.scene_container
Bug: 379248269
Change-Id: I1913a71a375ddbfaf03af030ae009497642fb01c
parent 65d1a4a6
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -16,15 +16,37 @@

package com.android.mechanics

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalViewConfiguration
import com.android.mechanics.spec.InputDirection
import kotlin.math.max
import kotlin.math.min

/**
 * Remembers [DistanceGestureContext] with the given initial distance / direction.
 *
 * Providing update [initDistance] or [initialDirection] will not re-create the
 * [DistanceGestureContext].
 *
 * The `directionChangeSlop` is derived from `ViewConfiguration.touchSlop` and kept current without
 * re-creating, should it ever change.
 */
@Composable
fun rememberDistanceGestureContext(
    initDistance: Float = 0f,
    initialDirection: InputDirection = InputDirection.Max,
): DistanceGestureContext {
    val touchSlop = LocalViewConfiguration.current.touchSlop
    return remember { DistanceGestureContext(initDistance, initialDirection, touchSlop) }
        .also { it.directionChangeSlop = touchSlop }
}

/**
 * Gesture-specific context to augment [MotionValue.currentInput].
 *