mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
fix(input): keep full insets while the kit fits, restore the library after a failed save
This commit is contained in:
1 parent
10b21321b7
commit
cd5e824c1b
5 files changed
+42
-13
No files matched your search
@@ -83,8 +83,12 @@ object InputLibraries {
|
||||
if (invalid) {
|
||||
val encoded = encode(updated)
|
||||
require(encoded.length <= 2_000_000) { "The input library is too large to save." }
|
||||
check(p.edit().putString(RECOVERY + UUID.nameUUIDFromBytes(stored!!.toByteArray(Charsets.UTF_8)), stored)
|
||||
.putString(KEY, encoded).commit()) { "Could not save the input library and its recovery copy." }
|
||||
val recovery = RECOVERY + UUID.nameUUIDFromBytes(stored!!.toByteArray(Charsets.UTF_8))
|
||||
if (!p.edit().putString(recovery, stored).putString(KEY, encoded).commit()) {
|
||||
// A failed commit has already changed the in-memory preferences; put the previous library back.
|
||||
p.edit().putString(KEY, stored).remove(recovery).apply()
|
||||
error("Could not save the input library and its recovery copy.")
|
||||
}
|
||||
} else write(context, updated)
|
||||
return updated
|
||||
}
|
||||
|
||||
@@ -41,8 +41,8 @@ object KitLayout {
|
||||
/** The widest pair of side controls on any kit: a larger size never pushes the two halves, or the rows, into each other. */
|
||||
private const val MIN_SPAN = 1200f
|
||||
private const val MAX_HEIGHT_MM = 80f
|
||||
/** Insets are kept as reported until together they take more than this share of a side, which would leave no room for a usable pad. */
|
||||
private const val MAX_INSETS = .4f
|
||||
/** The smallest half-size of any kit control in reference units. */
|
||||
private const val SMALLEST_HALF = 26f
|
||||
private const val CORNER_DP = 52f
|
||||
private const val ROW_DP = 60f
|
||||
private const val BUTTON_DP = 44f
|
||||
@@ -89,13 +89,15 @@ object KitLayout {
|
||||
/** The default controls for [kit] on a [width] × [height] screen. */
|
||||
fun elements(context: Context, kit: PadKit, width: Int, height: Int, inset: Rect = Rect(), scale: Float = 1f): List<InputSurfaceElement> {
|
||||
if (width <= 0 || height <= 0) return emptyList()
|
||||
fun fit(a: Int, b: Int, side: Int): Pair<Int, Int> {
|
||||
// Insets stay as reported unless what's left is too small to keep every control above the minimum element size.
|
||||
val smallestUnit = InputSurfaceElement.MIN_SIZE * max(width, height) / (2 * SMALLEST_HALF)
|
||||
fun fit(a: Int, b: Int, side: Int, reference: Float): Pair<Int, Int> {
|
||||
val first = a.coerceAtLeast(0); val second = b.coerceAtLeast(0)
|
||||
val room = (side * MAX_INSETS).toInt()
|
||||
return if (first + second <= room) first to second else (first * room / (first + second)) to (second * room / (first + second))
|
||||
val room = (side - (smallestUnit * reference).toInt() - 1).coerceAtLeast(0)
|
||||
return if (first + second <= room) first to second else (first.toLong() * room / (first + second)).toInt() to (second.toLong() * room / (first + second)).toInt()
|
||||
}
|
||||
val (left, right) = fit(inset.left, inset.right, width)
|
||||
val (top, bottom) = fit(inset.top, inset.bottom, height)
|
||||
val (left, right) = fit(inset.left, inset.right, width, MIN_SPAN)
|
||||
val (top, bottom) = fit(inset.top, inset.bottom, height, REF_H)
|
||||
val safe = Rect(left, top, right, bottom)
|
||||
if (height > width) return portrait(context, kit, width, height, safe, scale)
|
||||
val w = width.toFloat(); val h = height.toFloat()
|
||||
|
||||
@@ -200,8 +200,7 @@ private fun DefaultControlsGroup(host: MenuHost, revision: Int, onEdit: (Triple<
|
||||
var below by remember { mutableStateOf(SessionPrefs.unfoldedControls(context)) }
|
||||
SettingsGroup(stringResource(R.string.osc_group_for, stringResource(R.string.osc_default))) {
|
||||
ChoiceRow(host, "osc-layout", stringResource(R.string.osc_layout), null, options, choice) { id ->
|
||||
runCatching { ControlsChoice.choose(context, null, id, desktop = false) }.onFailure(onFailure)
|
||||
onChanged()
|
||||
runCatching { ControlsChoice.choose(context, null, id, desktop = false) }.onSuccess { onChanged() }.onFailure(onFailure)
|
||||
}
|
||||
if (choice != GameControls.STEAM) ActionRow(stringResource(R.string.osc_edit_layout), null, stringResource(R.string.ctrl_edit), {
|
||||
host.open = null
|
||||
|
||||
@@ -284,11 +284,11 @@ class KitLayoutTest {
|
||||
|
||||
@Test fun insetsThatLeaveLessRoomShrinkTheKitInsteadOfOverlappingIt() {
|
||||
for (kit in PadKit.entries) for ((w, h, safe) in listOf(Triple(1600, 900, Rect(0, 60, 0, 0)), Triple(1600, 900, Rect(0, 730, 0, 0)),
|
||||
Triple(1600, 900, Rect(0, 200, 0, 0)), Triple(1600, 900, Rect(0, 200, 0, 150)),
|
||||
Triple(1600, 900, Rect(0, 200, 0, 0)), Triple(1600, 900, Rect(0, 200, 0, 150)), Triple(1600, 900, Rect(0, 390, 0, 0)),
|
||||
Triple(1920, 1080, Rect(80, 120, 80, 90)), Triple(2400, 1080, Rect(120, 30, 60, 40)))) {
|
||||
val elements = KitLayout.elements(context, kit, w, h, safe)
|
||||
assertEquals("$kit $w x $h $safe", emptyList<Pair<String, String>>(), overlapping(elements, w, h))
|
||||
if (safe.top + safe.bottom <= h * .4f) for (e in elements) {
|
||||
if (safe.top + safe.bottom <= 400) for (e in elements) {
|
||||
val (x, y, ew, eh) = px(e, w, h).toList()
|
||||
assertTrue("$kit $safe ${e.id}", x >= safe.left - .5f && y >= safe.top - .5f && x + ew <= w - safe.right + .5f && y + eh <= h - safe.bottom + .5f)
|
||||
}
|
||||
|
||||
@@ -178,4 +178,28 @@ class OnScreenControlPrefsTest {
|
||||
val noDpad = edited.withLayout(saved.copy(elements = saved.elements.filterNot { it.id == KitLayout.DPAD_ID }))
|
||||
assertNull(noDpad.preset(bindings.id).elements["legacy:up"])
|
||||
}
|
||||
|
||||
@Test fun aFailedRecoverySaveLeavesTheStoredLibraryAsItWas() {
|
||||
val stored = JSONObject().put("version", 1).put("profiles", "broken").toString()
|
||||
val real = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE)
|
||||
real.edit().clear().putString("library", stored).commit()
|
||||
val failing = object : android.content.ContextWrapper(context) {
|
||||
override fun getSharedPreferences(name: String, mode: Int): android.content.SharedPreferences {
|
||||
val prefs = super.getSharedPreferences(name, mode)
|
||||
return object : android.content.SharedPreferences by prefs {
|
||||
override fun edit(): android.content.SharedPreferences.Editor {
|
||||
val editor = prefs.edit()
|
||||
return object : android.content.SharedPreferences.Editor by editor {
|
||||
override fun putString(key: String?, value: String?) = apply { editor.putString(key, value) }
|
||||
override fun remove(key: String?) = apply { editor.remove(key) }
|
||||
override fun commit(): Boolean { editor.apply(); return false }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assertTrue(runCatching { InputLibraries.update(failing) { it } }.isFailure)
|
||||
assertEquals(stored, real.getString("library", null))
|
||||
assertTrue(real.all.keys.none { it.startsWith("library-recovery.") })
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user