package eu.friedcutlet.yokan.ui.screens.novel import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.EnterTransition import androidx.compose.animation.ExitTransition import androidx.compose.animation.core.FastOutSlowInEasing import androidx.compose.animation.core.LinearEasing import androidx.compose.animation.core.tween import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeOut import androidx.compose.animation.slideInVertically import androidx.compose.animation.slideOutVertically import androidx.compose.foundation.ScrollState import androidx.compose.foundation.clickable import androidx.compose.foundation.gestures.animateScrollBy import androidx.compose.foundation.gestures.scrollBy import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxHeight 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.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material.icons.filled.Add import androidx.compose.material.icons.filled.Bookmark import androidx.compose.material.icons.filled.BookmarkBorder import androidx.compose.material.icons.filled.GraphicEq import androidx.compose.material.icons.filled.Pause import androidx.compose.material.icons.filled.PlayArrow import androidx.compose.material.icons.filled.Public import androidx.compose.material.icons.filled.Remove import androidx.compose.material.icons.filled.Settings import androidx.compose.material.icons.filled.SkipNext import androidx.compose.material.icons.filled.SkipPrevious import androidx.compose.material.icons.filled.Stop import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.LinearProgressIndicator import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Slider import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import eu.friedcutlet.yokan.domain.Chapter import eu.friedcutlet.yokan.ui.status.GlobalStatusAccessButton import kotlin.math.sign import kotlinx.coroutines.launch import org.jetbrains.compose.resources.stringResource import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.action_back import yokan.composeapp.generated.resources.tts_stop import yokan.composeapp.generated.resources.ui_tts private val chapterNumberRegex = Regex("(\\d+(?:\\.\\d+)?)") /** Slides the reader chrome in from [fromTop] or the bottom edge; e-ink shows it without motion. */ @Composable private fun NovelChromeVisibility(visible: Boolean, eink: Boolean, fromTop: Boolean, content: @Composable () -> Unit) { val direction = if (fromTop) -1 else 1 AnimatedVisibility( visible = visible, enter = if (eink) EnterTransition.None else slideInVertically( initialOffsetY = { direction * it }, animationSpec = tween(300, easing = FastOutSlowInEasing), ) + fadeIn(tween(300)), exit = if (eink) ExitTransition.None else slideOutVertically( targetOffsetY = { direction * it }, animationSpec = tween(300, easing = FastOutSlowInEasing), ) + fadeOut(tween(300)), ) { content() } } @OptIn(ExperimentalMaterial3Api::class) @Composable internal fun NovelReaderTopBar( visible: Boolean, eink: Boolean, title: String, chapter: Chapter, onBack: () -> Unit, onToggleBookmark: () -> Unit, onOpenWebView: () -> Unit, ) { NovelChromeVisibility(visible, eink, fromTop = true) { TopAppBar( title = { Column { Text( title, maxLines = 1, overflow = TextOverflow.Ellipsis, style = MaterialTheme.typography.titleMedium, ) Text( // The chapter number prefix of the name when present. "Ch ${chapterNumberRegex.find(chapter.name)?.value ?: "?"} - ${chapter.name}", maxLines = 1, overflow = TextOverflow.Ellipsis, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) } }, navigationIcon = { IconButton(onClick = onBack) { Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = stringResource(Res.string.action_back)) } }, actions = { GlobalStatusAccessButton() IconButton(onClick = onToggleBookmark) { Icon( if (chapter.isBookmarked) Icons.Filled.Bookmark else Icons.Filled.BookmarkBorder, contentDescription = "Bookmark", ) } IconButton(onClick = onOpenWebView) { Icon(Icons.Filled.Public, contentDescription = "Open in web view") } }, ) } } @Composable internal fun NovelReaderBottomBar( visible: Boolean, eink: Boolean, progress: Int, ttsEnabled: Boolean, onPreviousChapter: () -> Unit, onNextChapter: () -> Unit, onSeek: (Float) -> Unit, onChangeFontSize: (delta: Int) -> Unit, onStartTts: () -> Unit, onOpenSettings: () -> Unit, ) { NovelChromeVisibility(visible, eink, fromTop = false) { Surface( tonalElevation = 2.dp, color = MaterialTheme.colorScheme.surface, ) { Column( Modifier.fillMaxWidth().padding(horizontal = 12.dp, vertical = 8.dp), verticalArrangement = Arrangement.spacedBy(6.dp), ) { Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp), ) { IconButton(onClick = onPreviousChapter) { Icon(Icons.Filled.SkipPrevious, contentDescription = "Previous chapter") } Slider( value = progress / 100f, onValueChange = onSeek, modifier = Modifier.weight(1f), ) IconButton(onClick = onNextChapter) { Icon(Icons.Filled.SkipNext, contentDescription = "Next chapter") } } Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp), ) { Surface( shape = RoundedCornerShape(50), tonalElevation = 1.dp, color = MaterialTheme.colorScheme.surfaceVariant, ) { Row(verticalAlignment = Alignment.CenterVertically) { IconButton(onClick = { onChangeFontSize(-1) }) { Icon(Icons.Filled.Remove, contentDescription = "Smaller text") } Text( "Aa", style = MaterialTheme.typography.labelLarge, modifier = Modifier.padding(horizontal = 4.dp), ) IconButton(onClick = { onChangeFontSize(1) }) { Icon(Icons.Filled.Add, contentDescription = "Larger text") } } } Spacer(Modifier.weight(1f)) IconButton(enabled = ttsEnabled, onClick = onStartTts) { Icon(Icons.Filled.GraphicEq, contentDescription = stringResource(Res.string.ui_tts)) } IconButton(onClick = onOpenSettings) { Icon(Icons.Filled.Settings, contentDescription = "Settings") } } } } } } /** * Mangayomi tap-zone geometry: top/bottom bands (2:5:2) page by ±100 logical px; the middle row * splits into thirds where the center always toggles chrome. With tap-to-scroll off, every zone * toggles chrome (novel_reader_view.dart _gestureRightLeft/_gestureTopBottom). */ @Composable internal fun NovelTapZones( scrollState: ScrollState, tapToScroll: Boolean, eink: Boolean, onToggleChrome: () -> Unit, modifier: Modifier = Modifier, ) { val scope = rememberCoroutineScope() val interactionSource = remember { MutableInteractionSource() } val tapStep = with(LocalDensity.current) { 100.dp.toPx() } fun scrollOrToggle(delta: Float): () -> Unit = { if (!tapToScroll) onToggleChrome() else scope.launch { if (eink) { // Whole-screen jumps: smooth 100 px steps ghost on e-paper. scrollState.scrollBy(sign(delta) * scrollState.viewportSize * 0.9f) } else { scrollState.animateScrollBy(delta, tween(durationMillis = 100, easing = LinearEasing)) } } } fun Modifier.zone(onTap: () -> Unit): Modifier = clickable(interactionSource = interactionSource, indication = null, onClick = onTap) val back = scrollOrToggle(-tapStep) val forward = scrollOrToggle(tapStep) Column(modifier = modifier.fillMaxSize()) { Box(Modifier.fillMaxWidth().weight(2f).zone(back)) Row(Modifier.fillMaxWidth().weight(5f)) { Box(Modifier.fillMaxHeight().weight(1f).zone(back)) Box(Modifier.fillMaxHeight().weight(1f).zone(onToggleChrome)) Box(Modifier.fillMaxHeight().weight(1f).zone(forward)) } Box(Modifier.fillMaxWidth().weight(2f).zone(forward)) } } @Composable internal fun NovelProgressPill(progress: Int, modifier: Modifier = Modifier) { Surface( modifier = modifier.padding(start = 4.dp), color = MaterialTheme.colorScheme.surface.copy(alpha = 0.7f), shape = RoundedCornerShape(topEnd = 8.dp, bottomEnd = 8.dp), ) { Text( "$progress%", modifier = Modifier.padding(horizontal = 6.dp, vertical = 4.dp), style = MaterialTheme.typography.labelSmall, color = MaterialTheme.colorScheme.onSurface, ) } } @Composable internal fun NovelTtsBar( tts: NovelTtsUiState, bottomPadding: Dp, onEvent: (NovelReaderEvent) -> Unit, modifier: Modifier = Modifier, ) { val playing = tts.playback == NovelTtsPlaybackState.Playing Surface( modifier = modifier .fillMaxWidth() .padding(horizontal = 8.dp, vertical = 8.dp) .padding(bottom = bottomPadding), color = MaterialTheme.colorScheme.surfaceVariant, shape = RoundedCornerShape(16.dp), shadowElevation = 8.dp, ) { Column { LinearProgressIndicator( progress = { tts.progress }, modifier = Modifier.fillMaxWidth().height(3.dp), ) Row( modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp), ) { Icon(Icons.Filled.GraphicEq, contentDescription = null) Text( "Paragraph ${tts.paragraphIndex + 1} of ${tts.paragraphCount}", modifier = Modifier.weight(1f), style = MaterialTheme.typography.bodySmall, maxLines = 1, overflow = TextOverflow.Ellipsis, ) IconButton( enabled = tts.paragraphIndex > 0, onClick = { onEvent(NovelReaderEvent.SkipPreviousTts) }, ) { Icon(Icons.Filled.SkipPrevious, contentDescription = "Previous paragraph") } IconButton(onClick = { onEvent(NovelReaderEvent.ToggleTtsPlayback) }) { Icon( if (playing) Icons.Filled.Pause else Icons.Filled.PlayArrow, contentDescription = if (playing) "Pause TTS" else "Resume TTS", ) } IconButton( enabled = tts.paragraphIndex < tts.paragraphCount - 1, onClick = { onEvent(NovelReaderEvent.SkipNextTts) }, ) { Icon(Icons.Filled.SkipNext, contentDescription = "Next paragraph") } IconButton(onClick = { onEvent(NovelReaderEvent.StopTts) }) { Icon(Icons.Filled.Stop, contentDescription = stringResource(Res.string.tts_stop)) } } } } }