ReaderAutoScroll.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/reader/ReaderAutoScroll.kt
1package eu.friedcutlet.yokan.ui.screens.reader23import androidx.compose.animation.core.LinearEasing4import androidx.compose.animation.core.tween5import androidx.compose.animation.AnimatedVisibility6import androidx.compose.animation.scaleIn7import androidx.compose.animation.scaleOut8import androidx.compose.foundation.gestures.animateScrollBy9import androidx.compose.foundation.gestures.scrollBy10import androidx.compose.foundation.ScrollState11import androidx.compose.foundation.layout.BoxScope12import androidx.compose.foundation.layout.padding13import androidx.compose.foundation.lazy.LazyListState14import androidx.compose.material.icons.Icons15import androidx.compose.material.icons.filled.Pause16import androidx.compose.material.icons.filled.PlayArrow17import androidx.compose.material3.FloatingActionButton18import androidx.compose.material3.Icon19import androidx.compose.material3.Surface20import androidx.compose.material3.Text21import androidx.compose.material3.MaterialTheme22import androidx.compose.foundation.shape.RoundedCornerShape23import androidx.compose.runtime.Composable24import androidx.compose.runtime.LaunchedEffect25import androidx.compose.runtime.getValue26import androidx.compose.runtime.rememberUpdatedState27import androidx.compose.runtime.snapshotFlow28import androidx.compose.runtime.withFrameNanos29import androidx.compose.ui.Alignment30import androidx.compose.ui.Modifier31import androidx.compose.ui.unit.Dp32import androidx.compose.ui.unit.dp33import kotlinx.coroutines.delay34import kotlinx.coroutines.CancellationException35import kotlinx.coroutines.currentCoroutineContext36import kotlinx.coroutines.isActive37import kotlinx.coroutines.flow.first38import org.jetbrains.compose.resources.stringResource39import yokan.composeapp.generated.resources.Res40import yokan.composeapp.generated.resources.reader_scroll_pause41import yokan.composeapp.generated.resources.reader_scroll_play42import yokan.composeapp.generated.resources.reader_hold_speed43import yokan.composeapp.generated.resources.reader_hold_paused44import kotlin.math.roundToInt4546@Composable47internal fun BoxScope.ReaderHoldScrollIndicator(hold: ReaderHoldScroll) {48 if (hold.phase == ReaderHoldPhase.Idle) return49 Surface(50 modifier = Modifier.align(Alignment.BottomCenter).padding(bottom = 64.dp),51 shape = RoundedCornerShape(50),52 color = MaterialTheme.colorScheme.secondaryContainer,53 ) {54 val multiplier = (hold.speed / hold.baseSpeed.coerceAtLeast(2f) * 10f).roundToInt() / 10f55 Text(56 text = if (hold.phase == ReaderHoldPhase.Holding) stringResource(Res.string.reader_hold_paused)57 else stringResource(Res.string.reader_hold_speed, multiplier.toString()),58 modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp),59 style = MaterialTheme.typography.labelLarge,60 )61 }62}6364/**65 * Floating play/pause FAB shown in continuous modes (vertical, webtoon,66 * horizontal continuous). Mirrors the Flutter `auto_scroll_button.dart`.67 *68 * Auto-scroll itself is driven by [DriveAutoScroll] below — kept separate so69 * the FAB stays a pure render-only composable.70 */71@Composable72internal fun BoxScope.ReaderAutoScrollFab(73 visible: Boolean,74 isPlaying: Boolean,75 onToggle: () -> Unit,76 bottomPadding: Dp = 108.dp,77) {78 AnimatedVisibility(79 visible = visible,80 enter = scaleIn(),81 exit = scaleOut(),82 modifier = Modifier.align(Alignment.BottomEnd).padding(end = 18.dp, bottom = bottomPadding),83 ) {84 FloatingActionButton(onClick = onToggle) {85 if (isPlaying) Icon(Icons.Filled.Pause, contentDescription = stringResource(Res.string.reader_scroll_pause))86 else Icon(Icons.Filled.PlayArrow, contentDescription = stringResource(Res.string.reader_scroll_play))87 }88 }89}9091/**92 * One frame-driven controller for ordinary and temporary hold scrolling. Speed changes93 * do not restart it. At a loading boundary it suspends until the extent changes; at the94 * confirmed final transition it relinquishes ownership. The old loop advanced one offset95 * over a 100 ms animation after a 100 ms delay, hence five offsets per second.96 */97@Composable98internal fun DriveAutoScroll(99 state: LazyListState,100 isPlaying: Boolean,101 pageOffset: Float,102 endOfContent: Boolean = false,103 onFinished: () -> Unit = {},104) {105 val playing by rememberUpdatedState(isPlaying)106 val offset by rememberUpdatedState(pageOffset.coerceIn(0f, 60f))107 val terminal by rememberUpdatedState(endOfContent)108 val finished by rememberUpdatedState(onFinished)109 LaunchedEffect(state, isPlaying) {110 if (!playing) return@LaunchedEffect111 try {112 var previous = withFrameNanos { it }113 while (playing) {114 if (!state.canScrollForward) {115 if (terminal && state.layoutInfo.totalItemsCount > 0) {116 finished()117 break118 }119 snapshotFlow { state.canScrollForward || (terminal && state.layoutInfo.totalItemsCount > 0) || !playing }.first { it }120 previous = withFrameNanos { it }121 continue122 }123 if (offset <= 0f) {124 snapshotFlow { offset > 0f || !playing }.first { it }125 previous = withFrameNanos { it }126 continue127 }128 val now = withFrameNanos { it }129 val seconds = ((now - previous) / 1_000_000_000f).coerceIn(0f, 0.05f)130 if (playing) state.scrollBy(offset * 5f * seconds)131 previous = now132 }133 } catch (cancelled: CancellationException) {134 // A user drag can interrupt the list's scroll mutation while this effect is135 // still active. Disposing/replacing the effect must not stop a newer controller.136 if (currentCoroutineContext().isActive) finished()137 throw cancelled138 }139 }140}141142/** ScrollState counterpart used by the continuous novel reader. */143@Composable144internal fun DriveAutoScroll(145 state: ScrollState,146 isPlaying: Boolean,147 pageOffset: Float,148) {149 val playing by rememberUpdatedState(isPlaying)150 val offset by rememberUpdatedState(pageOffset.coerceIn(2f, 30f))151 LaunchedEffect(state, playing, offset) {152 if (!playing) return@LaunchedEffect153 while (playing) {154 delay(100L)155 if (!playing) break156 if (state.value < state.maxValue && state.maxValue != Int.MAX_VALUE) {157 state.animateScrollBy(158 value = offset,159 animationSpec = tween(durationMillis = 100, easing = LinearEasing),160 )161 }162 }163 }164}165