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

Commit 5787d4d5 authored by Jordan Demeulenaere's avatar Jordan Demeulenaere
Browse files

Add Modifier.noResizeDuringTransitions()

This CL adds a new Modifier to force a layout node to always be the same
size as when there is no transition. This can for instance be used to
make sure that a scrollable container/scene does not resize and thus
that its scrolling state does not change during a transition. This will
for instance be used by the QS pager in the demo app, so that the QS =>
Shade transition is smoother.

Bug: 308961608
Test: SizeTest
Flag: N/A
Change-Id: I98e65d0c1bffb47be04309b743587e957d7ca188
parent ba822b99
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import androidx.compose.ui.layout.intermediateLayout
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.zIndex
import com.android.compose.animation.scene.modifiers.noResizeDuringTransitions

/** A scene in a [SceneTransitionLayout]. */
@Stable
@@ -152,4 +153,8 @@ private class SceneScopeImpl(
        bounds: ElementKey,
        shape: Shape
    ): Modifier = punchHole(layoutImpl, element, bounds, shape)

    override fun Modifier.noResizeDuringTransitions(): Modifier {
        return noResizeDuringTransitions(layoutState = layoutImpl.state)
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -205,6 +205,12 @@ interface SceneScope {
     * the result.
     */
    fun Modifier.punchHole(element: ElementKey, bounds: ElementKey, shape: Shape): Modifier

    /**
     * Don't resize during transitions. This can for instance be used to make sure that scrollable
     * lists keep a constant size during transitions even if its elements are growing/shrinking.
     */
    fun Modifier.noResizeDuringTransitions(): Modifier
}

// TODO(b/291053742): Add animateSharedValueAsState(targetValue) without any ValueKey and ElementKey
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.compose.animation.scene.modifiers

import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.intermediateLayout
import androidx.compose.ui.unit.Constraints
import com.android.compose.animation.scene.SceneTransitionLayoutState

@OptIn(ExperimentalComposeUiApi::class)
internal fun Modifier.noResizeDuringTransitions(layoutState: SceneTransitionLayoutState): Modifier {
    return intermediateLayout { measurable, constraints ->
        if (layoutState.currentTransition == null) {
            return@intermediateLayout measurable.measure(constraints).run {
                layout(width, height) { place(0, 0) }
            }
        }

        // Make sure that this layout node has the same size than when we are at rest.
        val sizeAtRest = lookaheadSize
        measurable.measure(Constraints.fixed(sizeAtRest.width, sizeAtRest.height)).run {
            layout(width, height) { place(0, 0) }
        }
    }
}
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.compose.animation.scene.modifiers

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.unit.dp
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.compose.animation.scene.TestElements
import com.android.compose.animation.scene.element
import com.android.compose.animation.scene.testTransition
import com.android.compose.test.assertSizeIsEqualTo
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class SizeTest {
    @get:Rule val rule = createComposeRule()

    @Test
    fun noResizeDuringTransitions() {
        // The tag for the parent of the shared Foo element.
        val parentTag = "parent"

        rule.testTransition(
            fromSceneContent = { Box(Modifier.element(TestElements.Foo).size(100.dp)) },
            toSceneContent = {
                // Don't resize the parent of Foo during transitions so that it's always the same
                // size as when there is no transition (200dp).
                Box(Modifier.noResizeDuringTransitions().testTag(parentTag)) {
                    Box(Modifier.element(TestElements.Foo).size(200.dp))
                }
            },
            transition = { spec = tween(durationMillis = 4 * 16, easing = LinearEasing) },
        ) {
            at(16) { rule.onNodeWithTag(parentTag).assertSizeIsEqualTo(200.dp, 200.dp) }
            at(32) { rule.onNodeWithTag(parentTag).assertSizeIsEqualTo(200.dp, 200.dp) }
            at(48) { rule.onNodeWithTag(parentTag).assertSizeIsEqualTo(200.dp, 200.dp) }
            after { rule.onNodeWithTag(parentTag).assertSizeIsEqualTo(200.dp, 200.dp) }
        }
    }
}