refactor(ui): migrate to tone-based surfaces in MD3 (#619)

This commit is contained in:
junkfood 2024-03-10 20:34:12 +08:00 committed by GitHub
parent df239022e7
commit db65c3dca5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 58 additions and 14 deletions

View File

@ -13,6 +13,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalView import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import me.ash.reader.domain.model.general.Filter import me.ash.reader.domain.model.general.Filter
import me.ash.reader.infrastructure.preference.FlowFilterBarStylePreference import me.ash.reader.infrastructure.preference.FlowFilterBarStylePreference
import me.ash.reader.infrastructure.preference.LocalThemeIndex import me.ash.reader.infrastructure.preference.LocalThemeIndex
@ -37,10 +38,9 @@ fun FilterBar(
} onDark MaterialTheme.colorScheme.secondaryContainer } onDark MaterialTheme.colorScheme.secondaryContainer
NavigationBar( NavigationBar(
modifier = Modifier modifier = Modifier,
.background(MaterialTheme.colorScheme.surfaceColorAtElevation(filterBarTonalElevation)) containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(filterBarTonalElevation),
.navigationBarsPadding(), tonalElevation = 0.dp
tonalElevation = filterBarTonalElevation,
) { ) {
Spacer(modifier = Modifier.width(filterBarPadding)) Spacer(modifier = Modifier.width(filterBarPadding))
Filter.values.forEach { item -> Filter.values.forEach { item ->

View File

@ -24,7 +24,7 @@ fun BlockButton(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
text: String = "", text: String = "",
selected: Boolean = false, selected: Boolean = false,
containerColor: Color = MaterialTheme.colorScheme.surface.copy(0.7f) onDark MaterialTheme.colorScheme.inverseOnSurface, containerColor: Color = MaterialTheme.colorScheme.surfaceContainer onDark MaterialTheme.colorScheme.inverseOnSurface,
selectedContainerColor: Color = MaterialTheme.colorScheme.primaryContainer alwaysLight true, selectedContainerColor: Color = MaterialTheme.colorScheme.primaryContainer alwaysLight true,
contentColor: Color = MaterialTheme.colorScheme.inverseSurface, contentColor: Color = MaterialTheme.colorScheme.inverseSurface,
selectedContentColor: Color = MaterialTheme.colorScheme.onSurface alwaysLight true, selectedContentColor: Color = MaterialTheme.colorScheme.onSurface alwaysLight true,

View File

@ -45,7 +45,9 @@ fun RYScaffold(
navigationIcon = { navigationIcon?.invoke() }, navigationIcon = { navigationIcon?.invoke() },
actions = { actions?.invoke(this) }, actions = { actions?.invoke(this) },
colors = TopAppBarDefaults.smallTopAppBarColors( colors = TopAppBarDefaults.smallTopAppBarColors(
containerColor = Color.Transparent, containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(
topBarTonalElevation
),
) )
) )
} }

View File

@ -7,13 +7,36 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.compositeOver import androidx.compose.ui.graphics.compositeOver
import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import me.ash.reader.domain.model.constant.ElevationTokens
import kotlin.math.ln import kotlin.math.ln
import kotlin.math.roundToInt
@Composable
@Deprecated(
message = "Migrate to tone-based surfaces", level = DeprecationLevel.WARNING,
replaceWith = ReplaceWith(
"surfaceColorAtElevation(elevation: Dp)",
"androidx.compose.runtime.remember"
)
)
fun ColorScheme.surfaceColorAtElevation(
elevation: Dp,
color: Color = surface,
): Color = surfaceColorAtElevation(elevation = elevation)
@Composable @Composable
fun ColorScheme.surfaceColorAtElevation( fun ColorScheme.surfaceColorAtElevation(
elevation: Dp, elevation: Dp,
color: Color = surface, ): Color = remember(this, elevation) {
): Color = remember(this, elevation, color) { color.atElevation(surfaceTint, elevation) } when (elevation.value.roundToInt()) {
ElevationTokens.Level0 -> surface
ElevationTokens.Level1 -> surfaceContainerLow
ElevationTokens.Level2 -> surfaceContainer
ElevationTokens.Level3 -> surfaceContainerHigh
ElevationTokens.Level4, ElevationTokens.Level5 -> surfaceContainerHighest
else -> surface
}
}
fun Color.atElevation( fun Color.atElevation(
sourceColor: Color, sourceColor: Color,

View File

@ -25,6 +25,7 @@ import me.ash.reader.infrastructure.preference.LocalReadingPageTonalElevation
import me.ash.reader.ui.component.base.FeedbackIconButton import me.ash.reader.ui.component.base.FeedbackIconButton
import me.ash.reader.ui.component.base.RYExtensibleVisibility import me.ash.reader.ui.component.base.RYExtensibleVisibility
import me.ash.reader.ui.ext.share import me.ash.reader.ui.ext.share
import me.ash.reader.ui.ext.surfaceColorAtElevation
import me.ash.reader.ui.page.common.RouteName import me.ash.reader.ui.page.common.RouteName
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@ -83,7 +84,7 @@ fun TopBar(
) )
} }
}, colors = TopAppBarDefaults.smallTopAppBarColors( }, colors = TopAppBarDefaults.smallTopAppBarColors(
containerColor = MaterialTheme.colorScheme.surface, containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(tonalElevation.value.dp),
) )
) )
} }

