From a9d4f447905282e15139884dbc6264255d3070d7 Mon Sep 17 00:00:00 2001 From: Jakub Trzebiatowski Date: Sat, 14 Oct 2023 15:30:57 +0200 Subject: [PATCH] `EffectiveTextAttributeProvider`: Use Kotlin-style getters --- .../text/EffectiveTextAttributeProvider.kt | 40 +++---- .../text/HierarchicTextAttributeProvider.kt | 108 ++++++++++-------- .../internal/views/text/TextLayoutUtils.kt | 46 ++++---- 3 files changed, 106 insertions(+), 88 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/EffectiveTextAttributeProvider.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/EffectiveTextAttributeProvider.kt index 756e79d8a0b3c4..f97bdd17ed4e73 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/EffectiveTextAttributeProvider.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/EffectiveTextAttributeProvider.kt @@ -13,46 +13,46 @@ interface EffectiveTextAttributeProvider { const val UNSET = ReactFontManager.TypefaceStyle.UNSET } - fun getTextTransform(): TextTransform + val textTransform: TextTransform - fun getEffectiveLetterSpacing(): Float + val effectiveLetterSpacing: Float /** * @return The effective font size, or {@link #UNSET} if not set */ - fun getEffectiveFontSize(): Int + val effectiveFontSize: Int - fun getRole(): Role? + val role: Role? - fun getAccessibilityRole(): ReactAccessibilityDelegate.AccessibilityRole? + val accessibilityRole: ReactAccessibilityDelegate.AccessibilityRole? - fun isBackgroundColorSet(): Boolean + val isBackgroundColorSet: Boolean - fun getBackgroundColor(): Int + val backgroundColor: Int - fun isColorSet(): Boolean + val isColorSet: Boolean - fun getColor(): Int + val color: Int - fun getFontStyle(): Int + val fontStyle: Int - fun getFontWeight(): Int + val fontWeight: Int - fun getFontFamily(): String? + val fontFamily: String? - fun getFontFeatureSettings(): String? + val fontFeatureSettings: String? - fun isUnderlineTextDecorationSet(): Boolean + val isUnderlineTextDecorationSet: Boolean - fun isLineThroughTextDecorationSet(): Boolean + val isLineThroughTextDecorationSet: Boolean - fun getTextShadowOffsetDx(): Float + val textShadowOffsetDx: Float - fun getTextShadowOffsetDy(): Float + val textShadowOffsetDy: Float - fun getTextShadowRadius(): Float + val textShadowRadius: Float - fun getTextShadowColor(): Int + val textShadowColor: Int - fun getEffectiveLineHeight(): Float + val effectiveLineHeight: Float } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/HierarchicTextAttributeProvider.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/HierarchicTextAttributeProvider.kt index df03537a21cfe4..ab3c4836122b77 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/HierarchicTextAttributeProvider.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/HierarchicTextAttributeProvider.kt @@ -14,74 +14,92 @@ class HierarchicTextAttributeProvider( private val parentTextAttributes: TextAttributes?, private val textAttributes: TextAttributes ) : EffectiveTextAttributeProvider { - override fun getTextTransform(): TextTransform = textAttributes.textTransform + override val textTransform: TextTransform + get() = textAttributes.textTransform - override fun getRole(): ReactAccessibilityDelegate.Role? = textShadowNode.role + override val role: ReactAccessibilityDelegate.Role? + get() = textShadowNode.role - override fun getAccessibilityRole(): ReactAccessibilityDelegate.AccessibilityRole? = - textShadowNode.accessibilityRole + override val accessibilityRole: ReactAccessibilityDelegate.AccessibilityRole? + get() = textShadowNode.accessibilityRole - override fun isBackgroundColorSet(): Boolean = textShadowNode.isBackgroundColorSet + override val isBackgroundColorSet: Boolean + get() = textShadowNode.isBackgroundColorSet - override fun getBackgroundColor(): Int = textShadowNode.backgroundColor + override val backgroundColor: Int + get() = textShadowNode.backgroundColor - override fun isColorSet(): Boolean = textShadowNode.isColorSet + override val isColorSet: Boolean + get() = textShadowNode.isColorSet - override fun getColor(): Int = textShadowNode.color + override val color: Int + get() = textShadowNode.color - override fun getFontStyle(): Int = textShadowNode.fontStyle + override val fontStyle: Int + get() = textShadowNode.fontStyle - override fun getFontWeight(): Int = textShadowNode.fontWeight + override val fontWeight: Int + get() = textShadowNode.fontWeight - override fun getFontFamily(): String = textShadowNode.fontFamily + override val fontFamily: String + get() = textShadowNode.fontFamily - override fun getFontFeatureSettings(): String = textShadowNode.fontFeatureSettings + override val fontFeatureSettings: String + get() = textShadowNode.fontFeatureSettings - override fun isUnderlineTextDecorationSet(): Boolean = textShadowNode.isUnderlineTextDecorationSet + override val isUnderlineTextDecorationSet: Boolean + get() = textShadowNode.isUnderlineTextDecorationSet - override fun isLineThroughTextDecorationSet(): Boolean = - textShadowNode.isLineThroughTextDecorationSet + override val isLineThroughTextDecorationSet: Boolean + get() = textShadowNode.isLineThroughTextDecorationSet - override fun getTextShadowOffsetDx(): Float = textShadowNode.textShadowOffsetDx + override val textShadowOffsetDx: Float + get() = textShadowNode.textShadowOffsetDx - override fun getTextShadowOffsetDy(): Float = textShadowNode.textShadowOffsetDy + override val textShadowOffsetDy: Float + get() = textShadowNode.textShadowOffsetDy - override fun getTextShadowRadius(): Float = textShadowNode.textShadowRadius + override val textShadowRadius: Float + get() = textShadowNode.textShadowRadius - override fun getTextShadowColor(): Int = textShadowNode.textShadowColor + override val textShadowColor: Int + get() = textShadowNode.textShadowColor - override fun getEffectiveLetterSpacing(): Float { - val letterSpacing = textAttributes.effectiveLetterSpacing + override val effectiveLetterSpacing: Float + get() { + val letterSpacing = textAttributes.effectiveLetterSpacing - val isParentLetterSpacingDifferent = - parentTextAttributes == null || parentTextAttributes.effectiveLetterSpacing != letterSpacing + val isParentLetterSpacingDifferent = + parentTextAttributes == null || parentTextAttributes.effectiveLetterSpacing != letterSpacing - return if (!letterSpacing.isNaN() && isParentLetterSpacingDifferent) { - letterSpacing - } else { - Float.NaN + return if (!letterSpacing.isNaN() && isParentLetterSpacingDifferent) { + letterSpacing + } else { + Float.NaN + } } - } - override fun getEffectiveFontSize(): Int { - val fontSize = textAttributes.effectiveFontSize + override val effectiveFontSize: Int + get() { + val fontSize = textAttributes.effectiveFontSize - return if (parentTextAttributes == null || parentTextAttributes.effectiveFontSize != fontSize) { - fontSize - } else { - EffectiveTextAttributeProvider.UNSET + return if (parentTextAttributes == null || parentTextAttributes.effectiveFontSize != fontSize) { + fontSize + } else { + EffectiveTextAttributeProvider.UNSET + } } - } - override fun getEffectiveLineHeight(): Float { - val lineHeight = textAttributes.effectiveLineHeight - val isParentLineHeightDifferent = - parentTextAttributes == null || parentTextAttributes.effectiveLineHeight != lineHeight - - return if (!lineHeight.isNaN() && isParentLineHeightDifferent) { - lineHeight - } else { - Float.NaN + override val effectiveLineHeight: Float + get() { + val lineHeight = textAttributes.effectiveLineHeight + val isParentLineHeightDifferent = + parentTextAttributes == null || parentTextAttributes.effectiveLineHeight != lineHeight + + return if (!lineHeight.isNaN() && isParentLineHeightDifferent) { + lineHeight + } else { + Float.NaN + } } - } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/TextLayoutUtils.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/TextLayoutUtils.kt index d677ed17c4e7f0..495acd67a55099 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/TextLayoutUtils.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/views/text/TextLayoutUtils.kt @@ -56,7 +56,7 @@ object TextLayoutUtils { fun addText( sb: SpannableStringBuilder, text: String?, textAttributeProvider: EffectiveTextAttributeProvider ) { - sb.append(TextTransform.apply(text, textAttributeProvider.getTextTransform())) + sb.append(TextTransform.apply(text, textAttributeProvider.textTransform)) } fun addInlineViewPlaceholderSpan( @@ -114,8 +114,8 @@ object TextLayoutUtils { end: Int ) { val roleIsLink = - textAttributeProvider.getRole()?.let { it == ReactAccessibilityDelegate.Role.LINK } - ?: (textAttributeProvider.getAccessibilityRole() == ReactAccessibilityDelegate.AccessibilityRole.LINK) + textAttributeProvider.role?.let { it == ReactAccessibilityDelegate.Role.LINK } + ?: (textAttributeProvider.accessibilityRole == ReactAccessibilityDelegate.AccessibilityRole.LINK) if (roleIsLink) { ops.add(SetSpanOperation(start, end, ReactClickableSpan(reactTag))) } @@ -127,10 +127,10 @@ object TextLayoutUtils { start: Int, end: Int ) { - if (textAttributeProvider.isColorSet()) { + if (textAttributeProvider.isColorSet) { ops.add( SetSpanOperation( - start, end, ReactForegroundColorSpan(textAttributeProvider.getColor()) + start, end, ReactForegroundColorSpan(textAttributeProvider.color) ) ) } @@ -142,10 +142,10 @@ object TextLayoutUtils { start: Int, end: Int ) { - if (textAttributeProvider.isBackgroundColorSet()) { + if (textAttributeProvider.isBackgroundColorSet) { ops.add( SetSpanOperation( - start, end, ReactBackgroundColorSpan(textAttributeProvider.getBackgroundColor()) + start, end, ReactBackgroundColorSpan(textAttributeProvider.backgroundColor) ) ) } @@ -157,7 +157,7 @@ object TextLayoutUtils { start: Int, end: Int ) { - val effectiveLetterSpacing = textAttributeProvider.getEffectiveLetterSpacing() + val effectiveLetterSpacing = textAttributeProvider.effectiveLetterSpacing if (!effectiveLetterSpacing.isNaN()) { ops.add(SetSpanOperation(start, end, CustomLetterSpacingSpan(effectiveLetterSpacing))) @@ -171,7 +171,7 @@ object TextLayoutUtils { start: Int, end: Int ) { - val effectiveFontSize = textAttributeProvider.getEffectiveFontSize() + val effectiveFontSize = textAttributeProvider.effectiveFontSize if (effectiveFontSize != UNSET) { ops.add(SetSpanOperation(start, end, ReactAbsoluteSizeSpan(effectiveFontSize))) @@ -185,9 +185,9 @@ object TextLayoutUtils { start: Int, end: Int ) { - val fontStyle = textAttributeProvider.getFontStyle() - val fontWeight = textAttributeProvider.getFontWeight() - val fontFamily = textAttributeProvider.getFontFamily() + val fontStyle = textAttributeProvider.fontStyle + val fontWeight = textAttributeProvider.fontWeight + val fontFamily = textAttributeProvider.fontFamily if (fontStyle != UNSET || fontWeight != UNSET || fontFamily != null) { ops.add( @@ -195,7 +195,7 @@ object TextLayoutUtils { start, end, CustomStyleSpan( fontStyle, fontWeight, - textAttributeProvider.getFontFeatureSettings(), + textAttributeProvider.fontFeatureSettings, fontFamily, context.assets ) @@ -210,7 +210,7 @@ object TextLayoutUtils { start: Int, end: Int ) { - if (textAttributeProvider.isUnderlineTextDecorationSet()) { + if (textAttributeProvider.isUnderlineTextDecorationSet) { ops.add(SetSpanOperation(start, end, ReactUnderlineSpan())) } } @@ -221,7 +221,7 @@ object TextLayoutUtils { start: Int, end: Int ) { - if (textAttributeProvider.isLineThroughTextDecorationSet()) { + if (textAttributeProvider.isLineThroughTextDecorationSet) { ops.add(SetSpanOperation(start, end, ReactStrikethroughSpan())) } } @@ -233,18 +233,18 @@ object TextLayoutUtils { end: Int ) { val hasTextShadowOffset = - textAttributeProvider.getTextShadowOffsetDx() != 0f || textAttributeProvider.getTextShadowOffsetDy() != 0f - val hasTextShadowRadius = textAttributeProvider.getTextShadowRadius() != 0f - val hasTextShadowColorAlpha = Color.alpha(textAttributeProvider.getTextShadowColor()) != 0 + textAttributeProvider.textShadowOffsetDx != 0f || textAttributeProvider.textShadowOffsetDy != 0f + val hasTextShadowRadius = textAttributeProvider.textShadowRadius != 0f + val hasTextShadowColorAlpha = Color.alpha(textAttributeProvider.textShadowColor) != 0 if ((hasTextShadowOffset || hasTextShadowRadius) && hasTextShadowColorAlpha) { ops.add( SetSpanOperation( start, end, ShadowStyleSpan( - textAttributeProvider.getTextShadowOffsetDx(), - textAttributeProvider.getTextShadowOffsetDy(), - textAttributeProvider.getTextShadowRadius(), - textAttributeProvider.getTextShadowColor() + textAttributeProvider.textShadowOffsetDx, + textAttributeProvider.textShadowOffsetDy, + textAttributeProvider.textShadowRadius, + textAttributeProvider.textShadowColor ) ) ) @@ -257,7 +257,7 @@ object TextLayoutUtils { start: Int, end: Int ) { - val effectiveLineHeight = textAttributeProvider.getEffectiveLineHeight() + val effectiveLineHeight = textAttributeProvider.effectiveLineHeight if (!effectiveLineHeight.isNaN()) { ops.add(SetSpanOperation(start, end, CustomLineHeightSpan(effectiveLineHeight))) }