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

net.peanuuutz.fork.ui.foundation.input.Toggleable.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.Stable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import net.peanuuutz.fork.ui.foundation.input.interaction.MutableInteractionSource
import net.peanuuutz.fork.ui.foundation.input.interaction.PressInteraction
import net.peanuuutz.fork.ui.foundation.input.interaction.detectAndEmitPressInteractions
import net.peanuuutz.fork.ui.foundation.input.interaction.emitCancelOnPress
import net.peanuuutz.fork.ui.foundation.input.interaction.emitPress
import net.peanuuutz.fork.ui.foundation.input.interaction.emitRelease
import net.peanuuutz.fork.ui.foundation.input.interaction.tryEmitCancelOnPress
import net.peanuuutz.fork.ui.inspection.InspectInfo
import net.peanuuutz.fork.ui.ui.context.key.Key
import net.peanuuutz.fork.ui.ui.context.key.KeyEvent
import net.peanuuutz.fork.ui.ui.context.pointer.PointerEvent
import net.peanuuutz.fork.ui.ui.modifier.Modifier
import net.peanuuutz.fork.ui.ui.modifier.ModifierNodeElement
import net.peanuuutz.fork.ui.ui.modifier.input.SuspendingPointerInputModifierNode
import net.peanuuutz.fork.ui.ui.node.BranchingModifierNode
import net.peanuuutz.fork.ui.ui.node.KeyEventPass
import net.peanuuutz.fork.ui.ui.node.KeyEventPass.Out
import net.peanuuutz.fork.ui.ui.node.KeyInputModifierNode
import net.peanuuutz.fork.ui.ui.node.PointerEventPass
import net.peanuuutz.fork.ui.ui.node.PointerInputModifierNode

@Stable
fun Modifier.toggleable(
    interactionSource: MutableInteractionSource? = null,
    label: Any? = null,
    isEnabled: Boolean = true,
    onToggle: (CoroutineScope.() -> Unit)? = null
): Modifier {
    if (!isEnabled) {
        return this
    }
    val element = ToggleableModifier(
        interactionSource = interactionSource,
        label = label,
        onToggle = onToggle
    )
    return this then element
}

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

private data class ToggleableModifier(
    val interactionSource: MutableInteractionSource?,
    val label: Any?,
    val onToggle: (CoroutineScope.() -> Unit)?
) : ModifierNodeElement() {
    override fun create(): ToggleableModifierNode {
        return ToggleableModifierNode(
            interactionSource = interactionSource,
            label = label,
            onToggle = onToggle
        )
    }

    override fun update(node: ToggleableModifierNode) {
        node.interactionSource = interactionSource
        node.label = label
        node.onToggle = onToggle
    }

    override fun InspectInfo.inspect() {
        set("label", label)
    }
}

private class ToggleableModifierNode(
    interactionSource: MutableInteractionSource?,
    label: Any?,
    var onToggle: (CoroutineScope.() -> Unit)?
) : BranchingModifierNode(),
    PointerInputModifierNode,
    KeyInputModifierNode
{
    var interactionSource: MutableInteractionSource? = interactionSource
        set(value) {
            if (field == value) {
                return
            }
            field?.tryEmitCancelOnPress(
                stateProvider = this::pressState,
                stateUpdater = this::pressState::set
            )
            field = value
        }

    var label: Any? = label
        set(value) {
            if (field == value) {
                return
            }
            interactionSource?.tryEmitCancelOnPress(
                stateProvider = this::pressState,
                stateUpdater = this::pressState::set
            )
            field = value
        }

    private val pointerInputHandler: SuspendingPointerInputModifierNode = branch {
        SuspendingPointerInputModifierNode {
            coroutineScope {
                launch {
                    detectAndEmitPressInteractions(
                        interactionSourceProvider = this@ToggleableModifierNode::interactionSource,
                        stateProvider = this@ToggleableModifierNode::pressState,
                        stateUpdater = this@ToggleableModifierNode::pressState::set,
                        labelProvider = this@ToggleableModifierNode::label
                    )
                }
                launch {
                    detectPressRelease(
                        onRelease = {
                            val onToggle = onToggle
                            if (onToggle != null) {
                                onToggle()
                            }
                        }
                    )
                }
            }
        }
    }

    private var pressState: PressInteraction.Press? = null

    override fun onDetach() {
        interactionSource?.tryEmitCancelOnPress(
            stateProvider = this::pressState,
            stateUpdater = this::pressState::set
        )
    }

    override fun onPointerEvent(pass: PointerEventPass, pointerEvent: PointerEvent) {
        pointerInputHandler.onPointerEvent(pass, pointerEvent)
    }

    override fun onKeyEvent(pass: KeyEventPass, keyEvent: KeyEvent): Boolean {
        if (pass != Out || keyEvent.key != Key.Space) {
            return false
        }
        if (keyEvent.isPressed) {
            nodeScope.launch {
                val effectInteractionSource = interactionSource ?: return@launch
                effectInteractionSource.emitCancelOnPress(
                    stateProvider = this@ToggleableModifierNode::pressState,
                    stateUpdater = this@ToggleableModifierNode::pressState::set
                )
                effectInteractionSource.emitPress(
                    stateProvider = this@ToggleableModifierNode::pressState,
                    stateUpdater = this@ToggleableModifierNode::pressState::set,
                    labelProvider = this@ToggleableModifierNode::label
                )
            }
        } else {
            nodeScope.launch {
                interactionSource?.emitRelease(
                    stateProvider = this@ToggleableModifierNode::pressState,
                    stateUpdater = this@ToggleableModifierNode::pressState::set
                )
            }
            val onToggle = onToggle
            if (onToggle != null) {
                onToggle(nodeScope)
            }
        }
        return true
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy