Skip to content

Commit

Permalink
fix ScreenRotationEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
NieR4ever authored and Him188 committed Oct 17, 2024
1 parent 3e8411b commit 16625f4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 58 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
@@ -1,11 +1,24 @@
package me.him188.ani.app.ui.foundation.effects

import android.app.Activity
import android.content.res.Configuration
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalConfiguration
import me.him188.ani.app.platform.LocalContext
import me.him188.ani.app.ui.foundation.layout.hideBars
import me.him188.ani.app.ui.foundation.layout.showBars


@Composable
actual fun ScreenRotationEffectImpl(onChange: (isLandscape: Boolean) -> Unit) {
onChange(LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE)
val context = LocalContext.current
val isLandscape = LocalConfiguration.current.orientation == Configuration.ORIENTATION_LANDSCAPE
if (context is Activity) {
if (isLandscape) {
context.hideBars()
} else {
context.showBars()
}
}
onChange(isLandscape)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,7 @@ actual suspend fun Context.setRequestFullScreen(window: PlatformWindowMP, fullsc
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)
}
hideBars()

// keep screen on
this.window.addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
Expand All @@ -52,21 +35,7 @@ actual suspend fun Context.setRequestFullScreen(window: PlatformWindowMP, fullsc
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)
}
showBars()

// don't keep screen on
this.window.clearFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
Expand All @@ -81,6 +50,45 @@ actual suspend fun Context.setRequestFullScreen(window: PlatformWindowMP, fullsc
}
}

fun Activity.hideBars() {
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)
}
}

fun Activity.showBars() {
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)
}
}

// TODO: isSystemInFullscreen is written by ChatGPT, not tested
@Composable
actual fun isSystemInFullscreenImpl(): Boolean {
Expand Down

0 comments on commit 16625f4

Please sign in to comment.