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

net.peanuuutz.fork.ui.foundation.input.Hoverable.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 net.peanuuutz.fork.ui.foundation.input.interaction.HoverInteraction
import net.peanuuutz.fork.ui.foundation.input.interaction.MutableInteractionSource
import net.peanuuutz.fork.ui.foundation.input.interaction.detectAndEmitHoverInteractions
import net.peanuuutz.fork.ui.foundation.input.interaction.tryEmitExit
import net.peanuuutz.fork.ui.inspection.InspectInfo
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.PointerEventPass
import net.peanuuutz.fork.ui.ui.node.PointerInputModifierNode

@Stable
fun Modifier.hoverable(
    interactionSource: MutableInteractionSource,
    label: Any? = null,
    isEnabled: Boolean = true
): Modifier {
    if (!isEnabled) {
        return this
    }
    val element = HoverableModifier(
        interactionSource = interactionSource,
        label = label
    )
    return this then element
}

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

private data class HoverableModifier(
    val interactionSource: MutableInteractionSource,
    val label: Any?
) : ModifierNodeElement() {
    override fun create(): HoverableModifierNode {
        return HoverableModifierNode(
            interactionSource = interactionSource,
            label = label
        )
    }

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

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

private class HoverableModifierNode(
    interactionSource: MutableInteractionSource,
    label: Any?
) : BranchingModifierNode(), PointerInputModifierNode {
    var interactionSource: MutableInteractionSource = interactionSource
        set(value) {
            if (field == value) {
                return
            }
            field.tryEmitExit(
                stateProvider = this::hoverState,
                stateUpdater = this::hoverState::set
            )
            field = value
        }

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

    private val pointerInputHandler: SuspendingPointerInputModifierNode = branch {
        SuspendingPointerInputModifierNode {
            detectAndEmitHoverInteractions(
                interactionSourceProvider = this@HoverableModifierNode::interactionSource,
                stateProvider = this@HoverableModifierNode::hoverState,
                stateUpdater = this@HoverableModifierNode::hoverState::set,
                labelProvider = this@HoverableModifierNode::label
            )
        }
    }

    private var hoverState: HoverInteraction.Enter? = null

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy