From c552d15bc0437b179166b9db2088ad16fbef07f6 Mon Sep 17 00:00:00 2001 From: Michal Zielinski Date: Mon, 2 Dec 2024 03:15:21 -0800 Subject: [PATCH] Ship Kotlin implementation of SolidColor Summary: This diff ships SolidColor Kotlin implementation that uses Image primitive under the hood. SolidColor spec also used image under the hood. I've ran an experiment with a simplified Primitive SolidColor implementation but there were no perf improvements and it caused some issues. That's why I'm shipping SolidColor as a KComponent that uses Image primitive under the hood. Reviewed By: pentiumao Differential Revision: D66643557 fbshipit-source-id: 13ca7673a776af1a4d7e00c7865d2a28a78708bf --- .../litho/config/ComponentsConfiguration.kt | 6 ----- .../litho/widget/ExperimentalSolidColor.kt | 26 +++++++------------ .../facebook/litho/widget/SolidColorSpec.kt | 7 +---- 3 files changed, 10 insertions(+), 29 deletions(-) diff --git a/litho-core/src/main/java/com/facebook/litho/config/ComponentsConfiguration.kt b/litho-core/src/main/java/com/facebook/litho/config/ComponentsConfiguration.kt index feab98e2ba9..ea5f82301b2 100644 --- a/litho-core/src/main/java/com/facebook/litho/config/ComponentsConfiguration.kt +++ b/litho-core/src/main/java/com/facebook/litho/config/ComponentsConfiguration.kt @@ -264,12 +264,6 @@ internal constructor( /** This flag is used to enable a fix for the ANR issue with sticky header RecyclerView. */ @JvmField var enableFixForStickyHeader: Boolean = false - /** - * This flag is used to enable using PrimitiveComponent implementation of a SolidColor - * component. - */ - @JvmField var usePrimitiveSolidColor: Boolean = false - /** This config will enable logging of interactable components with 0 alpha */ @JvmField var isZeroAlphaLoggingEnabled: Boolean = false diff --git a/litho-widget/src/main/java/com/facebook/litho/widget/ExperimentalSolidColor.kt b/litho-widget/src/main/java/com/facebook/litho/widget/ExperimentalSolidColor.kt index 6f687000236..cb427389e0e 100644 --- a/litho-widget/src/main/java/com/facebook/litho/widget/ExperimentalSolidColor.kt +++ b/litho-widget/src/main/java/com/facebook/litho/widget/ExperimentalSolidColor.kt @@ -17,16 +17,15 @@ package com.facebook.litho.widget import android.graphics.drawable.ColorDrawable +import android.widget.ImageView import androidx.annotation.ColorInt import androidx.core.graphics.ColorUtils -import com.facebook.litho.LithoPrimitive -import com.facebook.litho.PrimitiveComponent -import com.facebook.litho.PrimitiveComponentScope +import com.facebook.litho.Component +import com.facebook.litho.ComponentScope +import com.facebook.litho.KComponent import com.facebook.litho.Style import com.facebook.litho.annotations.ExperimentalLithoApi import com.facebook.litho.useCached -import com.facebook.rendercore.primitives.DrawableAllocator -import com.facebook.rendercore.primitives.FillLayoutBehavior import kotlin.math.min /** @@ -41,9 +40,9 @@ class ExperimentalSolidColor( @ColorInt private val color: Int, private val alpha: Float = -1.0f, private val style: Style? = null -) : PrimitiveComponent() { +) : KComponent() { - override fun PrimitiveComponentScope.render(): LithoPrimitive { + override fun ComponentScope.render(): Component { val colorWithAlpha = useCached(color, alpha) { if (alpha >= 0f) { @@ -54,16 +53,9 @@ class ExperimentalSolidColor( } } - return LithoPrimitive( - layoutBehavior = FillLayoutBehavior(defaultWidth = 0, defaultHeight = 0), - mountBehavior = - MountBehavior(DrawableAllocator { ColorDrawable() }) { - bind(colorWithAlpha) { content -> - val defaultColor = content.color - content.color = colorWithAlpha - onUnbind { content.color = defaultColor } - } - }, + return ExperimentalImage( + drawable = ColorDrawable(colorWithAlpha), + scaleType = ImageView.ScaleType.FIT_XY, style = style) } } diff --git a/litho-widget/src/main/java/com/facebook/litho/widget/SolidColorSpec.kt b/litho-widget/src/main/java/com/facebook/litho/widget/SolidColorSpec.kt index f36d58e174b..3446a94f77c 100644 --- a/litho-widget/src/main/java/com/facebook/litho/widget/SolidColorSpec.kt +++ b/litho-widget/src/main/java/com/facebook/litho/widget/SolidColorSpec.kt @@ -26,7 +26,6 @@ import com.facebook.litho.annotations.Prop import com.facebook.litho.annotations.PropDefault import com.facebook.litho.annotations.Reason import com.facebook.litho.annotations.ResType -import com.facebook.litho.config.ComponentsConfiguration /** * A component that renders a solid color. @@ -48,10 +47,6 @@ internal object SolidColorSpec { @Prop(resType = ResType.COLOR) color: Int, @Prop(optional = true, isCommonProp = true, overrideCommonPropBehavior = true) alpha: Float ): Component { - return if (ComponentsConfiguration.usePrimitiveSolidColor) { - ExperimentalSolidColor(color = color, alpha = alpha) - } else { - SolidColorComponent.create(c).color(color).alpha(alpha).build() - } + return ExperimentalSolidColor(color = color, alpha = alpha) } }