fix: stabilize landscape controls and compact game library

This commit is contained in:
2026-09-13 21:02:12 +08:00
parent baeb1130eb
commit 70847c8b97
17 changed files with 146 additions and 47 deletions
+2 -2
View File
@@ -21,8 +21,8 @@ android {
applicationId = "org.neslite.app"
minSdk = 28
targetSdk = 36
versionCode = 9011
versionName = "0.9.11"
versionCode = 9012
versionName = "0.9.12"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
ndk {
@@ -31,6 +31,7 @@ import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.SeekBar
import android.widget.Space
import android.widget.TextView
import android.widget.Toast
import android.app.AlertDialog
@@ -57,6 +58,7 @@ import org.neslite.app.library.GameDatabase
import org.neslite.app.library.GameEntity
import org.neslite.app.library.LibraryRecovery
import org.neslite.app.library.RomImporter
import org.neslite.app.library.libraryGridColumns
import org.neslite.app.input.AxisHysteresis
import org.neslite.app.input.BindingKind
import org.neslite.app.input.ControllerIdentity
@@ -823,7 +825,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
libraryPanel = LinearLayout(context).apply {
orientation = LinearLayout.VERTICAL
visibility = View.GONE
addView(body("选择游戏后将返回主画面并自动开始运行。"))
addView(body("点游戏名开始运行;长按可查看详情、收藏或从列表移除。"))
searchInput = EditText(context).apply {
hint = "搜索游戏名称或哈希"
isSingleLine = true
@@ -1900,39 +1902,62 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
if (games.isEmpty()) {
gameList.addView(body(if (query.isEmpty()) "游戏库为空;请从文件导入 ROM。" else "没有匹配“$query”的游戏;可清空搜索后重试。"))
}
games.forEach { game ->
val columns = libraryGridColumns(resources.configuration.screenWidthDp - 40)
games.chunked(columns).forEach { group ->
gameList.addView(LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
addView(Button(context).apply {
text = (if (game.favorite) "★ " else "") + game.displayName
isAllCaps = false
isEnabled = game.status == GameEntity.STATUS_READY
setOnClickListener { loadGame(game) }
})
addView(body("${game.format} · ${formatBytes(game.sizeBytes)} · SHA ${game.sha256.take(12)} · ${game.status}\n" +
"导入 ${formatDate(game.addedAtEpochMs)} · 最近 ${if (game.lastPlayedAtEpochMs == 0L) "未游玩" else formatDate(game.lastPlayedAtEpochMs)}"))
addView(LinearLayout(context).apply {
orientation = LinearLayout.HORIZONTAL
orientation = LinearLayout.HORIZONTAL
group.forEach { game ->
addView(Button(context).apply {
text = if (game.favorite) "取消收藏" else "收藏"
text = (if (game.favorite) "★ " else "") + game.displayName
isAllCaps = false
isSingleLine = false
maxLines = Int.MAX_VALUE
ellipsize = null
textSize = 16f
minHeight = dp(56)
setOnClickListener {
libraryExecutor.execute {
database.games().setFavorite(game.sha256, !game.favorite)
runOnUiThread { refreshLibrary() }
}
if (game.status == GameEntity.STATUS_READY) loadGame(game)
else Toast.makeText(context, "游戏文件不可用,可长按查看详情", Toast.LENGTH_SHORT).show()
}
setOnLongClickListener {
showLibraryGameActions(game)
true
}
}, LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f).apply {
setMargins(dp(4), dp(3), dp(4), dp(3))
})
addView(Button(context).apply {
text = "从列表移除"
setOnClickListener { confirmHideGame(game) }
})
})
}
repeat(columns - group.size) {
addView(Space(context), LinearLayout.LayoutParams(0, 1, 1f))
}
})
}
}
}
}
private fun showLibraryGameActions(game: GameEntity) {
val actions = arrayOf("查看详情", if (game.favorite) "取消收藏" else "收藏", "从列表移除")
AlertDialog.Builder(this)
.setTitle(game.displayName)
.setItems(actions) { _, which ->
when (which) {
0 -> AlertDialog.Builder(this)
.setTitle(game.displayName)
.setMessage("${game.format} · ${formatBytes(game.sizeBytes)} · SHA ${game.sha256.take(12)} · ${game.status}\n" +
"导入 ${formatDate(game.addedAtEpochMs)} · 最近 ${if (game.lastPlayedAtEpochMs == 0L) "未游玩" else formatDate(game.lastPlayedAtEpochMs)}")
.setPositiveButton("关闭", null)
.show()
1 -> libraryExecutor.execute {
database.games().setFavorite(game.sha256, !game.favorite)
runOnUiThread { refreshLibrary() }
}
2 -> confirmHideGame(game)
}
}
.show()
}
private fun confirmHideGame(game: GameEntity) {
AlertDialog.Builder(this)
.setTitle("从列表移除?")
@@ -0,0 +1,4 @@
package org.neslite.app.library
/** Landscape library cards should stay wide enough to show complete ROM names. */
fun libraryGridColumns(availableWidthDp: Int): Int = if (availableWidthDp >= 720) 3 else 2
@@ -61,14 +61,19 @@ fun resolveLayoutSafeInsets(
systemGestures: SafeInsets,
): LayoutSafeInsets {
val gameplayEdges = mergeSafeInsets(displayCutout, systemGestures)
// A landscape rotation exchanges the physical left and right edges. Keep the
// playable geometry anchored to the same coordinates in either orientation.
val gameplaySide = maxOf(gameplayEdges.left, gameplayEdges.right)
return LayoutSafeInsets(
gameplay = SafeInsets(
left = gameplayEdges.left,
left = gameplaySide,
top = 0,
right = gameplayEdges.right,
right = gameplaySide,
bottom = 0,
),
overlay = mergeSafeInsets(systemBars, displayCutout, systemGestures),
// Menus are not touch controls: use the full width while keeping clear
// of visible top and bottom system bars.
overlay = SafeInsets(0, systemBars.top, 0, systemBars.bottom),
)
}
@@ -7,7 +7,7 @@ class BuildIdentityTest {
@Test
fun debugBuildUsesTheDocumentedIdentity() {
assertEquals("org.neslite.app.debug", BuildConfig.APPLICATION_ID)
assertEquals("0.9.11-debug", BuildConfig.VERSION_NAME)
assertEquals(9011, BuildConfig.VERSION_CODE)
assertEquals("0.9.12-debug", BuildConfig.VERSION_NAME)
assertEquals(9012, BuildConfig.VERSION_CODE)
}
}
@@ -3,6 +3,8 @@ package org.neslite.app
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.neslite.app.system.SafeInsets
import org.neslite.app.system.resolveLayoutSafeInsets
import kotlin.math.hypot
class VirtualGamepadLayoutTest {
@@ -123,6 +125,31 @@ class VirtualGamepadLayoutTest {
}
}
@Test
fun flippedLandscapeKeepsTheViewportAndEveryControlAtTheSameCoordinates() {
val first = resolveLayoutSafeInsets(
systemBars = SafeInsets(0, 0, 0, 0),
displayCutout = SafeInsets(238, 0, 0, 0),
systemGestures = SafeInsets(28, 0, 24, 0),
).gameplay
val flipped = resolveLayoutSafeInsets(
systemBars = SafeInsets(0, 0, 0, 0),
displayCutout = SafeInsets(0, 0, 238, 0),
systemGestures = SafeInsets(24, 0, 28, 0),
).gameplay
fun geometry(insets: SafeInsets) = computeVirtualGamepadGeometry(
width = 2760f,
height = 1256f,
maxRadius = 126f,
safeLeft = insets.left.toFloat(),
safeRight = insets.right.toFloat(),
dpadScale = 1.18f,
)
assertEquals(geometry(first), geometry(flipped))
}
private fun distance(first: VirtualControl, second: VirtualControl): Float =
hypot(first.x - second.x, first.y - second.y)
}
@@ -0,0 +1,14 @@
package org.neslite.app.library
import org.junit.Assert.assertEquals
import org.junit.Test
class LibraryGridLayoutTest {
@Test
fun usesThreeColumnsOnWideLandscapeAndTwoOnNarrowScreens() {
assertEquals(3, libraryGridColumns(920))
assertEquals(3, libraryGridColumns(720))
assertEquals(2, libraryGridColumns(719))
assertEquals(2, libraryGridColumns(540))
}
}
@@ -33,15 +33,33 @@ class SystemUiPolicyTest {
}
@Test
fun gameplayUsesOnlyHorizontalCutoutAndBackGestureInsets() {
fun gameplayIsSymmetricAndMenusUseTheFullWidth() {
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)
assertEquals(SafeInsets(54, 0, 54, 0), resolved.gameplay)
assertEquals(SafeInsets(0, 72, 0, 96), resolved.overlay)
}
@Test
fun landscapeFlipKeepsGameplayInsetsAndDropsHiddenBars() {
val first = resolveLayoutSafeInsets(
systemBars = SafeInsets(0, 0, 0, 0),
displayCutout = SafeInsets(238, 0, 0, 0),
systemGestures = SafeInsets(28, 0, 24, 0),
)
val flipped = resolveLayoutSafeInsets(
systemBars = SafeInsets(0, 0, 0, 0),
displayCutout = SafeInsets(0, 0, 238, 0),
systemGestures = SafeInsets(24, 0, 28, 0),
)
assertEquals(first.gameplay, flipped.gameplay)
assertEquals(SafeInsets(238, 0, 238, 0), first.gameplay)
assertEquals(SafeInsets(0, 0, 0, 0), first.overlay)
}
@Test