Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复重力旋转全屏 #1068

Merged
merged 6 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -98,10 +99,10 @@ import me.him188.ani.app.ui.foundation.effects.DarkStatusBarAppearance
import me.him188.ani.app.ui.foundation.effects.OnLifecycleEvent
import me.him188.ani.app.ui.foundation.effects.ScreenOnEffect
import me.him188.ani.app.ui.foundation.effects.ScreenRotationEffect
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 @@ -188,13 +189,17 @@ private fun EpisodeSceneContent(
VideoNotifEffect(vm)

DarkStatusBarAppearance()
if (vm.videoScaffoldConfig.autoFullscreenOnLandscapeMode && isInDebugMode()) {

if (vm.videoScaffoldConfig.autoFullscreenOnLandscapeMode) {
ScreenRotationEffect {
vm.isFullscreen = it
}
}

SideEffect {
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 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 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 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 fun Context.setSystemBarVisible(visible: Boolean) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ fun SettingsScope.PlayerGroup(
},
title = { Text("选择数据源后自动关闭弹窗") },
)
if (LocalPlatform.current.isMobile() && showDebug) {
if (LocalPlatform.current.isMobile()) {
HorizontalDividerItem()
SwitchItem(
checked = config.autoFullscreenOnLandscapeMode,
Expand Down
Loading