From 820ed6a46880af1e9390706dc9915f3c7d385c60 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Sat, 10 Dec 2022 22:01:16 +0700 Subject: [PATCH] Move system bar color set to the main composable (#8710) This one doesn't check navbar location before adding a scrim, doesn't really matter since now no body component is being drawn below the system bar. --- .../kanade/tachiyomi/ui/main/MainActivity.kt | 54 ++++++++++--------- .../util/system/ContextExtensions.kt | 8 +++ .../tachiyomi/util/view/WindowExtensions.kt | 13 +++-- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index fe21c80351..fee5c4286b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -16,8 +16,10 @@ import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.consumeWindowInsets import androidx.compose.foundation.layout.statusBars import androidx.compose.material3.AlertDialog +import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.material3.TextButton +import androidx.compose.material3.surfaceColorAtElevation import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue @@ -28,12 +30,11 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp import androidx.core.animation.doOnEnd import androidx.core.splashscreen.SplashScreen import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen -import androidx.core.view.ViewCompat import androidx.core.view.WindowCompat -import androidx.core.view.WindowInsetsCompat import androidx.interpolator.view.animation.FastOutSlowInInterpolator import androidx.interpolator.view.animation.LinearOutSlowInInterpolator import androidx.lifecycle.lifecycleScope @@ -68,11 +69,11 @@ import eu.kanade.tachiyomi.ui.manga.MangaScreen import eu.kanade.tachiyomi.ui.more.NewUpdateScreen import eu.kanade.tachiyomi.util.Constants import eu.kanade.tachiyomi.util.system.dpToPx +import eu.kanade.tachiyomi.util.system.isNavigationBarNeedsScrim import eu.kanade.tachiyomi.util.system.logcat import eu.kanade.tachiyomi.util.system.openInBrowser import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.view.setComposeContent -import eu.kanade.tachiyomi.util.view.setNavigationBarTransparentCompat import kotlinx.coroutines.cancel import kotlinx.coroutines.delay import kotlinx.coroutines.flow.drop @@ -84,6 +85,7 @@ import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy import kotlin.time.Duration.Companion.seconds +import androidx.compose.ui.graphics.Color.Companion as ComposeColor class MainActivity : BaseActivity() { @@ -150,13 +152,33 @@ class MainActivity : BaseActivity() { downloadedOnlyMode = download, incognitoMode = incognito, ) + + // Set statusbar color val systemUiController = rememberSystemUiController() + val isSystemInDarkTheme = isSystemInDarkTheme() val active = incognito || download - val useDarkIcons = if (isSystemInDarkTheme()) active else !active - LaunchedEffect(systemUiController, useDarkIcons) { + val useDarkStatusBarIcons = if (isSystemInDarkTheme) active else !active + LaunchedEffect(systemUiController, useDarkStatusBarIcons) { systemUiController.setStatusBarColor( - color = androidx.compose.ui.graphics.Color.Transparent, - darkIcons = useDarkIcons, + color = ComposeColor.Transparent, + darkIcons = useDarkStatusBarIcons, + transformColorForLightContent = { ComposeColor.Black }, + ) + } + + // Set navigation bar color + val context = LocalContext.current + val navbarScrimColor = MaterialTheme.colorScheme.surfaceColorAtElevation(3.dp) + LaunchedEffect(systemUiController, isSystemInDarkTheme, navbarScrimColor) { + systemUiController.setNavigationBarColor( + color = if (context.isNavigationBarNeedsScrim()) { + navbarScrimColor.copy(alpha = 0.7f) + } else { + ComposeColor.Transparent + }, + darkIcons = !isSystemInDarkTheme, + navigationBarContrastEnforced = false, + transformColorForLightContent = { ComposeColor.Black }, ) } @@ -296,20 +318,7 @@ class MainActivity : BaseActivity() { */ private fun setSplashScreenExitAnimation(splashScreen: SplashScreen?) { val root = findViewById(android.R.id.content) - val setNavbarScrim = { - // Make sure navigation bar is on bottom before we modify it - ViewCompat.setOnApplyWindowInsetsListener(root) { _, insets -> - if (insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom > 0) { - window.setNavigationBarTransparentCompat(this@MainActivity, 3.dpToPx.toFloat()) - } - insets - } - ViewCompat.requestApplyInsets(root) - } - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S && splashScreen != null) { - val oldStatusColor = window.statusBarColor - val oldNavigationColor = window.navigationBarColor window.statusBarColor = Color.TRANSPARENT window.navigationBarColor = Color.TRANSPARENT @@ -335,17 +344,12 @@ class MainActivity : BaseActivity() { } doOnEnd { splashProvider.remove() - window.statusBarColor = oldStatusColor - window.navigationBarColor = oldNavigationColor - setNavbarScrim() } } activityAnim.start() splashAnim.start() } - } else { - setNavbarScrim() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt index 77f351aeaa..29cbc68b13 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt @@ -374,3 +374,11 @@ fun Context.getApplicationIcon(pkgName: String): Drawable? { null } } + +/** + * Gets system's config_navBarNeedsScrim boolean flag added in Android 10, defaults to true. + */ +fun Context.isNavigationBarNeedsScrim(): Boolean { + return Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || + InternalResourceHelper.getBoolean(this, "config_navBarNeedsScrim", true) +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt index be387a6566..fc5a2fc1f9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/view/WindowExtensions.kt @@ -2,28 +2,27 @@ package eu.kanade.tachiyomi.util.view import android.content.Context import android.graphics.Color -import android.os.Build import android.view.Window import android.view.WindowManager import com.google.android.material.elevation.ElevationOverlayProvider -import eu.kanade.tachiyomi.util.system.InternalResourceHelper import eu.kanade.tachiyomi.util.system.getResourceColor +import eu.kanade.tachiyomi.util.system.isNavigationBarNeedsScrim /** * Sets navigation bar color to transparent if system's config_navBarNeedsScrim is false, * otherwise it will use the theme navigationBarColor with 70% opacity. + * + * @see isNavigationBarNeedsScrim */ fun Window.setNavigationBarTransparentCompat(context: Context, elevation: Float = 0F) { - navigationBarColor = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && - !InternalResourceHelper.getBoolean(context, "config_navBarNeedsScrim", true) - ) { - Color.TRANSPARENT - } else { + navigationBarColor = if (context.isNavigationBarNeedsScrim()) { // Set navbar scrim 70% of navigationBarColor ElevationOverlayProvider(context).compositeOverlayIfNeeded( context.getResourceColor(android.R.attr.navigationBarColor, 0.7F), elevation, ) + } else { + Color.TRANSPARENT } }