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

net.peanuuutz.fork.ui.foundation.text.HoverableText.kt Maven / Gradle / Ivy

The newest version!
package net.peanuuutz.fork.ui.foundation.text

import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import kotlinx.coroutines.isActive
import net.peanuuutz.fork.ui.foundation.text.MeasuredParagraph.Companion.NullIndex
import net.peanuuutz.fork.ui.ui.draw.text.Paragraph
import net.peanuuutz.fork.ui.ui.draw.text.TextStyle
import net.peanuuutz.fork.ui.ui.modifier.Modifier
import net.peanuuutz.fork.ui.ui.modifier.input.PointerInputScope
import net.peanuuutz.fork.ui.ui.modifier.input.pointerInput

@Composable
fun HoverableText(
    paragraph: Paragraph,
    onHover: (charIndex: Int) -> Unit,
    modifier: Modifier = Modifier,
    ignoreHoverOnGap: Boolean = true,
    textStyle: TextStyle = LocalTextStyle.current,
    overflow: TextOverflow = TextOverflow.Clip,
    softWrap: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    onTextLayout: ((TextLayoutResult) -> Unit)? = null,
    selector: TextSelector? = null
) {
    val layoutResultState = remember { mutableStateOf(null) }
    val ignoreHoverOnGapState = rememberUpdatedState(ignoreHoverOnGap)
    val onHoverState = rememberUpdatedState(onHover)

    BasicText(
        paragraph = paragraph,
        modifier = modifier.pointerInput {
            detectHoverOnText(
                layoutResultProvider = layoutResultState::value,
                ignoreHoverOnGapProvider = ignoreHoverOnGapState::value,
                onHoverProvider = onHoverState::value
            )
        },
        textStyle = textStyle,
        overflow = overflow,
        softWrap = softWrap,
        maxLines = maxLines,
        onTextLayout = { result ->
            layoutResultState.value = result
            onTextLayout?.invoke(result)
        },
        selector = selector
    )
}

// Internal

internal suspend fun PointerInputScope.detectHoverOnText(
    layoutResultProvider: () -> TextLayoutResult?,
    ignoreHoverOnGapProvider: () -> Boolean,
    onHoverProvider: () -> ((Int) -> Unit)
) {
    receive {
        var previousCharIndex = NullIndex
        while (listenerContext.isActive) {
            val event = listen()
            val result = layoutResultProvider() ?: continue
            val charIndex = if (event.isCaptured) {
                if (ignoreHoverOnGapProvider()) {
                    result.measuredParagraph.getCharIndexFromOffsetExactly(event.position)
                } else {
                    result.measuredParagraph.getCharIndexFromOffsetApproximately(event.position)
                }
            } else {
                NullIndex
            }
            if (previousCharIndex != charIndex) {
                onHoverProvider()(charIndex) // Do not filter NullIndex here
                previousCharIndex = charIndex
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy