Skip to content

Commit

Permalink
bring navigationBarColor back
Browse files Browse the repository at this point in the history
* fix navi background color

* refine
  • Loading branch information
lings03 authored Nov 9, 2024
1 parent f727c45 commit 3b73998
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
23 changes: 13 additions & 10 deletions app/src/main/java/top/laoxin/modmanager/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.tooling.preview.Preview
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsControllerCompat
import rikka.shizuku.Shizuku
import top.laoxin.modmanager.tools.PermissionTools
import top.laoxin.modmanager.ui.theme.ModManagerTheme
Expand All @@ -26,21 +28,23 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// 设置全屏模式,使内容可以扩展到状态栏和导航栏区域
WindowCompat.setDecorFitsSystemWindows(window, false)
// 通过背景色使状态栏和导航栏透明
window.setBackgroundDrawableResource(android.R.color.transparent)

// 获取屏幕宽度
val screenWidthDp = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// 使用 WindowMetrics 获取屏幕宽度(单位:dp)
val windowMetrics = windowManager.currentWindowMetrics
val widthPixels = windowMetrics.bounds.width()
widthPixels / resources.displayMetrics.density
} else {
// 兼容 Android 11 (API 30) 以下的版本
val displayMetrics = DisplayMetrics()
@Suppress("DEPRECATION")
windowManager.defaultDisplay.getMetrics(displayMetrics)
displayMetrics.widthPixels / displayMetrics.density
}

// 判定设备是否是平板(假设屏幕宽度大于等于 600dp 为平板)
requestedOrientation = if (screenWidthDp >= 600) {
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
} else {
Expand All @@ -50,27 +54,27 @@ class MainActivity : ComponentActivity() {
// 添加 Shizuku 权限请求监听
Shizuku.addRequestPermissionResultListener(PermissionTools.REQUEST_PERMISSION_RESULT_LISTENER)

// 设置全屏模式,使内容可以扩展到状态栏和导航栏区域
WindowCompat.setDecorFitsSystemWindows(window, false)

// 检查是否同意许可,跳转到 StartActivity 页面
val sharedPreferences = getSharedPreferences("AppLaunch", MODE_PRIVATE)
val isConfirm = sharedPreferences.getBoolean("isConfirm", false)

if (!isConfirm) {
// 如果未同意许可,跳转到 StartActivity 进行确认
val intent = Intent(this, StartActivity::class.java)
startActivity(intent)
finish()
return
}

// 启用 Edge-to-Edge 模式
enableEdgeToEdge()

setContent {
// 使用自定义主题适配深色模式
ModManagerTheme {
// 设置导航栏背景颜色和图标亮度
WindowInsetsControllerCompat(window, window.decorView).apply {
isAppearanceLightNavigationBars = true
@Suppress("DEPRECATION")
window.navigationBarColor = MaterialTheme.colorScheme.surfaceContainer.toArgb()
}
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
Expand All @@ -84,7 +88,6 @@ class MainActivity : ComponentActivity() {
override fun onDestroy() {
super.onDestroy()
if (PermissionTools.isShizukuAvailable) {
// 移除 Shizuku 权限请求回调
Shizuku.removeRequestPermissionResultListener(PermissionTools.REQUEST_PERMISSION_RESULT_LISTENER)
}
}
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/top/laoxin/modmanager/ui/theme/Theme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ fun ModManagerTheme(
if (!view.isInEditMode) {
SideEffect {
val window = (view.context as Activity).window
// 设置全屏模式,使内容可以扩展到状态栏和导航栏区域
WindowCompat.setDecorFitsSystemWindows(window, false)

// 通过背景色使状态栏和导航栏透明
window.setBackgroundDrawableResource(android.R.color.transparent)

// 设置状态栏和导航栏的图标颜色
WindowCompat.getInsetsController(window, view).let { controller ->
controller.isAppearanceLightStatusBars = !darkTheme
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/top/lings/start/StartActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@ import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.ui.graphics.toArgb
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsControllerCompat

class StartActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// 设置全屏模式,使内容可以扩展到状态栏和导航栏区域
WindowCompat.setDecorFitsSystemWindows(window, false)
// 通过背景色使状态栏和导航栏透明
window.setBackgroundDrawableResource(android.R.color.transparent)

// 启用 Edge-to-Edge 模式
enableEdgeToEdge()

setContent {
// 使用 Material3 主题适配深色模式
// 设置导航栏背景颜色和图标亮度
WindowInsetsControllerCompat(window, window.decorView).apply {
isAppearanceLightNavigationBars = true
@Suppress("DEPRECATION")
window.navigationBarColor = MaterialTheme.colorScheme.surfaceContainer.toArgb()
}
MaterialTheme(
colorScheme = if (isSystemInDarkTheme()) darkColorScheme() else lightColorScheme()
) {
Expand Down

0 comments on commit 3b73998

Please sign in to comment.