fix(agent): keys reach the pet's editor after its chat closes and opens again (#3666)

The pet's chat panel absorbed taps with a clickable disabled while the
chat was closed. A disabled clickable drops its focus node, so closing the
chat removed a node above the still-focused editor and reopening put back a
new, unfocused one in its place. Compose then found no focus target for key
events: backspace and other key presses were dropped, and with the pet's
view focused, DroidDeck crashed with "Event can't be processed because we
do not have an active focus target".
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-12-03 21:49:50 -05:00
1 parent f761e5f1db
commit bd016858c7
2 files changed
+59 -3

No files matched your search

@@ -21,6 +21,7 @@ import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
@@ -495,7 +496,13 @@ internal class PetWindows(private val context: Context) {
* under it. Its contents are built while it is closed, so opening only animates.
*/
@Composable
internal fun PetChat(open: Boolean, motion: PetMotion, onClose: () -> Unit, onClosed: () -> Unit) {
internal fun PetChat(
open: Boolean, motion: PetMotion, onClose: () -> Unit,
page: @Composable ColumnScope.(AgentMenuSlot) -> Unit = { slot ->
AgentPage(Modifier.weight(1f).fillMaxWidth(), visible = open, startTyping = AgentHost.entries.isEmpty(), hidesPet = false, menuSlot = slot)
},
onClosed: () -> Unit,
) {
val colors = MaterialTheme.colorScheme
val pal = LocalPalette.current
val density = androidx.compose.ui.platform.LocalDensity.current
@@ -552,14 +559,16 @@ internal fun PetChat(open: Boolean, motion: PetMotion, onClose: () -> Unit, onCl
alpha = fade.value
}
.clip(Shape16).background(colors.background).border(1.dp, pal.line, Shape16)
.clickable(remember { MutableInteractionSource() }, null, enabled = open) {}
// Never disabled: a disabled clickable leaves the focus tree, and the one it puts back on reopening
// sits unfocused above the still-focused editor, where key events then find no focus target.
.clickable(remember { MutableInteractionSource() }, null) {}
.padding(start = 12.dp, end = 12.dp, top = 6.dp),
) {
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth().padding(bottom = 2.dp)) {
Box(Modifier.weight(1f)) { menuSlot.content?.invoke() }
HeaderAction(Icons.Outlined.Close, stringResource(R.string.agent_close), size = 34.dp, tag = "agent-pet-close", onClick = onClose)
}
AgentPage(Modifier.weight(1f).fillMaxWidth(), visible = open, startTyping = AgentHost.entries.isEmpty(), hidesPet = false, menuSlot = menuSlot)
page(menuSlot)
}
}
}
@@ -0,0 +1,47 @@
package com.droiddeck.launcher.ui
import android.view.KeyEvent as AndroidKeyEvent
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performKeyPress
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [33], qualifiers = "w411dp-h891dp-port", application = android.app.Application::class)
class AgentPetChatFocusTest {
@get:Rule val compose = createRobolectricComposeRule()
@Test fun keysStillReachTheEditorAfterTheChatClosesAndOpensAgain() {
val open = mutableStateOf(true)
val text = mutableStateOf("ab")
val motion = PetMotion(RuntimeEnvironment.getApplication())
compose.setContent {
DroidDeckTheme {
PetChat(open.value, motion, onClose = { open.value = false }, page = {
BasicTextField(text.value, { text.value = it }, Modifier.testTag("editor"))
}) {}
}
}
compose.onNodeWithTag("editor").performClick()
open.value = false
compose.waitForIdle()
open.value = true
compose.waitForIdle()
compose.onNodeWithTag("editor").performKeyPress(KeyEvent(AndroidKeyEvent(AndroidKeyEvent.ACTION_DOWN, AndroidKeyEvent.KEYCODE_DEL)))
compose.waitForIdle()
assertEquals("a", text.value)
}
}