1+ package top.yukonga.miuix.kmp.basic
2+
3+ import androidx.compose.foundation.layout.Arrangement
4+ import androidx.compose.foundation.layout.Box
5+ import androidx.compose.foundation.layout.fillMaxHeight
6+ import androidx.compose.foundation.layout.fillMaxSize
7+ import androidx.compose.foundation.layout.fillMaxWidth
8+ import androidx.compose.foundation.layout.height
9+ import androidx.compose.foundation.layout.width
10+ import androidx.compose.foundation.lazy.LazyRow
11+ import androidx.compose.foundation.lazy.itemsIndexed
12+ import androidx.compose.foundation.lazy.rememberLazyListState
13+ import androidx.compose.runtime.Composable
14+ import androidx.compose.runtime.remember
15+ import androidx.compose.ui.Alignment
16+ import androidx.compose.ui.Modifier
17+ import androidx.compose.ui.graphics.Color
18+ import androidx.compose.ui.platform.LocalDensity
19+ import androidx.compose.ui.semantics.Role
20+ import androidx.compose.ui.semantics.role
21+ import androidx.compose.ui.semantics.semantics
22+ import androidx.compose.ui.unit.Dp
23+ import androidx.compose.ui.unit.dp
24+ import androidx.compose.ui.unit.times
25+ import top.yukonga.miuix.kmp.theme.MiuixTheme
26+ import top.yukonga.miuix.kmp.utils.SmoothRoundedCornerShape
27+ import top.yukonga.miuix.kmp.utils.getWindowSize
28+
29+
30+ /* *
31+ * A [TabRow] with Miuix style.
32+ *
33+ * @param tabs The text to be displayed in the [TabRow].
34+ * @param selectedTabIndex The selected tab index of the [TabRow]
35+ * @param modifier The modifier to be applied to the [TabRow].
36+ * @param backgroundColor The background color of the tab in [TabRow].
37+ * @param contentColor The text color of the tab in [TabRow].
38+ * @param selectedBackgroundColor The background color of the selected tab in [TabRow].
39+ * @param selectedColor The text color of the selected tab in [TabRow].
40+ * @param cornerRadius The round corner radius of the tab in [TabRow].
41+ * @param onSelect The callback when the tab of the [TabRow] is clicked.
42+ */
43+ @Composable
44+ fun TabRow (
45+ tabs : List <String >,
46+ selectedTabIndex : Int ,
47+ modifier : Modifier = Modifier ,
48+ backgroundColor : Color = MiuixTheme .colorScheme.background,
49+ contentColor : Color = MiuixTheme .colorScheme.onSurfaceVariantSummary,
50+ selectedBackgroundColor : Color = MiuixTheme .colorScheme.surface,
51+ selectedColor : Color = MiuixTheme .colorScheme.onSurface,
52+ cornerRadius : Dp = 8.dp,
53+ onSelect : ((Int ) -> Unit )? = null,
54+ ) {
55+ val listState = rememberLazyListState()
56+ val windowSize = getWindowSize()
57+ val density = LocalDensity .current
58+ var tabWidth: Dp
59+ with (density) {
60+ tabWidth =
61+ ((windowSize.width.toDp() - (tabs.size - 1 ) * 9 .dp) / tabs.size).coerceAtLeast(62 .dp)
62+ }
63+
64+ val shape = remember { SmoothRoundedCornerShape (cornerRadius) }
65+
66+ LazyRow (
67+ state = listState,
68+ modifier = modifier
69+ .fillMaxWidth().height(42 .dp),
70+ verticalAlignment = Alignment .CenterVertically ,
71+ horizontalArrangement = Arrangement .spacedBy(9 .dp)
72+ ) {
73+ itemsIndexed(tabs) { index, tabText ->
74+ Surface (
75+ shape = shape,
76+ onClick = {
77+ onSelect?.invoke(index)
78+ },
79+ enabled = onSelect != null ,
80+ color =
81+ if (selectedTabIndex == index) selectedBackgroundColor
82+ else backgroundColor,
83+ modifier = Modifier .fillMaxHeight()
84+ .width(tabWidth).semantics { role = Role .Tab }
85+ ) {
86+ Box (
87+ modifier = Modifier .fillMaxSize(),
88+ contentAlignment = Alignment .Center
89+ ) {
90+ Text (
91+ text = tabText,
92+ color = if (selectedTabIndex == index) selectedColor else contentColor,
93+ maxLines = 1
94+ )
95+ }
96+ }
97+ }
98+ }
99+
100+ }
0 commit comments