1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package androidx.compose.foundation.text
18
19 import androidx.compose.foundation.internal.requirePrecondition
20 import androidx.compose.runtime.Composable
21 import androidx.compose.runtime.Immutable
22 import androidx.compose.ui.text.AnnotatedString
23 import androidx.compose.ui.text.Placeholder
24
25 /** The annotation tag used by inline content. */
26 internal const val INLINE_CONTENT_TAG = "androidx.compose.foundation.text.inlineContent"
27 // A string that contains a replacement character specified by unicode. It's used as the default
28 // value of alternate text.
29 private const val REPLACEMENT_CHAR = "\uFFFD"
30
31 /**
32 * Used to insert composables into the text layout. This method can be used together with the
33 * inlineContent parameter of [BasicText]. It will append the [alternateText] to this
34 * [AnnotatedString] and also mark this range of text to be replaced by a composable. [BasicText]
35 * will try to find an [InlineTextContent] in the map defined by inlineContent whose key equals to
36 * [id], and it will use the [InlineTextContent.children] to replace this range of text.
37 *
38 * @sample androidx.compose.foundation.samples.InlineTextContentSample
39 * @param id The id used to look up the [InlineTextContent], it is referred by the inlineContent
40 * parameter of [BasicText] to replace the [alternateText] to the corresponding composable.
41 * @param alternateText The text to be replaced by the inline content. It's displayed when the
42 * inlineContent parameter of [BasicText] doesn't contain [id]. Accessibility features will also
43 * use this text to describe the inline content.
44 * @throws IllegalArgumentException if [alternateText] has zero length.
45 * @see InlineTextContent
46 * @see BasicText
47 */
appendInlineContentnull48 fun AnnotatedString.Builder.appendInlineContent(
49 id: String,
50 alternateText: String = REPLACEMENT_CHAR
51 ) {
52 requirePrecondition(alternateText.isNotEmpty()) { "alternateText can't be an empty string." }
53 pushStringAnnotation(INLINE_CONTENT_TAG, id)
54 append(alternateText)
55 pop()
56 }
57
58 /**
59 * A data class that stores a composable to be inserted into the text layout.
60 *
61 * Different from a regular composable, a [Placeholder] is also needed for text layout to reserve
62 * space. In this [placeholder], the size of the content and how it will be aligned within the text
63 * line is defined. When the children composable is measured, its size given in [Placeholder.width]
64 * and [Placeholder.height] will be converted into [androidx.compose.ui.unit.Constraints] and passed
65 * through [androidx.compose.ui.layout.Layout].
66 *
67 * @sample androidx.compose.foundation.samples.InlineTextContentSample
68 * @see BasicText
69 * @see Placeholder
70 */
71 @Immutable
72 class InlineTextContent(
73 /**
74 * The setting object that defines the size and vertical alignment of this composable in the
75 * text line. This is different from the measure of Layout
76 *
77 * @see Placeholder
78 */
79 val placeholder: Placeholder,
80 /**
81 * The composable to be inserted into the text layout. The string parameter passed to it will
82 * the alternateText given to [appendInlineContent].
83 */
84 val children: @Composable (String) -> Unit
85 )
86