132 lines
3.7 KiB
Kotlin
Raw Normal View History

2022-04-24 10:22:22 -04:00
package eu.kanade.presentation.components
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.RowScope
2022-04-24 10:22:22 -04:00
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
2022-04-24 10:22:22 -04:00
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SmallTopAppBar
2022-04-24 10:22:22 -04:00
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
2022-04-24 10:22:22 -04:00
import androidx.compose.ui.text.style.TextOverflow
import eu.kanade.tachiyomi.R
@Composable
fun AppBar(
title: String?,
subtitle: String? = null,
navigateUp: (() -> Unit)? = null,
navigationIcon: ImageVector = Icons.Default.ArrowBack,
actions: @Composable RowScope.() -> Unit = {},
) {
SmallTopAppBar(
navigationIcon = {
navigateUp?.let {
IconButton(onClick = it) {
Icon(
imageVector = navigationIcon,
contentDescription = stringResource(R.string.abc_action_bar_up_description),
)
}
}
},
title = {
AppBarTitle(title, subtitle)
// TODO: incognito/downloaded only banners
},
actions = actions,
)
}
2022-04-24 10:22:22 -04:00
@Composable
fun AppBarTitle(
title: String?,
subtitle: String? = null,
) {
Column {
title?.let {
Text(
text = it,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}
subtitle?.let {
Text(
text = it,
2022-04-24 14:39:51 -04:00
style = MaterialTheme.typography.bodyMedium,
2022-04-24 10:22:22 -04:00
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
}
}
}
@Composable
fun AppBarActions(
actions: List<AppBar.AppBarAction>,
) {
var showMenu by remember { mutableStateOf(false) }
actions.filterIsInstance<AppBar.Action>().map {
IconButton(
onClick = it.onClick,
2022-04-24 14:39:51 -04:00
enabled = it.enabled,
2022-04-24 10:22:22 -04:00
) {
Icon(
imageVector = it.icon,
contentDescription = it.title,
)
}
}
val overflowActions = actions.filterIsInstance<AppBar.OverflowAction>()
if (overflowActions.isNotEmpty()) {
IconButton(onClick = { showMenu = !showMenu }) {
Icon(Icons.Default.MoreVert, contentDescription = stringResource(R.string.label_more))
}
DropdownMenu(
expanded = showMenu,
2022-04-24 14:39:51 -04:00
onDismissRequest = { showMenu = false },
2022-04-24 10:22:22 -04:00
) {
overflowActions.map {
DropdownMenuItem(
onClick = {
it.onClick()
showMenu = false
},
text = { Text(it.title, fontWeight = FontWeight.Normal) },
2022-04-24 10:22:22 -04:00
)
}
}
}
}
sealed interface AppBar {
sealed interface AppBarAction
2022-04-24 10:22:22 -04:00
data class Action(
val title: String,
val icon: ImageVector,
val onClick: () -> Unit,
2022-04-24 14:39:51 -04:00
val enabled: Boolean = true,
2022-04-24 10:22:22 -04:00
) : AppBarAction
data class OverflowAction(
val title: String,
val onClick: () -> Unit,
) : AppBarAction
}