Skip to content

Commit

Permalink
Go back to object approach instead of manual drawable layer handling …
Browse files Browse the repository at this point in the history
…for CompositeBackgroundDrawable (facebook#48835)

Summary:
Pull Request resolved: facebook#48835

D65907786 ended up regressing a bit of the performance gains from new Background and Border Drawables. changing back to the previous approach.

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D68354292

fbshipit-source-id: f2db6d7ad5c1590d5d4d8261d76281f3592f488a
  • Loading branch information
jorge-cab authored and facebook-github-bot committed Jan 23, 2025
1 parent 0353648 commit f89f191
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import android.graphics.Path
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.os.Build
import android.view.View
import android.widget.ImageView
Expand Down Expand Up @@ -106,14 +105,15 @@ public object BackgroundStyleApplicator {

composite.borderInsets = composite.borderInsets ?: BorderInsets()
composite.borderInsets?.setBorderWidth(edge, width)
if (Build.VERSION.SDK_INT >= MIN_OUTSET_BOX_SHADOW_SDK_VERSION) {
for (shadow in composite.outerShadows.filterIsInstance<OutsetBoxShadowDrawable>()) {
shadow.borderRadius = composite.borderRadius
}
}

if (Build.VERSION.SDK_INT >= MIN_INSET_BOX_SHADOW_SDK_VERSION) {
val innerShadows = composite.innerShadows
if (innerShadows != null) {
for (i in 0 until innerShadows.numberOfLayers) {
val shadow = innerShadows.getDrawable(i)
(shadow as InsetBoxShadowDrawable).borderInsets = composite.borderInsets
shadow.invalidateSelf()
}
for (shadow in composite.innerShadows.filterIsInstance<InsetBoxShadowDrawable>()) {
shadow.borderRadius = composite.borderRadius
}
}
}
Expand Down Expand Up @@ -174,30 +174,16 @@ public object BackgroundStyleApplicator {
}

if (Build.VERSION.SDK_INT >= MIN_OUTSET_BOX_SHADOW_SDK_VERSION) {
val outerShadows = compositeBackgroundDrawable.outerShadows
if (outerShadows != null) {
for (i in 0 until outerShadows.numberOfLayers) {
val shadow = outerShadows.getDrawable(i)
if (shadow is OutsetBoxShadowDrawable) {
shadow.borderRadius = shadow.borderRadius ?: BorderRadiusStyle()
shadow.borderRadius?.set(corner, radius)
shadow.invalidateSelf()
}
}
for (shadow in
compositeBackgroundDrawable.outerShadows.filterIsInstance<OutsetBoxShadowDrawable>()) {
shadow.borderRadius = compositeBackgroundDrawable.borderRadius
}
}

if (Build.VERSION.SDK_INT >= MIN_INSET_BOX_SHADOW_SDK_VERSION) {
val innerShadows = compositeBackgroundDrawable.innerShadows
if (innerShadows != null) {
for (i in 0 until innerShadows.numberOfLayers) {
val shadow = innerShadows.getDrawable(i)
if (shadow is InsetBoxShadowDrawable) {
shadow.borderRadius = shadow.borderRadius ?: BorderRadiusStyle()
shadow.borderRadius?.set(corner, radius)
shadow.invalidateSelf()
}
}
for (shadow in
compositeBackgroundDrawable.outerShadows.filterIsInstance<InsetBoxShadowDrawable>()) {
shadow.borderRadius = compositeBackgroundDrawable.borderRadius
}
}

Expand Down Expand Up @@ -291,8 +277,8 @@ public object BackgroundStyleApplicator {
return
}

var innerShadows: LayerDrawable? = null
var outerShadows: LayerDrawable? = null
var innerShadows = mutableListOf<InsetBoxShadowDrawable>()
var outerShadows = mutableListOf<OutsetBoxShadowDrawable>()

val compositeBackgroundDrawable = ensureCompositeBackgroundDrawable(view)
val borderInsets = compositeBackgroundDrawable.borderInsets
Expand All @@ -302,7 +288,7 @@ public object BackgroundStyleApplicator {
* z-ordering of user-provided shadow-list is opposite direction of LayerDrawable z-ordering
* https://drafts.csswg.org/css-backgrounds/#shadow-layers
*/
for (boxShadow in shadows.asReversed()) {
for (boxShadow in shadows) {
val offsetX = boxShadow.offsetX
val offsetY = boxShadow.offsetY
val color = boxShadow.color ?: Color.BLACK
Expand All @@ -311,10 +297,7 @@ public object BackgroundStyleApplicator {
val inset = boxShadow.inset ?: false

if (inset && Build.VERSION.SDK_INT >= MIN_INSET_BOX_SHADOW_SDK_VERSION) {
if (innerShadows == null) {
innerShadows = LayerDrawable(emptyArray())
}
innerShadows.addLayer(
innerShadows.add(
InsetBoxShadowDrawable(
context = view.context,
borderRadius = borderRadius,
Expand All @@ -325,10 +308,7 @@ public object BackgroundStyleApplicator {
blurRadius = blurRadius,
spread = spreadDistance))
} else if (!inset && Build.VERSION.SDK_INT >= MIN_OUTSET_BOX_SHADOW_SDK_VERSION) {
if (outerShadows == null) {
outerShadows = LayerDrawable(emptyArray())
}
outerShadows.addLayer(
outerShadows.add(
OutsetBoxShadowDrawable(
context = view.context,
borderRadius = borderRadius,
Expand All @@ -340,8 +320,9 @@ public object BackgroundStyleApplicator {
}
}

view.background = ensureCompositeBackgroundDrawable(view).withNewOuterShadow(outerShadows)
view.background = ensureCompositeBackgroundDrawable(view).withNewInnerShadow(innerShadows)
view.background =
ensureCompositeBackgroundDrawable(view)
.withNewShadows(outerShadows = outerShadows, innerShadows = innerShadows)
}

@JvmStatic
Expand Down
Loading

0 comments on commit f89f191

Please sign in to comment.