-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUM-6195: Add support for Compose Checkbox
- Loading branch information
1 parent
a67406e
commit e951a44
Showing
15 changed files
with
942 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
252 changes: 252 additions & 0 deletions
252
...g/android/sessionreplay/compose/internal/mappers/semantics/CheckboxSemanticsNodeMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright CHECKBOX_SIZE16-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.sessionreplay.compose.internal.mappers.semantics | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.Paint | ||
import androidx.compose.ui.graphics.Matrix | ||
import androidx.compose.ui.graphics.Path | ||
import androidx.compose.ui.semantics.SemanticsNode | ||
import androidx.compose.ui.semantics.SemanticsProperties | ||
import androidx.compose.ui.semantics.getOrNull | ||
import androidx.compose.ui.state.ToggleableState | ||
import com.datadog.android.api.InternalLogger | ||
import com.datadog.android.sessionreplay.ImagePrivacy | ||
import com.datadog.android.sessionreplay.compose.internal.data.SemanticsWireframe | ||
import com.datadog.android.sessionreplay.compose.internal.data.UiContext | ||
import com.datadog.android.sessionreplay.compose.internal.utils.PathUtils | ||
import com.datadog.android.sessionreplay.compose.internal.utils.SemanticsUtils | ||
import com.datadog.android.sessionreplay.model.MobileSegment | ||
import com.datadog.android.sessionreplay.recorder.wrappers.BitmapWrapper | ||
import com.datadog.android.sessionreplay.recorder.wrappers.CanvasWrapper | ||
import com.datadog.android.sessionreplay.utils.AsyncJobStatusCallback | ||
import com.datadog.android.sessionreplay.utils.ColorStringFormatter | ||
import com.datadog.android.sessionreplay.utils.GlobalBounds | ||
|
||
internal class CheckboxSemanticsNodeMapper( | ||
colorStringFormatter: ColorStringFormatter, | ||
private val semanticsUtils: SemanticsUtils = SemanticsUtils(), | ||
private val pathUtils: PathUtils = PathUtils(), | ||
private val bitmapWrapper: BitmapWrapper = BitmapWrapper(), | ||
private val canvasWrapper: CanvasWrapper = CanvasWrapper(InternalLogger.UNBOUND) | ||
) : AbstractSemanticsNodeMapper(colorStringFormatter, semanticsUtils) { | ||
|
||
override fun map( | ||
semanticsNode: SemanticsNode, | ||
parentContext: UiContext, | ||
asyncJobStatusCallback: AsyncJobStatusCallback | ||
): SemanticsWireframe { | ||
val globalBounds = resolveBounds(semanticsNode) | ||
|
||
val wireframes = if (isCheckboxChecked(semanticsNode)) { | ||
resolveCheckedCheckbox(parentContext, asyncJobStatusCallback, semanticsNode, globalBounds) | ||
} else { | ||
listOf(resolveUncheckedCheckbox(semanticsNode, globalBounds)) | ||
} | ||
|
||
return SemanticsWireframe( | ||
uiContext = null, | ||
wireframes = wireframes | ||
) | ||
} | ||
|
||
private fun isCheckboxChecked(semanticsNode: SemanticsNode): Boolean = | ||
semanticsNode.config.getOrNull(SemanticsProperties.ToggleableState) == ToggleableState.On | ||
|
||
private fun resolveCheckedCheckbox( | ||
parentContext: UiContext, | ||
asyncJobStatusCallback: AsyncJobStatusCallback, | ||
semanticsNode: SemanticsNode, | ||
globalBounds: GlobalBounds | ||
): List<MobileSegment.Wireframe> { | ||
val checkMarkBitmap = semanticsUtils.resolveCheckPath(semanticsNode)?.let { | ||
convertPathToBitmap(semanticsNode, it) | ||
} | ||
|
||
return if (checkMarkBitmap != null) { | ||
parentContext.imageWireframeHelper.createImageWireframeByBitmap( | ||
id = resolveId(semanticsNode, 0), | ||
globalBounds = globalBounds, | ||
bitmap = checkMarkBitmap, | ||
density = parentContext.density, | ||
isContextualImage = false, | ||
imagePrivacy = ImagePrivacy.MASK_NONE, | ||
asyncJobStatusCallback = asyncJobStatusCallback, | ||
clipping = null, | ||
shapeStyle = null, | ||
border = null | ||
)?.let { | ||
listOf(it) | ||
} ?: createFallbackCheckmarkWireframe(semanticsNode, globalBounds) | ||
} else { | ||
createFallbackCheckmarkWireframe(semanticsNode, globalBounds) | ||
} | ||
} | ||
|
||
private fun createFallbackCheckmarkWireframe( | ||
semanticsNode: SemanticsNode, | ||
globalBounds: GlobalBounds | ||
): List<MobileSegment.Wireframe> { | ||
val backgroundColor = resolveCheckboxFillColor(semanticsNode) | ||
|
||
val background: MobileSegment.Wireframe = resolveUncheckedCheckbox( | ||
semanticsNode = semanticsNode, | ||
globalBounds = globalBounds, | ||
backgroundColor = backgroundColor | ||
) | ||
|
||
// ensure contrast | ||
val strokeColor = if (backgroundColor == DEFAULT_COLOR_WHITE) { | ||
DEFAULT_COLOR_BLACK | ||
} else { | ||
DEFAULT_COLOR_WHITE | ||
} | ||
|
||
val checkmarkWidth = globalBounds.width * STROKE_SIZE_FACTOR | ||
val checkmarkHeight = globalBounds.height * STROKE_SIZE_FACTOR | ||
val xPos = globalBounds.x + ((CHECKBOX_SIZE / 2) - (checkmarkWidth / 2)) | ||
val yPos = globalBounds.y + ((CHECKBOX_SIZE / 2) - (checkmarkHeight / 2)) | ||
val foreground: MobileSegment.Wireframe = MobileSegment.Wireframe.ShapeWireframe( | ||
id = resolveId(semanticsNode, 1), | ||
x = xPos.toLong(), | ||
y = yPos.toLong(), | ||
width = checkmarkWidth.toLong(), | ||
height = checkmarkHeight.toLong(), | ||
shapeStyle = MobileSegment.ShapeStyle( | ||
backgroundColor = strokeColor, | ||
opacity = 1f, | ||
cornerRadius = CHECKBOX_CORNER_RADIUS | ||
) | ||
) | ||
return listOf(background, foreground) | ||
} | ||
|
||
private fun resolveUncheckedCheckbox( | ||
semanticsNode: SemanticsNode, | ||
globalBounds: GlobalBounds, | ||
backgroundColor: String? = DEFAULT_COLOR_WHITE | ||
): MobileSegment.Wireframe { | ||
val borderColor = | ||
semanticsUtils.resolveBorderColor(semanticsNode) | ||
?.let { | ||
convertColor(it) | ||
} ?: DEFAULT_COLOR_BLACK | ||
|
||
return MobileSegment.Wireframe.ShapeWireframe( | ||
id = resolveId(semanticsNode, 0), | ||
x = globalBounds.x, | ||
y = globalBounds.y, | ||
width = globalBounds.width, | ||
height = globalBounds.height, | ||
shapeStyle = MobileSegment.ShapeStyle( | ||
backgroundColor = backgroundColor, | ||
opacity = 1f, | ||
cornerRadius = CHECKBOX_CORNER_RADIUS | ||
), | ||
border = MobileSegment.ShapeBorder( | ||
color = borderColor, | ||
width = BOX_BORDER_WIDTH | ||
) | ||
) | ||
} | ||
|
||
private fun convertPathToBitmap(semanticsNode: SemanticsNode, checkPath: Path): Bitmap? { | ||
val scaledPath = scalePathToBitmapSize(checkPath) | ||
|
||
// Create a Bitmap | ||
val bitmap = bitmapWrapper.createBitmap(CHECKBOX_SIZE, CHECKBOX_SIZE, Bitmap.Config.ARGB_8888) | ||
?: return null | ||
|
||
val canvas = canvasWrapper.createCanvas(bitmap) | ||
|
||
val fillColor = pathUtils.convertRgbaToArgb( | ||
resolveCheckboxFillColor(semanticsNode) | ||
) | ||
|
||
pathUtils.parseColorSafe(fillColor)?.let { | ||
canvas?.drawColor(it) | ||
} | ||
|
||
val checkMarkColor = semanticsUtils.resolveCheckmarkColor( | ||
semanticsNode | ||
)?.let { rawColor -> | ||
convertColor(rawColor)?.let { | ||
// Flip the alpha value position | ||
pathUtils.convertRgbaToArgb(it) | ||
} | ||
} ?: DEFAULT_COLOR_WHITE | ||
|
||
val parsedCheckmarkColor = pathUtils.parseColorSafe(checkMarkColor) | ||
?: return null | ||
|
||
val paint = Paint().apply { | ||
color = parsedCheckmarkColor | ||
style = Paint.Style.STROKE | ||
strokeWidth = STROKE_WIDTH | ||
isAntiAlias = true | ||
} | ||
|
||
// Draw the Path onto the Canvas | ||
pathUtils.asAndroidPathSafe(scaledPath)?.let { | ||
pathUtils.drawPathSafe(canvas, it, paint) | ||
} ?: return null | ||
|
||
return bitmap | ||
} | ||
|
||
private fun resolveCheckboxFillColor(semanticsNode: SemanticsNode): String = | ||
semanticsUtils.resolveCheckboxFillColor(semanticsNode) | ||
?.let { rawColor -> | ||
convertColor(rawColor) | ||
} | ||
?: DEFAULT_COLOR_WHITE | ||
|
||
private fun scalePathToBitmapSize(path: Path): Path { | ||
val originalBounds = path.getBounds() | ||
|
||
val scaleX = CHECKBOX_SIZE / originalBounds.width | ||
val scaleY = CHECKBOX_SIZE / originalBounds.height | ||
val scaleFactor = minOf(scaleX, scaleY) | ||
val scaledWidth = originalBounds.width * scaleFactor | ||
val scaledHeight = originalBounds.height * scaleFactor | ||
val canvasCenterX = CHECKBOX_SIZE / 2 | ||
val canvasCenterY = CHECKBOX_SIZE / 2 | ||
val translateX = canvasCenterX - (scaledWidth / 2) | ||
val translateY = canvasCenterY - (scaledHeight / 2) | ||
|
||
val matrix = Matrix().apply { | ||
scale(scaleFactor, scaleFactor) | ||
translate( | ||
translateX / scaleFactor - originalBounds.left, | ||
translateY / scaleFactor - originalBounds.top | ||
) | ||
} | ||
|
||
path.transform(matrix) | ||
|
||
return path | ||
} | ||
|
||
internal companion object { | ||
internal enum class CheckmarkFieldType { | ||
FILL_COLOR, | ||
CHECKMARK_COLOR, | ||
BORDER_COLOR | ||
} | ||
|
||
internal const val DEFAULT_COLOR_BLACK = "#000000FF" | ||
internal const val DEFAULT_COLOR_WHITE = "#FFFFFFFF" | ||
|
||
internal const val STROKE_SIZE_FACTOR = 0.6 | ||
|
||
// values from Checkbox sourcecode | ||
internal const val BOX_BORDER_WIDTH = 2L | ||
internal const val STROKE_WIDTH = 2f | ||
internal const val CHECKBOX_SIZE = 20 // dp | ||
internal const val CHECKBOX_CORNER_RADIUS = 2f | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.