feat: do not use MDC-Android tabs any more

This commit is contained in:
infi 2026-03-13 23:43:27 +01:00
parent 4ba9b59127
commit 998f433144
1 changed files with 16 additions and 54 deletions

View File

@ -1,73 +1,35 @@
package chat.stoat.composables.generic
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.PrimaryTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
import chat.stoat.api.settings.LoadedSettings
import chat.stoat.ui.theme.Theme
import com.google.android.material.tabs.TabLayout
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PrimaryTabs(tabs: List<String>, currentIndex: Int, onTabSelected: (Int) -> Unit) {
when (LoadedSettings.theme) {
Theme.M3Dynamic -> AndroidView(
factory = {
TabLayout(it).apply {
tabMode = TabLayout.MODE_FIXED
tabGravity = TabLayout.GRAVITY_FILL
tabs.forEach { tab ->
addTab(newTab().setText(tab))
}
addOnTabSelectedListener(object :
TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
onTabSelected(tab?.position ?: 0)
PrimaryTabRow(selectedTabIndex = currentIndex) {
tabs.forEachIndexed { index, tab ->
Tab(
selected = index == currentIndex,
onClick = { onTabSelected(index) },
text = {
Text(
text = tab,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
color = if (index == currentIndex) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.onSurface
}
override fun onTabUnselected(tab: TabLayout.Tab?) {
}
override fun onTabReselected(tab: TabLayout.Tab?) {
}
})
)
}
},
update = {
it.getTabAt(currentIndex)?.select()
},
modifier = Modifier.fillMaxWidth()
)
else -> PrimaryTabRow(selectedTabIndex = currentIndex) {
tabs.forEachIndexed { index, tab ->
Tab(
selected = index == currentIndex,
onClick = { onTabSelected(index) },
text = {
Text(
text = tab,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
color = if (index == currentIndex) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.onSurface
}
)
}
)
}
)
}
}
}