Yokan / rebuild
Course 1 8624561

ReaderOverlays.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/reader/ReaderOverlays.kt

implementation525 lines
1package eu.friedcutlet.yokan.ui.screens.reader23import androidx.compose.animation.EnterTransition4import androidx.compose.animation.ExitTransition5import androidx.compose.animation.AnimatedVisibility6import androidx.compose.animation.core.CubicBezierEasing7import androidx.compose.animation.core.tween8import androidx.compose.animation.expandVertically9import androidx.compose.animation.shrinkVertically10import androidx.compose.foundation.BorderStroke11import androidx.compose.foundation.background12import androidx.compose.foundation.layout.Arrangement13import androidx.compose.foundation.layout.Box14import androidx.compose.foundation.layout.BoxScope15import androidx.compose.foundation.layout.Column16import androidx.compose.foundation.layout.Row17import androidx.compose.foundation.layout.Spacer18import androidx.compose.foundation.layout.fillMaxHeight19import androidx.compose.foundation.layout.fillMaxSize20import androidx.compose.foundation.layout.fillMaxWidth21import androidx.compose.foundation.layout.height22import androidx.compose.foundation.layout.padding23import androidx.compose.foundation.layout.size24import androidx.compose.foundation.layout.width25import androidx.compose.foundation.layout.widthIn26import androidx.compose.foundation.lazy.LazyColumn27import androidx.compose.foundation.lazy.items28import androidx.compose.foundation.lazy.rememberLazyListState29import androidx.compose.foundation.shape.RoundedCornerShape30import androidx.compose.material.icons.Icons31import androidx.compose.material.icons.automirrored.filled.ArrowBack32import androidx.compose.material.icons.automirrored.filled.FormatListBulleted33import androidx.compose.material.icons.automirrored.filled.MenuBook34import androidx.compose.material.icons.automirrored.filled.NavigateNext35import androidx.compose.material.icons.filled.Bookmark36import androidx.compose.material.icons.filled.BookmarkBorder37import androidx.compose.material.icons.filled.CheckCircleOutline38import androidx.compose.material.icons.filled.Close39import androidx.compose.material.icons.filled.KeyboardArrowDown40import androidx.compose.material.icons.filled.Public41import androidx.compose.material.icons.filled.Refresh42import androidx.compose.material3.ExperimentalMaterial3Api43import androidx.compose.material3.HorizontalDivider44import androidx.compose.material3.Icon45import androidx.compose.material3.IconButton46import androidx.compose.material3.MaterialTheme47import androidx.compose.material3.Surface48import androidx.compose.material3.Text49import androidx.compose.material.icons.filled.MoreVert50import androidx.compose.material3.DropdownMenu51import androidx.compose.material3.DropdownMenuItem52import org.jetbrains.compose.resources.stringResource53import yokan.composeapp.generated.resources.Res54import yokan.composeapp.generated.resources.action_back55import yokan.composeapp.generated.resources.ui_close56import yokan.composeapp.generated.resources.reader_image_actions57import yokan.composeapp.generated.resources.reader_refresh_pages58import yokan.composeapp.generated.resources.reader_open_web59import yokan.composeapp.generated.resources.reader_actions_menu60import androidx.compose.material3.TopAppBar61import androidx.compose.material3.TopAppBarDefaults62import androidx.compose.runtime.Composable63import androidx.compose.runtime.getValue64import androidx.compose.runtime.setValue65import androidx.compose.runtime.mutableStateOf66import androidx.compose.runtime.remember67import androidx.compose.ui.Alignment68import androidx.compose.ui.Modifier69import androidx.compose.ui.draw.clip70import androidx.compose.ui.graphics.Color71import androidx.compose.ui.text.font.FontWeight72import androidx.compose.ui.text.style.TextAlign73import androidx.compose.ui.text.style.TextOverflow74import androidx.compose.ui.unit.dp75import androidx.compose.ui.window.Dialog76import androidx.compose.ui.window.DialogProperties77import eu.friedcutlet.yokan.domain.Chapter78import eu.friedcutlet.yokan.ui.status.GlobalStatusAccessButton7980/**81 * Reader top app bar — back / title / chapter-list / bookmark / refresh / web actions.82 *83 * Title is a two-line label: bold manga name + smaller chapter subtitle.84 * The bookmark icon swaps between filled/outline mirroring [isBookmarked].85 * The refresh and web buttons are hidden for local-archive chapters since they86 * have no source page list or remote URL.87 */88@OptIn(ExperimentalMaterial3Api::class)89@Composable90internal fun ReaderTopBar(91    mangaTitle: String,92    chapterTitle: String,93    isBookmarked: Boolean,94    showWebAction: Boolean,95    showRefreshAction: Boolean,96    onBack: () -> Unit,97    onChapterList: () -> Unit,98    onToggleBookmark: () -> Unit,99    onRefreshPages: () -> Unit,100    onWeb: () -> Unit,101    showImageActions: Boolean,102    onImageActions: () -> Unit,103    onOpenMenu: () -> Unit,104) {105    var menuOpen by remember { mutableStateOf(false) }106    TopAppBar(107        title = {108            Column {109                Text(110                    text = mangaTitle,111                    style = MaterialTheme.typography.titleMedium,112                    fontWeight = FontWeight.SemiBold,113                    maxLines = 1,114                    overflow = TextOverflow.Ellipsis,115                )116                Text(117                    text = chapterTitle,118                    style = MaterialTheme.typography.bodySmall,119                    color = MaterialTheme.colorScheme.onSurfaceVariant,120                    maxLines = 1,121                    overflow = TextOverflow.Ellipsis,122                )123            }124        },125        navigationIcon = {126            IconButton(onClick = onBack) {127                Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = stringResource(Res.string.action_back))128            }129        },130        actions = {131            GlobalStatusAccessButton()132            IconButton(onClick = onChapterList) {133                Icon(134                    Icons.AutoMirrored.Filled.FormatListBulleted,135                    contentDescription = "Chapter list",136                )137            }138            IconButton(onClick = onToggleBookmark) {139                Icon(140                    imageVector = if (isBookmarked) Icons.Filled.Bookmark else Icons.Filled.BookmarkBorder,141                    contentDescription = if (isBookmarked) "Remove bookmark" else "Bookmark",142                )143            }144            Box {145                IconButton(onClick = { onOpenMenu(); menuOpen = true }) {146                    Icon(Icons.Filled.MoreVert, contentDescription = stringResource(Res.string.reader_actions_menu))147                }148                DropdownMenu(expanded = menuOpen, onDismissRequest = { menuOpen = false }) {149                    if (showImageActions) DropdownMenuItem(150                        text = { Text(stringResource(Res.string.reader_image_actions)) },151                        onClick = { menuOpen = false; onImageActions() },152                    )153                    if (showRefreshAction) DropdownMenuItem(154                        text = { Text(stringResource(Res.string.reader_refresh_pages)) },155                        onClick = { menuOpen = false; onRefreshPages() },156                    )157                    if (showWebAction) DropdownMenuItem(158                        text = { Text(stringResource(Res.string.reader_open_web)) },159                        onClick = { menuOpen = false; onWeb() },160                    )161                }162            }163        },164        colors = TopAppBarDefaults.topAppBarColors(165            containerColor = MaterialTheme.colorScheme.surface.copy(alpha = 0.94f),166        ),167    )168}169170@Composable171internal fun ReaderChapterListDialog(172    chapters: List<Chapter>,173    currentChapterId: Long,174    onDismiss: () -> Unit,175    onSelect: (Chapter) -> Unit,176) {177    val currentIndex = remember(chapters, currentChapterId) {178        chapters.indexOfFirst { it.id == currentChapterId }.coerceAtLeast(0)179    }180    val listState = rememberLazyListState(181        initialFirstVisibleItemIndex = (currentIndex - 2).coerceAtLeast(0),182    )183    Dialog(184        onDismissRequest = onDismiss,185        properties = DialogProperties(usePlatformDefaultWidth = false),186    ) {187        Surface(188            modifier = Modifier.fillMaxWidth(0.85f).fillMaxHeight(0.8f),189            shape = RoundedCornerShape(20.dp),190            tonalElevation = 8.dp,191        ) {192            Column {193                Row(194                    modifier = Modifier.fillMaxWidth().padding(start = 20.dp, end = 8.dp, top = 8.dp, bottom = 8.dp),195                    verticalAlignment = Alignment.CenterVertically,196                ) {197                    Icon(198                        imageVector = Icons.AutoMirrored.Filled.MenuBook,199                        contentDescription = null,200                        tint = MaterialTheme.colorScheme.primary,201                    )202                    Spacer(Modifier.width(12.dp))203                    Text(204                        text = "Chapters",205                        style = MaterialTheme.typography.titleLarge,206                        color = MaterialTheme.colorScheme.primary,207                        fontWeight = FontWeight.Bold,208                        modifier = Modifier.weight(1f),209                    )210                    IconButton(onClick = onDismiss) {211                        Icon(Icons.Filled.Close, contentDescription = stringResource(Res.string.ui_close))212                    }213                }214                HorizontalDivider(color = MaterialTheme.colorScheme.primary.copy(alpha = 0.2f))215                LazyColumn(216                    state = listState,217                    modifier = Modifier.fillMaxSize(),218                    contentPadding = androidx.compose.foundation.layout.PaddingValues(12.dp),219                    verticalArrangement = Arrangement.spacedBy(6.dp),220                ) {221                    items(chapters, key = Chapter::id) { chapter ->222                        val current = chapter.id == currentChapterId223                        Surface(224                            onClick = {225                                if (!current) onSelect(chapter)226                            },227                            modifier = Modifier.fillMaxWidth(),228                            shape = RoundedCornerShape(12.dp),229                            color = if (current) {230                                MaterialTheme.colorScheme.primary.copy(alpha = 0.15f)231                            } else {232                                Color.Transparent233                            },234                            border = if (current) {235                                BorderStroke(1.5.dp, MaterialTheme.colorScheme.primary.copy(alpha = 0.4f))236                            } else {237                                null238                            },239                        ) {240                            Row(241                                modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 12.dp),242                                verticalAlignment = Alignment.CenterVertically,243                            ) {244                                Box(245                                    Modifier246                                        .width(4.dp)247                                        .height(48.dp)248                                        .clip(RoundedCornerShape(2.dp))249                                        .background(250                                            if (chapter.isRead) {251                                                MaterialTheme.colorScheme.onSurface.copy(alpha = 0.25f)252                                            } else {253                                                MaterialTheme.colorScheme.primary254                                            },255                                        ),256                                )257                                Spacer(Modifier.width(12.dp))258                                Column(Modifier.weight(1f)) {259                                    Text(260                                        text = chapter.name,261                                        style = MaterialTheme.typography.bodyMedium,262                                        fontWeight = if (current) FontWeight.Bold else FontWeight.Medium,263                                        color = if (chapter.isRead) {264                                            MaterialTheme.colorScheme.onSurface.copy(alpha = 0.45f)265                                        } else {266                                            MaterialTheme.colorScheme.onSurface267                                        },268                                        maxLines = 2,269                                        overflow = TextOverflow.Ellipsis,270                                    )271                                    listOf(chapter.scanlator, chapter.dateUpload)272                                        .filter(String::isNotBlank)273                                        .joinToString(" · ")274                                        .takeIf(String::isNotBlank)275                                        ?.let { metadata ->276                                            Spacer(Modifier.height(4.dp))277                                            Text(278                                                text = metadata,279                                                style = MaterialTheme.typography.bodySmall,280                                                color = MaterialTheme.colorScheme.onSurfaceVariant,281                                                maxLines = 1,282                                                overflow = TextOverflow.Ellipsis,283                                            )284                                        }285                                }286                                if (chapter.isBookmarked) {287                                    Icon(288                                        Icons.Filled.Bookmark,289                                        contentDescription = "Bookmarked",290                                        tint = MaterialTheme.colorScheme.primary,291                                    )292                                }293                            }294                        }295                    }296                }297            }298        }299    }300}301302/**303 * Expands and collapses reader chrome height over the authority's 300 ms ease304 * curve. The bars stay pinned to their respective screen edges while content305 * is clipped by the size transition.306 */307@Composable308internal fun BoxScope.ReaderOverlayBars(309    visible: Boolean,310    topBar: @Composable () -> Unit,311    bottomBar: @Composable () -> Unit,312) {313    val eink = eu.friedcutlet.yokan.ui.theme.LocalEinkMode.current314    AnimatedVisibility(315        visible = visible,316        enter = if (eink) EnterTransition.None else expandVertically(317            animationSpec = tween(300, easing = ReaderChromeEasing),318            expandFrom = Alignment.Top,319        ),320        exit = if (eink) ExitTransition.None else shrinkVertically(321            animationSpec = tween(300, easing = ReaderChromeEasing),322            shrinkTowards = Alignment.Top,323        ),324        modifier = Modifier.align(Alignment.TopCenter),325    ) {326        topBar()327    }328    AnimatedVisibility(329        visible = visible,330        enter = if (eink) EnterTransition.None else expandVertically(331            animationSpec = tween(300, easing = ReaderChromeEasing),332            expandFrom = Alignment.Bottom,333        ),334        exit = if (eink) ExitTransition.None else shrinkVertically(335            animationSpec = tween(300, easing = ReaderChromeEasing),336            shrinkTowards = Alignment.Bottom,337        ),338        modifier = Modifier.align(Alignment.BottomCenter),339    ) {340        bottomBar()341    }342}343344private val ReaderChromeEasing = CubicBezierEasing(0.25f, 0.1f, 0.25f, 1f)345346/**347 * Floating page indicator pill at bottom-center, shown when chrome is hidden.348 * Mirrors the Flutter behavior — when the user dismisses the bars they still349 * want to know where they are. Gated by `DbSettings.showPagesNumber` upstream.350 */351@Composable352internal fun BoxScope.ReaderPageIndicatorPill(currentLabel: String, total: Int) {353    Box(354        modifier = Modifier355            .align(Alignment.BottomCenter)356            .padding(bottom = 18.dp)357            .clip(RoundedCornerShape(percent = 50))358            .background(Color.Black.copy(alpha = 0.55f))359            .padding(horizontal = 14.dp, vertical = 6.dp),360    ) {361        Text(362            text = "$currentLabel / $total",363            color = Color.White,364            style = MaterialTheme.typography.labelMedium,365        )366    }367}368369@Composable370internal fun ReaderChapterTransitionPage(371    currentChapter: Chapter,372    nextChapter: Chapter?,373    mangaTitle: String,374    vertical: Boolean,375    isRightToLeft: Boolean,376    modifier: Modifier = Modifier,377) {378    Box(379        modifier = modifier.background(MaterialTheme.colorScheme.surface),380        contentAlignment = Alignment.Center,381    ) {382        if (vertical) {383            Column(384                modifier = Modifier.fillMaxWidth().widthIn(max = 480.dp).padding(24.dp),385                horizontalAlignment = Alignment.CenterHorizontally,386            ) {387                TransitionHeading()388                Spacer(Modifier.height(20.dp))389                TransitionChapterCard("Chapter completed", currentChapter.name, primary = false)390                Spacer(Modifier.height(16.dp))391                Icon(392                    imageVector = if (nextChapter == null) {393                        Icons.Filled.CheckCircleOutline394                    } else {395                        Icons.Filled.KeyboardArrowDown396                    },397                    contentDescription = null,398                    modifier = Modifier.size(32.dp),399                    tint = MaterialTheme.colorScheme.primary,400                )401                Spacer(Modifier.height(16.dp))402                if (nextChapter != null) {403                    TransitionChapterCard("Next chapter", nextChapter.name, primary = true)404                } else {405                    TransitionChapterCard("End of manga", mangaTitle, primary = true)406                }407                Spacer(Modifier.height(20.dp))408                Text(409                    text = if (nextChapter != null) {410                        "Continue to the next chapter"411                    } else {412                        "Return to the chapter list"413                    },414                    color = MaterialTheme.colorScheme.onSurfaceVariant,415                    textAlign = TextAlign.Center,416                    style = MaterialTheme.typography.bodyMedium,417                )418            }419        } else {420            Column(421                modifier = Modifier.fillMaxSize().padding(24.dp),422                horizontalAlignment = Alignment.CenterHorizontally,423                verticalArrangement = Arrangement.Center,424            ) {425                TransitionHeading()426                Spacer(Modifier.height(20.dp))427                Row(428                    modifier = Modifier.fillMaxWidth().widthIn(max = 600.dp),429                    verticalAlignment = Alignment.CenterVertically,430                    horizontalArrangement = Arrangement.spacedBy(12.dp),431                ) {432                    val currentCard: @Composable () -> Unit = {433                        TransitionChapterCard("Chapter completed", currentChapter.name, primary = false)434                    }435                    val nextCard: @Composable () -> Unit = {436                        if (nextChapter != null) {437                            TransitionChapterCard("Next chapter", nextChapter.name, primary = true)438                        } else {439                            TransitionChapterCard("End of manga", mangaTitle, primary = true)440                        }441                    }442                    if (isRightToLeft) {443                        Box(Modifier.weight(1f)) { nextCard() }444                        Icon(Icons.AutoMirrored.Filled.NavigateNext, contentDescription = null)445                        Box(Modifier.weight(1f)) { currentCard() }446                    } else {447                        Box(Modifier.weight(1f)) { currentCard() }448                        Icon(Icons.AutoMirrored.Filled.NavigateNext, contentDescription = null)449                        Box(Modifier.weight(1f)) { nextCard() }450                    }451                }452                Spacer(Modifier.height(16.dp))453                Text(454                    text = if (nextChapter != null) {455                        "Continue to the next chapter"456                    } else {457                        "Return to the chapter list"458                    },459                    color = MaterialTheme.colorScheme.onSurfaceVariant,460                    textAlign = TextAlign.Center,461                    style = MaterialTheme.typography.bodyMedium,462                )463            }464        }465    }466}467468@Composable469private fun TransitionHeading() {470    Icon(471        Icons.AutoMirrored.Filled.MenuBook,472        contentDescription = null,473        modifier = Modifier.size(48.dp),474        tint = MaterialTheme.colorScheme.primary,475    )476    Spacer(Modifier.height(20.dp))477    Text(478        text = "End of chapter",479        style = MaterialTheme.typography.headlineSmall,480        fontWeight = FontWeight.Bold,481        textAlign = TextAlign.Center,482    )483}484485@Composable486private fun TransitionChapterCard(487    label: String,488    chapterName: String,489    primary: Boolean,490) {491    Surface(492        modifier = Modifier.fillMaxWidth(),493        shape = RoundedCornerShape(12.dp),494        color = if (primary) {495            MaterialTheme.colorScheme.primaryContainer496        } else {497            MaterialTheme.colorScheme.surfaceVariant498        },499        tonalElevation = if (primary) 2.dp else 0.dp,500    ) {501        Column(Modifier.padding(16.dp)) {502            Text(503                label,504                style = MaterialTheme.typography.labelMedium,505                color = if (primary) {506                    MaterialTheme.colorScheme.onPrimaryContainer.copy(alpha = 0.8f)507                } else {508                    MaterialTheme.colorScheme.onSurfaceVariant509                },510            )511            Spacer(Modifier.height(4.dp))512            Text(513                chapterName,514                style = MaterialTheme.typography.titleMedium,515                color = if (primary) {516                    MaterialTheme.colorScheme.onPrimaryContainer517                } else {518                    MaterialTheme.colorScheme.onSurface519                },520                maxLines = 2,521                overflow = TextOverflow.Ellipsis,522            )523        }524    }525}526

Search chapters, reference and source filenames.