package eu.friedcutlet.yokan.ui.screens.settings import androidx.compose.foundation.border 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.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.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyItemScope import androidx.compose.foundation.lazy.LazyListScope import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight import androidx.compose.material.icons.filled.Info import androidx.compose.material3.Button import androidx.compose.material3.Icon import androidx.compose.material3.ListItem import androidx.compose.material3.ListItemDefaults import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.material3.Switch import androidx.compose.material3.Text 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.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.semantics.Role import androidx.compose.ui.semantics.contentDescription import androidx.compose.ui.semantics.heading import androidx.compose.ui.semantics.role import androidx.compose.ui.semantics.semantics import androidx.compose.ui.semantics.stateDescription import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import eu.friedcutlet.yokan.ui.components.ChoiceDialog import eu.friedcutlet.yokan.ui.components.ScrollbarBox import eu.friedcutlet.yokan.ui.navigation.SettingsPage import eu.friedcutlet.yokan.ui.components.YokanTopBar import eu.friedcutlet.yokan.ui.components.hoverHighlight import org.jetbrains.compose.resources.stringResource import yokan.composeapp.generated.resources.Res import yokan.composeapp.generated.resources.settings_search_open_option import eu.friedcutlet.yokan.ui.theme.YokanScaffold as Scaffold @Composable internal fun SettingsScaffold( title: String, onBack: () -> Unit, listState: LazyListState = rememberLazyListState(), content: LazyListScope.() -> Unit, ) { Scaffold( topBar = { YokanTopBar( title = title, onBack = onBack, ) }, ) { padding -> ScrollbarBox( state = listState, modifier = Modifier .fillMaxSize() .padding(padding), ) { SearchableSettingsList( state = listState, modifier = Modifier.fillMaxSize(), content = content, ) } } } @Composable private fun SearchableSettingsList( state: LazyListState, modifier: Modifier, content: LazyListScope.() -> Unit, ) { val target = LocalSettingsSearchTarget.current var targetIndex by remember(target) { mutableStateOf(null) } LaunchedEffect(targetIndex, target) { targetIndex?.let { state.scrollToItem(it) } } Column(modifier) { if (target != null) { Text(target.title, style = MaterialTheme.typography.titleSmall, modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp).semantics { heading() }) if (!target.anchored) Text( listOf(stringResource(Res.string.settings_search_open_option), target.description) .filter { it.isNotBlank() }.joinToString(". "), style = MaterialTheme.typography.bodySmall, modifier = Modifier.padding(horizontal = 16.dp, vertical = 4.dp), ) } LazyColumn(state = state, modifier = Modifier.weight(1f).fillMaxWidth()) { val delegate = this var index = 0 content(object : LazyListScope by delegate { override fun item( key: Any?, contentType: Any?, content: @Composable LazyItemScope.() -> Unit, ) { val selected = target != null && key == target.id if (selected) targetIndex = index index++ delegate.item(key, contentType) { if (selected) { Box(Modifier.border(2.dp, MaterialTheme.colorScheme.primary, MaterialTheme.shapes.small)) { content() } } else content() } } }) } } } @Composable internal fun YokanMenuTile( title: String, modifier: Modifier = Modifier, subtitle: String? = null, trailing: @Composable (() -> Unit)? = null, enabled: Boolean = true, onClick: () -> Unit = {}, ) { val rowModifier = modifier .fillMaxWidth() .let { if (enabled) it.hoverHighlight(onClick = onClick) else it } ListItem( modifier = rowModifier, headlineContent = { Text( text = title, color = if (enabled) { MaterialTheme.colorScheme.onSurface } else { MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f) }, ) }, supportingContent = subtitle?.let { { Text( text = it, fontSize = 11.sp, color = MaterialTheme.colorScheme.secondary, ) } }, trailingContent = trailing, colors = ListItemDefaults.colors(containerColor = Color.Transparent), ) } @Composable internal fun RouteTile( page: SettingsPage, title: String, subtitle: String? = null, onOpen: (SettingsPage) -> Unit, ) { YokanMenuTile( title = title, subtitle = subtitle, trailing = { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null) }, onClick = { onOpen(page) }, ) } @Composable internal fun SettingTile( title: String, subtitle: String?, enabled: Boolean = true, onClick: (() -> Unit)? = null, ) { YokanMenuTile(title = title, subtitle = subtitle, enabled = enabled, onClick = onClick ?: {}) } /** * Tile that opens a modal single-choice picker instead of cycling on tap. Replaces the * "tap to rotate" pattern across reader/player/sync settings so the user sees every option at once * (matching how Aniyomi exposes enum settings). */ @Composable internal fun ChoiceTile( title: String, options: List, selected: T?, label: (T) -> String, enabled: Boolean = true, onSelect: (T) -> Unit, ) { var open by remember { mutableStateOf(false) } if (open) { ChoiceDialog( title = title, options = options, selected = selected, label = label, onSelect = onSelect, onDismiss = { open = false }, ) } val subtitle = selected?.let(label) ?: "—" YokanMenuTile(title = title, subtitle = subtitle, enabled = enabled, onClick = { open = true }) } @Composable internal fun SwitchTile( title: String, subtitle: String?, value: Boolean, onValueChange: (Boolean) -> Unit, enabled: Boolean = true, ) { YokanMenuTile( modifier = Modifier.semantics(mergeDescendants = true) { role = Role.Switch stateDescription = if (value) "On" else "Off" contentDescription = subtitle?.let { "$title. $it" } ?: title }, title = title, subtitle = subtitle, enabled = enabled, onClick = { if (enabled) onValueChange(!value) }, trailing = { Switch(checked = value, onCheckedChange = onValueChange, enabled = enabled) }, ) } @Composable internal fun SettingsSection(title: String) { Text( // Uppercase + slight tracking gives section headers the "label" feel // used in MUI's `` rows. text = title.uppercase(), modifier = Modifier.padding(start = 24.dp, top = 24.dp, bottom = 6.dp), style = MaterialTheme.typography.labelMedium, color = MaterialTheme.colorScheme.primary, fontWeight = FontWeight.SemiBold, letterSpacing = 1.sp, ) } @Composable internal fun InfoRow(text: String) { // MUI `` shape — soft tonal surface, leading icon // tinted with the primary container palette, no chevron. Replaces the // previous "row that looks like a navigation item" rendering, which made // every information banner read as something the user should tap. Surface( modifier = Modifier .fillMaxWidth() .padding(horizontal = 16.dp, vertical = 4.dp), shape = MaterialTheme.shapes.large, color = MaterialTheme.colorScheme.surfaceContainerHigh, ) { Row( modifier = Modifier.padding(horizontal = 14.dp, vertical = 12.dp), verticalAlignment = Alignment.Top, horizontalArrangement = Arrangement.spacedBy(10.dp), ) { Icon( Icons.Filled.Info, contentDescription = null, tint = MaterialTheme.colorScheme.primary, modifier = Modifier.size(20.dp), ) Text( text, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.onSurfaceVariant, ) } } } @Composable internal fun ButtonRow(text: String, danger: Boolean = false, onClick: () -> Unit = {}) { Box(modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp)) { Button(onClick = onClick, modifier = Modifier.fillMaxWidth().height(48.dp), shape = RoundedCornerShape(50)) { Text(text, color = if (danger) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onPrimary) } } }