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

Commit d2797724 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Use layoutId instead of checking measurables size" into main

parents b61bc8b0 40759710
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.common.ui.compose

import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.layoutId

inline fun <reified LayoutIdType> List<Measurable>.byLayoutId(): Map<LayoutIdType, Measurable> {
    var index = -1
    return associateBy { measurable ->
        index++
        val id = measurable.layoutId
        checkNotNull(id) { "Measurable at index $index doesn't have a layoutId modifier!" }
        try {
            id as LayoutIdType
        } catch (e: ClassCastException) {
            throw IllegalStateException(
                "Measurable at index $index has a layoutId of type ${id.javaClass.simpleName}, " +
                    "but LayoutIdType ${LayoutIdType::class.java.simpleName} was expected.",
                e,
            )
        }
    }
}

fun <LayoutIdType> List<Measurable>.singleton(layoutId: LayoutIdType): Measurable {
    check(size == 1) {
        "Only one Measurable expected but there are actually $size measurables. Use byLayoutId instead"
    }
    return first { it.layoutId == layoutId }
}
+20 −8
Original line number Diff line number Diff line
@@ -109,6 +109,7 @@ import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.layout
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.node.Ref
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLayoutDirection
@@ -144,7 +145,9 @@ import com.android.systemui.common.shared.model.Icon
import com.android.systemui.common.shared.model.asImageBitmap
import com.android.systemui.common.ui.compose.Icon
import com.android.systemui.common.ui.compose.PagerDots
import com.android.systemui.common.ui.compose.byLayoutId
import com.android.systemui.common.ui.compose.load
import com.android.systemui.common.ui.compose.singleton
import com.android.systemui.communal.ui.compose.extensions.detectLongPressGesture
import com.android.systemui.compose.modifiers.sysuiResTag
import com.android.systemui.lifecycle.rememberViewModel
@@ -458,7 +461,7 @@ private fun ContentScope.CardForeground(
                fillHeight = fillHeight,
                colorScheme = colorScheme,
                modifier =
                    Modifier.graphicsLayer {
                    Modifier.layoutId(Media.LayoutId.CardForeground).graphicsLayer {
                        compositingStrategy = CompositingStrategy.ModulateAlpha
                        alpha = 1f - gutsAlphaAnimatable.value
                    },
@@ -468,7 +471,7 @@ private fun ContentScope.CardForeground(
                viewModel = viewModel.guts,
                colorScheme = colorScheme,
                modifier =
                    Modifier.graphicsLayer {
                    Modifier.layoutId(Media.LayoutId.CardGuts).graphicsLayer {
                        compositingStrategy = CompositingStrategy.ModulateAlpha
                        alpha = gutsAlphaAnimatable.value
                    },
@@ -476,12 +479,13 @@ private fun ContentScope.CardForeground(
        },
        modifier = modifier,
    ) { measurables, constraints ->
        check(measurables.size == 2)
        val contentPlaceable = measurables[0].measure(constraints)
        val measurableByLayoutId = measurables.byLayoutId<Media.LayoutId>()
        val contentPlaceable =
            measurableByLayoutId[Media.LayoutId.CardForeground]!!.measure(constraints)
        // Guts should always have the exact dimensions as the content, even if we don't show the
        // content.
        val gutsPlaceable =
            measurables[1].measure(
            measurableByLayoutId[Media.LayoutId.CardGuts]!!.measure(
                Constraints.fixed(contentPlaceable.width, contentPlaceable.height)
            )

@@ -1412,7 +1416,8 @@ private fun RevealedContent(
            Icon(
                icon = viewModel.icon,
                modifier =
                    Modifier.size(48.dp)
                    Modifier.layoutId(Media.LayoutId.CardRevealedContent)
                        .size(48.dp)
                        .padding(12.dp)
                        .graphicsLayer {
                            alpha = abs(revealAmount()).fastCoerceIn(0f, 1f)
@@ -1423,8 +1428,9 @@ private fun RevealedContent(
        },
        modifier = modifier,
    ) { measurables, constraints ->
        check(measurables.size == 1)
        val placeable = measurables[0].measure(constraints)
        val placeable =
            measurables.singleton(Media.LayoutId.CardRevealedContent).measure(constraints)

        val totalWidth =
            min(horizontalPadding.roundToPx() * 2 + placeable.measuredWidth, constraints.maxWidth)

@@ -1547,6 +1553,12 @@ object Media {
            return ElementKey(debugName = name, identity = name)
        }
    }

    enum class LayoutId {
        CardForeground,
        CardGuts,
        CardRevealedContent,
    }
}

private fun MediaPresentationStyle.toScene(): SceneKey {