|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.uimanager.drawable |
| 9 | + |
| 10 | +import android.content.Context |
| 11 | +import android.graphics.Canvas |
| 12 | +import android.graphics.ColorFilter |
| 13 | +import android.graphics.Rect |
| 14 | +import android.graphics.RenderEffect |
| 15 | +import android.graphics.RenderNode |
| 16 | +import androidx.annotation.RequiresApi |
| 17 | +import com.facebook.react.uimanager.FilterHelper |
| 18 | +import com.facebook.react.uimanager.PixelUtil |
| 19 | +import kotlin.math.roundToInt |
| 20 | + |
| 21 | +// "the resulting shadow must approximate {...} a Gaussian blur with a standard deviation equal |
| 22 | +// to half the blur radius" |
| 23 | +// https://www.w3.org/TR/css-backgrounds-3/#shadow-blur |
| 24 | +private const val SHADOW_RADIUS_SIGMA_SCALE = 0.5f |
| 25 | + |
| 26 | +/** Draws an outer-box shadow https://www.w3.org/TR/css-backgrounds-3/#shadow-shape */ |
| 27 | +@RequiresApi(31) |
| 28 | +public class BoxShadowDrawable( |
| 29 | + context: Context, |
| 30 | + private val background: CSSBackgroundDrawable, |
| 31 | + private val shadowColor: Int, |
| 32 | + private val offsetX: Float, |
| 33 | + private val offsetY: Float, |
| 34 | + private val blur: Float, |
| 35 | + private val spread: Float, |
| 36 | +) : DecorationDrawable() { |
| 37 | + |
| 38 | + private val shadowShapeDrawable = CSSBackgroundDrawable(context).apply { color = shadowColor } |
| 39 | + |
| 40 | + private val renderNode = |
| 41 | + RenderNode("box-shadow").apply { |
| 42 | + setClipToBounds(false) |
| 43 | + setRenderEffect(createBlurEffect()) |
| 44 | + } |
| 45 | + |
| 46 | + override fun draw(canvas: Canvas) { |
| 47 | + val spreadPx = PixelUtil.toPixelFromDIP(spread).roundToInt().coerceAtLeast(0) |
| 48 | + val boundsWithSpread = Rect(bounds).apply { inset(-spreadPx, -spreadPx) } |
| 49 | + |
| 50 | + with(shadowShapeDrawable) { |
| 51 | + setBounds(0, 0, boundsWithSpread.width(), boundsWithSpread.height()) |
| 52 | + setRadius(background.fullBorderRadius) |
| 53 | + for (location in CSSBackgroundDrawable.BorderRadiusLocation.values()) { |
| 54 | + setRadius(background.getBorderRadius(location), location.ordinal) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + with(renderNode) { |
| 59 | + val offsetBounds = |
| 60 | + Rect(boundsWithSpread).apply { |
| 61 | + offset( |
| 62 | + PixelUtil.toPixelFromDIP(offsetX).roundToInt(), |
| 63 | + PixelUtil.toPixelFromDIP(offsetY).roundToInt()) |
| 64 | + } |
| 65 | + |
| 66 | + setPosition(offsetBounds) |
| 67 | + discardDisplayList() |
| 68 | + beginRecording().let { nodeCanvas -> shadowShapeDrawable.draw(nodeCanvas) } |
| 69 | + endRecording() |
| 70 | + } |
| 71 | + |
| 72 | + with(canvas) { |
| 73 | + clipOutPath(background.borderBoxPath()) |
| 74 | + drawRenderNode(renderNode) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + override fun setAlpha(alpha: Int) { |
| 79 | + renderNode.alpha = alpha / 255f |
| 80 | + } |
| 81 | + |
| 82 | + override fun setColorFilter(colorFilter: ColorFilter?) { |
| 83 | + val chainedEffect = colorFilter?.let { RenderEffect.createColorFilterEffect(it) } |
| 84 | + renderNode.setRenderEffect(createBlurEffect(chainedEffect)) |
| 85 | + } |
| 86 | + |
| 87 | + private fun createBlurEffect(chainedEffect: RenderEffect? = null): RenderEffect? = |
| 88 | + if (blur <= 0f) chainedEffect |
| 89 | + else FilterHelper.createBlurEffect(blur * SHADOW_RADIUS_SIGMA_SCALE, chainedEffect) |
| 90 | + |
| 91 | + override fun getOpacity(): Int = (renderNode.alpha * 255).roundToInt() |
| 92 | +} |
0 commit comments