View File

@ -251,7 +251,7 @@ fun Palettes(
.clip(RoundedCornerShape(16.dp)) .clip(RoundedCornerShape(16.dp))
.background( .background(
MaterialTheme.colorScheme.inverseOnSurface MaterialTheme.colorScheme.inverseOnSurface
onLight MaterialTheme.colorScheme.surface.copy(0.7f), onLight MaterialTheme.colorScheme.surfaceContainer,
) )
.clickable {}, .clickable {},
horizontalArrangement = Arrangement.Center, horizontalArrangement = Arrangement.Center,
@ -330,7 +330,7 @@ fun SelectableMiniPalette(
.copy(0.5f) onDark MaterialTheme.colorScheme.onPrimaryContainer.copy(0.3f) .copy(0.5f) onDark MaterialTheme.colorScheme.onPrimaryContainer.copy(0.3f)
} else { } else {
MaterialTheme.colorScheme MaterialTheme.colorScheme
.inverseOnSurface onLight MaterialTheme.colorScheme.surface.copy(0.7f) .inverseOnSurface onLight MaterialTheme.colorScheme.surfaceContainer
}, },
) { ) {
Surface( Surface(

View File

@ -89,7 +89,9 @@ fun FeedsPagePreview(
tint = MaterialTheme.colorScheme.onSurface, tint = MaterialTheme.colorScheme.onSurface,
) )
}, colors = TopAppBarDefaults.smallTopAppBarColors( }, colors = TopAppBarDefaults.smallTopAppBarColors(
containerColor = Color.Transparent, containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(
topBarTonalElevation.value.dp
),
) )
) )
Spacer(modifier = Modifier.height(12.dp)) Spacer(modifier = Modifier.height(12.dp))

View File

@ -77,7 +77,7 @@ fun FlowPagePreview(
tint = MaterialTheme.colorScheme.onSurface, tint = MaterialTheme.colorScheme.onSurface,
) {} ) {}
}, colors = TopAppBarDefaults.smallTopAppBarColors( }, colors = TopAppBarDefaults.smallTopAppBarColors(
containerColor = Color.Transparent, containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(topBarTonalElevation.value.dp),
) )
) )
Spacer(modifier = Modifier.height(12.dp)) Spacer(modifier = Modifier.height(12.dp))

View File

@ -30,7 +30,7 @@ fun dynamicLightColorScheme(): ColorScheme {
onTertiaryContainer = palettes tertiary 10, onTertiaryContainer = palettes tertiary 10,
background = palettes neutral 99, background = palettes neutral 99,
onBackground = palettes neutral 10, onBackground = palettes neutral 10,
surface = palettes neutral 99, surface = palettes neutral 98,
onSurface = palettes neutral 10, onSurface = palettes neutral 10,
surfaceVariant = palettes neutralVariant 90, surfaceVariant = palettes neutralVariant 90,
onSurfaceVariant = palettes neutralVariant 30, onSurfaceVariant = palettes neutralVariant 30,
@ -38,6 +38,14 @@ fun dynamicLightColorScheme(): ColorScheme {
inverseSurface = palettes neutral 20, inverseSurface = palettes neutral 20,
inverseOnSurface = palettes neutral 95, inverseOnSurface = palettes neutral 95,
outline = palettes neutralVariant 50, outline = palettes neutralVariant 50,
outlineVariant = palettes neutralVariant 80,
surfaceBright = palettes neutral 98,
surfaceDim = palettes neutral 87,
surfaceContainerLowest = palettes neutral 100,
surfaceContainerLow = palettes neutral 96,
surfaceContainer = palettes neutral 94,
surfaceContainerHigh = palettes neutral 92,
surfaceContainerHighest = palettes neutral 90,
) )
} }
@ -70,6 +78,14 @@ fun dynamicDarkColorScheme(): ColorScheme {
inverseSurface = palettes neutral 90, inverseSurface = palettes neutral 90,
inverseOnSurface = palettes neutral 20, inverseOnSurface = palettes neutral 20,
outline = palettes neutralVariant 60, outline = palettes neutralVariant 60,
outlineVariant = palettes neutralVariant 30,
surfaceBright = palettes neutral 24,
surfaceDim = palettes neutral 6,
surfaceContainerLowest = palettes neutral 4,
surfaceContainerLow = palettes neutral 10,
surfaceContainer = palettes neutral 12,
surfaceContainerHigh = palettes neutral 17,
surfaceContainerHighest = palettes neutral 22,
) )
} }