1 /*
2  * Copyright 2023 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.modifiers
18 
19 import androidx.compose.foundation.text.DefaultMinLines
20 import androidx.compose.ui.graphics.ColorProducer
21 import androidx.compose.ui.node.ModifierNodeElement
22 import androidx.compose.ui.platform.InspectorInfo
23 import androidx.compose.ui.text.TextStyle
24 import androidx.compose.ui.text.font.FontFamily
25 import androidx.compose.ui.text.style.TextOverflow
26 
27 /**
28  * Modifier element for String based text
29  *
30  * This is faster than [TextAnnotatedStringElement]
31  */
32 internal class TextStringSimpleElement(
33     private val text: String,
34     private val style: TextStyle,
35     private val fontFamilyResolver: FontFamily.Resolver,
36     private val overflow: TextOverflow = TextOverflow.Clip,
37     private val softWrap: Boolean = true,
38     private val maxLines: Int = Int.MAX_VALUE,
39     private val minLines: Int = DefaultMinLines,
40     private val color: ColorProducer? = null
41 ) : ModifierNodeElement<TextStringSimpleNode>() {
42 
createnull43     override fun create(): TextStringSimpleNode =
44         TextStringSimpleNode(
45             text,
46             style,
47             fontFamilyResolver,
48             overflow,
49             softWrap,
50             maxLines,
51             minLines,
52             color
53         )
54 
55     override fun update(node: TextStringSimpleNode) {
56         node.doInvalidations(
57             drawChanged = node.updateDraw(color, style),
58             textChanged = node.updateText(text = text),
59             layoutChanged =
60                 node.updateLayoutRelatedArgs(
61                     style = style,
62                     minLines = minLines,
63                     maxLines = maxLines,
64                     softWrap = softWrap,
65                     fontFamilyResolver = fontFamilyResolver,
66                     overflow = overflow
67                 )
68         )
69     }
70 
equalsnull71     override fun equals(other: Any?): Boolean {
72         if (this === other) return true
73 
74         if (other !is TextStringSimpleElement) return false
75 
76         // these three are most likely to actually change
77         if (color != other.color) return false
78         if (text != other.text) return false /* expensive to check, do after color */
79         if (style != other.style) return false
80 
81         // these are equally unlikely to change
82         if (fontFamilyResolver != other.fontFamilyResolver) return false
83         if (overflow != other.overflow) return false
84         if (softWrap != other.softWrap) return false
85         if (maxLines != other.maxLines) return false
86         if (minLines != other.minLines) return false
87 
88         return true
89     }
90 
hashCodenull91     override fun hashCode(): Int {
92         var result = text.hashCode()
93         result = 31 * result + style.hashCode()
94         result = 31 * result + fontFamilyResolver.hashCode()
95         result = 31 * result + overflow.hashCode()
96         result = 31 * result + softWrap.hashCode()
97         result = 31 * result + maxLines
98         result = 31 * result + minLines
99         result = 31 * result + (color?.hashCode() ?: 0)
100         return result
101     }
102 
inspectablePropertiesnull103     override fun InspectorInfo.inspectableProperties() {
104         // Show nothing in the inspector.
105     }
106 }
107