Skip to content

Commit df3f7f5

Browse files
committed
Highlight selected item in TremotesfComboBox
1 parent 767c042 commit df3f7f5

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

app/src/main/kotlin/org/equeim/tremotesf/ui/components/TremotesfComboBox.kt

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@
55
package org.equeim.tremotesf.ui.components
66

77
import androidx.annotation.StringRes
8+
import androidx.compose.foundation.background
89
import androidx.compose.foundation.layout.IntrinsicSize
910
import androidx.compose.foundation.layout.fillMaxWidth
1011
import androidx.compose.foundation.layout.width
12+
import androidx.compose.material.icons.Icons
13+
import androidx.compose.material.icons.filled.Check
1114
import androidx.compose.material3.DropdownMenuItem
1215
import androidx.compose.material3.ExperimentalMaterial3Api
1316
import androidx.compose.material3.ExposedDropdownMenuAnchorType
1417
import androidx.compose.material3.ExposedDropdownMenuBox
1518
import androidx.compose.material3.ExposedDropdownMenuDefaults
19+
import androidx.compose.material3.Icon
20+
import androidx.compose.material3.MaterialTheme
21+
import androidx.compose.material3.MenuDefaults
1622
import androidx.compose.material3.OutlinedTextField
1723
import androidx.compose.material3.Text
1824
import androidx.compose.runtime.Composable
@@ -22,6 +28,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
2228
import androidx.compose.runtime.setValue
2329
import androidx.compose.ui.Modifier
2430
import androidx.compose.ui.res.stringResource
31+
import org.equeim.tremotesf.R
2532

2633
@OptIn(ExperimentalMaterial3Api::class)
2734
@Composable
@@ -44,7 +51,9 @@ fun <T> TremotesfComboBox(
4451
OutlinedTextField(
4552
value = itemDisplayString(currentItem()),
4653
onValueChange = {},
47-
modifier = Modifier.fillMaxWidth().menuAnchor(ExposedDropdownMenuAnchorType.PrimaryNotEditable, enabled),
54+
modifier = Modifier
55+
.fillMaxWidth()
56+
.menuAnchor(ExposedDropdownMenuAnchorType.PrimaryNotEditable, enabled),
4857
enabled = enabled,
4958
readOnly = true,
5059
label = label.takeIf { it != 0 }?.let { { Text(stringResource(label)) } },
@@ -53,16 +62,52 @@ fun <T> TremotesfComboBox(
5362
)
5463
ExposedDropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {
5564
for (item in items) {
65+
val selected = (item == currentItem())
5666
DropdownMenuItem(
5767
text = { Text(itemDisplayString(item)) },
58-
leadingIcon = leadingIcon?.let { { it(item) } },
68+
leadingIcon = when {
69+
leadingIcon != null -> {
70+
{ leadingIcon(item) }
71+
}
72+
73+
selected -> {
74+
{ SelectedIcon() }
75+
}
76+
77+
else -> null
78+
},
79+
trailingIcon = if (selected && leadingIcon != null) {
80+
{ SelectedIcon() }
81+
} else {
82+
null
83+
},
5984
onClick = {
6085
updateCurrentItem(item)
6186
expanded = false
6287
},
6388
contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding,
89+
colors = if (selected) {
90+
val contentColor = MaterialTheme.colorScheme.onSecondaryContainer
91+
MenuDefaults.itemColors(
92+
textColor = contentColor,
93+
leadingIconColor = contentColor,
94+
trailingIconColor = contentColor
95+
)
96+
} else {
97+
MenuDefaults.itemColors()
98+
},
99+
modifier = if (selected) {
100+
Modifier.background(MaterialTheme.colorScheme.secondaryContainer)
101+
} else {
102+
Modifier
103+
}
64104
)
65105
}
66106
}
67107
}
68108
}
109+
110+
@Composable
111+
private fun SelectedIcon() {
112+
Icon(Icons.Filled.Check, contentDescription = stringResource(R.string.selected))
113+
}

0 commit comments

Comments
 (0)