Skip to content

Commit

Permalink
Use fun setWindowBarColor to set either status or navBar colors
Browse files Browse the repository at this point in the history
  • Loading branch information
nbradbury committed Jan 21, 2025
1 parent 42e2169 commit 85f9f01
Showing 1 changed file with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,48 @@ fun Window.setEdgeToEdgeContentDisplay(isEnabled: Boolean) {
WindowCompat.setDecorFitsSystemWindows(this, decorFitsSystemWindows)
}

@Suppress("DEPRECATION")
fun Window.setWindowStatusBarColor(color: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
// Android 15+
decorView.setOnApplyWindowInsetsListener { view, insets ->
val statusBarInsets = insets.getInsets(WindowInsets.Type.statusBars())
view.setBackgroundColor(color)
// Adjust padding to avoid overlap
view.setPadding(0, statusBarInsets.top, 0, 0)
insets
}
} else {
// Android 14-
statusBarColor = color
val windowInsetsController = WindowInsetsControllerCompat(this, decorView)
windowInsetsController.isAppearanceLightStatusBars = ColorUtils.isColorLight(statusBarColor)
setWindowBarColor(color, InsetsType.STATUS_BAR)
}

// we need to set the light navigation appearance here because, for some reason, changing the status bar also
// changes the navigation bar appearance but this method is supposed to only change the status bar
windowInsetsController.isAppearanceLightNavigationBars = ColorUtils.isColorLight(navigationBarColor)
}
fun Window.setWindowNavigationBarColor(color: Int) {
setWindowBarColor(color, InsetsType.NAVIGATION_BAR)
}

/**
* Sets the status bar or navigation bar color
* TODO Setting both the status bar color and navigation bar color causes the insets to be set twice
*/
@Suppress("DEPRECATION")
fun Window.setWindowNavigationBarColor(color: Int) {
private fun Window.setWindowBarColor(color: Int, insetsType: InsetsType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
// Android 15+
decorView.setOnApplyWindowInsetsListener { view, insets ->
view.setBackgroundColor(color)
val barInsets = insets.getInsets(
when (insetsType) {
InsetsType.STATUS_BAR -> WindowInsets.Type.statusBars()
InsetsType.NAVIGATION_BAR -> WindowInsets.Type.navigationBars()
}
)
// Adjust padding to avoid overlap
val navBarInsets = insets.getInsets(WindowInsets.Type.navigationBars())
view.setPadding(0, navBarInsets.top, 0, 0)
view.setPadding(0, barInsets.top, 0, 0)
insets
}
} else {
// Android 14-
when(insetsType) {
InsetsType.STATUS_BAR -> statusBarColor = color
InsetsType.NAVIGATION_BAR -> navigationBarColor = color
}
val windowInsetsController = WindowInsetsControllerCompat(this, decorView)
navigationBarColor = color
if (insetsType == InsetsType.STATUS_BAR) {
windowInsetsController.isAppearanceLightStatusBars = ColorUtils.isColorLight(statusBarColor)
}
windowInsetsController.isAppearanceLightNavigationBars = ColorUtils.isColorLight(navigationBarColor)
}
}

private enum class InsetsType {
STATUS_BAR,
NAVIGATION_BAR
}

0 comments on commit 85f9f01

Please sign in to comment.