fix(input): save per-game copies with the edit, keep failed choices visible and grips clear of the d-pad

This commit is contained in:
Kurt Himebauch committed 2026-11-19 00:11:01 -05:00
1 parent 885c4f0a44
commit 10b21321b7
7 files changed
+45 -18

No files matched your search

@@ -4413,7 +4413,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
else -> getString(R.string.osc_scope_game, layoutName, name)
}
layoutEditorSplit = if (game != null && name != null && shared) com.droiddeck.launcher.ui.InputEditorSplit(getString(R.string.osc_just_this_game),
getString(R.string.osc_scope_game, layoutName, name)) { com.droiddeck.launcher.input.ControlsChoice.newForGame(this, game, name, desktop) } else null
getString(R.string.osc_scope_game, layoutName, name),
{ library, layoutId, bindingsId -> com.droiddeck.launcher.input.ControlsChoice.copyForGame(this, library, game, name, desktop, layoutId, bindingsId) },
{ bindingsId -> com.droiddeck.launcher.input.ControlsChoice.useForGame(this, game, name, bindingsId) }) else null
releasePads()
drawerOpen = false
layoutEditor = target
@@ -5349,12 +5351,13 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
private fun controlsPlan(): com.droiddeck.launcher.input.ControlsPresenter.Plan {
val steam = SessionState.mode == SessionService.MODE_STEAM
if (!PadBridge.anyControllerConnected() && !SteamControllerBle.connected) controllerInUse = false
return com.droiddeck.launcher.input.ControlsPresenter.plan(com.droiddeck.launcher.input.ControlsPresenter.State(
mode = SessionPrefs.oscMode(this),
desktop = SessionState.mode == SessionService.MODE_DESKTOP,
virtualPad = !steam || SessionPrefs.builtinPad(this),
covered = controlsCovered(),
controllerInUse = controllerInUse && (PadBridge.anyControllerConnected() || SteamControllerBle.connected),
controllerInUse = controllerInUse,
hiddenByTap = controlsHiddenByTap,
steamLayout = steam && com.droiddeck.launcher.input.GameControls.steamLayout(this, controlsGameId()),
bottomScreen = bottomGame?.surfaceView?.parent is android.view.ViewGroup,
@@ -45,14 +45,14 @@ object ControlsChoice {
val bindings = if (steam) null else {
val library = InputLibraries.read(context)
val layout = library.layouts.first { it.id == id }
library.bindings.firstOrNull { !it.physical && it.compatible(layout) }?.id ?: newInputId().also { newId ->
library.bindings.firstOrNull { !it.physical && it.layoutId == layout.id && it.compatible(layout) }?.id ?: newInputId().also { newId ->
InputLibraries.update(context) { it.withPreset(SavedInputBindings(newId, layout.name, false, layout.id)) }
}
}
if (game != null) GameControls.choose(context, game, bindings, steam, gameName)
else {
SessionPrefs.setSteamTouchLayout(context, steam)
if (bindings != null) InputLibraries.update(context) { it.selectPart(desktop, InputRole.PRIMARY, bindings) }
SessionPrefs.setSteamTouchLayout(context, steam)
}
}
@@ -60,7 +60,14 @@ object ControlsChoice {
fun newForGame(context: Context, game: Long, name: String, desktop: Boolean): Pair<String, String> {
val layoutId = newInputId()
val bindingsId = newInputId()
InputLibraries.update(context) { latest ->
InputLibraries.update(context) { latest -> copyForGame(context, latest, game, name, desktop, layoutId, bindingsId) }
GameControls.choose(context, game, bindingsId, steam = false, name = name)
return layoutId to bindingsId
}
/** [latest] with a copy of the layout [game] draws now, actions included, under the given ids. Nothing is chosen for the game. */
fun copyForGame(context: Context, latest: InputLibrary, game: Long, name: String, desktop: Boolean, layoutId: String, bindingsId: String): InputLibrary {
run {
val chosen = own(context, latest, game)
val part = (if (chosen == null) latest else latest.selectPart(desktop, InputRole.PRIMARY, chosen)).activePart(desktop, InputRole.PRIMARY)
val source = latest.layouts.firstOrNull { it.id == part.layout && it.editable } ?: latest.layouts.first { it.id == InputLibraries.OVERLAY }
@@ -68,12 +75,13 @@ object ControlsChoice {
?: latest.bindings.firstOrNull { !it.physical && it.compatible(source) }
?: SavedInputBindings(bindingsId, source.name, false, source.id)
val title = name.trim().take(80).ifEmpty { source.name }
latest.withLayout(source.derived(layoutId, title)).withPreset(actions.copy(id = bindingsId, name = title, layoutId = layoutId))
return latest.withLayout(source.derived(layoutId, title)).withPreset(actions.copy(id = bindingsId, name = title, layoutId = layoutId))
}
GameControls.choose(context, game, bindingsId, steam = false, name = name)
return layoutId to bindingsId
}
/** Uses [bindingsId], made by [copyForGame], for [game] only. */
fun useForGame(context: Context, game: Long, name: String, bindingsId: String) = GameControls.choose(context, game, bindingsId, steam = false, name = name)
/** The profile, layout and actions the editor opens for [game], or null when its layout can't be edited here. */
fun editTarget(context: Context, game: Long?, desktop: Boolean): Triple<String, String, String>? {
val library = library(context, game, desktop)
@@ -90,7 +90,7 @@ object GamepadGeometry {
val rows = (middle.size + columns - 1) / columns
val gridTop = h - pad - rows * buttonH - (rows - 1) * 10 * density
middleGrid(columns, gap - 2 * gutter, gridTop)
if (grips) gripsBetween(boxes, w, gap - 2 * gutter, shoulderY + buttonH / 2f + gutter, gridTop - gutter, density)
if (grips) gripsBetween(boxes, w, w - 2 * (side * 0.74f + second / 2f) - 2 * gutter, shoulderY + buttonH / 2f + gutter, gridTop - gutter, density)
val panelsW = min(112 * density, w - 2 * (side * 0.57f + shoulderW / 2f) - 2 * gutter)
if (panelsW >= buttonH) boxes[Slot.PANELS] = Box(w / 2f, shoulderY, panelsW, buttonH)
}
@@ -116,7 +116,8 @@ private fun InputSettingsContent(onBack: () -> Unit, onChanged: () -> Unit, embe
InputPage.HOME -> {
if (InputLibraries.hasPendingScreenKeyMigration(context)) InputHint(stringResource(R.string.input_import_failed))
if (InputLibraries.hasLibraryRecovery(context)) InputHint(stringResource(R.string.input_library_recovered))
DefaultControlsGroup(host, revision, onEdit = { surface = it }, onChanged = ::changed)
DefaultControlsGroup(host, revision, onEdit = { surface = it }, onChanged = ::changed,
onFailure = { error = it.message ?: context.getString(R.string.input_save_failed) })
SettingsGroup(stringResource(R.string.drawer_page_controller)) {
var padType by remember { mutableStateOf(SessionPrefs.steamController(context)) }
ChoiceRow(host, "pad-type", stringResource(R.string.input_virtual_controller), null,
@@ -189,7 +190,8 @@ private fun InputSettingsContent(onBack: () -> Unit, onChanged: () -> Unit, embe
/** The in-game "On-screen controls" group, for games without a layout of their own. */
@Composable
private fun DefaultControlsGroup(host: MenuHost, revision: Int, onEdit: (Triple<String, String, String>) -> Unit, onChanged: () -> Unit) {
private fun DefaultControlsGroup(host: MenuHost, revision: Int, onEdit: (Triple<String, String, String>) -> Unit, onChanged: () -> Unit,
onFailure: (Throwable) -> Unit) {
val context = LocalContext.current
val options = remember(revision) { ControlsChoice.options(context, null, steamMode = true) }
val choice = remember(revision) { ControlsChoice.current(context, null, steamMode = true) }
@@ -198,7 +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) }
runCatching { ControlsChoice.choose(context, null, id, desktop = false) }.onFailure(onFailure)
onChanged()
}
if (choice != GameControls.STEAM) ActionRow(stringResource(R.string.osc_edit_layout), null, stringResource(R.string.ctrl_edit), {
@@ -129,7 +129,8 @@ private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputB
}
/** Offered when the layout being edited is shared: [create] makes the copy that Done then saves into, and [scope] describes it. */
class InputEditorSplit(val label: String, val scope: String, val create: () -> Pair<String, String>)
/** Turns an edit of a shared layout into a copy for one game: [copy] adds it to the library being saved, [use] picks it once that save succeeded. */
class InputEditorSplit(val label: String, val scope: String, val copy: (InputLibrary, String, String) -> InputLibrary, val use: (String) -> Unit)
@Composable
fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () -> Unit,
@@ -164,8 +165,10 @@ fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () ->
fun leave() { if (draft.dirty) draft.confirmDiscard = true else onDismiss() }
fun save() {
runCatching {
val (layoutTarget, bindingsTarget) = split?.takeIf { splitting }?.create?.invoke() ?: (savedLayout.id to savedBindings.id)
InputLibraries.update(context) { latest ->
val copy = split?.takeIf { splitting }
val (layoutTarget, bindingsTarget) = if (copy != null) newInputId() to newInputId() else savedLayout.id to savedBindings.id
InputLibraries.update(context) { saved ->
val latest = copy?.copy?.invoke(saved, layoutTarget, bindingsTarget) ?: saved
val l = latest.layouts.first { it.id == layoutTarget }
val p = latest.preset(bindingsTarget)
val generated = draft.kitAtDefaults && OnScreenLayout.generated(l.copy(elements = emptyList(), kitless = false))
@@ -174,6 +177,7 @@ fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () ->
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 } }))
}
copy?.use?.invoke(bindingsTarget)
}.onSuccess { onChanged(); onDismiss() }.onFailure { failure = it.message ?: context.getString(R.string.input_save_failed) }
}
DisposableEffect(manager) {
@@ -235,6 +239,7 @@ fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () ->
(remote || target == null) && draft.properties && element != null -> propertiesEditor(element)
target == null -> SettingsPage(rememberMenuHost(), savedLayout.name, ::leave) {
Text(stringResource(R.string.input_display_unavailable))
failure?.let { Text(it, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.error) }
if (draft.confirmDiscard) {
Text(stringResource(R.string.input_editor_discard), style = MaterialTheme.typography.titleSmall)
SecondaryButton(stringResource(R.string.input_keep_editing)) { draft.confirmDiscard = false }
@@ -245,6 +250,7 @@ fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () ->
remote -> SettingsPage(rememberMenuHost(), stringResource(R.string.input_editor_remote), ::leave, lede = editorScope) {
splitLabel?.let { SecondaryButton(it, compact = true, modifier = Modifier.padding(horizontal = 8.dp).testTag("input-editor-split")) { splitting = true } }
SurfaceToolbar(draft, ::save, ::leave, onDismiss)
failure?.let { Text(it, Modifier.padding(horizontal = 8.dp), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.error) }
SurfaceSelection(draft)
when (draft.popover) { EditorPopover.ADD -> AddRows(draft); EditorPopover.HIDDEN -> HiddenRows(draft); null -> Unit }
}
@@ -184,4 +184,12 @@ class GamepadGeometryTest {
for ((slot, box) in b) assertTrue("${panel.name}: $slot", box.top >= -.5f && box.bottom <= panel.controlsHeight + .5f)
}
}
@Test fun gripsNeverOverlapTheClustersOnSmallTallPanels() {
for ((w, h) in listOf(360f to 296f, 480f to 300f, 640f to 420f, 900f to 520f)) {
val b = GamepadGeometry.layout(w, h, 1f, grips = true)
for (g in grips.filter { it in b }) for ((slot, box) in b) if (slot != g)
assertFalse("$w x $h: $g overlaps $slot", b.getValue(g).overlaps(box))
}
}
}
@@ -190,9 +190,9 @@ class InputSettingsTest {
compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", { closed = true }, {}, role = InputRole.PRIMARY,
layoutId = layout.id, bindingsId = library.profile("game").binding(InputRole.PRIMARY),
scope = context.getString(R.string.osc_scope_default, layout.name),
split = InputEditorSplit(context.getString(R.string.osc_just_this_game), "Gamepad · saved for Castle Crashers") {
ControlsChoice.newForGame(context, 7, "Castle Crashers", desktop = false)
}) } }
split = InputEditorSplit(context.getString(R.string.osc_just_this_game), "Gamepad · saved for Castle Crashers",
{ lib, layoutId, bindingsId -> ControlsChoice.copyForGame(context, lib, 7, "Castle Crashers", false, layoutId, bindingsId) },
{ bindingsId -> ControlsChoice.useForGame(context, 7, "Castle Crashers", bindingsId) }) ) } }
compose.onNodeWithTag("input-editor-split").performClick()
compose.onNodeWithText("Gamepad · saved for Castle Crashers").assertExists()
compose.onNodeWithTag("input-editor-split").assertDoesNotExist()