Support WallPaper DynamicColor on O_MR1 above
This commit is contained in:
1 parent
b9e59ee2f8
commit
cfd248f818
1 file changed
+47
-18
@@ -1,6 +1,10 @@
|
||||
package com.hippo.ehviewer.ui.theme
|
||||
|
||||
import android.app.WallpaperManager
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.compose.foundation.scrollbar.LocalScrollbarStyle
|
||||
import androidx.compose.material3.ColorScheme
|
||||
import androidx.compose.material3.LocalContentColor
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.MotionScheme
|
||||
@@ -16,34 +20,47 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.collectAsState
|
||||
import com.hippo.ehviewer.ui.tools.scrollbarStyle
|
||||
import com.hippo.ehviewer.util.isAtLeastOMR1
|
||||
import com.hippo.ehviewer.util.isAtLeastS
|
||||
import com.materialkolor.dynamicColorScheme
|
||||
|
||||
fun ColorScheme.amoled(amoled: Boolean) = if (amoled) {
|
||||
copy(
|
||||
surface = Color.Black,
|
||||
onSurface = Color.White,
|
||||
background = Color.Black,
|
||||
onBackground = Color.White,
|
||||
)
|
||||
} else {
|
||||
this
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun EhTheme(useDarkTheme: Boolean, content: @Composable () -> Unit) {
|
||||
val amoled by Settings.blackDarkTheme.collectAsState()
|
||||
val context = LocalContext.current
|
||||
val colors = if (useDarkTheme) {
|
||||
if (isAtLeastS) {
|
||||
dynamicDarkColorScheme(context)
|
||||
val colors = if (isAtLeastS) {
|
||||
if (useDarkTheme) {
|
||||
dynamicDarkColorScheme(context).amoled(amoled)
|
||||
} else {
|
||||
darkColorScheme()
|
||||
}.let {
|
||||
if (amoled) {
|
||||
it.copy(
|
||||
surface = Color.Black,
|
||||
onSurface = Color.White,
|
||||
background = Color.Black,
|
||||
onBackground = Color.White,
|
||||
)
|
||||
} else {
|
||||
it
|
||||
}
|
||||
dynamicLightColorScheme(context)
|
||||
}
|
||||
} else {
|
||||
if (isAtLeastS) {
|
||||
dynamicLightColorScheme(context)
|
||||
val color = if (isAtLeastOMR1) extractWallPaperPalette() else null
|
||||
if (color != null) {
|
||||
dynamicColorScheme(
|
||||
primary = color.first,
|
||||
isDark = useDarkTheme,
|
||||
isAmoled = amoled,
|
||||
secondary = color.second,
|
||||
tertiary = color.third,
|
||||
)
|
||||
} else {
|
||||
expressiveLightColorScheme()
|
||||
if (useDarkTheme) {
|
||||
darkColorScheme().amoled(amoled)
|
||||
} else {
|
||||
expressiveLightColorScheme()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +74,18 @@ fun EhTheme(useDarkTheme: Boolean, content: @Composable () -> Unit) {
|
||||
}
|
||||
}
|
||||
|
||||
typealias WallPaperPalette = Triple<Color, Color?, Color?>
|
||||
|
||||
@Composable
|
||||
@RequiresApi(Build.VERSION_CODES.O_MR1)
|
||||
fun extractWallPaperPalette(): WallPaperPalette? {
|
||||
val colors = WallpaperManager.getInstance(LocalContext.current).getWallpaperColors(WallpaperManager.FLAG_SYSTEM) ?: return null
|
||||
val primary = colors.primaryColor.toArgb().let { Color(it) }
|
||||
val secondary = colors.secondaryColor?.toArgb()?.let { Color(it) }
|
||||
val tertiary = colors.tertiaryColor?.toArgb()?.let { Color(it) }
|
||||
return WallPaperPalette(primary, secondary, tertiary)
|
||||
}
|
||||
|
||||
// https://issuetracker.google.com/363892346
|
||||
object CustomMotionScheme : MotionScheme by MotionScheme.expressive() {
|
||||
override fun <T> defaultSpatialSpec() = defaultEffectsSpec<T>()
|
||||
|
||||
Reference in new issue
Block a user