|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2016-Present Datadog, Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.datadog.android.sessionreplay.compose.internal.utils |
| 8 | + |
| 9 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 10 | +import androidx.compose.ui.Modifier |
| 11 | +import androidx.compose.ui.geometry.Size |
| 12 | +import androidx.compose.ui.graphics.Color |
| 13 | +import androidx.compose.ui.graphics.Shape |
| 14 | +import androidx.compose.ui.semantics.SemanticsNode |
| 15 | +import androidx.compose.ui.unit.Density |
| 16 | +import com.datadog.android.api.InternalLogger |
| 17 | +import com.datadog.android.sessionreplay.compose.internal.utils.SemanticsUtils.Companion.COLOR_UNSPECIFIED |
| 18 | +import com.datadog.android.sessionreplay.utils.GlobalBounds |
| 19 | + |
| 20 | +internal class BackgroundResolver( |
| 21 | + private val reflectionUtils: ReflectionUtils, |
| 22 | + private val innerBoundsOf: (SemanticsNode) -> GlobalBounds, |
| 23 | + private val internalLogger: InternalLogger = InternalLogger.UNBOUND |
| 24 | +) { |
| 25 | + internal fun resolveBackgroundInfo(semanticsNode: SemanticsNode): List<BackgroundInfo> { |
| 26 | + val backgroundInfoList = mutableListOf<BackgroundInfo>() |
| 27 | + // CurrentBackgroundInfo is to store bounds, color and shape information in sequence of modifiers. |
| 28 | + var currentBackgroundInfo = BackgroundInfo() |
| 29 | + var currentBounds: GlobalBounds = resolveOuterBounds(semanticsNode) |
| 30 | + // If the currentBounds is already invalid, return with the existing wireframes |
| 31 | + if (currentBounds.width <= 0 || currentBounds.height <= 0) { |
| 32 | + return backgroundInfoList |
| 33 | + } |
| 34 | + val density = semanticsNode.layoutInfo.density |
| 35 | + // Iterate all the modifiers in user calling sequence, when meet: |
| 36 | + // -> clip(): calculate the corner radius and update `currentBackgroundInfo` |
| 37 | + // -> padding(): shrink the bounds from the previous bounds and update `currentBackgroundInfo` |
| 38 | + // -> background(): retrieve the color and use `currentBackgroundInfo` to generate wireframes, |
| 39 | + // then reset `currentBackgroundInfo`. |
| 40 | + semanticsNode.layoutInfo.getModifierInfo().forEach { modifierInfo -> |
| 41 | + if (reflectionUtils.isBackgroundElement(modifierInfo.modifier)) { |
| 42 | + val color = resolveBackgroundElementColor(modifierInfo.modifier) |
| 43 | + currentBackgroundInfo = currentBackgroundInfo.copy(globalBounds = currentBounds, color = color) |
| 44 | + backgroundInfoList.add(currentBackgroundInfo) |
| 45 | + currentBackgroundInfo = BackgroundInfo() |
| 46 | + } else if (reflectionUtils.isPaddingElement(modifierInfo.modifier)) { |
| 47 | + currentBounds = shrinkBounds(modifierInfo.modifier, currentBounds) |
| 48 | + currentBackgroundInfo = currentBackgroundInfo.copy(globalBounds = currentBounds) |
| 49 | + } else if (reflectionUtils.isGraphicsLayerElement(modifierInfo.modifier)) { |
| 50 | + val cornerRadius = reflectionUtils.getClipShape(modifierInfo.modifier) |
| 51 | + ?.let { resolveCornerRadius(it, currentBounds, density) } ?: 0f |
| 52 | + currentBackgroundInfo = currentBackgroundInfo.copy(cornerRadius = cornerRadius) |
| 53 | + } |
| 54 | + } |
| 55 | + return backgroundInfoList |
| 56 | + } |
| 57 | + |
| 58 | + internal fun resolveBackgroundColor(semanticsNode: SemanticsNode): Long? { |
| 59 | + val topmostBackground = |
| 60 | + semanticsNode.layoutInfo.getModifierInfo().lastOrNull { modifierInfo -> |
| 61 | + reflectionUtils.isBackgroundElement(modifierInfo.modifier) |
| 62 | + } |
| 63 | + return topmostBackground?.let { resolveBackgroundElementColor(it.modifier) } |
| 64 | + } |
| 65 | + |
| 66 | + internal fun resolveBackgroundShape(semanticsNode: SemanticsNode): Shape? { |
| 67 | + val backgroundModifier = semanticsNode.layoutInfo.getModifierInfo().lastOrNull { |
| 68 | + reflectionUtils.isBackgroundElement(it.modifier) |
| 69 | + }?.modifier |
| 70 | + return backgroundModifier?.let { reflectionUtils.getShape(it) } |
| 71 | + } |
| 72 | + |
| 73 | + internal fun resolveCornerRadius(shape: Shape, currentBounds: GlobalBounds, density: Density): Float { |
| 74 | + val size = Size( |
| 75 | + currentBounds.width.toFloat() * density.density, |
| 76 | + currentBounds.height.toFloat() * density.density |
| 77 | + ) |
| 78 | + // We only have a single value for corner radius, so we default to using the |
| 79 | + // top left (i.e.: topStart) corner's value and apply it to all corners |
| 80 | + return if (shape is RoundedCornerShape) { |
| 81 | + shape.topStart.toPx(size, density) / density.density |
| 82 | + } else { |
| 83 | + 0f |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private fun resolveOuterBounds(semanticsNode: SemanticsNode): GlobalBounds { |
| 88 | + var currentBounds = innerBoundsOf(semanticsNode) |
| 89 | + semanticsNode.layoutInfo.getModifierInfo().filter { |
| 90 | + reflectionUtils.isPaddingElement(it.modifier) |
| 91 | + }.forEach { |
| 92 | + val top = reflectionUtils.getTopPadding(it.modifier) |
| 93 | + val start = reflectionUtils.getStartPadding(it.modifier) |
| 94 | + val end = reflectionUtils.getEndPadding(it.modifier) |
| 95 | + val bottom = reflectionUtils.getBottomPadding(it.modifier) |
| 96 | + currentBounds = GlobalBounds( |
| 97 | + x = currentBounds.x - start.toLong(), |
| 98 | + y = currentBounds.y - top.toLong(), |
| 99 | + width = currentBounds.width + (end + start).toLong(), |
| 100 | + height = currentBounds.height + (bottom + top).toLong() |
| 101 | + ) |
| 102 | + } |
| 103 | + return currentBounds |
| 104 | + } |
| 105 | + |
| 106 | + private fun shrinkBounds(modifier: Modifier, currentBounds: GlobalBounds): GlobalBounds { |
| 107 | + val top = reflectionUtils.getTopPadding(modifier) |
| 108 | + val start = reflectionUtils.getStartPadding(modifier) |
| 109 | + val end = reflectionUtils.getEndPadding(modifier) |
| 110 | + val bottom = reflectionUtils.getBottomPadding(modifier) |
| 111 | + return GlobalBounds( |
| 112 | + x = currentBounds.x + start.toLong(), |
| 113 | + y = currentBounds.y + top.toLong(), |
| 114 | + width = currentBounds.width - (end + start).toLong(), |
| 115 | + height = currentBounds.height - (bottom + top).toLong() |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + private fun resolveBackgroundElementColor(modifier: Modifier): Long? { |
| 120 | + val rawColor = reflectionUtils.getColor(modifier) |
| 121 | + val alpha = reflectionUtils.getAlpha(modifier) |
| 122 | + if (rawColor != null && rawColor != COLOR_UNSPECIFIED) { |
| 123 | + return applyAlphaToColor(rawColor, alpha) |
| 124 | + } |
| 125 | + return resolveBrushColor(modifier, alpha) |
| 126 | + } |
| 127 | + |
| 128 | + private fun resolveBrushColor(modifier: Modifier, alpha: Float?): Long? { |
| 129 | + val brush = reflectionUtils.getBrush(modifier) ?: return null |
| 130 | + val colors = reflectionUtils.getBrushColors(brush) |
| 131 | + return when { |
| 132 | + colors == null -> { |
| 133 | + logBrushIssue(brush, InternalLogger.Level.INFO) { |
| 134 | + "Unsupported Brush type for Compose background: ${brush.javaClass.name}" |
| 135 | + } |
| 136 | + null |
| 137 | + } |
| 138 | + colors.isEmpty() -> { |
| 139 | + logBrushIssue(brush, InternalLogger.Level.WARN) { |
| 140 | + "Known Brush type but failed to read color list via reflection: ${brush.javaClass.name}" |
| 141 | + } |
| 142 | + null |
| 143 | + } |
| 144 | + else -> { |
| 145 | + @Suppress("UnsafeThirdPartyFunctionCall") |
| 146 | + applyAlphaToColor(colors.first().value.toLong(), alpha) |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // alpha <= 0f is treated as invisible: returns COLOR_UNSPECIFIED so convertColor produces null fill |
| 152 | + private fun applyAlphaToColor(colorValue: Long, alpha: Float?): Long { |
| 153 | + return when { |
| 154 | + alpha == null || alpha >= 1f -> colorValue |
| 155 | + alpha <= 0f -> COLOR_UNSPECIFIED |
| 156 | + else -> { |
| 157 | + val color = Color(colorValue.toULong()) |
| 158 | + color.copy(alpha = color.alpha * alpha).value.toLong() |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private fun logBrushIssue(brush: Any, level: InternalLogger.Level, messageBuilder: () -> String) { |
| 164 | + internalLogger.log( |
| 165 | + level = level, |
| 166 | + targets = listOf(InternalLogger.Target.MAINTAINER, InternalLogger.Target.TELEMETRY), |
| 167 | + messageBuilder = messageBuilder, |
| 168 | + onlyOnce = true, |
| 169 | + additionalProperties = mapOf("brush.type" to brush.javaClass.name, COMPONENT_KEY to COMPONENT_NAME) |
| 170 | + ) |
| 171 | + } |
| 172 | + |
| 173 | + private companion object { |
| 174 | + private const val COMPONENT_NAME = "BackgroundResolver" |
| 175 | + private const val COMPONENT_KEY = "component" |
| 176 | + } |
| 177 | +} |
0 commit comments