Valentin Gusselnikov
09/21/2022, 3:44 PMModifier.progressSemantics
causes recompositions.
I've a custom ProgressIndicator
, and if I use Modifier.progressSemantics
Android Studio "Layout inspector" shows thousand recompositions.
Is it a bug of "Layout inspector", or it is normal?Valentin Gusselnikov
09/21/2022, 3:46 PMModifier.progressSemantics
tad
09/21/2022, 7:00 PMSegmentedProgressIndicator
to know what the ultimate cause is. But you might try replacing that with an indeterminate CircularProgressIndicator
just to see if that also recomposes every frame.Albert Chang
09/22/2022, 1:27 AMModifier.progressSemantics
reads the value in the current recomposition scope. This usually shouldn't be a problem because when you draw a progress bar you likely need it to be recomposed anyway. If you want to reduce recomposition, you can use Modifier.semantics(mergeDescendants = true) { progressBarRangeInfo = ... }
(see the source of Modifier.progressSemantics
).Valentin Gusselnikov
09/22/2022, 8:27 AMprogress: Float
with
progressProvider: () -> Float
Valentin Gusselnikov
09/22/2022, 8:30 AM@Composable
fun SegmentedProgressIndicator(
progressProvider: () -> Float,
numberOfSegments: Int,
modifier: Modifier = Modifier,
color: Color = Color.White,
backgroundColor: Color = color.copy(alpha = BackgroundOpacity),
progressHeight: Dp = ProgressHeight,
segmentGap: Dp = SegmentGap
) {
val gap: Float
val barHeight: Float
with(LocalDensity.current) {
gap = segmentGap.toPx()
barHeight = progressHeight.toPx()
}
Canvas(
modifier
.semantics(mergeDescendants = true) {
progressBarRangeInfo = ProgressBarRangeInfo(progressProvider().coerceIn(0f..1f), 0f..1f, 0)
}
.height(progressHeight)
) {
drawSegments(1f, backgroundColor, barHeight, numberOfSegments, gap)
drawSegments(progressProvider(), color, barHeight, numberOfSegments, gap)
}
}