fix(input): removed kit controls keep their actions in gamepad layouts

This commit is contained in:
Kurt Himebauch committed 2026-11-19 00:44:41 -05:00
1 parent cd5e824c1b
commit dcebdac765
4 files changed
+46 -7

No files matched your search

@@ -315,6 +315,11 @@ internal fun InputAction.validFor(element: InputSurfaceElement): Boolean = when
else -> target.dimensions == 1 && target != InputControl.LT && target != InputControl.RT
}
/** A saved element, or, in a gamepad that keeps no kit positions, the kit control that [id] names. */
/**
* A saved element, or the kit control that [id] names: in a layout made from the built-in gamepad every kit control keeps its
* binding while it's removed, so Reset or Restore brings it back with its action.
*/
internal fun SavedInputLayout.element(id: String): InputSurfaceElement? =
elements.firstOrNull { it.id == id } ?: KitLayout.element(id)?.takeIf { OnScreenLayout.generated(this) || KitLayout.bindsTo(id, elements) }
elements.firstOrNull { it.id == id } ?: KitLayout.element(id)?.takeIf {
this.id == InputLibraries.OVERLAY || origin == InputLibraries.OVERLAY || KitLayout.bindsTo(id, elements)
}
@@ -116,7 +116,10 @@ private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputB
}
fun hiddenDefaults(): List<InputSurfaceElement> = defaults?.let { refit(it, defaultsFrame) }?.filter { d -> elements.none { it.id == d.id } }.orEmpty()
fun add(element: InputSurfaceElement) { checkpoint(); elements = elements + element; selected = element.id; popover = null }
fun remove(id: String) { checkpoint(); elements = elements.filterNot { it.id == id }; actions = actions - id; clicks = clicks - id; selected = null }
fun remove(id: String) {
checkpoint(); elements = elements.filterNot { it.id == id }; selected = null
if (!KitLayout.isKit(id)) { actions = actions - id; clicks = clicks - id }
}
fun resizeSelected(factor: Float) {
val e = element ?: return
checkpoint()
@@ -173,9 +176,10 @@ fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () ->
val p = latest.preset(bindingsTarget)
val generated = draft.kitAtDefaults && OnScreenLayout.generated(l.copy(elements = emptyList(), kitless = false))
val extras = draft.elements.filterNot { KitLayout.isKit(it.id) }
latest.withLayout(if (generated) l.copy(elements = extras, aspect = if (extras.isEmpty()) 0f else draft.aspect, scale = draft.layoutScale, kitless = false)
else l.copy(elements = draft.elements, aspect = draft.aspect, scale = draft.layoutScale, kitless = draft.elements.none { KitLayout.isKit(it.id) })).withPreset(p.copy(elements = draft.actions.filterKeys { id -> KitLayout.bindsTo(id, draft.elements) },
clicks = draft.clicks.filterKeys { id -> draft.elements.any { it.id == id && it.hasClick } }))
val edited = if (generated) l.copy(elements = extras, aspect = if (extras.isEmpty()) 0f else draft.aspect, scale = draft.layoutScale, kitless = false)
else l.copy(elements = draft.elements, aspect = draft.aspect, scale = draft.layoutScale, kitless = draft.elements.none { KitLayout.isKit(it.id) })
latest.withLayout(edited).withPreset(p.copy(elements = draft.actions.filterKeys { id -> edited.element(id) != null },
clicks = draft.clicks.filterKeys { id -> edited.element(id)?.hasClick == true }))
}
copy?.use?.invoke(bindingsTarget)
}.onSuccess { onChanged(); onDismiss() }.onFailure { failure = it.message ?: context.getString(R.string.input_save_failed) }
@@ -176,7 +176,23 @@ class OnScreenControlPrefsTest {
val edited = withUp.withLayout(saved.copy(style = saved.style.copy(opacity = 60))).validate()
assertEquals(InputAction.Keys(listOf(103)), edited.preset(bindings.id).elements["legacy:up"])
val noDpad = edited.withLayout(saved.copy(elements = saved.elements.filterNot { it.id == KitLayout.DPAD_ID }))
assertNull(noDpad.preset(bindings.id).elements["legacy:up"])
assertEquals(InputAction.Keys(listOf(103)), noDpad.preset(bindings.id).elements["legacy:up"])
}
@Test fun removingAKitControlFromAGamepadLayoutKeepsItsActionForReset() {
val library = InputLibraries.read(context)
val overlay = library.layouts.single { it.id == InputLibraries.OVERLAY }
val kit = KitLayout.elements(context, PadKit.XBOX, 1920, 1080)
val bindings = library.preset(library.activePart(false, InputRole.PRIMARY).bindings)
val withA = library.withLayout(overlay.copy(elements = kit, aspect = 16f / 9))
.withPreset(bindings.copy(elements = bindings.elements + ("legacy:a" to InputAction.Keys(listOf(30)))))
val noA = withA.withLayout(overlay.copy(elements = kit.filterNot { it.id == "legacy:a" }, aspect = 16f / 9)).validate()
assertEquals(InputAction.Keys(listOf(30)), noA.preset(bindings.id).elements["legacy:a"])
val kitless = noA.withLayout(overlay.copy(elements = emptyList(), kitless = true)).validate()
assertEquals(InputAction.Keys(listOf(30)), kitless.preset(bindings.id).elements["legacy:a"])
val custom = SavedInputLayout("custom", "Custom", listOf(InputSurfaceElement(InputControl.B, .1f, .1f, .1f, .1f, "legacy:b")))
assertNull(custom.element("legacy:a"))
assertNull(overlay.element("imported:404"))
}
@Test fun aFailedRecoverySaveLeavesTheStoredLibraryAsItWas() {
@@ -272,6 +272,20 @@ class InputSettingsTest {
compose.runOnIdle { assertTrue(OnScreenLayout.generated(InputLibraries.read(context).layouts.first { it.id == layout.id })) }
}
@Test @Config(qualifiers = "w833dp-h468dp-land-369dpi")
fun removingAndRestoringAKitButtonKeepsItsAction() {
val library = InputLibraries.read(context)
val layout = library.layout("game", InputRole.PRIMARY)!!
val bindingsId = library.profile("game").binding(InputRole.PRIMARY)
InputLibraries.update(context) { lib -> lib.preset(bindingsId).let { b -> lib.withPreset(b.copy(elements = b.elements + ("legacy:a" to InputAction.Keys(listOf(30))))) } }
val a = InputLibraries.defaultPositions(context, library, layout)!!.first.single { it.id == "legacy:a" }
compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY, layoutId = layout.id, bindingsId = bindingsId) } }
compose.onNodeWithTag("input-layout-canvas").performTouchInput { click(percentOffset(a.x + a.width / 2, a.y + a.height / 2)) }
compose.onNodeWithText(context.getString(R.string.input_remove_element)).performClick()
compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick()
compose.runOnIdle { assertEquals(InputAction.Keys(listOf(30)), InputLibraries.read(context).preset(bindingsId).elements["legacy:a"]) }
}
@Test fun resetAndSaveKeepTheDpadDirectionOverrides() {
val library = InputLibraries.read(context)
val layout = library.layout("game", InputRole.PRIMARY)!!