Keep adaptive sticks visible at rest

Separate adaptive stick capture from idle visibility and keep dock touch capture bounded.
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-12-05 23:33:11 -05:00
1 parent a02ea07386
commit cad55fc2b9
11 files changed
+79 -14

No files matched your search

@@ -156,7 +156,13 @@ object InputLibraries {
update(context) { library ->
library.copy(layouts = library.layouts.map { layout ->
if (layout.id != layoutId || !layout.editable) layout
else layout.copy(elements = layout.elements.map { if (it.kind == InputElementKind.STICK) it.copy(floating = on, hideWhenIdle = on) else it })
else {
fun updateSticks(elements: List<InputSurfaceElement>) = elements.map {
if (it.kind == InputElementKind.STICK) it.copy(floating = on, hideWhenIdle = it.hideStickAtRest, adaptiveCapture = on)
else it
}
layout.copy(elements = updateSticks(layout.elements), rotated = updateSticks(layout.rotated))
}
})
}
}.onSuccess { ControllerPrefs.setAdaptiveSticks(context, on) }.isSuccess
@@ -328,7 +334,8 @@ object InputLibraries {
for (e in list) elements.put(JSONObject().put("id", e.id).put("kind", e.kind.name)
.put("control", e.control.id).put("name", e.name).put("x", e.x.toDouble()).put("y", e.y.toDouble())
.put("width", e.width.toDouble()).put("height", e.height.toDouble()).put("floating", e.floating).put("hideWhenIdle", e.hideWhenIdle).put("mouseGestures", e.mouseGestures).put("toggle", e.toggle).put("icon", e.icon).put("image", e.image)
.put("elementOpacity", e.opacity.toDouble()).put("buttonShape", e.buttonShape.name).put("showLabel", e.showLabel).put("alwaysShown", e.alwaysShown))
.put("elementOpacity", e.opacity.toDouble()).put("buttonShape", e.buttonShape.name).put("showLabel", e.showLabel).put("alwaysShown", e.alwaysShown)
.put("adaptiveCapture", e.adaptiveCapture))
}
private fun decodeElements(array: JSONArray) = array.objects(64).map { e ->
@@ -336,7 +343,8 @@ object InputLibraries {
e.getDouble("y").toFloat(), e.getDouble("width").toFloat(), e.getDouble("height").toFloat(),
e.getString("id"), InputElementKind.valueOf(e.getString("kind")), e.getString("name"),
e.optBoolean("floating", true), e.optBoolean("hideWhenIdle", false), e.optBoolean("mouseGestures", false), e.optBoolean("toggle", false), e.optInt("icon", 0), e.optString("image", ""),
e.optDouble("elementOpacity", 1.0).toFloat(), InputButtonShape.valueOf(e.optString("buttonShape", InputButtonShape.ROUNDED.name)), e.optBoolean("showLabel", true), e.optBoolean("alwaysShown", false))
e.optDouble("elementOpacity", 1.0).toFloat(), InputButtonShape.valueOf(e.optString("buttonShape", InputButtonShape.ROUNDED.name)), e.optBoolean("showLabel", true), e.optBoolean("alwaysShown", false),
if (e.isNull("adaptiveCapture")) null else e.optBoolean("adaptiveCapture", false))
}
internal fun encode(library: InputLibrary): String {
@@ -32,7 +32,7 @@ data class SavedInputLayout(
fun withQuickButton(button: InputCommand, shown: Boolean) =
copy(style = style.copy(hidden = if (shown) style.hidden - button else style.hidden + button))
fun derived(id: String, name: String) = copy(id = id, name = name, origin = origin ?: this.id)
val adaptiveSticks get() = elements.filter { it.kind == InputElementKind.STICK }.takeIf { editable && it.isNotEmpty() }?.all { it.floating && it.hideWhenIdle }
val adaptiveSticks get() = elements.filter { it.kind == InputElementKind.STICK }.takeIf { editable && it.isNotEmpty() }?.all { it.adaptiveStick }
fun supports(role: InputRole) = screen.supports(role) && when (template) {
InputLayoutTemplate.OVERLAY -> role == InputRole.PRIMARY
InputLayoutTemplate.DECK, InputLayoutTemplate.TRACKPAD -> role == InputRole.SECONDARY
@@ -37,7 +37,12 @@ data class InputSurfaceElement(
val buttonShape: InputButtonShape = InputButtonShape.ROUNDED,
val showLabel: Boolean = true,
val alwaysShown: Boolean = false,
val adaptiveCapture: Boolean? = null,
) {
// Old layouts encoded Adaptive sticks as Floating + Hide when idle.
val adaptiveStick get() = kind == InputElementKind.STICK && floating && (adaptiveCapture ?: hideWhenIdle)
val hideStickAtRest get() = hideWhenIdle && (adaptiveCapture != null || !adaptiveStick)
val hasClick get() = kind == InputElementKind.STICK || kind == InputElementKind.TRACKPAD
fun bounded() = copy(x = x.coerceIn(0f, 0.97f), y = y.coerceIn(0f, 0.97f),
@@ -188,7 +188,7 @@ internal class LegacyGamepadLayout(private val context: Context, private val wid
settings.mapping[it.id] != ControllerPrefs.OFF && (it.id != "guide" || settings.steamButton) && (it.id != "qam" || settings.qamButton)
}.map { c ->
InputSurfaceElement(requireNotNull(InputControl.fromId(c.id)), (c.cx - c.halfW) / width, (c.cy - c.halfH) / height,
2 * c.halfW / width, 2 * c.halfH / height, floating = settings.adaptiveSticks, hideWhenIdle = c.stick >= 0 && settings.adaptiveSticks,
2 * c.halfW / width, 2 * c.halfH / height, floating = settings.adaptiveSticks, adaptiveCapture = if (c.stick >= 0) settings.adaptiveSticks else null,
buttonShape = if (c.id in shoulderIds) InputButtonShape.PILL else InputButtonShape.CIRCLE).let { if (previousStickGeometry) widenStickPreviously(it) else widenStick(it) }.bounded()
}.toMutableList()
if (settings.keyboardButton) result += keyboard()
@@ -37,8 +37,10 @@ object OnScreenLayout {
buttonsOnly: Boolean = false, dock: Dock? = null, rearButtons: Boolean? = null,
trackpads: Boolean? = null): List<InputSurfaceElement> {
val placed = placeElements(context, saved, width, height, safe, buttonsOnly, dock, rearButtons, trackpads)
// Below the game a stick covers nothing, so it keeps its base in view instead of appearing only under the thumb.
return if (dock == null) placed else placed.map { if (it.kind == InputElementKind.STICK) it.copy(hideWhenIdle = false) else it }
// Docked sticks stay visible and only pick up touches inside their own bounds.
return if (dock == null) placed else placed.map {
if (it.kind == InputElementKind.STICK) it.copy(hideWhenIdle = false, adaptiveCapture = false) else it
}
}
private fun placeElements(context: Context, saved: SavedInputLayout?, width: Int, height: Int, safe: Rect,
@@ -67,8 +67,18 @@ internal fun InputElementProperties(element: InputSurfaceElement, action: InputA
}
if (element.kind == InputElementKind.STICK) {
val host = rememberMenuHost()
ToggleRow(host, "element-floating", stringResource(R.string.input_floating_stick), null, element.floating) { onChange(element.copy(floating = it)) }
ToggleRow(host, "element-hide-idle", stringResource(R.string.input_hide_idle), null, element.hideWhenIdle) { onChange(element.copy(hideWhenIdle = it)) }
val adaptive = element.adaptiveCapture ?: element.adaptiveStick
ToggleRow(host, "element-floating", stringResource(R.string.input_floating_stick), null, element.floating) {
onChange(element.copy(floating = it, hideWhenIdle = element.hideStickAtRest, adaptiveCapture = adaptive))
}
if (element.floating) {
ToggleRow(host, "element-adaptive-capture", stringResource(R.string.input_adaptive_capture), null, element.adaptiveStick) {
onChange(element.copy(hideWhenIdle = element.hideStickAtRest, adaptiveCapture = it))
}
}
ToggleRow(host, "element-hide-idle", stringResource(R.string.input_hide_idle), null, element.hideStickAtRest) {
onChange(element.copy(hideWhenIdle = it, adaptiveCapture = adaptive))
}
}
val host = rememberMenuHost()
if ((element.kind == InputElementKind.BUTTON || element.kind == InputElementKind.TRIGGER) && element.icon == 0 && element.image.isEmpty() ||
@@ -236,11 +236,11 @@ class InputSurfaceView(
super.dispatchDraw(canvas)
}
}
canvas.clipChildren = customLayout.none { it.kind == InputElementKind.STICK && (classic(it) || adaptive(it)) }
canvas.clipChildren = customLayout.none { it.kind == InputElementKind.STICK && (classic(it) || it.adaptiveStick) }
for (element in customLayout) {
val source = pad?.let { InputSurfaceSource(it, inputRole, element).also { s -> elementSources += s } }
canvas.addView(when (element.kind) {
InputElementKind.STICK -> StickView(context, element, source, element.floating, element.hideWhenIdle && pad != null)
InputElementKind.STICK -> StickView(context, element, source, element.floating, element.hideStickAtRest && pad != null)
InputElementKind.TRACKPAD -> TrackpadView(context, element.control == InputControl.RIGHT_PAD, captioned = element.showLabel,
stock = element.id == "legacy:${element.control.id}", source = source)
InputElementKind.DPAD -> DirectionPad(context, element, source)
@@ -641,7 +641,7 @@ class InputSurfaceView(
private val ring = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE }
private val knob = Paint(Paint.ANTI_ALIAS_FLAG)
val adaptive get() = adaptive(element)
val adaptive get() = element.adaptiveStick
fun reach() = min(width, height) * if (classic(element)) CLASSIC_REACH else PANEL_REACH
@@ -859,8 +859,6 @@ class InputSurfaceView(
private val TEXT_DIM = Color.rgb(0x7A, 0x80, 0x8A)
private val KNOB = Color.rgb(0x2A, 0x2D, 0x33)
private fun adaptive(element: InputSurfaceElement) = element.kind == InputElementKind.STICK && element.floating && element.hideWhenIdle
private fun classic(element: InputSurfaceElement) = (element.id == "legacy:${element.control.id}" || element.id == "builtin:keyboard") &&
(element.buttonShape == InputButtonShape.CIRCLE || element.buttonShape == InputButtonShape.PILL)
@@ -74,6 +74,7 @@
<string name="input_keep_shared">Keep shared</string>
<string name="input_independent_copy">Independent copy</string>
<string name="input_floating_stick">Start stick at touch position</string>
<string name="input_adaptive_capture">Adaptive touch area</string>
<string name="input_hide_idle">Hide stick when not in use</string>
<string name="input_tools">Tools</string>
<string name="input_quick_auto">Auto (%1$s)</string>
@@ -467,6 +467,7 @@ class InputLibraryTest {
val enabled = InputLibraries.read(context)
val changed = enabled.layouts.single { it.id == overlay.id }
assertEquals(true, changed.adaptiveSticks)
assertTrue(changed.elements.filter { it.kind == InputElementKind.STICK }.all { !it.hideStickAtRest && it.adaptiveCapture == true })
assertTrue(ControllerPrefs.read(context).adaptiveSticks)
assertEquals(overlay.elements.filter { it.kind != InputElementKind.STICK }, changed.elements.filter { it.kind != InputElementKind.STICK })
assertEquals(library.layouts.filter { it.id != overlay.id }, enabled.layouts.filter { it.id != overlay.id })
@@ -336,6 +336,20 @@ class OnScreenLayoutTest {
}
}
@Test fun dockedSticksStayVisibleWithoutExpandingTheirTouchArea() {
val custom = InputSurfaceElement(InputControl.LS, .2f, .2f, .2f, .3f,
id = "custom-stick", hideWhenIdle = true, adaptiveCapture = true)
val layout = SavedInputLayout("custom", "Custom", listOf(custom), aspect = 1.5f)
val overlay = OnScreenLayout.elements(context, layout, 1200, 800, Rect()).single()
assertTrue(overlay.adaptiveStick)
assertTrue(overlay.hideStickAtRest)
val docked = OnScreenLayout.elements(context, layout, 1200, 800, Rect(),
dock = OnScreenLayout.Dock(false, false)).single()
assertFalse(docked.adaptiveStick)
assertFalse(docked.hideStickAtRest)
assertTrue(docked.floating)
}
@Test fun dockedControlsStayInsideTheSafeAreaAtTheDefaultSizeWithoutAControllerKit() {
SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_NONE)
ControllerPrefs.setSize(context, 100)
@@ -1,6 +1,9 @@
package com.droiddeck.launcher.ui
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.view.HapticFeedbackConstants
import android.view.MotionEvent
import android.view.View
@@ -18,6 +21,7 @@ import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34], manifest = Config.NONE, shadows = [JoyConPadTest.RecordingWriter::class])
@@ -355,6 +359,28 @@ class InputSurfaceTouchTest {
touch(MotionEvent.ACTION_UP, 1 to (490f to 170f))
}
@Test @GraphicsMode(GraphicsMode.Mode.NATIVE)
fun adaptiveSticksDrawAtRestUnlessExplicitlyHidden() {
val legacy = InputSurfaceElement(InputControl.LS, .3f, .3f, .2f, .4f, hideWhenIdle = true)
fun idlePixel(e: InputSurfaceElement): Int {
show(e)
val target = element(view, e.id)
val bitmap = Bitmap.createBitmap(target.width, target.height, Bitmap.Config.ARGB_8888)
return try {
target.draw(Canvas(bitmap))
bitmap.getPixel(target.width / 2, target.height / 2)
} finally {
bitmap.recycle()
}
}
assertTrue(Color.alpha(idlePixel(legacy)) > 0)
val visible = legacy.copy(adaptiveCapture = true, hideWhenIdle = false)
assertTrue(Color.alpha(idlePixel(visible)) > 0)
assertTrue(view.takesTouchAt(10f, 300f))
assertEquals(0, Color.alpha(idlePixel(visible.copy(hideWhenIdle = true))))
assertTrue(view.takesTouchAt(10f, 300f))
}
@Test fun adaptiveSticksCaptureAroundThemAndTheLeftOneToTheEdge() {
val left = InputSurfaceElement(InputControl.LS, .3f, .3f, .2f, .4f, hideWhenIdle = true)
val right = InputSurfaceElement(InputControl.RS, .6f, .3f, .2f, .4f, hideWhenIdle = true)