commonMain.androidx.compose.foundation.text.AnnotatedStringResolveInlineContent.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of foundation-desktop Show documentation
Show all versions of foundation-desktop Show documentation
Higher level abstractions of the Compose UI primitives. This library is design system agnostic, providing the high-level building blocks for both application and design-system developers
/*
* Copyright 2022 The Android Open Source Project
*
* 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 androidx.compose.foundation.text
import androidx.compose.runtime.Composable
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.util.fastForEach
import androidx.compose.ui.util.fastMap
internal typealias PlaceholderRange = AnnotatedString.Range
internal typealias InlineContentRange = AnnotatedString.Range<@Composable (String) -> Unit>
/**
* Attempts to match AnnotatedString placeholders with passed [InlineTextContent]
*
* Matches will produce a entry in both returned lists.
*
* Non-matches will be ignored silently.
*/
internal fun AnnotatedString.resolveInlineContent(
inlineContent: Map?
): Pair, List> {
if (inlineContent.isNullOrEmpty()) {
return EmptyInlineContent
}
val inlineContentAnnotations = getStringAnnotations(INLINE_CONTENT_TAG, 0, text.length)
val placeholders = mutableListOf>()
val inlineComposables = mutableListOf Unit>>()
inlineContentAnnotations.fastForEach { annotation ->
inlineContent[annotation.item]?.let { inlineTextContent ->
placeholders.add(
AnnotatedString.Range(
inlineTextContent.placeholder,
annotation.start,
annotation.end
)
)
inlineComposables.add(
AnnotatedString.Range(
inlineTextContent.children,
annotation.start,
annotation.end
)
)
}
}
return Pair(placeholders, inlineComposables)
}
internal fun AnnotatedString.hasInlineContent(): Boolean =
hasStringAnnotations(INLINE_CONTENT_TAG, 0, text.length)
@Composable
internal fun InlineChildren(
text: AnnotatedString,
inlineContents: List
) {
inlineContents.fastForEach { (content, start, end) ->
Layout(
content = { content(text.subSequence(start, end).text) }
) { children, constrains ->
val placeables = children.fastMap { it.measure(constrains) }
layout(width = constrains.maxWidth, height = constrains.maxHeight) {
placeables.fastForEach { it.placeRelative(0, 0) }
}
}
}
}
private val EmptyInlineContent: Pair, List> =
Pair(emptyList(), emptyList())
© 2015 - 2025 Weber Informatics LLC | Privacy Policy