-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a87b2e8
commit 68aa3f2
Showing
4 changed files
with
231 additions
and
249 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
...roid/src/main/kotlin/com/datadog/reactnative/sessionreplay/ReactTextPropertiesResolver.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,133 @@ | ||
package com.datadog.reactnative.sessionreplay | ||
|
||
import android.view.Gravity | ||
import android.widget.TextView | ||
import com.datadog.android.sessionreplay.model.MobileSegment | ||
import com.datadog.reactnative.sessionreplay.extensions.convertToDensityNormalized | ||
import com.datadog.reactnative.sessionreplay.extensions.densityNormalized | ||
import com.facebook.react.views.text.ReactTextShadowNode | ||
|
||
internal class ReactTextPropertiesResolver( | ||
private val reactTextShadowNodeUtils: ReactTextShadowNodeUtils | ||
) { | ||
internal fun resolveShadowProperties( | ||
view: TextView, | ||
pixelDensity: Float, | ||
shapeStyle: MobileSegment.ShapeStyle?, | ||
border: MobileSegment.ShapeBorder?, | ||
textWireframe: MobileSegment.Wireframe.TextWireframe | ||
): MobileSegment.Wireframe { | ||
|
||
val shadowNode = reactTextShadowNodeUtils.getShadowNode(view.id) as? ReactTextShadowNode | ||
|
||
val textStyle = resolveTextStyle(view, pixelDensity, shadowNode) | ||
val textPosition = resolveTextPosition(view, pixelDensity, shadowNode) | ||
|
||
return textWireframe.copy( | ||
shapeStyle = shapeStyle, | ||
border = border, | ||
textStyle = textStyle, | ||
textPosition = textPosition | ||
) | ||
} | ||
|
||
private fun resolveTextPosition( | ||
view: TextView, | ||
pixelsDensity: Float, | ||
shadowNode: ReactTextShadowNode? | ||
): MobileSegment.TextPosition { | ||
return MobileSegment.TextPosition( | ||
resolvePadding(view, pixelsDensity), | ||
resolveAlignment(view, shadowNode) | ||
) | ||
} | ||
|
||
private fun resolveTextStyle( | ||
view: TextView, | ||
pixelsDensity: Float, | ||
shadowNode: ReactTextShadowNode? | ||
): MobileSegment.TextStyle { | ||
return MobileSegment.TextStyle( | ||
family = reactTextShadowNodeUtils.getFontFamily(view, shadowNode), | ||
size = reactTextShadowNodeUtils.getFontSize(view, shadowNode).convertToDensityNormalized(pixelsDensity), | ||
color = resolveTextColor(view, shadowNode) | ||
) | ||
} | ||
|
||
private fun resolvePadding(textView: TextView, pixelsDensity: Float): MobileSegment.Padding { | ||
return MobileSegment.Padding( | ||
top = textView.totalPaddingTop.densityNormalized(pixelsDensity).toLong(), | ||
bottom = textView.totalPaddingBottom.densityNormalized(pixelsDensity).toLong(), | ||
left = textView.totalPaddingStart.densityNormalized(pixelsDensity).toLong(), | ||
right = textView.totalPaddingEnd.densityNormalized(pixelsDensity).toLong() | ||
) | ||
} | ||
|
||
private fun resolveAlignment( | ||
textView: TextView, | ||
shadowNode: ReactTextShadowNode? | ||
): MobileSegment.Alignment { | ||
return when (textView.textAlignment) { | ||
TextView.TEXT_ALIGNMENT_GRAVITY -> resolveShadowAlignment(textView, shadowNode = shadowNode) | ||
TextView.TEXT_ALIGNMENT_CENTER -> MobileSegment.Alignment( | ||
horizontal = MobileSegment.Horizontal.CENTER, | ||
vertical = MobileSegment.Vertical.CENTER | ||
) | ||
|
||
TextView.TEXT_ALIGNMENT_TEXT_END, | ||
TextView.TEXT_ALIGNMENT_VIEW_END -> MobileSegment.Alignment( | ||
horizontal = MobileSegment.Horizontal.RIGHT, | ||
vertical = MobileSegment.Vertical.CENTER | ||
) | ||
|
||
TextView.TEXT_ALIGNMENT_TEXT_START, | ||
TextView.TEXT_ALIGNMENT_VIEW_START -> MobileSegment.Alignment( | ||
horizontal = MobileSegment.Horizontal.LEFT, | ||
vertical = MobileSegment.Vertical.CENTER | ||
) | ||
|
||
else -> MobileSegment.Alignment( | ||
horizontal = MobileSegment.Horizontal.LEFT, | ||
vertical = MobileSegment.Vertical.CENTER | ||
) | ||
} | ||
} | ||
|
||
private fun resolveShadowAlignment( | ||
view: TextView, | ||
shadowNode: ReactTextShadowNode? | ||
): MobileSegment.Alignment { | ||
|
||
val shadowGravity = reactTextShadowNodeUtils.getGravity(view, shadowNode) | ||
|
||
|
||
val horizontalAlignment = when (shadowGravity.and(Gravity.HORIZONTAL_GRAVITY_MASK)) { | ||
Gravity.START, | ||
Gravity.LEFT -> MobileSegment.Horizontal.LEFT | ||
|
||
Gravity.END, | ||
Gravity.RIGHT -> MobileSegment.Horizontal.RIGHT | ||
|
||
Gravity.CENTER, | ||
Gravity.CENTER_HORIZONTAL -> MobileSegment.Horizontal.CENTER | ||
|
||
else -> MobileSegment.Horizontal.LEFT | ||
} | ||
val verticalAlignment = when (shadowGravity.and(Gravity.VERTICAL_GRAVITY_MASK)) { | ||
Gravity.TOP -> MobileSegment.Vertical.TOP | ||
Gravity.BOTTOM -> MobileSegment.Vertical.BOTTOM | ||
|
||
Gravity.CENTER_VERTICAL, | ||
Gravity.CENTER -> MobileSegment.Vertical.CENTER | ||
|
||
else -> MobileSegment.Vertical.CENTER | ||
} | ||
|
||
return MobileSegment.Alignment(horizontalAlignment, verticalAlignment) | ||
} | ||
|
||
private fun resolveTextColor(view: TextView, shadowNode: ReactTextShadowNode?): String { | ||
return formatAsRgba(reactTextShadowNodeUtils.getColor(view, shadowNode)) | ||
} | ||
} | ||
|
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.