Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6e011cf4 authored by Ahmed Mehfooz's avatar Ahmed Mehfooz Committed by Android (Google) Code Review
Browse files

Merge "[SB] Fix performance regression in UnifiedBattery" into main

parents 13a0f53a eecff986
Loading
Loading
Loading
Loading
+18 −18
Original line number Diff line number Diff line
@@ -388,13 +388,16 @@ fun BatteryBody(
    modifier: Modifier = Modifier,
    contentDescription: String = "",
) {
    val colors = colorsProvider()
    val glyphs = glyphsProvider()
    val frameColor = rememberBatteryFrameColor(isFullProvider(), glyphs.isNotEmpty(), colors)

    Canvas(modifier = modifier, contentDescription = contentDescription) {
        val rtl = layoutDirection == LayoutDirection.Rtl

        val level = levelProvider()
        val colors = colorsProvider()
        val glyphs = glyphsProvider()
        val isFull = isFullProvider()
        val frameColor =
            getBatteryFrameColor(isFull = isFull, hasGlyphs = glyphs.isNotEmpty(), colors = colors)

        val totalGlyphWidth =
            if (glyphs.isEmpty()) {
                0f
@@ -407,7 +410,7 @@ fun BatteryBody(

        val s = pathSpec.scaleTo(size.width, size.height)
        scale(scale = s, pivot = Offset.Zero) {
            if (isFullProvider()) {
            if (isFull) {
                // If full, the frameColor is already the fill color.
                drawPath(pathSpec.path, frameColor)
            } else {
@@ -464,13 +467,13 @@ fun BatteryCap(
) {
    val pathSpec = BatteryFrame.capPathSpec
    val rtl = LocalLayoutDirection.current == LayoutDirection.Rtl

    Canvas(modifier = modifier.scale(scaleX = if (rtl) -1f else 1f, scaleY = 1f)) {
        val colors = colorsProvider()
        val isFull = isFullProvider()
        val hasGlyphs = glyphsProvider().isNotEmpty()
        val color = getBatteryFrameColor(isFull = isFull, hasGlyphs = hasGlyphs, colors = colors)

    val color = rememberBatteryFrameColor(isFull, hasGlyphs, colors)

    Canvas(modifier = modifier.scale(scaleX = if (rtl) -1f else 1f, scaleY = 1f)) {
        val s = pathSpec.scaleTo(size.width, size.height)
        scale(s, pivot = Offset.Zero) { drawPath(pathSpec.path, color = color) }
    }
@@ -500,17 +503,14 @@ fun BatteryAttribution(
}

/** Determines the correct color for the battery frame (body and cap) based on its state. */
@Composable
private fun rememberBatteryFrameColor(
private fun getBatteryFrameColor(
    isFull: Boolean,
    hasGlyphs: Boolean,
    colors: BatteryColors,
): Color {
    return remember(isFull, hasGlyphs, colors) {
        when {
    return when {
        isFull -> colors.fill
        hasGlyphs -> colors.backgroundWithGlyph
        else -> colors.backgroundOnly
    }
}
}