Fix for message composer losing focus in Compose 1.8.0 (#4853)

Co-authored-by: ElementBot <android@element.io>
This commit is contained in:
Jorge Martin Espinosa
2025-06-10 18:39:26 +02:00
committed by GitHub
parent d2e2080489
commit cfb9cc3edc
12 changed files with 46 additions and 25 deletions

View File

@@ -10,7 +10,10 @@
package io.element.android.features.messages.impl
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.SheetValue
import androidx.compose.material3.rememberBottomSheetScaffoldState
@@ -112,7 +115,7 @@ internal fun ExpandableBottomSheetScaffold(
}
SubcomposeLayout(
modifier = modifier,
modifier = modifier.windowInsetsPadding(WindowInsets.ime),
measurePolicy = { constraints: Constraints ->
val sheetContentSub = subcompose(Slot.SheetContent(sheetContentKey)) { sheetContent(true) }.map {
it.measure(Constraints(maxWidth = constraints.maxWidth))
@@ -123,7 +126,7 @@ internal fun ExpandableBottomSheetScaffold(
val dragHandleHeight = dragHandleSub?.height?.toDp() ?: 0.dp
val maxHeight = constraints.maxHeight.toDp()
val contentHeight = sheetContentSub.height.toDp() + dragHandleHeight
val contentHeight = sheetContentSub.measuredHeight.toDp() + dragHandleHeight
contentOverflows = contentHeight > maxHeight
@@ -140,7 +143,7 @@ internal fun ExpandableBottomSheetScaffold(
measurePolicy = { measurables, constraints ->
val constraintHeight = constraints.maxHeight
val offset = tryOrNull { scaffoldState.bottomSheetState.requireOffset() } ?: 0f
val height = Integer.max(0, constraintHeight - offset.roundToInt())
val height = Integer.max(peekHeight.roundToPx(), constraintHeight - offset.roundToInt())
val top = measurables[0].measure(
constraints.copy(
minHeight = height,

View File

@@ -27,6 +27,11 @@ fun View.showKeyboard(andRequestFocus: Boolean = false) {
imm?.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
fun View.isKeyboardVisible(): Boolean {
val imm = context?.getSystemService<InputMethodManager>()
return imm?.isAcceptingText == true
}
suspend fun View.awaitWindowFocus() = suspendCancellableCoroutine { continuation ->
if (hasWindowFocus()) {
continuation.resume(Unit)

View File

@@ -7,6 +7,8 @@
package io.element.android.libraries.textcomposer
import android.os.Build
import android.view.WindowInsets
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
@@ -14,6 +16,7 @@ import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.viewinterop.AndroidView
import io.element.android.libraries.androidutils.ui.awaitWindowFocus
import io.element.android.libraries.androidutils.ui.isKeyboardVisible
import io.element.android.libraries.androidutils.ui.showKeyboard
/**
@@ -40,11 +43,17 @@ internal fun <T> SoftKeyboardEffect(
// Await window focus in case returning from a dialog
view.awaitWindowFocus()
// Show the keyboard, temporarily using the root view for focus
view.showKeyboard(andRequestFocus = true)
if (!view.isKeyboardVisible()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
view.windowInsetsController?.show(WindowInsets.Type.ime())
} else {
// Show the keyboard, temporarily using the root view for focus
view.showKeyboard(andRequestFocus = true)
}
// Refocus to the correct view
latestOnRequestFocus()
// Refocus to the correct view
latestOnRequestFocus()
}
}
}
}

View File

@@ -18,12 +18,14 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeightIn
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
@@ -35,6 +37,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
@@ -432,8 +435,9 @@ private fun TextFormattingLayout(
sendButton: @Composable () -> Unit,
modifier: Modifier = Modifier
) {
val bottomPadding = with(LocalDensity.current) { WindowInsets.systemBars.getBottom(this).toDp() + 8.dp }
Column(
modifier = modifier.padding(vertical = 4.dp),
modifier = modifier.padding(vertical = 4.dp).padding(bottom = bottomPadding),
verticalArrangement = Arrangement.spacedBy(4.dp),
) {
if (isRoomEncrypted == false) {

View File

@@ -8,7 +8,6 @@
package io.element.android.libraries.textcomposer.components.markdown
import android.content.Context
import android.view.View
import androidx.appcompat.widget.AppCompatEditText
internal class MarkdownEditText(
@@ -37,8 +36,4 @@ internal class MarkdownEditText(
onSelectionChangeListener?.invoke(selStart, selEnd)
}
}
override fun focusSearch(direction: Int): View? {
return null
}
}

View File

@@ -118,6 +118,11 @@ fun MarkdownTextInput(
)
}
state.requestFocusAction = { this.requestFocus() }
} else {
isEnabled = false
isFocusable = false
isFocusableInTouchMode = false
isClickable = false
}
}
},