Migrate TopBar to material3
This commit is contained in:
1 parent
80f35961f0
commit
7fec65f776
2 files changed
+38
-68
No files matched your search
@@ -71,7 +71,6 @@ internal fun M3ColorScheme.surfaceColorAtElevation(
|
||||
return primary.copy(alpha = alpha).compositeOver(surface)
|
||||
}
|
||||
|
||||
|
||||
@Composable
|
||||
fun materialColors(colorScheme: M3ColorScheme, isDark: Boolean) = remember(colorScheme, isDark) {
|
||||
Colors(
|
||||
|
||||
@@ -18,28 +18,21 @@ package app.lawnchair.ui.preferences.components
|
||||
|
||||
import androidx.activity.compose.LocalOnBackPressedDispatcherOwner
|
||||
import androidx.compose.animation.ExperimentalAnimationApi
|
||||
import androidx.compose.animation.animateColorAsState
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.material.*
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.ArrowBack
|
||||
import androidx.compose.material.icons.rounded.ArrowForward
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.LocalLayoutDirection
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.LayoutDirection
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.google.accompanist.insets.navigationBarsPadding
|
||||
import com.google.accompanist.insets.statusBarsPadding
|
||||
|
||||
@ExperimentalAnimationApi
|
||||
@@ -51,76 +44,54 @@ fun TopBar(
|
||||
actions: @Composable RowScope.() -> Unit = {},
|
||||
) {
|
||||
val backDispatcher = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher
|
||||
val scrollFraction = if (floating) 1f else 0f
|
||||
|
||||
TopBarSurface(floating = floating) {
|
||||
CompositionLocalProvider(LocalContentColor provides MaterialTheme.colors.onSurface) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(topBarSize)
|
||||
.padding(horizontal = 8.dp)
|
||||
) {
|
||||
if (backArrowVisible) {
|
||||
val colors = TopAppBarDefaults.smallTopAppBarColors()
|
||||
val appBarContainerColor by colors.containerColor(scrollFraction)
|
||||
val navigationIconContentColor = colors.navigationIconContentColor(scrollFraction)
|
||||
val titleContentColor = colors.titleContentColor(scrollFraction)
|
||||
val actionIconContentColor = colors.actionIconContentColor(scrollFraction)
|
||||
|
||||
Surface(color = appBarContainerColor) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier
|
||||
.statusBarsPadding()
|
||||
.fillMaxWidth()
|
||||
.height(topBarSize)
|
||||
.padding(horizontal = 8.dp)
|
||||
) {
|
||||
if (backArrowVisible) {
|
||||
CompositionLocalProvider(LocalContentColor provides navigationIconContentColor.value) {
|
||||
ClickableIcon(
|
||||
imageVector = backIcon(),
|
||||
onClick = { backDispatcher?.onBackPressed() }
|
||||
)
|
||||
}
|
||||
Text(
|
||||
text = label,
|
||||
style = MaterialTheme.typography.h6,
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 8.dp)
|
||||
.weight(1f),
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis
|
||||
)
|
||||
}
|
||||
Text(
|
||||
text = label,
|
||||
style = MaterialTheme.typography.titleLarge,
|
||||
color = titleContentColor.value,
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 8.dp)
|
||||
.weight(1f),
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis
|
||||
)
|
||||
|
||||
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
|
||||
Row(
|
||||
Modifier.fillMaxHeight(),
|
||||
horizontalArrangement = Arrangement.End,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
content = actions
|
||||
)
|
||||
}
|
||||
CompositionLocalProvider(LocalContentColor provides actionIconContentColor.value) {
|
||||
Row(
|
||||
Modifier.fillMaxHeight(),
|
||||
horizontalArrangement = Arrangement.End,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
content = actions
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun TopBarSurface(floating: Boolean, content: @Composable () -> Unit) {
|
||||
val (normalColor, floatingColor) = topBarColors()
|
||||
val color by animateColorAsState(targetValue = if (floating) floatingColor else normalColor)
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.navigationBarsPadding(bottom = false)
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.background(color)
|
||||
.statusBarsPadding()
|
||||
.pointerInput(remember { MutableInteractionSource() }) {
|
||||
// consume touch
|
||||
}
|
||||
) {
|
||||
content()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun topBarColors(): Pair<Color, Color> {
|
||||
val elevationOverlay = LocalElevationOverlay.current
|
||||
val backgroundColor = MaterialTheme.colors.background
|
||||
val surfaceColor = MaterialTheme.colors.surface
|
||||
val floatingColor = elevationOverlay?.apply(surfaceColor, 4.dp) ?: surfaceColor
|
||||
return Pair(backgroundColor, floatingColor)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun backIcon(): ImageVector =
|
||||
if (LocalLayoutDirection.current == LayoutDirection.Ltr) {
|
||||
@@ -129,4 +100,4 @@ fun backIcon(): ImageVector =
|
||||
Icons.Rounded.ArrowForward
|
||||
}
|
||||
|
||||
val topBarSize = 56.dp
|
||||
val topBarSize = 64.dp
|
||||
Reference in new issue
Block a user