Skip to content

Commit

Permalink
fix setting background drawable when background color is transparent
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperzolkiewski committed Sep 12, 2024
1 parent b4b909c commit c3b7d92
Showing 1 changed file with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,11 @@ class RNGestureHandlerButtonViewManager : ViewGroupManager<ButtonViewGroup>(), R
return false
}

private fun updateBackgroundColor(backgroundColor: Int, selectable: Drawable?) {
private fun updateBackgroundColor(backgroundColor: Int, borderDrawable: Drawable, selectable: Drawable?) {
val colorDrawable = PaintDrawable(backgroundColor)
val borderDrawable = PaintDrawable(Color.TRANSPARENT)

if (hasBorderRadii) {
colorDrawable.setCornerRadii(buildBorderRadii())
borderDrawable.setCornerRadii(buildBorderRadii())
}

if (borderWidth > 0f) {
borderDrawable.paint.apply {
style = Paint.Style.STROKE
strokeWidth = borderWidth
color = borderColor ?: Color.BLACK
pathEffect = buildBorderStyle()
}
}

val layerDrawable = LayerDrawable(if (selectable != null) arrayOf(colorDrawable, selectable, borderDrawable) else arrayOf(colorDrawable, borderDrawable))
Expand All @@ -324,6 +313,7 @@ class RNGestureHandlerButtonViewManager : ViewGroupManager<ButtonViewGroup>(), R
}

val selectable = createSelectableDrawable()
val borderDrawable = createBorderDrawable()

if (hasBorderRadii && selectable is RippleDrawable) {
val mask = PaintDrawable(Color.WHITE)
Expand All @@ -334,15 +324,34 @@ class RNGestureHandlerButtonViewManager : ViewGroupManager<ButtonViewGroup>(), R
if (useDrawableOnForeground && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
foreground = selectable
if (_backgroundColor != Color.TRANSPARENT) {
updateBackgroundColor(_backgroundColor, null)
updateBackgroundColor(_backgroundColor, borderDrawable, null)
}
} else if (_backgroundColor == Color.TRANSPARENT && rippleColor == null) {
background = selectable
background = LayerDrawable(arrayOf(selectable, borderDrawable))
} else {
updateBackgroundColor(_backgroundColor, selectable)
updateBackgroundColor(_backgroundColor, borderDrawable, selectable)
}
}

private fun createBorderDrawable(): Drawable {
val borderDrawable = PaintDrawable(Color.TRANSPARENT)

if (hasBorderRadii) {
borderDrawable.setCornerRadii(buildBorderRadii())
}

if (borderWidth > 0f) {
borderDrawable.paint.apply {
style = Paint.Style.STROKE
strokeWidth = borderWidth
color = borderColor ?: Color.BLACK
pathEffect = buildBorderStyle()
}
}

return borderDrawable
}

private fun createSelectableDrawable(): Drawable? {
// don't create ripple drawable at all when it's not supposed to be visible
if (rippleColor == Color.TRANSPARENT) {
Expand Down

0 comments on commit c3b7d92

Please sign in to comment.