Skip to content

Commit

Permalink
use setSystemBarVisible
Browse files Browse the repository at this point in the history
  • Loading branch information
NieR4ever committed Oct 19, 2024
1 parent be30d0a commit 12dd908
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 61 deletions.
24 changes: 0 additions & 24 deletions app/android/src/main/kotlin/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@

package me.him188.ani.android.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.WindowInsets
import android.view.WindowInsetsController
import android.widget.Toast
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
Expand Down Expand Up @@ -129,24 +125,4 @@ class MainActivity : AniComponentActivity() {
}
}
}

override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val controller = window.insetsController ?: return
if (hasFocus && requestedOrientation == android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
} else {
controller.show(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_DEFAULT
} else {
@Suppress("DEPRECATION")
@SuppressLint("WrongConstant")
controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_SWIPE
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -102,6 +103,7 @@ import me.him188.ani.app.ui.foundation.isInDebugMode
import me.him188.ani.app.ui.foundation.layout.LocalPlatformWindow
import me.him188.ani.app.ui.foundation.layout.desktopTitleBarPadding
import me.him188.ani.app.ui.foundation.layout.setRequestFullScreen
import me.him188.ani.app.ui.foundation.layout.setSystemBarVisible
import me.him188.ani.app.ui.foundation.navigation.BackHandler
import me.him188.ani.app.ui.foundation.pagerTabIndicatorOffset
import me.him188.ani.app.ui.foundation.rememberImageViewerHandler
Expand Down Expand Up @@ -195,6 +197,12 @@ private fun EpisodeSceneContent(
}
}

SideEffect {
scope.launch {
context.setSystemBarVisible(!vm.isFullscreen)
}
}

BoxWithConstraints(modifier) {
val showExpandedUI =
currentWindowAdaptiveInfo().windowSizeClass.windowWidthSizeClass != WindowWidthSizeClass.COMPACT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,12 @@ actual suspend fun Context.setRequestFullScreen(window: PlatformWindowMP, fullsc
// go landscape
requestedOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

// hide bars
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
this.window.insetsController?.hide(
WindowInsets.Type.statusBars().or(WindowInsets.Type.navigationBars()),
)
this.window.insetsController?.systemBarsBehavior =
BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
} else {
val decorView = this.window.decorView
@Suppress("DEPRECATION")
decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_IMMERSIVE // Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}

// keep screen on
this.window.addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
// cancel landscape
requestedOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED

// show bars
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
this.window.insetsController?.show(
WindowInsets.Type.statusBars().or(WindowInsets.Type.navigationBars()),
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
this.window.insetsController?.systemBarsBehavior =
android.view.WindowInsetsController.BEHAVIOR_DEFAULT
}
} else {
val decorView = this.window.decorView
@Suppress("DEPRECATION")
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE
@Suppress("DEPRECATION")
(this as Activity).window.clearFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN)
}

// don't keep screen on
this.window.clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
Expand Down Expand Up @@ -143,4 +106,46 @@ private fun isInFullscreenMode(context: Context): Boolean {
val decorView = window.decorView
(decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_FULLSCREEN) != 0
}
}

actual suspend fun Context.setSystemBarVisible(visible: Boolean) {
if (this !is Activity) return
if (visible) {
// show bars
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
this.window.insetsController?.show(
WindowInsets.Type.statusBars().or(WindowInsets.Type.navigationBars()),
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
this.window.insetsController?.systemBarsBehavior =
android.view.WindowInsetsController.BEHAVIOR_DEFAULT
}
} else {
val decorView = this.window.decorView
@Suppress("DEPRECATION")
decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE
@Suppress("DEPRECATION")
this.window.clearFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN)
}
} else {
// hide bars
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
this.window.insetsController?.hide(
WindowInsets.Type.statusBars().or(WindowInsets.Type.navigationBars()),
)
this.window.insetsController?.systemBarsBehavior =
BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
} else {
val decorView = this.window.decorView
@Suppress("DEPRECATION")
decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_IMMERSIVE // Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import me.him188.ani.app.platform.Context
*/
expect suspend fun Context.setRequestFullScreen(window: PlatformWindowMP, fullscreen: Boolean)

expect suspend fun Context.setSystemBarVisible(visible: Boolean)

@Suppress("NOTHING_TO_INLINE", "KotlinRedundantDiagnosticSuppress")
@Composable
inline fun isSystemInFullscreen(): Boolean = isSystemInFullscreenImpl()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ actual suspend fun Context.setRequestFullScreen(window: PlatformWindow, fullscre
windowState.placement = if (fullscreen) WindowPlacement.Fullscreen else WindowPlacement.Floating
}
}

actual suspend fun Context.setSystemBarVisible(visible: Boolean) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ actual fun isSystemInFullscreenImpl(): Boolean {
actual suspend fun Context.setRequestFullScreen(window: PlatformWindowMP, fullscreen: Boolean) {
TODO("Not yet implemented setRequestFullScreen")
}

actual suspend fun Context.setSystemBarVisible(visible: Boolean) {
}

0 comments on commit 12dd908

Please sign in to comment.