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

Commit b363ed1d authored by Jordan Demeulenaere's avatar Jordan Demeulenaere Committed by Android (Google) Code Review
Browse files

Merge changes from topic "stl-swipe-source" into main

* changes:
  Extract compute(Swipes,SwipesResults) in SceneGestureHandler
  Enforce that onDragStarted.overSlop is != 0f
  Migrate Modifier.swipeToScene to the Modifier.Node API
  Replace swipe Edge (detector) by SwipeSource (detector) (1/2)
  Make STL swipe distances configurable
parents 01473b52 e66735e3
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -84,13 +84,14 @@ fun CommunalContainer(
    SceneTransitionLayout(
        state = sceneTransitionLayoutState,
        modifier = modifier.fillMaxSize(),
        edgeDetector = FixedSizeEdgeDetector(ContainerDimensions.EdgeSwipeSize),
        swipeSourceDetector = FixedSizeEdgeDetector(ContainerDimensions.EdgeSwipeSize),
    ) {
        scene(
            TransitionSceneKey.Blank,
            userActions =
                mapOf(
                    Swipe(SwipeDirection.Left, fromEdge = Edge.Right) to TransitionSceneKey.Communal
                    Swipe(SwipeDirection.Left, fromSource = Edge.Right) to
                        TransitionSceneKey.Communal
                )
        ) {
            // This scene shows nothing only allowing for transitions to the communal scene.
@@ -101,7 +102,7 @@ fun CommunalContainer(
            TransitionSceneKey.Communal,
            userActions =
                mapOf(
                    Swipe(SwipeDirection.Right, fromEdge = Edge.Left) to TransitionSceneKey.Blank
                    Swipe(SwipeDirection.Right, fromSource = Edge.Left) to TransitionSceneKey.Blank
                ),
        ) {
            CommunalScene(viewModel, modifier = modifier)
+1 −1
Original line number Diff line number Diff line
@@ -183,7 +183,7 @@ private fun UserAction.toTransitionUserAction(): SceneTransitionUserAction {
        is UserAction.Swipe ->
            Swipe(
                pointerCount = pointerCount,
                fromEdge =
                fromSource =
                    when (this.fromEdge) {
                        null -> null
                        Edge.LEFT -> SceneTransitionEdge.Left
+9 −14
Original line number Diff line number Diff line
@@ -23,24 +23,19 @@ import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp

interface EdgeDetector {
    /**
     * Return the [Edge] associated to [position] inside a layout of size [layoutSize], given
     * [density] and [orientation].
     */
    fun edge(
        layoutSize: IntSize,
        position: IntOffset,
        density: Density,
        orientation: Orientation,
    ): Edge?
/** The edge of a [SceneTransitionLayout]. */
enum class Edge : SwipeSource {
    Left,
    Right,
    Top,
    Bottom,
}

val DefaultEdgeDetector = FixedSizeEdgeDetector(40.dp)

/** An [EdgeDetector] that detects edges assuming a fixed edge size of [size]. */
class FixedSizeEdgeDetector(val size: Dp) : EdgeDetector {
    override fun edge(
/** An [SwipeSourceDetector] that detects edges assuming a fixed edge size of [size]. */
class FixedSizeEdgeDetector(val size: Dp) : SwipeSourceDetector {
    override fun source(
        layoutSize: IntSize,
        position: IntOffset,
        density: Density,
+5 −1
Original line number Diff line number Diff line
@@ -44,7 +44,7 @@ sealed class Key(val debugName: String, val identity: Any) {
class SceneKey(
    name: String,
    identity: Any = Object(),
) : Key(name, identity) {
) : Key(name, identity), UserActionResult {
    @VisibleForTesting
    // TODO(b/240432457): Make internal once PlatformComposeSceneTransitionLayoutTestsUtils can
    // access internal members.
@@ -53,6 +53,10 @@ class SceneKey(
    /** The unique [ElementKey] identifying this scene's root element. */
    val rootElementKey = ElementKey(name, identity)

    // Implementation of [UserActionResult].
    override val toScene: SceneKey = this
    override val distance: UserActionDistance? = null

    override fun toString(): String {
        return "SceneKey(debugName=$debugName)"
    }
+12 −13
Original line number Diff line number Diff line
@@ -64,8 +64,8 @@ import androidx.compose.ui.util.fastForEach
@Stable
internal fun Modifier.multiPointerDraggable(
    orientation: Orientation,
    enabled: Boolean,
    startDragImmediately: Boolean,
    enabled: () -> Boolean,
    startDragImmediately: () -> Boolean,
    onDragStarted: (startedPosition: Offset, overSlop: Float, pointersDown: Int) -> Unit,
    onDragDelta: (delta: Float) -> Unit,
    onDragStopped: (velocity: Float) -> Unit,
@@ -83,8 +83,8 @@ internal fun Modifier.multiPointerDraggable(

private data class MultiPointerDraggableElement(
    private val orientation: Orientation,
    private val enabled: Boolean,
    private val startDragImmediately: Boolean,
    private val enabled: () -> Boolean,
    private val startDragImmediately: () -> Boolean,
    private val onDragStarted:
        (startedPosition: Offset, overSlop: Float, pointersDown: Int) -> Unit,
    private val onDragDelta: (Float) -> Unit,
@@ -110,10 +110,10 @@ private data class MultiPointerDraggableElement(
    }
}

private class MultiPointerDraggableNode(
internal class MultiPointerDraggableNode(
    orientation: Orientation,
    enabled: Boolean,
    var startDragImmediately: Boolean,
    enabled: () -> Boolean,
    var startDragImmediately: () -> Boolean,
    var onDragStarted: (startedPosition: Offset, overSlop: Float, pointersDown: Int) -> Unit,
    var onDragDelta: (Float) -> Unit,
    var onDragStopped: (velocity: Float) -> Unit,
@@ -122,7 +122,7 @@ private class MultiPointerDraggableNode(
    private val delegate = delegate(SuspendingPointerInputModifierNode(pointerInputHandler))
    private val velocityTracker = VelocityTracker()

    var enabled: Boolean = enabled
    var enabled: () -> Boolean = enabled
        set(value) {
            // Reset the pointer input whenever enabled changed.
            if (value != field) {
@@ -133,7 +133,7 @@ private class MultiPointerDraggableNode(

    var orientation: Orientation = orientation
        set(value) {
            // Reset the pointer input whenever enabled orientation.
            // Reset the pointer input whenever orientation changed.
            if (value != field) {
                field = value
                delegate.resetPointerInputHandler()
@@ -149,7 +149,7 @@ private class MultiPointerDraggableNode(
    ) = delegate.onPointerEvent(pointerEvent, pass, bounds)

    private suspend fun PointerInputScope.pointerInput() {
        if (!enabled) {
        if (!enabled()) {
            return
        }

@@ -163,8 +163,7 @@ private class MultiPointerDraggableNode(
        val onDragEnd: () -> Unit = {
            val maxFlingVelocity =
                currentValueOf(LocalViewConfiguration).maximumFlingVelocity.let { max ->
                    val maxF = max.toFloat()
                    Velocity(maxF, maxF)
                    Velocity(max, max)
                }

            val velocity = velocityTracker.calculateVelocity(maxFlingVelocity)
@@ -183,7 +182,7 @@ private class MultiPointerDraggableNode(

        detectDragGestures(
            orientation = orientation,
            startDragImmediately = { startDragImmediately },
            startDragImmediately = startDragImmediately,
            onDragStart = onDragStart,
            onDragEnd = onDragEnd,
            onDragCancel = onDragCancel,
Loading