feat: separate gameplay and overlay safe insets

This commit is contained in:
2026-09-12 22:56:55 +08:00
parent 10bc3bde1b
commit 19cc49c099
14 changed files with 97 additions and 44 deletions
+2 -2
View File
@@ -21,8 +21,8 @@ android {
applicationId = "org.neslite.app"
minSdk = 28
targetSdk = 36
versionCode = 9007
versionName = "0.9.7"
versionCode = 9008
versionName = "0.9.8"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
ndk {
@@ -85,7 +85,7 @@ import org.neslite.app.system.RotatingDiagnosticLog
import org.neslite.app.system.SafeInsets
import org.neslite.app.system.SystemUiPolicy
import org.neslite.app.system.UiLayer
import org.neslite.app.system.mergeSafeInsets
import org.neslite.app.system.resolveLayoutSafeInsets
import org.neslite.app.system.finishRefreshRateProbe
import org.neslite.app.system.sessionCommandAllowed
import org.neslite.app.system.sessionTransitionSettled
@@ -369,13 +369,27 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
val value = windowInsets.getInsets(type)
return SafeInsets(value.left, value.top, value.right, value.bottom)
}
val merged = mergeSafeInsets(
safe(WindowInsetsCompat.Type.systemBars()),
safe(WindowInsetsCompat.Type.displayCutout()),
safe(WindowInsetsCompat.Type.mandatorySystemGestures()),
fun safeIgnoringVisibility(type: Int): SafeInsets {
val value = windowInsets.getInsetsIgnoringVisibility(type)
return SafeInsets(value.left, value.top, value.right, value.bottom)
}
val layoutInsets = resolveLayoutSafeInsets(
systemBars = safeIgnoringVisibility(WindowInsetsCompat.Type.systemBars()),
displayCutout = safe(WindowInsetsCompat.Type.displayCutout()),
systemGestures = safe(WindowInsetsCompat.Type.systemGestures()),
)
view.setPadding(0, 0, 0, 0)
layoutInsets.gameplay.run {
mainScreen.setPadding(left, top, right, bottom)
}
layoutInsets.overlay.run {
overlayScreen.setPadding(left, top, right, bottom)
}
recordSystemEvent(
"safe_insets_game_${layoutInsets.gameplay.left}_0_${layoutInsets.gameplay.right}_0" +
"_overlay_${layoutInsets.overlay.left}_${layoutInsets.overlay.top}_" +
"${layoutInsets.overlay.right}_${layoutInsets.overlay.bottom}",
)
view.setPadding(merged.left, merged.top, merged.right, merged.bottom)
recordSystemEvent("safe_insets_${merged.left}_${merged.top}_${merged.right}_${merged.bottom}")
windowInsets
}
ViewCompat.requestApplyInsets(rootShell)
@@ -387,6 +401,8 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
androidx.core.view.WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
if (visible) controller.show(WindowInsetsCompat.Type.systemBars())
else controller.hide(WindowInsetsCompat.Type.systemBars())
ViewCompat.requestApplyInsets(rootShell)
rootShell.post { ViewCompat.requestApplyInsets(rootShell) }
}
private fun showEmulatorStatus(message: CharSequence) {
@@ -739,15 +755,15 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
addView(
LinearLayout(context).apply {
orientation = LinearLayout.VERTICAL
orientation = LinearLayout.HORIZONTAL
background = roundedBackground(Color.argb(120, 34, 39, 49), dp(10).toFloat())
setPadding(dp(4), dp(4), dp(4), dp(4))
libraryRailButton = railButton("≡\n列表", ::openLibraryPage)
setPadding(dp(3), dp(3), dp(3), dp(3))
libraryRailButton = railButton("≡", "游戏列表", ::openLibraryPage)
addView(libraryRailButton)
addView(railButton("+\n导入", ::openImportPage))
addView(railButton("⚙\n设置", ::showSettings))
addView(railButton("+", "导入游戏", ::openImportPage))
addView(railButton("⚙", "设置", ::showSettings))
},
FrameLayout.LayoutParams(dp(58), ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.START or Gravity.TOP).apply {
FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dp(44), Gravity.START or Gravity.TOP).apply {
leftMargin = dp(8)
topMargin = dp(8)
},
@@ -2403,10 +2419,12 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
setTextIsSelectable(true)
}
private fun railButton(label: String, action: () -> Unit) = Button(this).apply {
text = label
private fun railButton(icon: String, label: String, action: () -> Unit) = Button(this).apply {
text = icon
contentDescription = label
ViewCompat.setTooltipText(this, label)
isAllCaps = false
textSize = 11f
textSize = 19f
setTextColor(Color.WHITE)
minHeight = 0
minimumHeight = 0
@@ -2420,9 +2438,9 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
radius = dp(8).toFloat(),
)
layoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
dp(42),
).apply { setMargins(0, dp(2), 0, dp(2)) }
dp(38),
dp(38),
).apply { setMargins(dp(2), 0, dp(2), 0) }
setOnClickListener { action() }
}
@@ -43,6 +43,11 @@ object SystemUiPolicy {
data class SafeInsets(val left: Int, val top: Int, val right: Int, val bottom: Int)
data class LayoutSafeInsets(
val gameplay: SafeInsets,
val overlay: SafeInsets,
)
fun mergeSafeInsets(vararg insets: SafeInsets): SafeInsets = SafeInsets(
left = insets.maxOfOrNull(SafeInsets::left) ?: 0,
top = insets.maxOfOrNull(SafeInsets::top) ?: 0,
@@ -50,6 +55,23 @@ fun mergeSafeInsets(vararg insets: SafeInsets): SafeInsets = SafeInsets(
bottom = insets.maxOfOrNull(SafeInsets::bottom) ?: 0,
)
fun resolveLayoutSafeInsets(
systemBars: SafeInsets,
displayCutout: SafeInsets,
systemGestures: SafeInsets,
): LayoutSafeInsets {
val gameplayEdges = mergeSafeInsets(displayCutout, systemGestures)
return LayoutSafeInsets(
gameplay = SafeInsets(
left = gameplayEdges.left,
top = 0,
right = gameplayEdges.right,
bottom = 0,
),
overlay = mergeSafeInsets(systemBars, displayCutout, systemGestures),
)
}
fun sessionCommandAllowed(pending: String?, command: String, state: String): Boolean {
val transition = command in setOf("load", "run", "pause", "stop")
if (pending == "audio_rebuild" && command == "stop") return state in setOf("Paused", "Pausing")
@@ -7,7 +7,7 @@ class BuildIdentityTest {
@Test
fun debugBuildUsesTheDocumentedIdentity() {
assertEquals("org.neslite.app.debug", BuildConfig.APPLICATION_ID)
assertEquals("0.9.7-debug", BuildConfig.VERSION_NAME)
assertEquals(9007, BuildConfig.VERSION_CODE)
assertEquals("0.9.8-debug", BuildConfig.VERSION_NAME)
assertEquals(9008, BuildConfig.VERSION_CODE)
}
}
@@ -32,6 +32,18 @@ class SystemUiPolicyTest {
)
}
@Test
fun gameplayUsesOnlyHorizontalCutoutAndBackGestureInsets() {
val resolved = resolveLayoutSafeInsets(
systemBars = SafeInsets(0, 72, 0, 96),
displayCutout = SafeInsets(54, 28, 12, 0),
systemGestures = SafeInsets(24, 0, 30, 48),
)
assertEquals(SafeInsets(54, 0, 30, 0), resolved.gameplay)
assertEquals(SafeInsets(54, 72, 30, 96), resolved.overlay)
}
@Test
fun refreshSamplesRotateAtCapacity() {
val buffer = RefreshRateSampleBuffer(2)