@@ -1,5 +1,9 @@
11package soy.iko.opencode.ui.screens
22
3+import android.content.ClipData
4+import android.content.ClipboardManager
5+import android.content.Context
6+import android.widget.Toast
37import androidx.compose.animation.AnimatedVisibility
48import androidx.compose.animation.core.RepeatMode
59import androidx.compose.animation.core.animateFloat
@@ -10,8 +14,10 @@ import androidx.compose.animation.fadeIn
1014import androidx.compose.animation.fadeOut
1115import androidx.compose.animation.scaleIn
1216import androidx.compose.animation.scaleOut
17+import androidx.compose.foundation.ExperimentalFoundationApi
1318import androidx.compose.foundation.background
1419import androidx.compose.foundation.clickable
20+import androidx.compose.foundation.combinedClickable
1521import androidx.compose.foundation.layout.Arrangement
1622import androidx.compose.foundation.layout.Box
1723import androidx.compose.foundation.layout.BoxScope
@@ -20,6 +26,7 @@ import androidx.compose.foundation.layout.Row
2026import androidx.compose.foundation.layout.Spacer
2127import androidx.compose.foundation.layout.fillMaxSize
2228import androidx.compose.foundation.layout.fillMaxWidth
29+import androidx.compose.foundation.layout.heightIn
2330import androidx.compose.foundation.layout.PaddingValues
2431import androidx.compose.foundation.layout.WindowInsets
2532import androidx.compose.foundation.layout.ime
@@ -36,8 +43,10 @@ import androidx.compose.foundation.text.KeyboardOptions
3643import androidx.compose.material.icons.Icons
3744import androidx.compose.material.icons.automirrored.filled.ArrowBack
3845import androidx.compose.material.icons.automirrored.filled.Send
46+import androidx.compose.material.icons.filled.ChatBubbleOutline
3947import androidx.compose.material.icons.filled.Close
4048import androidx.compose.material.icons.filled.KeyboardArrowDown
49+import androidx.compose.material.icons.filled.KeyboardArrowUp
4150import androidx.compose.material.icons.filled.Stop
4251import androidx.compose.material3.CircularProgressIndicator
4352import androidx.compose.material3.ExperimentalMaterial3Api
@@ -52,6 +61,7 @@ import androidx.compose.material3.OutlinedTextField
5261import androidx.compose.material3.Scaffold
5362import androidx.compose.material3.Surface
5463import androidx.compose.material3.Text
64+import androidx.compose.material3.TextButton
5565import androidx.compose.material3.TopAppBar
5666import androidx.compose.runtime.Composable
5767import androidx.compose.runtime.LaunchedEffect
@@ -65,6 +75,8 @@ import androidx.compose.ui.Alignment
6575import androidx.compose.ui.Modifier
6676import androidx.compose.ui.draw.clip
6777import androidx.compose.ui.hapticfeedback.HapticFeedbackType
78+import androidx.compose.ui.platform.LocalClipboardManager
79+import androidx.compose.ui.platform.LocalContext
6880import androidx.compose.ui.platform.LocalDensity
6981import androidx.compose.ui.platform.LocalHapticFeedback
7082import androidx.compose.ui.text.font.FontStyle
@@ -77,11 +89,12 @@ import soy.iko.opencode.core.Event
7789import soy.iko.opencode.core.UiState
7890import soy.iko.opencode.ui.components.MessageBubble
7991
80−@OptIn(ExperimentalMaterial3Api::class)
92+@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
8193@Composable
8294fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
8395 val listState = rememberLazyListState()
8496 val coroutineScope = rememberCoroutineScope()
97+ val context = LocalContext.current
8598
8699 // Hide the jump-to-bottom FAB while the keyboard is up: it would otherwise
87100 // sit over the top of the IME. Reading the ime inset bottom during composition
@@ -100,6 +113,12 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
100113 }
101114 }
102115
116+ // Offer a jump-to-top shortcut once the conversation is long and the user has
117+ // scrolled a few messages down — saves flick-scrolling back through history.
118+ val showScrollTop by remember {
119+ derivedStateOf { state.messages.size > 10 && listState.firstVisibleItemIndex > 3 }
120+ }
121+
103122 // Track how many messages the user has "seen" (i.e. was at the bottom for). While
104123 // they're scrolled up, any messages beyond that count are unseen — flag the FAB.
105124 var lastSeenCount by remember { mutableStateOf(0) }
@@ -134,7 +153,15 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
134153 text = if (state.messages.isNotEmpty()) "$title (${state.messages.size})" else title,
135154 maxLines = 1,
136155 overflow = TextOverflow.Ellipsis,
137− modifier = Modifier.clickable { dispatch(Event.NavigateToSessions) },
156+ modifier = Modifier.combinedClickable(
157+ onClick = { dispatch(Event.NavigateToSessions) },
158+ onLongClick = {
159+ state.currentSessionId?.let { id ->
160+ copyToClipboard(context, id)
161+ Toast.makeText(context, "Session ID copied", Toast.LENGTH_SHORT).show()
162+ }
163+ },
164+ ),
138165 )
139166 },
140167 navigationIcon = {
@@ -178,12 +205,24 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
178205 }
179206 Box(Modifier.fillMaxWidth().weight(1f)) {
180207 if (state.messages.isEmpty() && !state.loading) {
181− Text(
182− text = "Send a message to start the conversation.",
183− style = MaterialTheme.typography.bodyMedium,
184− color = MaterialTheme.colorScheme.onSurfaceVariant,
208+ Column(
185209 modifier = Modifier.align(Alignment.Center).padding(32.dp),
186− )
210+ horizontalAlignment = Alignment.CenterHorizontally,
211+ ) {
212+ Icon(
213+ Icons.Filled.ChatBubbleOutline,
214+ contentDescription = null,
215+ modifier = Modifier.size(48.dp),
216+ tint = MaterialTheme.colorScheme.primary.copy(alpha = 0.5f),
217+ )
218+ Spacer(Modifier.size(16.dp))
219+ Text(
220+ text = "Send a message to start the conversation.",
221+ style = MaterialTheme.typography.bodyMedium,
222+ color = MaterialTheme.colorScheme.onSurfaceVariant,
223+ textAlign = TextAlign.Center,
224+ )
225+ }
187226 }
188227 LazyColumn(
189228 state = listState,
@@ -219,11 +258,25 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
219258 onClick = {
220259 coroutineScope.launch {
221260 if (state.messages.isNotEmpty()) {
222− listState.animateScrollToItem(state.messages.size - 1)
261+ // When the typing indicator is showing it occupies the item
262+ // just past the last message, so scroll one further to reach it.
263+ val target = if (state.generating && state.messages.none { it.streaming })
264+ state.messages.size
265+ else
266+ state.messages.size - 1
267+ listState.animateScrollToItem(target)
223268 }
224269 }
225270 },
226271 )
272+ ScrollToTopButton(
273+ visible = showScrollTop,
274+ onClick = {
275+ coroutineScope.launch {
276+ listState.animateScrollToItem(0)
277+ }
278+ },
279+ )
227280 }
228281 }
229282 }
@@ -263,6 +316,35 @@ private fun BoxScope.ScrollToBottomButton(visible: Boolean, showBadge: Boolean,
263316 }
264317}
265318
319+// Mirror of ScrollToBottomButton, pinned to the top-start corner. Same BoxScope
320+// extension trick so the top-level AnimatedVisibility overload resolves cleanly.
321+@Composable
322+private fun BoxScope.ScrollToTopButton(visible: Boolean, onClick: () -> Unit) {
323+ AnimatedVisibility(
324+ visible = visible,
325+ modifier = Modifier.align(Alignment.TopStart),
326+ enter = scaleIn() + fadeIn(),
327+ exit = scaleOut() + fadeOut(),
328+ ) {
329+ Box(Modifier.padding(16.dp)) {
330+ FloatingActionButton(
331+ onClick = onClick,
332+ modifier = Modifier.size(44.dp),
333+ ) {
334+ Icon(
335+ Icons.Filled.KeyboardArrowUp,
336+ contentDescription = "Scroll to top",
337+ )
338+ }
339+ }
340+ }
341+}
342+
343+private fun copyToClipboard(context: Context, text: String) {
344+ val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
345+ clipboard.setPrimaryClip(ClipData.newPlainText("session", text))
346+}
347+
266348private val dateSeparatorFormat = java.time.format.DateTimeFormatter.ofPattern("MMM d")
267349
268350/** Convert an epoch-millis timestamp to the local calendar day it falls on. */
@@ -415,19 +497,56 @@ private fun ChatInputBar(
415497 onStop: () -> Unit,
416498) {
417499 val haptic = LocalHapticFeedback.current
500+ val clipboardManager = LocalClipboardManager.current
418501 Surface(tonalElevation = 3.dp) {
419− Row(
502+ Column(
420503 modifier = Modifier.fillMaxWidth().imePadding().padding(horizontal = 12.dp, vertical = 8.dp),
504+ ) {
505+ Row(
506+ modifier = Modifier.fillMaxWidth(),
421507 verticalAlignment = Alignment.Bottom,
422508 horizontalArrangement = Arrangement.spacedBy(8.dp),
423509 ) {
424510 OutlinedTextField(
425511 value = draft,
426512 onValueChange = onDraftChange,
427− modifier = Modifier.weight(1f),
513+ modifier = Modifier.weight(1f).heightIn(min = 48.dp),
428514 enabled = !loading,
429515 placeholder = { Text("Message opencode…") },
430516 maxLines = 5,
517+ // Offer a quick way to wipe the draft once there's something to clear
518+ // and we're not mid-generation (where the field is a poor place to fuss).
519+ trailingIcon = if (draft.isNotEmpty() && !generating) {
520+ {
521+ IconButton(
522+ onClick = { onDraftChange("") },
523+ modifier = Modifier.size(20.dp),
524+ ) {
525+ Icon(
526+ Icons.Filled.Close,
527+ contentDescription = "Clear draft",
528+ modifier = Modifier.size(16.dp),
529+ )
530+ }
531+ }
532+ } else {
533+ null
534+ },
535+ // Surface a running count once the draft gets long, so the user has a
536+ // sense of scale before sending a wall of text.
537+ supportingText = if (draft.length > 500) {
538+ {
539+ Text(
540+ text = "${draft.length} characters",
541+ style = MaterialTheme.typography.labelSmall,
542+ color = MaterialTheme.colorScheme.onSurfaceVariant,
543+ textAlign = TextAlign.End,
544+ modifier = Modifier.fillMaxWidth(),
545+ )
546+ }
547+ } else {
548+ null
549+ },
431550 keyboardOptions = KeyboardOptions(imeAction = ImeAction.Send),
432551 keyboardActions = KeyboardActions(onSend = {
433552 if (!generating && !loading && draft.isNotBlank()) onSend()
@@ -455,5 +574,17 @@ private fun ChatInputBar(
455574 }
456575 }
457576 }
577+ // With nothing typed yet, offer a one-tap way to drop in clipboard contents.
578+ if (draft.isEmpty() && !generating && !loading) {
579+ TextButton(
580+ onClick = {
581+ clipboardManager.getText()?.text?.let { onDraftChange(it) }
582+ },
583+ contentPadding = PaddingValues(0.dp),
584+ ) {
585+ Text("Paste")
586+ }
587+ }
588+ }
458589 }
459590}