package com.peachpasswords.ui.settings import androidx.activity.compose.BackHandler import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.animateDpAsState import androidx.compose.animation.core.animateFloat import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.infiniteRepeatable import androidx.compose.animation.core.keyframes import androidx.compose.animation.core.rememberInfiniteTransition import androidx.compose.animation.core.tween import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.gestures.detectDragGestures import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.runtime.collectAsState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.drawWithCache import androidx.compose.ui.draw.shadow import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.BlendMode import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.Dp import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.em import androidx.compose.ui.unit.sp import androidx.compose.ui.zIndex import androidx.hilt.navigation.compose.hiltViewModel import androidx.compose.material3.Text import com.peachpasswords.ui.theme.LocalPeachAccent import com.peachpasswords.ui.theme.LocalReduceMotion import com.peachpasswords.ui.theme.peachColors import com.peachpasswords.ui.theme.PeachMotion import com.peachpasswords.ui.theme.PeachTheme import com.peachpasswords.ui.theme.PeachStackColors import com.peachpasswords.ui.theme.PeachType import com.peachpasswords.ui.theme.ShellBackground import kotlinx.coroutines.delay import kotlinx.coroutines.launch // Deck geometry — ported verbatim from StackDeck.tsx constants. The active card // sits at top:0; every other card is a thin tab fanned below it, ordered // cyclically starting just after the active card so the stack reads as a loop. private val EXPANDED_H = 258.dp private val TAB_H = 46.dp private val TAB_OVERLAP = 14.dp private val TAB_CARD_H = TAB_H + 4.dp /** Drag distance past which a stack card flips between focused and deck states. */ private val DRAG_THRESHOLD = 60.dp /** Outcome of releasing a vertical drag on a stack card. */ internal enum class DragOutcome { EXPAND, COLLAPSE, NONE } /** * Resolve a released drag on a stack card (brief Fix 7). Dragging a collapsed * (deck) card up past the threshold focuses it; dragging the focused card down * past the threshold returns to the deck. Anything short of the threshold is a no-op. */ internal fun resolveStackDrag(dragOffsetPx: Float, thresholdPx: Float, isExpanded: Boolean): DragOutcome = when { dragOffsetPx < -thresholdPx && !isExpanded -> DragOutcome.EXPAND dragOffsetPx > thresholdPx && isExpanded -> DragOutcome.COLLAPSE else -> DragOutcome.NONE } /** * The vertical stride between consecutive collapsed tabs, sized so the deck fans * out to fill the available height instead of bunching in the top ~half (brief * Fix 7). Falls back to [min] on short screens and is capped at [max] so tabs * never spread too thin on tall ones. */ internal fun collapsedTabStride( available: Dp, expanded: Dp, tabCard: Dp, tabCount: Int, min: Dp, max: Dp, ): Dp { if (tabCount <= 0) return min val raw = (available - expanded - tabCard) / tabCount return raw.coerceIn(min, max) } @Composable fun SettingsScreen( onBack: () -> Unit, onNavigate: (com.peachpasswords.ui.PeachNav) -> Unit = {}, vm: SettingsViewModel = hiltViewModel(), ) { val entries by vm.entries.collectAsState() val settings by vm.settings.collectAsState() val isPro by vm.isPro.collectAsState() val showCelebration by vm.showCelebration.collectAsState() val reduceMotion = LocalReduceMotion.current val accent = LocalPeachAccent.current val scope = rememberCoroutineScope() var active by remember { mutableStateOf(CardId.Pro) } var deckOpen by remember { mutableStateOf(true) } var fanning by remember { mutableStateOf(false) } var celebrating by remember { mutableStateOf(null) } val activeIndex = STACK_CARDS.indexOfFirst { it.id == active }.coerceAtLeast(0) fun handleBack() { if (!deckOpen) deckOpen = true else onBack() } BackHandler { handleBack() } // Pro celebration: replay every card in a quick fan, land back on Pro, shake. fun celebrate() { if (fanning) return scope.launch { fanning = true active = CardId.Pro CELEBRATION_FAN.forEachIndexed { i, id -> delay(150L) active = id } delay(150L) active = CardId.Pro fanning = false celebrating = CardId.Pro delay(900L) celebrating = null } } // The Settings stack always renders dark, regardless of the app theme // (spec §5.5/§8): force a dark PeachTheme around it — this also darkens the // ShellBackground base (which keys off MaterialTheme) — while keeping the // user's chosen accent and reduce-motion preference. PeachTheme(darkTheme = true, accent = accent, reduceMotion = reduceMotion) { ShellBackground { Column(Modifier.fillMaxSize().statusBarsPadding().padding(horizontal = 12.dp)) { // Masthead: back chevron · "Settings" · card counter. Row( Modifier.fillMaxWidth().padding(top = 10.dp, bottom = 8.dp), verticalAlignment = Alignment.CenterVertically, ) { Box( Modifier.size(28.dp).clip(RoundedCornerShape(999.dp)) .background(Color(0x04F0E8D8)) .clickable { handleBack() }, contentAlignment = Alignment.Center, ) { Text("‹", color = peachColors.T1, fontFamily = PeachType.Mono, fontSize = 16.sp) } Text( "Settings", modifier = Modifier.weight(1f).padding(start = 10.dp), color = peachColors.T0, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 22.sp, ) Text( "${activeIndex + 1} / ${STACK_CARDS.size}", color = peachColors.T2, fontFamily = PeachType.Mono, fontSize = 9.sp, letterSpacing = 0.22.em, ) } BoxWithConstraints(Modifier.fillMaxSize()) { val focusHeight = maxHeight - 8.dp // Fan the collapsed tabs across the whole height instead of the top ~half. val tabStride = collapsedTabStride( available = maxHeight, expanded = EXPANDED_H, tabCard = TAB_CARD_H, tabCount = STACK_CARDS.size - 1, min = TAB_H - TAB_OVERLAP, max = 72.dp, ) STACK_CARDS.forEachIndexed { index, card -> val isActive = card.id == active val distance = when { index == activeIndex -> -1 index > activeIndex -> index - activeIndex - 1 else -> STACK_CARDS.size - activeIndex + index - 1 } val targetTop = when { isActive -> 0.dp !deckOpen -> focusHeight + 44.dp + (distance * 8).dp else -> EXPANDED_H - TAB_OVERLAP + (distance * tabStride.value).dp } val targetHeight = when { isActive && deckOpen -> EXPANDED_H isActive -> focusHeight else -> TAB_CARD_H } val concealed = !deckOpen && !isActive val animSpec = if (fanning) { tween(140, easing = PeachMotion.Celebrate) } else { tween(PeachMotion.Stack, easing = PeachMotion.StackSpring) } val top by animateDpAsState( targetTop, animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate) else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring), label = "top", ) val cardH by animateDpAsState( targetHeight, animationSpec = if (fanning) tween(140, easing = PeachMotion.Celebrate) else tween(PeachMotion.Stack, easing = PeachMotion.StackSpring), label = "h", ) val alpha by animateFloatAsState(if (concealed) 0f else 1f, animSpec, label = "a") val zIndex = if (isActive) 99f else (50 - distance).toFloat() // Celebration shake — keyframed bob + tilt as the Pro card lands. val shakeDy = remember { Animatable(0f) } val shakeRot = remember { Animatable(0f) } LaunchedEffect(celebrating == card.id) { if (celebrating == card.id && !reduceMotion) { launch { shakeRot.animateTo(0f, keyframes { durationMillis = 880 0f at 0; -1.3f at 211; 0.9f at 422; -0.45f at 616; 0f at 757 }) } shakeDy.animateTo(0f, keyframes { durationMillis = 880 0f at 0; -10f at 211; 5f at 422; -4f at 616; 2f at 757 }) } } val density = LocalDensity.current StackCard( spec = card, active = isActive, focused = isActive && !deckOpen, lede = vm.lede(card.id, entries.count { it.deletedAt == null }, settings), showSparkle = card.id == CardId.Pro && isPro, onIconClick = if (card.id == CardId.Pro && isPro) ({ celebrate() }) else null, body = { CardBody( id = card.id, fg = card.fg, vm = vm, settings = settings, entriesCount = entries.count { it.deletedAt == null }, onNavigate = onNavigate, ) }, modifier = Modifier .fillMaxWidth() .align(Alignment.TopStart) .zIndex(zIndex) // Drag the deck like a physical stack: flick a tab up to focus // it, drag the focused card down to drop back into the deck. .pointerInput(card.id, deckOpen, isActive) { var dragY = 0f val thresholdPx = with(density) { DRAG_THRESHOLD.toPx() } detectDragGestures( onDragStart = { dragY = 0f }, onDrag = { change, amount -> change.consume(); dragY += amount.y }, onDragEnd = { when (resolveStackDrag(dragY, thresholdPx, isActive && !deckOpen)) { DragOutcome.EXPAND -> { active = card.id; deckOpen = false } DragOutcome.COLLAPSE -> { deckOpen = true } DragOutcome.NONE -> {} } }, ) } .graphicsLayer { translationY = top.toPx() + with(density) { shakeDy.value.dp.toPx() } rotationZ = shakeRot.value this.alpha = alpha } .height(cardH), onClick = { if (isActive) { if (deckOpen) deckOpen = false } else { active = card.id deckOpen = false } }, enabled = !concealed, ) } } } if (showCelebration) { ProCelebration(onDone = { vm.dismissCelebration(); celebrate() }) } } } } @Composable private fun StackCard( spec: StackCardSpec, active: Boolean, focused: Boolean, lede: String, showSparkle: Boolean, onIconClick: (() -> Unit)?, body: @Composable () -> Unit, modifier: Modifier, onClick: () -> Unit, enabled: Boolean, ) { val interaction = remember { MutableInteractionSource() } Box( modifier .shadow(if (active) 18.dp else 8.dp, RoundedCornerShape(PeachStackColors.CardRadius), clip = false) .clip(RoundedCornerShape(PeachStackColors.CardRadius)) .drawBehind { drawRect(brush = cardGradient155(spec.bgStart, spec.bgEnd)) } .clickable( interactionSource = interaction, indication = null, enabled = enabled && !active || (active && !focused), ) { onClick() } .padding( horizontal = if (active) 18.dp else 16.dp, vertical = if (active) 16.dp else 12.dp, ), ) { if (showSparkle) SparkleDust(Modifier.fillMaxSize()) if (active) { Column(Modifier.fillMaxSize()) { Row( Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically, ) { Text( spec.num, color = spec.fg.copy(alpha = 0.7f), fontFamily = PeachType.Mono, fontSize = 8.5.sp, letterSpacing = 0.26.em, ) Box( Modifier.clip(RoundedCornerShape(999.dp)) .then(if (onIconClick != null) Modifier.clickable { onIconClick() } else Modifier) .padding(2.dp), ) { CardIcon(spec.icon, spec.fg.copy(alpha = 0.85f)) } } Text( spec.title, modifier = Modifier.padding(top = 6.dp), color = spec.fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 26.sp, ) Text( lede, modifier = Modifier.padding(top = 4.dp), color = spec.fg.copy(alpha = 0.72f), fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontSize = 12.5.sp, ) // Body scrolls independently; only revealed at full height in // focus mode, but kept mounted so deck-mode shows a preview. Column( Modifier.fillMaxWidth().weight(1f) .padding(top = 14.dp) .verticalScroll(rememberScrollState()), ) { body() Spacer(Modifier.height(24.dp)) } } } else { // Tab: just the title, single line, vertically centred. Box(Modifier.fillMaxSize(), contentAlignment = Alignment.CenterStart) { Text( spec.title, color = spec.fg, fontFamily = PeachType.Display, fontStyle = FontStyle.Italic, fontWeight = FontWeight.Normal, fontSize = 19.sp, maxLines = 1, overflow = TextOverflow.Ellipsis, ) } } } } /** * Settings card gradient at CSS 155° across the drawn card (spec §5.5). Computed * from the live draw size so the angle holds on any card height, unlike Compose's * default corner-to-corner linear gradient. */ private fun DrawScope.cardGradient155(start: Color, end: Color): Brush { val a = Math.toRadians(155.0) val dx = kotlin.math.sin(a).toFloat() val dy = -kotlin.math.cos(a).toFloat() val cx = size.width / 2f val cy = size.height / 2f val half = (kotlin.math.abs(dx) * size.width + kotlin.math.abs(dy) * size.height) / 2f return Brush.linearGradient( colors = listOf(start, end), start = Offset(cx - dx * half, cy - dy * half), end = Offset(cx + dx * half, cy + dy * half), ) } /** * Ambient drifting sparkle dust for the Pro card. Three offset dot grids slowly * translate diagonally and loop, screen-blended at low opacity — the Compose * analogue of the browser's `stk-pro-dust` keyframes. */ @Composable private fun SparkleDust(modifier: Modifier = Modifier) { val reduceMotion = LocalReduceMotion.current val t = rememberInfiniteTransition(label = "dust") val drift by if (reduceMotion) { remember { mutableStateOf(0f) } } else { t.animateFloat( initialValue = 0f, targetValue = 1f, animationSpec = infiniteRepeatable(tween(14000, easing = androidx.compose.animation.core.LinearEasing)), label = "drift", ) } Box( modifier.drawWithCache { val tile = 48.dp.toPx() val dx = drift * tile onDrawBehind { val cols = (size.width / tile).toInt() + 2 val rows = (size.height / tile).toInt() + 2 for (r in 0..rows) { for (c in 0..cols) { val baseX = c * tile + dx val baseY = r * tile + dx drawCircle(Color(0xFFFFF8DC), 0.6f, Offset(baseX % (size.width + tile), baseY % (size.height + tile)), alpha = 0.11f, blendMode = BlendMode.Screen) drawCircle(Color(0xFFFFEEAA), 1f, Offset((baseX + 19f) % (size.width + tile), (baseY + 15f) % (size.height + tile)), alpha = 0.08f, blendMode = BlendMode.Screen) } } } }, ) }