make the system bar fit the fullscreen color

This commit is contained in:
Christian Schabesberger 2024-07-29 16:34:33 +02:00
parent a64faae788
commit 491ecc4331
2 changed files with 41 additions and 11 deletions

View File

@ -20,6 +20,7 @@
package net.newpipe.newplayer.ui package net.newpipe.newplayer.ui
import android.app.Activity
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
import android.view.SurfaceView import android.view.SurfaceView
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
@ -27,9 +28,11 @@ import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@ -40,8 +43,12 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver import androidx.lifecycle.LifecycleEventObserver
import androidx.media3.common.Player import androidx.media3.common.Player
@ -65,10 +72,35 @@ fun VideoPlayerUI(
mutableStateOf(Lifecycle.Event.ON_CREATE) mutableStateOf(Lifecycle.Event.ON_CREATE)
} }
val context = LocalContext.current val activity = LocalContext.current as Activity
val view = LocalView.current
val window = activity.window
val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
val lifecycleOwner = LocalLifecycleOwner.current val lifecycleOwner = LocalLifecycleOwner.current
// Setup fullscreen
if (uiState.fullscreen) {
LaunchedEffect(key1 = true) {
WindowCompat.getInsetsController(window, view)
.isAppearanceLightStatusBars = false
}
}
// Setup immersive mode
if (uiState.fullscreen && ! uiState.uiVissible) {
LaunchedEffect(key1 = true) {
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
}
} else {
LaunchedEffect(key1 = false) {
windowInsetsController.show(WindowInsetsCompat.Type.systemBars())
}
}
// Prepare stuff for the SurfaceView to which the video will be rendered // Prepare stuff for the SurfaceView to which the video will be rendered
DisposableEffect(lifecycleOwner) { DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event -> val observer = LifecycleEventObserver { _, event ->
@ -81,7 +113,6 @@ fun VideoPlayerUI(
} }
} }
// Set Screen Rotation // Set Screen Rotation
if (uiState.fullscreen) { if (uiState.fullscreen) {
if (uiState.contentRatio < 1) { if (uiState.contentRatio < 1) {
@ -91,7 +122,7 @@ fun VideoPlayerUI(
} }
} }
val displayMetrics = context.resources.displayMetrics val displayMetrics = activity.resources.displayMetrics
val screenRatio = val screenRatio =
displayMetrics.widthPixels.toFloat() / displayMetrics.heightPixels.toFloat() displayMetrics.widthPixels.toFloat() / displayMetrics.heightPixels.toFloat()

View File

@ -67,10 +67,9 @@ class MainActivity : AppCompatActivity() {
//videoPlayerViewModel.maxContentRatio = 4F/3F //videoPlayerViewModel.maxContentRatio = 4F/3F
videoPlayerViewModel.contentFitMode = ContentScale.FIT_INSIDE videoPlayerViewModel.contentFitMode = ContentScale.FIT_INSIDE
//val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView) //windowInsetsController.systemBarsBehavior =
windowInsetsController.systemBarsBehavior = //WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
val setupFullscreen = { val setupFullscreen = {
@ -84,7 +83,7 @@ class MainActivity : AppCompatActivity() {
embeddedPlayer.viewModel = null embeddedPlayer.viewModel = null
fullscreenPlayer.viewModel = videoPlayerViewModel fullscreenPlayer.viewModel = videoPlayerViewModel
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()) //windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
} }
val setupEmbeddedView = { val setupEmbeddedView = {
@ -107,7 +106,7 @@ class MainActivity : AppCompatActivity() {
fullscreenPlayer.viewModel = null fullscreenPlayer.viewModel = null
embeddedPlayer.viewModel = videoPlayerViewModel embeddedPlayer.viewModel = videoPlayerViewModel
windowInsetsController.show(WindowInsetsCompat.Type.systemBars()) //windowInsetsController.show(WindowInsetsCompat.Type.systemBars())
} }
if (videoPlayerViewModel.uiState.value.fullscreen) { if (videoPlayerViewModel.uiState.value.fullscreen) {
@ -126,9 +125,9 @@ class MainActivity : AppCompatActivity() {
override fun onUiVissibleToggle(isVissible: Boolean) { override fun onUiVissibleToggle(isVissible: Boolean) {
if (isVissible) { if (isVissible) {
windowInsetsController.show(WindowInsetsCompat.Type.systemBars()) //windowInsetsController.show(WindowInsetsCompat.Type.systemBars())
} else { } else {
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars()) //windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
} }
} }
} }