SettingsTiles.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/SettingsTiles.kt
1package eu.friedcutlet.yokan.ui.screens.settings23import androidx.compose.foundation.border4import androidx.compose.foundation.layout.Arrangement5import androidx.compose.foundation.layout.Box6import androidx.compose.foundation.layout.Column7import androidx.compose.foundation.layout.Row8import androidx.compose.foundation.layout.fillMaxSize9import androidx.compose.foundation.layout.fillMaxWidth10import androidx.compose.foundation.layout.height11import androidx.compose.foundation.layout.padding12import androidx.compose.foundation.layout.size13import androidx.compose.foundation.lazy.LazyColumn14import androidx.compose.foundation.lazy.LazyItemScope15import androidx.compose.foundation.lazy.LazyListScope16import androidx.compose.foundation.lazy.LazyListState17import androidx.compose.foundation.lazy.rememberLazyListState18import androidx.compose.foundation.shape.RoundedCornerShape19import androidx.compose.material.icons.Icons20import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight21import androidx.compose.material.icons.filled.Info22import androidx.compose.material3.Button23import androidx.compose.material3.Icon24import androidx.compose.material3.ListItem25import androidx.compose.material3.ListItemDefaults26import androidx.compose.material3.MaterialTheme27import androidx.compose.material3.Surface28import androidx.compose.material3.Switch29import androidx.compose.material3.Text30import androidx.compose.runtime.Composable31import androidx.compose.runtime.LaunchedEffect32import androidx.compose.runtime.getValue33import androidx.compose.runtime.mutableStateOf34import androidx.compose.runtime.remember35import androidx.compose.runtime.setValue36import androidx.compose.ui.Alignment37import androidx.compose.ui.Modifier38import androidx.compose.ui.graphics.Color39import androidx.compose.ui.semantics.Role40import androidx.compose.ui.semantics.contentDescription41import androidx.compose.ui.semantics.heading42import androidx.compose.ui.semantics.role43import androidx.compose.ui.semantics.semantics44import androidx.compose.ui.semantics.stateDescription45import androidx.compose.ui.text.font.FontWeight46import androidx.compose.ui.unit.dp47import androidx.compose.ui.unit.sp48import eu.friedcutlet.yokan.ui.components.ChoiceDialog49import eu.friedcutlet.yokan.ui.components.ScrollbarBox50import eu.friedcutlet.yokan.ui.navigation.SettingsPage51import eu.friedcutlet.yokan.ui.components.YokanTopBar52import eu.friedcutlet.yokan.ui.components.hoverHighlight53import org.jetbrains.compose.resources.stringResource54import yokan.composeapp.generated.resources.Res55import yokan.composeapp.generated.resources.settings_search_open_option56import eu.friedcutlet.yokan.ui.theme.YokanScaffold as Scaffold5758@Composable59internal fun SettingsScaffold(60 title: String,61 onBack: () -> Unit,62 listState: LazyListState = rememberLazyListState(),63 content: LazyListScope.() -> Unit,64) {65 Scaffold(66 topBar = {67 YokanTopBar(68 title = title,69 onBack = onBack,70 )71 },72 ) { padding ->73 ScrollbarBox(74 state = listState,75 modifier = Modifier76 .fillMaxSize()77 .padding(padding),78 ) {79 SearchableSettingsList(80 state = listState,81 modifier = Modifier.fillMaxSize(),82 content = content,83 )84 }85 }86}8788@Composable89private fun SearchableSettingsList(90 state: LazyListState,91 modifier: Modifier,92 content: LazyListScope.() -> Unit,93) {94 val target = LocalSettingsSearchTarget.current95 var targetIndex by remember(target) { mutableStateOf<Int?>(null) }96 LaunchedEffect(targetIndex, target) {97 targetIndex?.let { state.scrollToItem(it) }98 }99 Column(modifier) {100 if (target != null) {101 Text(target.title, style = MaterialTheme.typography.titleSmall,102 modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp).semantics { heading() })103 if (!target.anchored) Text(104 listOf(stringResource(Res.string.settings_search_open_option), target.description)105 .filter { it.isNotBlank() }.joinToString(". "),106 style = MaterialTheme.typography.bodySmall,107 modifier = Modifier.padding(horizontal = 16.dp, vertical = 4.dp),108 )109 }110 LazyColumn(state = state, modifier = Modifier.weight(1f).fillMaxWidth()) {111 val delegate = this112 var index = 0113 content(object : LazyListScope by delegate {114 override fun item(115 key: Any?,116 contentType: Any?,117 content: @Composable LazyItemScope.() -> Unit,118 ) {119 val selected = target != null && key == target.id120 if (selected) targetIndex = index121 index++122 delegate.item(key, contentType) {123 if (selected) {124 Box(Modifier.border(2.dp, MaterialTheme.colorScheme.primary, MaterialTheme.shapes.small)) {125 content()126 }127 } else content()128 }129 }130 })131 }132 }133}134135@Composable136internal fun YokanMenuTile(137 title: String,138 modifier: Modifier = Modifier,139 subtitle: String? = null,140 trailing: @Composable (() -> Unit)? = null,141 enabled: Boolean = true,142 onClick: () -> Unit = {},143) {144 val rowModifier = modifier145 .fillMaxWidth()146 .let { if (enabled) it.hoverHighlight(onClick = onClick) else it }147 ListItem(148 modifier = rowModifier,149 headlineContent = {150 Text(151 text = title,152 color = if (enabled) {153 MaterialTheme.colorScheme.onSurface154 } else {155 MaterialTheme.colorScheme.onSurface.copy(alpha = 0.38f)156 },157 )158 },159 supportingContent = subtitle?.let {160 {161 Text(162 text = it,163 fontSize = 11.sp,164 color = MaterialTheme.colorScheme.secondary,165 )166 }167 },168 trailingContent = trailing,169 colors = ListItemDefaults.colors(containerColor = Color.Transparent),170 )171}172173@Composable174internal fun RouteTile(175 page: SettingsPage,176 title: String,177 subtitle: String? = null,178 onOpen: (SettingsPage) -> Unit,179) {180 YokanMenuTile(181 title = title,182 subtitle = subtitle,183 trailing = { Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, contentDescription = null) },184 onClick = { onOpen(page) },185 )186}187188@Composable189internal fun SettingTile(190 title: String,191 subtitle: String?,192 enabled: Boolean = true,193 onClick: (() -> Unit)? = null,194) {195 YokanMenuTile(title = title, subtitle = subtitle, enabled = enabled, onClick = onClick ?: {})196}197198/**199 * Tile that opens a modal single-choice picker instead of cycling on tap. Replaces the200 * "tap to rotate" pattern across reader/player/sync settings so the user sees every option at once201 * (matching how Aniyomi exposes enum settings).202 */203@Composable204internal fun <T> ChoiceTile(205 title: String,206 options: List<T>,207 selected: T?,208 label: (T) -> String,209 enabled: Boolean = true,210 onSelect: (T) -> Unit,211) {212 var open by remember { mutableStateOf(false) }213 if (open) {214 ChoiceDialog(215 title = title,216 options = options,217 selected = selected,218 label = label,219 onSelect = onSelect,220 onDismiss = { open = false },221 )222 }223 val subtitle = selected?.let(label) ?: "—"224 YokanMenuTile(title = title, subtitle = subtitle, enabled = enabled, onClick = { open = true })225}226227@Composable228internal fun SwitchTile(229 title: String,230 subtitle: String?,231 value: Boolean,232 onValueChange: (Boolean) -> Unit,233 enabled: Boolean = true,234) {235 YokanMenuTile(236 modifier = Modifier.semantics(mergeDescendants = true) {237 role = Role.Switch238 stateDescription = if (value) "On" else "Off"239 contentDescription = subtitle?.let { "$title. $it" } ?: title240 },241 title = title,242 subtitle = subtitle,243 enabled = enabled,244 onClick = { if (enabled) onValueChange(!value) },245 trailing = { Switch(checked = value, onCheckedChange = onValueChange, enabled = enabled) },246 )247}248249@Composable250internal fun SettingsSection(title: String) {251 Text(252 // Uppercase + slight tracking gives section headers the "label" feel253 // used in MUI's `<Typography variant="overline">` rows.254 text = title.uppercase(),255 modifier = Modifier.padding(start = 24.dp, top = 24.dp, bottom = 6.dp),256 style = MaterialTheme.typography.labelMedium,257 color = MaterialTheme.colorScheme.primary,258 fontWeight = FontWeight.SemiBold,259 letterSpacing = 1.sp,260 )261}262263@Composable264internal fun InfoRow(text: String) {265 // MUI `<Alert severity="info">` shape — soft tonal surface, leading icon266 // tinted with the primary container palette, no chevron. Replaces the267 // previous "row that looks like a navigation item" rendering, which made268 // every information banner read as something the user should tap.269 Surface(270 modifier = Modifier271 .fillMaxWidth()272 .padding(horizontal = 16.dp, vertical = 4.dp),273 shape = MaterialTheme.shapes.large,274 color = MaterialTheme.colorScheme.surfaceContainerHigh,275 ) {276 Row(277 modifier = Modifier.padding(horizontal = 14.dp, vertical = 12.dp),278 verticalAlignment = Alignment.Top,279 horizontalArrangement = Arrangement.spacedBy(10.dp),280 ) {281 Icon(282 Icons.Filled.Info,283 contentDescription = null,284 tint = MaterialTheme.colorScheme.primary,285 modifier = Modifier.size(20.dp),286 )287 Text(288 text,289 style = MaterialTheme.typography.bodySmall,290 color = MaterialTheme.colorScheme.onSurfaceVariant,291 )292 }293 }294}295296@Composable297internal fun ButtonRow(text: String, danger: Boolean = false, onClick: () -> Unit = {}) {298 Box(modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp)) {299 Button(onClick = onClick, modifier = Modifier.fillMaxWidth().height(48.dp), shape = RoundedCornerShape(50)) {300 Text(text, color = if (danger) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onPrimary)301 }302 }303}304