Yokan / rebuild
Course 1 8624561

NavigationStack.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/navigation/NavigationStack.kt

implementation139 lines
1package eu.friedcutlet.yokan.ui.navigation23import androidx.compose.animation.AnimatedContent4import androidx.compose.animation.EnterTransition5import androidx.compose.animation.ExitTransition6import androidx.compose.animation.core.CubicBezierEasing7import androidx.compose.animation.core.tween8import androidx.compose.animation.core.updateTransition9import androidx.compose.animation.fadeIn10import androidx.compose.animation.fadeOut11import androidx.compose.animation.scaleIn12import androidx.compose.animation.scaleOut13import androidx.compose.animation.slideInHorizontally14import androidx.compose.animation.slideOutHorizontally15import androidx.compose.animation.togetherWith16import androidx.compose.foundation.background17import androidx.compose.foundation.layout.Box18import androidx.compose.foundation.layout.fillMaxSize19import androidx.compose.runtime.Composable20import androidx.compose.runtime.DisposableEffect21import androidx.compose.runtime.LaunchedEffect22import androidx.compose.runtime.getValue23import androidx.compose.runtime.movableContentOf24import androidx.compose.runtime.mutableFloatStateOf25import androidx.compose.runtime.remember26import androidx.compose.runtime.rememberCoroutineScope27import androidx.compose.runtime.rememberUpdatedState28import androidx.compose.runtime.setValue29import androidx.compose.ui.Modifier30import androidx.compose.ui.draw.clipToBounds31import androidx.compose.ui.graphics.graphicsLayer32import androidx.compose.ui.layout.onSizeChanged33import androidx.compose.ui.platform.LocalDensity34import androidx.compose.ui.unit.IntOffset35import androidx.compose.ui.unit.dp36import eu.friedcutlet.yokan.ui.theme.LocalYokanScaffoldColor3738/** Page motion is confined to graphics layers; content changes only at gesture/navigation boundaries. */39@Composable40internal fun NavigationStack(41    backStack: List<NavEntry>,42    direction: Int,43    cupertino: Boolean,44    playerMinimized: Boolean,45    animationsEnabled: Boolean,46    onCompleteBack: (NavEntry) -> Unit,47    modifier: Modifier = Modifier,48    rootContent: @Composable () -> Unit,49    entryContent: @Composable (NavEntry) -> Unit,50) {51    val completeBack by rememberUpdatedState(onCompleteBack)52    val edgeWidthPx = with(LocalDensity.current) { 24.dp.toPx() }53    val settleScope = rememberCoroutineScope()54    var routeWidthPx by remember { mutableFloatStateOf(1f) }55    val top = backStack.lastOrNull()56    val previous = backStack.getOrNull(backStack.size - 2)57    val transition = updateTransition(top, label = "Navigation")58    val gesture = remember(top) { InteractiveBackGesture(settleScope) }59    DisposableEffect(gesture) { onDispose { gesture.dispose() } }60    val interactiveBackModifier = if (cupertino && animationsEnabled && top != null && !playerMinimized) {61        Modifier.interactiveBackGesture(62            gesture = gesture,63            edgeWidth = edgeWidthPx,64            canStart = { !transition.isRunning && transition.currentState === transition.targetState },65            onComplete = { completeBack(top) },66        )67    } else Modifier68    val currentRoot by rememberUpdatedState(rootContent)69    val currentEntry by rememberUpdatedState(entryContent)70    val movableRoot = remember { movableContentOf { currentRoot() } }71    // Move a preview into its final slot on pop; never mount the same saveable entry twice.72    val entryContents = remember { mutableMapOf<NavEntry, @Composable () -> Unit>() }73    fun contentFor(entry: NavEntry?): @Composable () -> Unit = if (entry == null) movableRoot else {74        entryContents.getOrPut(entry) { movableContentOf { currentEntry(entry) } }75    }76    LaunchedEffect(backStack, transition.currentState, transition.targetState) {77        entryContents.keys.retainAll(backStack.toSet() + listOfNotNull(transition.currentState, transition.targetState))78    }79    val pageColor = LocalYokanScaffoldColor.current80    Box(81        modifier = modifier82            .fillMaxSize()83            .clipToBounds()84            .onSizeChanged { routeWidthPx = it.width.toFloat().coerceAtLeast(1f) }85            .then(interactiveBackModifier),86    ) {87        if ((!cupertino && (top == null || playerMinimized)) ||88            (cupertino && playerMinimized && top != null)89        ) movableRoot()9091        if (gesture.active) {92            Box(93                Modifier.fillMaxSize().graphicsLayer {94                    translationX = -routeWidthPx / 3f * (1f - gesture.progress)95                }.background(pageColor),96            ) { contentFor(previous)() }97        }9899        transition.AnimatedContent(100            contentKey = { if (cupertino) it?.id ?: "root" else it?.route?.destination() ?: "root" },101            transitionSpec = {102                // Chapter changes and same-page argument replacements must not run two readers103                // through a push animation. They still get distinct entry/saveable identities.104                val replacing = direction > 0 && initialState != null && targetState != null &&105                    initialState?.route?.destination() == targetState?.route?.destination()106                if (direction == 0 || !animationsEnabled || replacing) {107                    EnterTransition.None togetherWith ExitTransition.None108                } else if (cupertino) {109                    val motion = tween<IntOffset>(320, easing = CubicBezierEasing(0.25f, 0.1f, 0.25f, 1f))110                    if (direction > 0) {111                        (slideInHorizontally(motion) { it } togetherWith112                            slideOutHorizontally(motion) { -it / 3 }).apply { targetContentZIndex = 1f }113                    } else {114                        (slideInHorizontally(motion) { -it / 3 } togetherWith115                            slideOutHorizontally(motion) { it }).apply { targetContentZIndex = -1f }116                    }117                } else if (direction > 0) {118                    (119                        fadeIn(tween(220)) + scaleIn(tween(220), initialScale = 0.94f)120                        ) togetherWith fadeOut(tween(140))121                } else {122                    fadeIn(tween(180)) togetherWith (123                        fadeOut(tween(180)) + scaleOut(tween(220), targetScale = 0.94f)124                        )125                }126            },127            modifier = Modifier.fillMaxSize(),128        ) { entry ->129            val entryGesture = remember(entry) { gesture }130            Box(131                Modifier.fillMaxSize().graphicsLayer {132                    translationX = routeWidthPx * entryGesture.progress133                }.then(if (cupertino && !playerMinimized) Modifier.background(pageColor) else Modifier),134            ) {135                if (entry != null || (cupertino && !playerMinimized)) contentFor(entry)()136            }137        }138    }139}140

Search chapters, reference and source filenames.