From bd016858c7a3167d3767a650db2e6e6c986f945a Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Thu, 3 Dec 2026 21:49:50 -0500 Subject: [PATCH] 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". --- .../droiddeck/launcher/ui/AgentPetWindows.kt | 15 ++++-- .../launcher/ui/AgentPetChatFocusTest.kt | 47 +++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/ui/AgentPetChatFocusTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AgentPetWindows.kt b/app/src/main/java/com/droiddeck/launcher/ui/AgentPetWindows.kt index 72e5d0b635..e2381ca2a4 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AgentPetWindows.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AgentPetWindows.kt @@ -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) } } } diff --git a/app/src/test/java/com/droiddeck/launcher/ui/AgentPetChatFocusTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/AgentPetChatFocusTest.kt new file mode 100644 index 0000000000..e5214e1e98 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/AgentPetChatFocusTest.kt @@ -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) + } +}