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.
This commit is contained in:
Ivan Iskandar 2022-12-10 22:01:16 +07:00 committed by GitHub
parent 7cbe18d325
commit 820ed6a468
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 32 deletions

View File

@ -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<View>(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()
}
}

View File

@ -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)
}

View File

@ -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
}
}