DropdownMenu.kt 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package eu.kanade.presentation.components
  2. import androidx.compose.foundation.layout.ColumnScope
  3. import androidx.compose.foundation.layout.sizeIn
  4. import androidx.compose.material.icons.Icons
  5. import androidx.compose.material.icons.automirrored.outlined.ArrowRight
  6. import androidx.compose.material.icons.outlined.RadioButtonChecked
  7. import androidx.compose.material.icons.outlined.RadioButtonUnchecked
  8. import androidx.compose.material3.DropdownMenuItem
  9. import androidx.compose.material3.Icon
  10. import androidx.compose.material3.MaterialTheme
  11. import androidx.compose.runtime.Composable
  12. import androidx.compose.runtime.getValue
  13. import androidx.compose.runtime.mutableStateOf
  14. import androidx.compose.runtime.remember
  15. import androidx.compose.runtime.setValue
  16. import androidx.compose.ui.Modifier
  17. import androidx.compose.ui.unit.DpOffset
  18. import androidx.compose.ui.unit.dp
  19. import androidx.compose.ui.window.PopupProperties
  20. import tachiyomi.i18n.MR
  21. import tachiyomi.presentation.core.i18n.stringResource
  22. import androidx.compose.material3.DropdownMenu as ComposeDropdownMenu
  23. @Composable
  24. fun DropdownMenu(
  25. expanded: Boolean,
  26. onDismissRequest: () -> Unit,
  27. modifier: Modifier = Modifier,
  28. offset: DpOffset = DpOffset(8.dp, (-56).dp),
  29. properties: PopupProperties = PopupProperties(focusable = true),
  30. content: @Composable ColumnScope.() -> Unit,
  31. ) {
  32. ComposeDropdownMenu(
  33. expanded = expanded,
  34. onDismissRequest = onDismissRequest,
  35. modifier = modifier.sizeIn(minWidth = 196.dp, maxWidth = 196.dp),
  36. offset = offset,
  37. properties = properties,
  38. content = content,
  39. )
  40. }
  41. @Composable
  42. fun RadioMenuItem(
  43. text: @Composable () -> Unit,
  44. isChecked: Boolean,
  45. onClick: () -> Unit,
  46. ) {
  47. DropdownMenuItem(
  48. text = text,
  49. onClick = onClick,
  50. trailingIcon = {
  51. if (isChecked) {
  52. Icon(
  53. imageVector = Icons.Outlined.RadioButtonChecked,
  54. contentDescription = stringResource(MR.strings.selected),
  55. tint = MaterialTheme.colorScheme.primary,
  56. )
  57. } else {
  58. Icon(
  59. imageVector = Icons.Outlined.RadioButtonUnchecked,
  60. contentDescription = stringResource(MR.strings.not_selected),
  61. )
  62. }
  63. },
  64. )
  65. }
  66. @Composable
  67. fun NestedMenuItem(
  68. text: @Composable () -> Unit,
  69. children: @Composable ColumnScope.(() -> Unit) -> Unit,
  70. ) {
  71. var nestedExpanded by remember { mutableStateOf(false) }
  72. val closeMenu = { nestedExpanded = false }
  73. DropdownMenuItem(
  74. text = text,
  75. onClick = { nestedExpanded = true },
  76. trailingIcon = {
  77. Icon(
  78. imageVector = Icons.AutoMirrored.Outlined.ArrowRight,
  79. contentDescription = null,
  80. )
  81. },
  82. )
  83. DropdownMenu(
  84. expanded = nestedExpanded,
  85. onDismissRequest = closeMenu,
  86. ) {
  87. children(closeMenu)
  88. }
  89. }