From c1f6817caaaf1deaee989d2e7616929916898a48 Mon Sep 17 00:00:00 2001 From: Anindra Date: Thu, 19 Nov 2026 15:39:56 +0530 Subject: [PATCH 1/2] fix: the attach tray is the tray on Develop again Merging feat/attach-tray kept Develop's older list sheet wired into the chat: AttachTray shipped, the sheet stayed, and attach still dimmed the conversation behind a scrim with the keyboard up underneath. Only the tray's own view was left out of the merge; everything it needs was there. The chat now opens AttachTray in its own bottom bar, under the composer: - the plus turns into the cross that closes the tray, and closing it hands focus back to the field, so the draft is never lost; - opening the tray clears focus and hides the keyboard, and the tray is built to the measured keyboard height, so the composer holds still (0 px across the swap, per AttachTrayHeight); - the composer clears nothing while the tray holds that slot, and its keyboard height comes from ImeGuard, so an OEM that overstates the inset cannot hand the tray a window-sized slot; - Back closes the tray before it leaves the chat; - every picker still goes through launchOrExplain, so a device without a camera app explains itself instead of crashing. AttachSheet is removed with its preview: with the tray wired in it was a second, unused attach UI. JUnit: AttachTrayWiringTest (the tray in the bottom bar, all five tiles, the keyboard swap, the Back handler, the dead sheet) and AudioAttachmentTest reads the tray's Audio tile instead of the sheet's row. scripts/test-attach-tray.sh: 26 checks on emulator-5556, 0 failed; on the sheet build the same script fails 5, including "the keyboard stayed up under the tray" and "the conversation disappeared behind the tray". --- .../messages/ui/previews/ComponentPreviews.kt | 16 - .../com/anindra/messages/ui/ChatScreen.kt | 325 +++++++++++------- .../java/com/anindra/messages/ui/ImeGuard.kt | 17 +- .../messages/ui/AttachTrayWiringTest.kt | 110 ++++++ .../messages/ui/AudioAttachmentTest.kt | 20 +- scripts | 2 +- 6 files changed, 338 insertions(+), 152 deletions(-) create mode 100644 app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt diff --git a/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt b/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt index fe97e951..d008c8d2 100644 --- a/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt +++ b/app/src/debug/java/com/anindra/messages/ui/previews/ComponentPreviews.kt @@ -31,7 +31,6 @@ import com.anindra.messages.data.SimCard import com.anindra.messages.data.SwipeAction import com.anindra.messages.data.DatabaseCompatibility import com.anindra.messages.ui.AttachAction -import com.anindra.messages.ui.AttachSheet import com.anindra.messages.ui.AttachTile import com.anindra.messages.ui.AttachTray import com.anindra.messages.ui.BubblePosition @@ -723,21 +722,6 @@ private fun SuggestionChipsPreview() { } } -@PreviewLightDark -@Preview(name = "Attach sheet", showBackground = true, widthDp = 380, heightDp = 640) -@Composable -private fun AttachSheetPreview() { - MessagesTheme(mode = "system") { - Box( - Modifier - .fillMaxWidth() - .background(MaterialTheme.colorScheme.background) - ) { - AttachSheet(onGallery = {}, onCamera = {}, onAudio = {}, onLocation = {}, onContact = {}, onDismiss = {}) - } - } -} - @PreviewLightDark @Preview(name = "Skeleton rows", showBackground = true, widthDp = 380) @Composable diff --git a/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt b/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt index 03d23f5d..7c74f9e0 100644 --- a/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt +++ b/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt @@ -68,7 +68,10 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.navigationBars import androidx.compose.foundation.layout.navigationBarsPadding +import androidx.compose.foundation.layout.asPaddingValues +import androidx.compose.foundation.layout.ime import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width @@ -120,7 +123,6 @@ import androidx.compose.material3.IconButton import androidx.compose.material3.LocalMinimumInteractiveComponentSize import androidx.compose.material3.LocalTextStyle import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.ModalBottomSheet import androidx.compose.material3.RadioButton import androidx.compose.material3.Scaffold import androidx.compose.material3.SnackbarDuration @@ -140,7 +142,6 @@ import androidx.compose.material3.TextFieldDefaults import androidx.compose.material3.TimePicker import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBarDefaults -import androidx.compose.material3.rememberModalBottomSheetState import androidx.compose.material3.rememberTimePickerState import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider @@ -164,8 +165,10 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.clip +import androidx.compose.ui.draw.rotate import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.graphics.TransformOrigin import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.graphics.Color @@ -182,7 +185,9 @@ import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.platform.LocalLayoutDirection +import androidx.compose.ui.platform.LocalSoftwareKeyboardController import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.AnnotatedString @@ -995,7 +1000,47 @@ fun ChatScreen( var draftLoaded by remember { mutableStateOf(false) } var showEmoji by remember { mutableStateOf(false) } var menuOpen by remember { mutableStateOf(false) } - var attachSheet by remember { mutableStateOf(false) } + // The attachment tray takes the keyboard's place rather than covering the + // chat, so opening it dismisses the IME and closing it gives the field back. + var attachOpen by remember { mutableStateOf(false) } + var showLocationDialog by remember { mutableStateOf(false) } + val fieldFocus = remember { FocusRequester() } + val keyboard = LocalSoftwareKeyboardController.current + val focusManager = LocalFocusManager.current + // The keyboard's height while it is up. The tray takes this slot over, so + // the composer keeps its place across the swap instead of being pushed by + // both for a frame. The height comes from ImeGuard, not the raw report: an + // OEM that overstates the inset would otherwise hand the tray a slot the + // size of the window. + val imeBottom = imeBottomDp() + val navBottom = WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding() + // The keyboard's inset already covers the navigation bar's strip, and the + // bar's own column pads that separately: without taking one out, the + // composer floats a navigation bar above the keyboard. + val keyboardInset = (imeBottom - navBottom).coerceAtLeast(0.dp) + // Remember how tall this device's keyboard is. The tray replaces that slot, + // so it is built to the same height and follows a resized keyboard (a taller + // one in landscape, a different IME, the user's own height setting). + var keyboardSlot by rememberSaveable { mutableFloatStateOf(0f) } + // The IME's inset arrives in animation frames — the first ones describe a + // half-open keyboard — so the height is read again once it has settled. + // Every open re-reads it, which is what makes the tray follow a resized + // keyboard instead of remembering the first one it ever saw. + var imeSeen by remember { mutableFloatStateOf(0f) } + LaunchedEffect(imeBottom) { imeSeen = imeBottom.value } + LaunchedEffect(imeBottom > 0.dp) { + if (imeBottom <= 0.dp) return@LaunchedEffect + delay(450) + val settled = imeSeen - navBottom.value + if (settled > 120f) keyboardSlot = settled + } + LaunchedEffect(imeBottom, navBottom, keyboardSlot, attachOpen) { + android.util.Log.d( + "AttachTrayHeight", + "ime=${imeBottom.value} nav=${navBottom.value} inset=${keyboardInset.value} " + + "slot=$keyboardSlot open=$attachOpen" + ) + } // Picked media waits here until the user sends it, so a picture can be // explained or dropped before it leaves — it is no longer sent on pick. val pendingMedia = remember { mutableStateListOf() } @@ -1204,6 +1249,18 @@ fun ChatScreen( } } + // A device without a camera app, a photo picker, a document picker or a + // contact picker throws instead of opening anything: an unhandled + // ActivityNotFoundException crashed the chat the moment that tile was + // tapped, which is how an AVD with the camera package removed behaved. + fun launchOrExplain(messageRes: Int, launch: () -> Unit) { + try { + launch() + } catch (_: android.content.ActivityNotFoundException) { + Toast.makeText(context, context.getString(messageRes), Toast.LENGTH_SHORT).show() + } + } + fun leaveChat() { vm.saveDraftAndMaybeTrash(conversationId, draft.trim()) onBack() @@ -1441,6 +1498,9 @@ fun ChatScreen( // back closes it instead of leaving the chat. BackHandler(enabled = previewImage != null) { previewImage = null } BackHandler(enabled = backHandled, onBack = ::leaveChat) + // Registered after leave-chat so it wins: the last enabled handler is the + // one Compose calls, and the tray is a keyboard replacement, not a screen. + BackHandler(enabled = attachOpen) { attachOpen = false } BackHandler(enabled = selectionActive) { clearSelection() } BackHandler(enabled = searchOpen) { searchOpen = false @@ -1855,8 +1915,33 @@ fun ChatScreen( showSchedulePicker = true } }, - onEmojiToggle = { showEmoji = !showEmoji }, - onAttach = { attachSheet = true }, + onEmojiToggle = { + attachOpen = false + showEmoji = !showEmoji + }, + onAttach = { + if (attachOpen) { + attachOpen = false + fieldFocus.requestFocus() + } else { + attachOpen = true + showEmoji = false + // Clearing the whole focus owner, not just this + // requester: that is what drops the IME, and the + // tray then grows into the space it left. + focusManager.clearFocus() + keyboard?.hide() + } + }, + attachOpen = attachOpen, + fieldFocusRequester = fieldFocus, + onFieldFocus = { attachOpen = false }, + // While the tray holds the keyboard's slot the composer + // must not reserve the IME's inset as well, or the tray + // lands on top of it. Closed, it clears the keyboard's + // own height: the bottom bar already pads the navigation + // strip, and one out keeps the bar off the keyboard. + composerBottomInset = if (attachOpen) 0.dp else keyboardInset, showEmojiButton = vm.settings.emojiButtonEnabled, voiceEnabled = hasMicrophone, voiceMaxSeconds = voiceMaxSeconds, @@ -1905,6 +1990,71 @@ fun ChatScreen( } else { AlphanumericNotice(address = chatAddress) } + AnimatedVisibility( + visible = attachOpen && !selectionActive, + // No growth animation: the tray takes the keyboard's height + // in the same frame the keyboard's inset is released, so the + // composer does not ride up and back down while they swap. + enter = if (reduceMotion) fadeIn(tween(0)) + else fadeIn(motionTween(false, Motion.DURATION_SHORT4)), + exit = if (reduceMotion) fadeOut(tween(0)) + else fadeOut(motionTween(false, Motion.DURATION_SHORT4)) + ) { + AttachTray( + cameraLabel = stringResource(R.string.chat_media_camera), + cameraIcon = Icons.Rounded.CameraAlt, + onCamera = { + attachOpen = false + val dir = File(context.cacheDir, "camera").apply { mkdirs() } + val file = File(dir, "photo_${System.currentTimeMillis()}.jpg") + cameraFileUri = FileProvider.getUriForFile( + context, "${context.packageName}.fileprovider", file + ) + launchOrExplain(R.string.chat_camera_unavailable) { + cameraLauncher.launch(cameraFileUri!!) + } + }, + keyboardHeight = keyboardSlot.takeIf { it > 0f }?.dp, + actions = listOf( + AttachAction( + label = stringResource(R.string.chat_media_gallery), + icon = Icons.Rounded.Image + ) { + attachOpen = false + launchOrExplain(R.string.chat_gallery_unavailable) { + galleryLauncher.launch( + PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly) + ) + } + }, + AttachAction( + label = stringResource(R.string.chat_media_audio), + icon = Icons.Rounded.Audiotrack + ) { + attachOpen = false + launchOrExplain(R.string.chat_gallery_unavailable) { + audioLauncher.launch(arrayOf("audio/*")) + } + }, + AttachAction( + label = stringResource(R.string.chat_media_location), + icon = Icons.Rounded.LocationOn + ) { + attachOpen = false + showLocationDialog = true + }, + AttachAction( + label = stringResource(R.string.chat_media_contact), + icon = Icons.Rounded.Person + ) { + attachOpen = false + launchOrExplain(R.string.chat_contact_unavailable) { + contactLauncher.launch(null) + } + } + ) + ) + } } } ) { padding -> @@ -2127,8 +2277,6 @@ fun ChatScreen( } } - var showLocationDialog by remember { mutableStateOf(false) } - if (showLocationDialog) { LocationDialog( onDismiss = { showLocationDialog = false }, @@ -2140,52 +2288,6 @@ fun ChatScreen( // contact picker throws instead of opening anything: an unhandled // ActivityNotFoundException crashed the chat the moment that tile was // tapped, which is how an AVD with the camera package removed behaved. - fun launchOrExplain(messageRes: Int, launch: () -> Unit) { - try { - launch() - } catch (_: android.content.ActivityNotFoundException) { - Toast.makeText(context, context.getString(messageRes), Toast.LENGTH_SHORT).show() - } - } - - if (attachSheet) { - AttachSheet( - onGallery = { - attachSheet = false - launchOrExplain(R.string.chat_gallery_unavailable) { - galleryLauncher.launch( - PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly) - ) - } - }, - onAudio = { - attachSheet = false - launchOrExplain(R.string.chat_gallery_unavailable) { - audioLauncher.launch(arrayOf("audio/*")) - } - }, - onLocation = { - attachSheet = false - showLocationDialog = true - }, - onContact = { - attachSheet = false - launchOrExplain(R.string.chat_contact_unavailable) { - contactLauncher.launch(null) - } - }, - onCamera = { - attachSheet = false - val dir = File(context.cacheDir, "camera").apply { mkdirs() } - val file = File(dir, "photo_${System.currentTimeMillis()}.jpg") - cameraFileUri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file) - launchOrExplain(R.string.chat_camera_unavailable) { - cameraLauncher.launch(cameraFileUri!!) - } - }, - onDismiss = { attachSheet = false } - ) - } if (showRetrySimPicker && retryingMessageId > 0) { SimPickerDialog( @@ -3675,6 +3777,14 @@ internal fun InputBar( schedulingEnabled: Boolean = true, onEmojiToggle: () -> Unit, onAttach: () -> Unit, + /** The tray is open under the bar, so the attach button offers to close it. */ + attachOpen: Boolean = false, + /** Lets the caller hand focus back when the tray closes. */ + fieldFocusRequester: FocusRequester? = null, + /** Tapped into the field while the tray was open. */ + onFieldFocus: () -> Unit = {}, + /** Height the bar sits above. Null means the IME's guarded inset decides. */ + composerBottomInset: Dp? = null, showEmojiButton: Boolean = false, hasAttachments: Boolean = false, sims: List = emptyList(), @@ -3752,7 +3862,17 @@ internal fun InputBar( stopRecording(send = true) } } - Column(modifier = Modifier.fillMaxWidth().imeGuardedPadding()) { + Column( + modifier = Modifier + .fillMaxWidth() + // The tray replaces the keyboard's slot, so while it is open the bar + // clears nothing and the tray sits directly under it. Closed, the + // guarded IME inset is the only thing that decides the padding. + .then( + if (composerBottomInset != null) Modifier.padding(bottom = composerBottomInset) + else Modifier.imeGuardedPadding() + ) + ) { if (suggestions.isNotEmpty()) { SuggestionChips( suggestions = suggestions, @@ -3798,14 +3918,26 @@ internal fun InputBar( ) { Box(Modifier.size(48.dp), contentAlignment = Alignment.Center) { IconButton(onClick = onAttach) { + // One ring, one animation: the plus turns 45° into the + // cross that closes the tray, so closing it is the same + // control it was opened with. + val turn by animateFloatAsState( + targetValue = if (attachOpen) 45f else 0f, + animationSpec = motionSpring(LocalReduceMotion.current), + label = "attachTurn" + ) // GM badges the attach button with a dot that overlaps the // ring's top-right; the ring shows through as broken // because the dot carries a ring of field colour around it. Box { Icon( Icons.Rounded.AddCircleOutline, - stringResource(R.string.icon_attach), - tint = MaterialTheme.colorScheme.onSurfaceVariant + stringResource( + if (attachOpen) R.string.common_close + else R.string.icon_attach + ), + tint = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.rotate(turn) ) val dot = MaterialTheme.colorScheme.composerAttachDot // A dot colour has no text equivalent, so it names @@ -3825,6 +3957,9 @@ internal fun InputBar( // GM's dot measures ~5dp across; the 8dp box // is the cut-out ring plus that dot. .size(8.dp) + // The badge belongs to "something to + // attach", so it fades with the turn. + .alpha(if (attachOpen) 0f else 1f) .background(MaterialTheme.colorScheme.inputPill, CircleShape) .padding(1.5.dp) .background(dot, CircleShape) @@ -3852,7 +3987,18 @@ internal fun InputBar( capitalization = androidx.compose.ui.text.input.KeyboardCapitalization.Sentences ), maxLines = 4, - modifier = Modifier.fillMaxWidth().padding(vertical = 16.dp) + modifier = Modifier + .fillMaxWidth() + // Handed back when the tray closes, so the + // crossed attach button returns typing to the + // field it came from. + .then( + if (fieldFocusRequester != null) + Modifier.focusRequester(fieldFocusRequester) + else Modifier + ) + .onFocusChanged { if (it.isFocused) onFieldFocus() } + .padding(vertical = 16.dp) ) } if (SimSwitcher.shouldShowSwitch(sims.size)) { @@ -4320,71 +4466,6 @@ private const val HALO_ALPHA = 0.35f private const val HALO_BREATHE_GROWTH = 0.22f private const val HALO_BREATHE_ALPHA = 0.20f -@OptIn(ExperimentalMaterial3Api::class) -@Composable -internal fun AttachSheet( - onGallery: () -> Unit, - onCamera: () -> Unit, - onAudio: () -> Unit, - onLocation: () -> Unit, - onContact: () -> Unit, - onDismiss: () -> Unit -) { - ModalBottomSheet( - onDismissRequest = onDismiss, - sheetState = rememberModalBottomSheetState() - ) { - Column(Modifier.padding(horizontal = 16.dp, vertical = 12.dp)) { - Text( - stringResource(R.string.chat_share), - style = MaterialTheme.typography.titleMedium, - fontWeight = FontWeight.SemiBold, - modifier = Modifier.padding(bottom = 12.dp) - ) - Row( - Modifier.fillMaxWidth().clickable { onGallery() }.padding(vertical = 14.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon(Icons.Rounded.Image, null, modifier = Modifier.size(24.dp)) - Spacer(Modifier.width(16.dp)) - Text(stringResource(R.string.chat_media_gallery), style = MaterialTheme.typography.bodyLarge) - } - Row( - Modifier.fillMaxWidth().clickable { onContact() }.padding(vertical = 14.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon(Icons.Rounded.Person, null, modifier = Modifier.size(24.dp)) - Spacer(Modifier.width(16.dp)) - Text(stringResource(R.string.chat_media_contact), style = MaterialTheme.typography.bodyLarge) - } - Row( - Modifier.fillMaxWidth().clickable { onLocation() }.padding(vertical = 14.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon(Icons.Rounded.LocationOn, null, modifier = Modifier.size(24.dp)) - Spacer(Modifier.width(16.dp)) - Text(stringResource(R.string.chat_media_location), style = MaterialTheme.typography.bodyLarge) - } - Row( - Modifier.fillMaxWidth().clickable { onAudio() }.padding(vertical = 14.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon(Icons.Rounded.Audiotrack, null, modifier = Modifier.size(24.dp)) - Spacer(Modifier.width(16.dp)) - Text(stringResource(R.string.chat_media_audio), style = MaterialTheme.typography.bodyLarge) - } - Row( - Modifier.fillMaxWidth().clickable { onCamera() }.padding(vertical = 14.dp), - verticalAlignment = Alignment.CenterVertically - ) { - Icon(Icons.Rounded.CameraAlt, null, modifier = Modifier.size(24.dp)) - Spacer(Modifier.width(16.dp)) - Text(stringResource(R.string.chat_media_camera), style = MaterialTheme.typography.bodyLarge) - } - } - } -} - @Composable internal fun SimPickerDialog( sims: List, diff --git a/app/src/main/java/com/anindra/messages/ui/ImeGuard.kt b/app/src/main/java/com/anindra/messages/ui/ImeGuard.kt index 731b6f42..f06e6b12 100644 --- a/app/src/main/java/com/anindra/messages/ui/ImeGuard.kt +++ b/app/src/main/java/com/anindra/messages/ui/ImeGuard.kt @@ -8,6 +8,7 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalView +import androidx.compose.ui.unit.Dp /** What the platform says the keyboard covers, with the lying weeded out. * @@ -69,17 +70,21 @@ object ImeGuard { } } -/** The composer's bottom padding: the reported IME inset, clamped to what the - * window can actually show above the keyboard. */ +/** The keyboard's height right now, with an OEM's dishonest report clamped by + * [ImeGuard]. The composer's padding and the tray's slot are both sized from + * this one number, so the two cannot disagree about how tall the keyboard is. */ @Composable -fun Modifier.imeGuardedPadding(): Modifier { +fun imeBottomDp(): Dp { val density = LocalDensity.current val root = LocalView.current.rootView val reported = ImeGuard.reportedBottomPx(WindowInsets.ime.getBottom(density)) val applied = androidx.compose.runtime.remember(reported) { ImeGuard.appliedBottomPx(root, reported) } - return this.then( - Modifier.padding(bottom = with(density) { applied.toDp() }) - ) + return with(density) { applied.toDp() } } + +/** The composer's bottom padding: the reported IME inset, clamped to what the + * window can actually show above the keyboard. */ +@Composable +fun Modifier.imeGuardedPadding(): Modifier = this.then(Modifier.padding(bottom = imeBottomDp())) diff --git a/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt b/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt new file mode 100644 index 00000000..3b7a5e65 --- /dev/null +++ b/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt @@ -0,0 +1,110 @@ +package com.anindra.messages.ui + +import java.io.File +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +/** + * The tray the attach button opens is the tile grid that sits in the keyboard's + * slot, not the modal sheet it used to open. + * + * Merging the branch that built the tray kept the older `AttachSheet` wired into + * the chat, so `AttachTray` compiled and shipped unused: the screen still dimmed + * the conversation behind a sheet and left the keyboard up underneath it. These + * assertions hold the chat to the tray, in the chat's own bottom bar. + */ +class AttachTrayWiringTest { + + private val chat: String by lazy { + val app = generateSequence(File("").absoluteFile) { it.parentFile } + .map { File(it, "app") } + .firstOrNull { File(it, "src").isDirectory } + ?: error("app/src not found") + File(app, "src/main/java/com/anindra/messages/ui/ChatScreen.kt").readText() + } + + /** The chat's bottom bar: the composer and, under it, the tray. */ + private val bottomBar: String + get() = chat.substringAfter("bottomBar = {").substringBefore(") { padding ->") + + @Test + fun theAttachButtonOpensTheTray() { + assertTrue( + "the chat must draw AttachTray", + bottomBar.contains("AttachTray(") + ) + assertTrue( + "the tray belongs to the bottom bar, under the composer, not to the " + + "screen's root as an overlay", + bottomBar.indexOf("AttachTray(") > bottomBar.indexOf("InputBar(") + ) + } + + @Test + fun theTrayTakesItsHeightFromTheKeyboard() { + assertTrue( + "the tray is built to the keyboard's slot, so the composer holds still", + bottomBar.contains("keyboardHeight = keyboardSlot") + ) + } + + @Test + fun theTrayOffersEveryAction() { + val tray = bottomBar.substringAfter("AttachTray(") + for (resource in listOf( + "chat_media_camera", "chat_media_gallery", "chat_media_audio", + "chat_media_location", "chat_media_contact" + )) { + assertTrue("$resource must have a tile", tray.contains("R.string.$resource")) + } + } + + @Test + fun theOldSheetIsGone() { + assertFalse( + "AttachSheet is dead once the tray is wired in; leaving it behind ships " + + "a second, unused attach UI", + chat.contains("fun AttachSheet(") + ) + } + + @Test + fun openingTheTrayTakesTheKeyboardAway() { + val attach = chat.substringAfter("onAttach = {").substringBefore("showEmojiButton") + assertTrue("opening the tray must clear focus", attach.contains("focusManager.clearFocus()")) + assertTrue("and hide the keyboard", attach.contains("keyboard?.hide()")) + assertTrue( + "closing it must hand the field back", + attach.contains("fieldFocus.requestFocus()") + ) + } + + @Test + fun backClosesTheTrayBeforeTheChat() { + assertTrue( + "Back must close the tray instead of leaving the conversation", + chat.contains("BackHandler(enabled = attachOpen) { attachOpen = false }") + ) + val leaveChat = chat.indexOf("BackHandler(enabled = backHandled, onBack = ::leaveChat)") + val trayBack = chat.indexOf("BackHandler(enabled = attachOpen)") + assertTrue( + "the tray's handler is registered after leave-chat, so it wins", + trayBack > leaveChat + ) + } + + @Test + fun theComposerDoesNotReserveTheKeyboardUnderTheTray() { + assertTrue( + "with the tray open the composer must clear nothing, or the tray " + + "lands on top of it", + chat.contains("composerBottomInset = if (attachOpen) 0.dp else keyboardInset") + ) + assertTrue( + "the bar's height comes from the guarded IME inset, minus the strip " + + "the bottom bar already pads", + chat.contains("val keyboardInset = (imeBottom - navBottom).coerceAtLeast(0.dp)") + ) + } +} diff --git a/app/src/test/java/com/anindra/messages/ui/AudioAttachmentTest.kt b/app/src/test/java/com/anindra/messages/ui/AudioAttachmentTest.kt index 1f5ec2a6..150d1c90 100644 --- a/app/src/test/java/com/anindra/messages/ui/AudioAttachmentTest.kt +++ b/app/src/test/java/com/anindra/messages/ui/AudioAttachmentTest.kt @@ -8,9 +8,9 @@ import java.io.File * Issue #481: an audio file can be attached, not only a voice note. * * The transport already handled `audio` — a voice note is stored that way — so - * what was missing was the way in: a row in the attach sheet and a picker that - * can reach music and recordings. The photo picker cannot, so the entry has to - * go through the document picker. + * what was missing was the way in: an Audio tile in the attach tray and a picker + * that can reach music and recordings. The photo picker cannot, so the entry has + * to go through the document picker. */ class AudioAttachmentTest { @@ -25,10 +25,16 @@ class AudioAttachmentTest { } @Test - fun theAttachSheetOffersAudio() { - val sheet = chat.substringAfter("internal fun AttachSheet(").substringBefore("@Composable") - assertTrue("the sheet must take an onAudio callback", sheet.contains("onAudio: () -> Unit")) - assertTrue("and draw a row for it", sheet.contains("R.string.chat_media_audio")) + fun theAttachTrayOffersAudio() { + val tray = chat.substringAfter("AttachTray(") + assertTrue( + "the tray must offer an Audio tile", + tray.contains("R.string.chat_media_audio") + ) + assertTrue( + "and the tile must be its own action with its own icon", + tray.contains("Icons.Rounded.Audiotrack") + ) } @Test diff --git a/scripts b/scripts index 96984298..3836f4ec 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit 969842981861d4ac5e76554249b96df682d93ba8 +Subproject commit 3836f4ece0171e5cfe70bf2d3306f17b7ec3a955 From b11e8ad87a36ba2e35d2664ee72d1a01133352f3 Mon Sep 17 00:00:00 2001 From: Anindra Date: Thu, 19 Nov 2026 16:11:48 +0530 Subject: [PATCH 2/2] fix: the composer holds its place when the tray swaps out MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tapping the crossed attach button closed the tray and the composer fell to the bottom of the window, then climbed back up as the keyboard rose — a 800 px flicker over the swap. It happened because the composer cleared its inset the moment the tray closed, while the keyboard had not arrived yet: its padding went to zero and the keyboard's own animation pushed it back afterwards. The tray's visibility is now read through a transition state, so the composer clears nothing for as long as the tray is on screen (including its fade-out), and a swap flag holds the slot the tray vacated until the keyboard actually arrives. The tray also leaves in 90 ms instead of 200, so it is off screen before the returning keyboard reaches it rather than hanging over the keys. The tray's fade-in and the whole of the keyboard's rise are unchanged, so the composer sits at the same height throughout: 0 px in the regression script's measurement, and no frame in a screen recording shows it move. scripts/test-attach-tray.sh reads the composer's height from the app's own AttachTrayHeight log for the swap back (27 checks, 0 failed; the build without the hold reports a composer of 283 dp against a 295 dp slot while the keyboard returns). JUnit: AttachTrayWiringTest pins the swap-aware inset, the held slot and the faster tray exit. --- .../com/anindra/messages/ui/ChatScreen.kt | 71 +++++++++++++++---- .../messages/ui/AttachTrayWiringTest.kt | 36 +++++++++- scripts | 2 +- 3 files changed, 92 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt b/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt index 7c74f9e0..1c708b8e 100644 --- a/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt +++ b/app/src/main/java/com/anindra/messages/ui/ChatScreen.kt @@ -21,6 +21,7 @@ import androidx.compose.animation.animateColorAsState import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.FastOutSlowInEasing import androidx.compose.animation.core.LinearEasing +import androidx.compose.animation.core.MutableTransitionState import androidx.compose.animation.core.animateFloat import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.RepeatMode @@ -1034,13 +1035,6 @@ fun ChatScreen( val settled = imeSeen - navBottom.value if (settled > 120f) keyboardSlot = settled } - LaunchedEffect(imeBottom, navBottom, keyboardSlot, attachOpen) { - android.util.Log.d( - "AttachTrayHeight", - "ime=${imeBottom.value} nav=${navBottom.value} inset=${keyboardInset.value} " + - "slot=$keyboardSlot open=$attachOpen" - ) - } // Picked media waits here until the user sends it, so a picture can be // explained or dropped before it leaves — it is no longer sent on pick. val pendingMedia = remember { mutableStateListOf() } @@ -1661,6 +1655,41 @@ fun ChatScreen( } } + // The tray is drawn (or still fading out) and therefore owns the keyboard's + // slot right now. Read off the animation itself, so the composer keeps + // clearing nothing until the tray has actually left. + val tray = remember { MutableTransitionState(false) } + tray.targetState = attachOpen && !selectionActive + val trayOnScreen = tray.currentState || tray.targetState + // The tray just closed to give the field back, and the keyboard is on its + // way up. Until it arrives the composer holds the slot the tray vacated — + // without this it drops to the bottom of the window for a frame and climbs + // back with the keyboard, which is the flicker this is here to stop. + var keyboardSwap by remember { mutableStateOf(false) } + LaunchedEffect(imeBottom) { + if (imeBottom > 0.dp) keyboardSwap = false + } + LaunchedEffect(keyboardSwap) { + if (!keyboardSwap) return@LaunchedEffect + // Focus was asked for but no keyboard ever came: stop holding the slot. + delay(1200) + keyboardSwap = false + } + // What the composer clears: nothing while the tray holds the slot, the + // keyboard's own height while it returns, the guarded inset otherwise. + val composerInset = when { + trayOnScreen -> 0.dp + keyboardSwap -> maxOf(keyboardInset, keyboardSlot.dp) + else -> keyboardInset + } + LaunchedEffect(imeBottom, navBottom, keyboardSlot, attachOpen, keyboardSwap) { + android.util.Log.d( + "AttachTrayHeight", + "ime=${imeBottom.value} nav=${navBottom.value} inset=${keyboardInset.value} " + + "slot=$keyboardSlot composer=${composerInset.value} " + + "open=$attachOpen swap=$keyboardSwap" + ) + } Scaffold( containerColor = MaterialTheme.colorScheme.background, snackbarHost = { SnackbarHost(snackbarHostState) }, @@ -1922,9 +1951,13 @@ fun ChatScreen( onAttach = { if (attachOpen) { attachOpen = false + // The keyboard is coming back, so hold its slot + // from the moment the tray starts leaving. + keyboardSwap = true fieldFocus.requestFocus() } else { attachOpen = true + keyboardSwap = false showEmoji = false // Clearing the whole focus owner, not just this // requester: that is what drops the IME, and the @@ -1935,13 +1968,19 @@ fun ChatScreen( }, attachOpen = attachOpen, fieldFocusRequester = fieldFocus, - onFieldFocus = { attachOpen = false }, + onFieldFocus = { + // Tapping the field while the tray is open is the + // other way back to typing: close the tray into the + // keyboard rather than past it. + if (attachOpen) { + attachOpen = false + keyboardSwap = true + } + }, // While the tray holds the keyboard's slot the composer - // must not reserve the IME's inset as well, or the tray - // lands on top of it. Closed, it clears the keyboard's - // own height: the bottom bar already pads the navigation - // strip, and one out keeps the bar off the keyboard. - composerBottomInset = if (attachOpen) 0.dp else keyboardInset, + // must not reserve an inset as well, or the tray lands on + // top of it. + composerBottomInset = composerInset, showEmojiButton = vm.settings.emojiButtonEnabled, voiceEnabled = hasMicrophone, voiceMaxSeconds = voiceMaxSeconds, @@ -1991,14 +2030,16 @@ fun ChatScreen( AlphanumericNotice(address = chatAddress) } AnimatedVisibility( - visible = attachOpen && !selectionActive, + visibleState = tray, // No growth animation: the tray takes the keyboard's height // in the same frame the keyboard's inset is released, so the // composer does not ride up and back down while they swap. + // It leaves quickly, before the returning keyboard reaches + // it, instead of hanging over the keys for a few frames. enter = if (reduceMotion) fadeIn(tween(0)) else fadeIn(motionTween(false, Motion.DURATION_SHORT4)), exit = if (reduceMotion) fadeOut(tween(0)) - else fadeOut(motionTween(false, Motion.DURATION_SHORT4)) + else fadeOut(tween(90)) ) { AttachTray( cameraLabel = stringResource(R.string.chat_media_camera), diff --git a/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt b/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt index 3b7a5e65..6256b74d 100644 --- a/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt +++ b/app/src/test/java/com/anindra/messages/ui/AttachTrayWiringTest.kt @@ -99,7 +99,7 @@ class AttachTrayWiringTest { assertTrue( "with the tray open the composer must clear nothing, or the tray " + "lands on top of it", - chat.contains("composerBottomInset = if (attachOpen) 0.dp else keyboardInset") + chat.contains("trayOnScreen -> 0.dp") ) assertTrue( "the bar's height comes from the guarded IME inset, minus the strip " + @@ -107,4 +107,38 @@ class AttachTrayWiringTest { chat.contains("val keyboardInset = (imeBottom - navBottom).coerceAtLeast(0.dp)") ) } + + @Test + fun theComposerHoldsItsPlaceWhenTheKeyboardComesBack() { + assertTrue( + "the composer's inset must be the swap-aware one, not the raw keyboard inset", + chat.contains("composerBottomInset = composerInset") + ) + assertTrue( + "closing the tray into the keyboard must hold the slot the tray vacated", + chat.contains("keyboardSwap -> maxOf(keyboardInset, keyboardSlot.dp)") + ) + assertTrue( + "closing the tray by its own button asks for the keyboard back", + chat.contains("keyboardSwap = true\n fieldFocus.requestFocus()") + ) + assertTrue( + "tapping the field while the tray is open is the same swap", + chat.contains("if (attachOpen) {\n attachOpen = false\n keyboardSwap = true\n }") + ) + } + + @Test + fun theTrayLeavesBeforeTheKeyboardArrives() { + assertTrue( + "the tray's visibility is read back by the composer, so it must be " + + "driven through a transition state", + chat.contains("visibleState = tray") + ) + assertTrue( + "the tray fades out faster than the keyboard rises, so it never sits " + + "over the keys", + chat.contains("fadeOut(tween(90))") + ) + } } diff --git a/scripts b/scripts index 3836f4ec..0649647a 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit 3836f4ece0171e5cfe70bf2d3306f17b7ec3a955 +Subproject commit 0649647ae59623f2f23b4d2fdc107506e6551424