ChoiceDialog.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/components/ChoiceDialog.kt
1package eu.friedcutlet.yokan.ui.components23import androidx.compose.foundation.clickable4import androidx.compose.foundation.layout.Arrangement5import androidx.compose.foundation.layout.Row6import androidx.compose.foundation.layout.Spacer7import androidx.compose.foundation.layout.fillMaxWidth8import androidx.compose.foundation.layout.heightIn9import androidx.compose.foundation.layout.padding10import androidx.compose.foundation.layout.width11import androidx.compose.foundation.lazy.LazyColumn12import androidx.compose.foundation.lazy.rememberLazyListState13import androidx.compose.material3.AlertDialog14import androidx.compose.material3.MaterialTheme15import androidx.compose.material3.RadioButton16import androidx.compose.material3.Surface17import androidx.compose.material3.Text18import androidx.compose.material3.TextButton19import androidx.compose.runtime.Composable20import androidx.compose.ui.Alignment21import androidx.compose.ui.Modifier22import androidx.compose.ui.draw.clip23import androidx.compose.ui.graphics.Color24import androidx.compose.ui.text.font.FontWeight25import androidx.compose.ui.unit.dp26import org.jetbrains.compose.resources.stringResource27import yokan.composeapp.generated.resources.Res28import yokan.composeapp.generated.resources.action_cancel2930/**31 * Modal single-choice picker. Replaces the "tap to cycle" pattern from the early settings tiles32 * (where each click rotated through values) with a dialog that shows every option with a33 * RadioButton, matching how Aniyomi / Mihon expose enum settings.34 */35@Composable36fun <T> ChoiceDialog(37 title: String,38 options: List<T>,39 selected: T?,40 label: (T) -> String,41 onSelect: (T) -> Unit,42 onDismiss: () -> Unit,43) {44 AlertDialog(45 onDismissRequest = onDismiss,46 modifier = Modifier.compactDialogWidth(),47 // MUI's `<Select>` overlay reads as a single rounded card with each48 // option as its own pill-shaped row — pick state tints the row instead49 // of relying on the bare radio control.50 shape = MaterialTheme.shapes.extraLarge,51 title = {52 Text(title, style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)53 },54 text = {55 // Wrapping the option list in a LazyColumn + ScrollbarBox lets56 // long pickers (font family, audio language, date format, etc.)57 // scroll instead of overflowing the dialog. The trailing scrollbar58 // makes the overflow discoverable. Bounded height keeps short59 // pickers (2-3 options) from claiming the whole screen.60 val listState = rememberLazyListState()61 // Important: heightIn lives on the LazyColumn itself, not on the62 // surrounding Box. If we put the height constraint on the Box and63 // let the LazyColumn use matchParentSize(), the Box has no64 // measurable children and collapses to 0 height — the dialog65 // renders with no visible options. Letting the LazyColumn size to66 // its own content (capped at 480.dp) and the Box wrap that keeps67 // short pickers compact AND keeps long pickers scrollable.68 ScrollbarBox(69 state = listState,70 modifier = Modifier.fillMaxWidth(),71 ) {72 LazyColumn(73 state = listState,74 modifier = Modifier75 .fillMaxWidth()76 .heightIn(max = 360.dp),77 verticalArrangement = Arrangement.spacedBy(4.dp),78 ) {79 items(options.size, key = { options[it]?.hashCode() ?: it }) { index ->80 val option = options[index]81 val isSelected = selected == option82 Surface(83 modifier = Modifier84 .fillMaxWidth()85 .clip(MaterialTheme.shapes.large)86 .clickable {87 onSelect(option)88 onDismiss()89 },90 shape = MaterialTheme.shapes.large,91 color = if (isSelected) MaterialTheme.colorScheme.primaryContainer.copy(alpha = 0.55f) else Color.Transparent,92 ) {93 Row(94 modifier = Modifier.padding(horizontal = 8.dp, vertical = 8.dp),95 verticalAlignment = Alignment.CenterVertically,96 ) {97 RadioButton(98 selected = isSelected,99 onClick = {100 onSelect(option)101 onDismiss()102 },103 )104 Spacer(Modifier.width(8.dp))105 Text(106 label(option),107 modifier = Modifier.weight(1f),108 style = MaterialTheme.typography.bodyLarge,109 color = if (isSelected) MaterialTheme.colorScheme.onPrimaryContainer else MaterialTheme.colorScheme.onSurface,110 fontWeight = if (isSelected) FontWeight.Medium else FontWeight.Normal,111 )112 }113 }114 }115 }116 }117 },118 confirmButton = {},119 dismissButton = {120 TextButton(onClick = onDismiss) { Text(stringResource(Res.string.action_cancel)) }121 },122 )123}124