All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.peanuuutz.fork.ui.foundation.input.DragState.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 The Android Open Source Project
 * Modifications Copyright 2022 Peanuuutz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.peanuuutz.fork.ui.foundation.input

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import net.peanuuutz.fork.ui.animation.animate
import net.peanuuutz.fork.ui.animation.spec.target.DefaultFloatOffsetAnimationSpec
import net.peanuuutz.fork.ui.animation.spec.target.composite.FiniteAnimationSpec
import net.peanuuutz.fork.ui.animation.vector.VectorConvertor
import net.peanuuutz.fork.ui.ui.unit.FloatOffset
import net.peanuuutz.fork.ui.util.MutationPriority
import net.peanuuutz.fork.ui.util.MutatorMutex

@Stable
interface DragState {
    val isDragging: Boolean

    suspend fun drag(
        priority: MutationPriority = MutationPriority.Default,
        scope: suspend DragScope.() -> Unit
    )

    fun dragBy(movement: FloatOffset): FloatOffset
}

fun DragState(
    onDrag: (movement: FloatOffset) -> FloatOffset
): DragState {
    return DefaultDragState(onDrag)
}

@Composable
fun rememberDragState(
    onDrag: (movement: FloatOffset) -> FloatOffset
): DragState {
    val onDragRemembered by rememberUpdatedState(onDrag)
    return remember { DragState { onDragRemembered(it) } }
}

interface DragScope : Flingable {
    fun dragBy(movement: FloatOffset): FloatOffset

    override fun flingBy(offset: FloatOffset): FloatOffset {
        return dragBy(offset)
    }
}

// -------- Extensions --------

suspend fun DragState.interrupt(
    priority: MutationPriority = MutationPriority.Default
) {
    drag(priority) {}
}

suspend fun DragState.animateDragBy(
    movement: FloatOffset,
    animationSpec: FiniteAnimationSpec = DefaultFloatOffsetAnimationSpec,
    priority: MutationPriority = MutationPriority.Default
): FloatOffset {
    var previousMovement = FloatOffset.Zero
    drag(priority) {
        animate(
            convertor = FloatOffset.VectorConvertor,
            initialValue = FloatOffset.Zero,
            targetValue = movement,
            animationSpec = animationSpec
        ) { offset, _ ->
            previousMovement += dragBy(offset - previousMovement)
        }
    }
    return previousMovement
}

// ======== Internal ========

private class DefaultDragState(private val onDrag: (FloatOffset) -> FloatOffset) : DragState {
    private val dragScope: DragScope = object : DragScope {
        override fun dragBy(movement: FloatOffset): FloatOffset {
            return onDrag(movement)
        }
    }

    private val mutatorMutex: MutatorMutex = MutatorMutex()

    override var isDragging: Boolean by mutableStateOf(false)

    override suspend fun drag(priority: MutationPriority, scope: suspend DragScope.() -> Unit) {
        isDragging = true
        try {
            mutatorMutex.mutate(
                receiver = dragScope,
                priority = priority,
                block = scope
            )
        } finally {
            isDragging = false
        }
    }

    override fun dragBy(movement: FloatOffset): FloatOffset {
        return onDrag(movement)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy