-
Notifications
You must be signed in to change notification settings - Fork 70
RUM-6199: Add Semantics mapper for Switch #2471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
162 changes: 162 additions & 0 deletions
162
...dog/android/sessionreplay/compose/internal/mappers/semantics/SwitchSemanticsNodeMapper.kt
This file contains hidden or 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,162 @@ | ||
/* | ||
* 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 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.sessionreplay.compose.internal.mappers.semantics | ||
|
||
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.sessionreplay.TextAndInputPrivacy | ||
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.SemanticsUtils | ||
import com.datadog.android.sessionreplay.model.MobileSegment | ||
import com.datadog.android.sessionreplay.utils.AsyncJobStatusCallback | ||
import com.datadog.android.sessionreplay.utils.ColorStringFormatter | ||
import com.datadog.android.sessionreplay.utils.GlobalBounds | ||
|
||
internal class SwitchSemanticsNodeMapper( | ||
colorStringFormatter: ColorStringFormatter, | ||
semanticsUtils: SemanticsUtils = SemanticsUtils() | ||
) : AbstractSemanticsNodeMapper(colorStringFormatter, semanticsUtils) { | ||
override fun map( | ||
semanticsNode: SemanticsNode, | ||
parentContext: UiContext, | ||
asyncJobStatusCallback: AsyncJobStatusCallback | ||
): SemanticsWireframe { | ||
val isSwitchOn = isSwitchOn(semanticsNode) | ||
val globalBounds = resolveBounds(semanticsNode) | ||
|
||
val switchWireframes = if (isSwitchMasked(parentContext)) { | ||
listOf( | ||
resolveMaskedWireframes( | ||
semanticsNode = semanticsNode, | ||
globalBounds = globalBounds, | ||
wireframeIndex = 0 | ||
) | ||
) | ||
} else { | ||
val trackWireframe = createTrackWireframe( | ||
semanticsNode = semanticsNode, | ||
globalBounds = globalBounds, | ||
wireframeIndex = 0, | ||
isSwitchOn = isSwitchOn | ||
) | ||
|
||
val thumbWireframe = createThumbWireframe( | ||
semanticsNode = semanticsNode, | ||
globalBounds = globalBounds, | ||
wireframeIndex = 1, | ||
isSwitchOn = isSwitchOn | ||
) | ||
|
||
listOfNotNull(trackWireframe, thumbWireframe) | ||
} | ||
|
||
return SemanticsWireframe( | ||
uiContext = null, | ||
wireframes = switchWireframes | ||
) | ||
} | ||
|
||
private fun createTrackWireframe( | ||
semanticsNode: SemanticsNode, | ||
globalBounds: GlobalBounds, | ||
wireframeIndex: Int, | ||
isSwitchOn: Boolean | ||
): MobileSegment.Wireframe { | ||
val trackColor = if (isSwitchOn) { | ||
DEFAULT_COLOR_BLACK | ||
} else { | ||
DEFAULT_COLOR_WHITE | ||
} | ||
|
||
@Suppress("MagicNumber") | ||
return MobileSegment.Wireframe.ShapeWireframe( | ||
resolveId(semanticsNode, wireframeIndex), | ||
x = globalBounds.x, | ||
y = globalBounds.y + (globalBounds.height / 4), | ||
width = TRACK_WIDTH_DP, | ||
height = THUMB_DIAMETER_DP.toLong() / 2, | ||
shapeStyle = MobileSegment.ShapeStyle( | ||
cornerRadius = CORNER_RADIUS_DP, | ||
backgroundColor = trackColor | ||
), | ||
border = MobileSegment.ShapeBorder( | ||
color = DEFAULT_COLOR_BLACK, | ||
width = BORDER_WIDTH_DP | ||
) | ||
) | ||
} | ||
|
||
private fun createThumbWireframe( | ||
semanticsNode: SemanticsNode, | ||
globalBounds: GlobalBounds, | ||
wireframeIndex: Int, | ||
isSwitchOn: Boolean | ||
): MobileSegment.Wireframe { | ||
val xPosition = if (!isSwitchOn) { | ||
globalBounds.x | ||
} else { | ||
globalBounds.x + globalBounds.width - THUMB_DIAMETER_DP | ||
} | ||
|
||
@Suppress("MagicNumber") | ||
val yPosition = globalBounds.y + (globalBounds.height / 4) - (THUMB_DIAMETER_DP / 4) | ||
|
||
val thumbColor = if (!isSwitchOn) { | ||
DEFAULT_COLOR_WHITE | ||
} else { | ||
DEFAULT_COLOR_BLACK | ||
} | ||
|
||
return MobileSegment.Wireframe.ShapeWireframe( | ||
resolveId(semanticsNode, wireframeIndex), | ||
x = xPosition, | ||
y = yPosition, | ||
width = THUMB_DIAMETER_DP.toLong(), | ||
height = THUMB_DIAMETER_DP.toLong(), | ||
shapeStyle = MobileSegment.ShapeStyle( | ||
cornerRadius = CORNER_RADIUS_DP, | ||
backgroundColor = thumbColor | ||
), | ||
border = MobileSegment.ShapeBorder( | ||
color = DEFAULT_COLOR_BLACK, | ||
width = BORDER_WIDTH_DP | ||
) | ||
) | ||
} | ||
|
||
private fun isSwitchOn(semanticsNode: SemanticsNode): Boolean = | ||
semanticsNode.config.getOrNull(SemanticsProperties.ToggleableState) == ToggleableState.On | ||
|
||
private fun isSwitchMasked(parentContext: UiContext): Boolean = | ||
parentContext.textAndInputPrivacy != TextAndInputPrivacy.MASK_SENSITIVE_INPUTS | ||
|
||
private fun resolveMaskedWireframes( | ||
semanticsNode: SemanticsNode, | ||
globalBounds: GlobalBounds, | ||
wireframeIndex: Int | ||
): MobileSegment.Wireframe { | ||
// TODO RUM-5118: Decide how to display masked, currently use empty track, | ||
return createTrackWireframe( | ||
semanticsNode = semanticsNode, | ||
globalBounds = globalBounds, | ||
wireframeIndex = wireframeIndex, | ||
isSwitchOn = false | ||
) | ||
} | ||
|
||
internal companion object { | ||
const val TRACK_WIDTH_DP = 34L | ||
const val CORNER_RADIUS_DP = 20 | ||
const val THUMB_DIAMETER_DP = 20 | ||
const val BORDER_WIDTH_DP = 1L | ||
const val DEFAULT_COLOR_BLACK = "#000000" | ||
const val DEFAULT_COLOR_WHITE = "#FFFFFF" | ||
} | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.