Merge pull request 'android: keep the chat composer above the keyboard (ime insets)' (#67) from wt/chat-ime-insets into main
Reviewed-on: #67
This commit is contained in:
commit
ba261a86eb
5 changed files with 161 additions and 21 deletions
|
|
@ -2,10 +2,12 @@ package com.siriusdevops.onionwire.ui
|
|||
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
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
|
||||
|
|
@ -15,11 +17,16 @@ import androidx.compose.foundation.layout.width
|
|||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.itemsIndexed
|
||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.KeyboardActions
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.LocalContentColor
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
|
|
@ -34,10 +41,14 @@ import androidx.compose.runtime.remember
|
|||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.semantics.contentDescription
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.KeyboardCapitalization
|
||||
import androidx.compose.ui.unit.dp
|
||||
import kotlinx.coroutines.delay
|
||||
import uniffi.onionwire_sdk.ChatMessage
|
||||
|
|
@ -73,6 +84,18 @@ fun ChatScreen(
|
|||
var notice by remember { mutableStateOf<String?>(null) }
|
||||
val listState = rememberLazyListState()
|
||||
|
||||
// The composer is the only place a message can be sent from, so after a
|
||||
// send the keyboard is left up and the caret goes back into the field —
|
||||
// the next message is one keystroke away instead of a re-tap.
|
||||
val inputFocus = remember { FocusRequester() }
|
||||
val send: () -> Unit = {
|
||||
if (draft.isNotBlank() && pending == null) {
|
||||
pending = draft
|
||||
draft = ""
|
||||
inputFocus.requestFocus()
|
||||
}
|
||||
}
|
||||
|
||||
// Poll: the node appends inbound messages in the background and the retry
|
||||
// worker moves queued rows along; there is no push channel into the UI in
|
||||
// v1, so the delivery markers refresh on the same cadence as the bodies.
|
||||
|
|
@ -139,8 +162,17 @@ fun ChatScreen(
|
|||
),
|
||||
)
|
||||
},
|
||||
// The top bar carries its own status-bar inset; the content root applies
|
||||
// systemBars + IME once (see `screenContentInsets`) so the navigation
|
||||
// bar is not counted twice while the keyboard is up.
|
||||
contentWindowInsets = WindowInsets(0, 0, 0, 0),
|
||||
) { pad ->
|
||||
Column(Modifier.padding(pad).fillMaxSize()) {
|
||||
Column(
|
||||
Modifier
|
||||
.padding(pad)
|
||||
.fillMaxSize()
|
||||
.screenContentInsets(),
|
||||
) {
|
||||
Text(
|
||||
"onion: $onion",
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
|
|
@ -152,14 +184,21 @@ fun ChatScreen(
|
|||
val list = messages
|
||||
when {
|
||||
list == null -> {
|
||||
Spacer(Modifier.height(16.dp))
|
||||
CircularProgressIndicator(Modifier.padding(16.dp))
|
||||
// The body region always claims the leftover height, so the
|
||||
// composer below it can never be pushed off the bottom —
|
||||
// least of all by the keyboard.
|
||||
Box(Modifier.weight(1f).fillMaxWidth(), contentAlignment = Alignment.Center) {
|
||||
CircularProgressIndicator(Modifier.padding(16.dp))
|
||||
}
|
||||
}
|
||||
|
||||
list.isEmpty() -> {
|
||||
Column(
|
||||
Modifier.fillMaxWidth().padding(24.dp),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
Modifier
|
||||
.weight(1f)
|
||||
.fillMaxWidth()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(24.dp),
|
||||
) {
|
||||
Text("No messages", style = MaterialTheme.typography.titleSmall)
|
||||
Spacer(Modifier.height(6.dp))
|
||||
|
|
@ -274,28 +313,52 @@ fun ChatScreen(
|
|||
)
|
||||
}
|
||||
|
||||
// The composer sticks to the bottom of the window: `screenContentInsets`
|
||||
// already lifted it above the keyboard, and its row is never allowed
|
||||
// to be squeezed out by the messages above it (they hold the weight).
|
||||
Row(
|
||||
Modifier.fillMaxWidth().padding(8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(8.dp)
|
||||
.testTag("chat-composer"),
|
||||
verticalAlignment = Alignment.Bottom,
|
||||
) {
|
||||
OutlinedTextField(
|
||||
value = draft,
|
||||
onValueChange = { draft = it },
|
||||
modifier = Modifier.weight(1f),
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.focusRequester(inputFocus)
|
||||
.testTag("chat-input"),
|
||||
maxLines = 4,
|
||||
label = { Text("message") },
|
||||
// The IME gets its own send affordance: on a phone in
|
||||
// landscape, or with the keyboard covering the button, the
|
||||
// action key is the shortest path to sending.
|
||||
keyboardOptions = KeyboardOptions(
|
||||
capitalization = KeyboardCapitalization.Sentences,
|
||||
imeAction = ImeAction.Send,
|
||||
),
|
||||
keyboardActions = KeyboardActions(onSend = { send() }),
|
||||
)
|
||||
Spacer(Modifier.width(8.dp))
|
||||
if (pending != null) {
|
||||
CircularProgressIndicator()
|
||||
} else {
|
||||
Button(
|
||||
enabled = draft.isNotBlank(),
|
||||
onClick = {
|
||||
pending = draft
|
||||
draft = ""
|
||||
},
|
||||
) { Text("Send") }
|
||||
// Always drawn, never swapped out for a spinner: a button that
|
||||
// changes size under the thumb — or disappears — is how "I hit
|
||||
// send and nothing happened" gets reported.
|
||||
Button(
|
||||
enabled = draft.isNotBlank() && pending == null,
|
||||
onClick = send,
|
||||
modifier = Modifier.testTag("chat-send"),
|
||||
) {
|
||||
if (pending != null) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(14.dp),
|
||||
strokeWidth = 2.dp,
|
||||
color = LocalContentColor.current,
|
||||
)
|
||||
Spacer(Modifier.width(6.dp))
|
||||
}
|
||||
Text("Send")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
package com.siriusdevops.onionwire.ui
|
||||
|
||||
import androidx.compose.foundation.layout.WindowInsets
|
||||
import androidx.compose.foundation.layout.WindowInsetsSides
|
||||
import androidx.compose.foundation.layout.ime
|
||||
import androidx.compose.foundation.layout.only
|
||||
import androidx.compose.foundation.layout.systemBars
|
||||
import androidx.compose.foundation.layout.union
|
||||
import androidx.compose.foundation.layout.windowInsetsPadding
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
|
||||
/**
|
||||
* The bottom + horizontal window insets one screen's content root must apply
|
||||
* itself.
|
||||
*
|
||||
* `MainActivity` calls `enableEdgeToEdge()`, which means
|
||||
* `setDecorFitsSystemWindows(window, false)`. On API 30+ that stops the
|
||||
* framework from resizing the window for the soft keyboard: the manifest's
|
||||
* `android:windowSoftInputMode="adjustResize"` has nothing left to resize, and
|
||||
* the IME arrives as a *window inset* instead. `Scaffold`'s
|
||||
* `contentWindowInsets` only covers `systemBars`, so anything anchored to the
|
||||
* bottom of the window — the chat composer, a submit button under a text field
|
||||
* — ends up drawn behind the keyboard, reachable only by dismissing it.
|
||||
*
|
||||
* The IME's bottom inset already covers the navigation bar, so the two are
|
||||
* unioned (per-side `max`), never summed: keyboard height while typing,
|
||||
* navigation-bar height otherwise, and no double padding in between.
|
||||
*
|
||||
* The `Top` side is deliberately excluded — a `Scaffold`'s `pad.top` is the app
|
||||
* bar's own measured height, and the app bar carries its own status-bar inset
|
||||
* (`TopAppBarDefaults.windowInsets`).
|
||||
*
|
||||
* Pair every use with `Scaffold(contentWindowInsets = WindowInsets(0, 0, 0, 0))`
|
||||
* so the same inset is not applied twice.
|
||||
*
|
||||
* `@Composable` because `WindowInsets.systemBars` / `WindowInsets.ime` are
|
||||
* composable reads — call it from a composable (every call site is one).
|
||||
*/
|
||||
@Composable
|
||||
fun Modifier.screenContentInsets(): Modifier =
|
||||
windowInsetsPadding(
|
||||
WindowInsets.systemBars.union(WindowInsets.ime)
|
||||
.only(WindowInsetsSides.Horizontal + WindowInsetsSides.Bottom),
|
||||
)
|
||||
|
|
@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.Arrangement
|
|||
import androidx.compose.foundation.layout.Column
|
||||
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
|
||||
|
|
@ -62,9 +63,17 @@ fun ShareInviteScreen(ownerInvite: suspend () -> String, onBack: () -> Unit) {
|
|||
),
|
||||
)
|
||||
},
|
||||
// The root applies systemBars + IME itself (see `screenContentInsets`),
|
||||
// so Scaffold must not also pad for the navigation bar.
|
||||
contentWindowInsets = WindowInsets(0, 0, 0, 0),
|
||||
) { pad ->
|
||||
Column(
|
||||
Modifier.padding(pad).fillMaxSize().verticalScroll(rememberScrollState()).padding(16.dp),
|
||||
Modifier
|
||||
.padding(pad)
|
||||
.fillMaxSize()
|
||||
.screenContentInsets()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(16.dp),
|
||||
) {
|
||||
Text(
|
||||
"Hand this to one person, over a channel you already trust. " +
|
||||
|
|
@ -149,9 +158,18 @@ fun PasteInviteScreen(
|
|||
),
|
||||
)
|
||||
},
|
||||
// Same inset handoff as the chat composer: with the keyboard up, the
|
||||
// Verify / Approve buttons under the invite field must still be
|
||||
// reachable by scrolling, not by dismissing the keyboard.
|
||||
contentWindowInsets = WindowInsets(0, 0, 0, 0),
|
||||
) { pad ->
|
||||
Column(
|
||||
Modifier.padding(pad).fillMaxSize().verticalScroll(rememberScrollState()).padding(16.dp),
|
||||
Modifier
|
||||
.padding(pad)
|
||||
.fillMaxSize()
|
||||
.screenContentInsets()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(16.dp),
|
||||
) {
|
||||
OutlinedTextField(
|
||||
value = raw,
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.Arrangement
|
|||
import androidx.compose.foundation.layout.Column
|
||||
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
|
||||
|
|
@ -59,9 +60,17 @@ fun RotateScreen(
|
|||
),
|
||||
)
|
||||
},
|
||||
// Root owns systemBars + IME (see `screenContentInsets`) so the confirm
|
||||
// button under the passphrase field stays reachable with the keyboard up.
|
||||
contentWindowInsets = WindowInsets(0, 0, 0, 0),
|
||||
) { pad ->
|
||||
Column(
|
||||
Modifier.padding(pad).fillMaxSize().verticalScroll(rememberScrollState()).padding(16.dp),
|
||||
Modifier
|
||||
.padding(pad)
|
||||
.fillMaxSize()
|
||||
.screenContentInsets()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(16.dp),
|
||||
) {
|
||||
Text("Rotate onion address?", style = MaterialTheme.typography.titleLarge)
|
||||
Spacer(Modifier.height(12.dp))
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ 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.safeDrawingPadding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
|
|
@ -68,6 +69,10 @@ fun UnlockScreen(
|
|||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
// This screen has no Scaffold, so its root owns every window inset:
|
||||
// without this the keyboard covers the passphrase field's Open
|
||||
// button and the status bar overlaps the title.
|
||||
.safeDrawingPadding()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(24.dp),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
|
|
|
|||
Loading…
Reference in a new issue