Skip to content

Commit

Permalink
Ship Kotlin implementation of SolidColor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zielinskimz authored and facebook-github-bot committed Dec 2, 2024
1 parent 7a5316a commit c552d15
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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) {
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
}

0 comments on commit c552d15

Please sign in to comment.