From e7c597fe437feaa1f9bdcd499bfebb6c66d1d353 Mon Sep 17 00:00:00 2001 From: Arkadii Ivanov Date: Sat, 7 Sep 2024 12:50:28 +0100 Subject: [PATCH] Disable input during predictive back animation --- .../stack/animation/AbstractStackAnimation.kt | 18 ++--------------- .../predictiveback/PredictiveBackAnimation.kt | 5 +++++ .../compose/utils/InputConsumingOverlay.kt | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/utils/InputConsumingOverlay.kt diff --git a/extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/stack/animation/AbstractStackAnimation.kt b/extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/stack/animation/AbstractStackAnimation.kt index bba80774b..5b079530b 100644 --- a/extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/stack/animation/AbstractStackAnimation.kt +++ b/extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/stack/animation/AbstractStackAnimation.kt @@ -8,9 +8,9 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier -import androidx.compose.ui.input.pointer.pointerInput import com.arkivanov.decompose.Child import com.arkivanov.decompose.ExperimentalDecomposeApi +import com.arkivanov.decompose.extensions.compose.utils.InputConsumingOverlay import com.arkivanov.decompose.router.stack.ChildStack @OptIn(ExperimentalDecomposeApi::class) @@ -56,25 +56,11 @@ internal abstract class AbstractStackAnimation( // A workaround until https://issuetracker.google.com/issues/214231672. // Normally only the exiting child should be disabled. if (disableInputDuringAnimation && (items.size > 1)) { - Overlay(modifier = Modifier.matchParentSize()) + InputConsumingOverlay(modifier = Modifier.matchParentSize()) } } } - @Composable - private fun Overlay(modifier: Modifier) { - Box( - modifier = modifier.pointerInput(Unit) { - awaitPointerEventScope { - while (true) { - val event = awaitPointerEvent() - event.changes.forEach { it.consume() } - } - } - } - ) - } - private fun getAnimationItems(newStack: ChildStack, oldStack: ChildStack?): Map> = when { oldStack == null -> diff --git a/extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/stack/animation/predictiveback/PredictiveBackAnimation.kt b/extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/stack/animation/predictiveback/PredictiveBackAnimation.kt index 90874b735..637719bd7 100644 --- a/extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/stack/animation/predictiveback/PredictiveBackAnimation.kt +++ b/extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/stack/animation/predictiveback/PredictiveBackAnimation.kt @@ -18,6 +18,7 @@ import com.arkivanov.decompose.Ref import com.arkivanov.decompose.extensions.compose.stack.animation.LocalStackAnimationProvider import com.arkivanov.decompose.extensions.compose.stack.animation.StackAnimation import com.arkivanov.decompose.extensions.compose.stack.animation.emptyStackAnimation +import com.arkivanov.decompose.extensions.compose.utils.InputConsumingOverlay import com.arkivanov.decompose.router.stack.ChildStack import com.arkivanov.essenty.backhandler.BackCallback import com.arkivanov.essenty.backhandler.BackEvent @@ -95,6 +96,10 @@ private class PredictiveBackAnimation( ) } } + + if (handler.items.size > 1) { + InputConsumingOverlay(modifier = Modifier.matchParentSize()) + } } if (stack.backStack.isNotEmpty()) { diff --git a/extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/utils/InputConsumingOverlay.kt b/extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/utils/InputConsumingOverlay.kt new file mode 100644 index 000000000..badefc43c --- /dev/null +++ b/extensions-compose/src/commonMain/kotlin/com/arkivanov/decompose/extensions/compose/utils/InputConsumingOverlay.kt @@ -0,0 +1,20 @@ +package com.arkivanov.decompose.extensions.compose.utils + +import androidx.compose.foundation.layout.Box +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.input.pointer.pointerInput + +@Composable +internal fun InputConsumingOverlay(modifier: Modifier) { + Box( + modifier = modifier.pointerInput(Unit) { + awaitPointerEventScope { + while (true) { + val event = awaitPointerEvent() + event.changes.forEach { it.consume() } + } + } + } + ) +}