@@ -1,5 +1,9 @@
1package soy.iko.opencode.ui.screens
1package soy.iko.opencode.ui.screens
3+import android.content.ClipData
4+import android.content.ClipboardManager
5+import android.content.Context
6+import android.widget.Toast
3import androidx.compose.animation.AnimatedVisibility
7import androidx.compose.animation.AnimatedVisibility
4import androidx.compose.animation.core.RepeatMode
8import androidx.compose.animation.core.RepeatMode
5import androidx.compose.animation.core.animateFloat
9import androidx.compose.animation.core.animateFloat
@@ -10,8 +14,10 @@ import androidx.compose.animation.fadeIn
10import androidx.compose.animation.fadeOut
14import androidx.compose.animation.fadeOut
11import androidx.compose.animation.scaleIn
15import androidx.compose.animation.scaleIn
12import androidx.compose.animation.scaleOut
16import androidx.compose.animation.scaleOut
17+import androidx.compose.foundation.ExperimentalFoundationApi
13import androidx.compose.foundation.background
18import androidx.compose.foundation.background
14import androidx.compose.foundation.clickable
19import androidx.compose.foundation.clickable
20+import androidx.compose.foundation.combinedClickable
15import androidx.compose.foundation.layout.Arrangement
21import androidx.compose.foundation.layout.Arrangement
16import androidx.compose.foundation.layout.Box
22import androidx.compose.foundation.layout.Box
17import androidx.compose.foundation.layout.BoxScope
23import androidx.compose.foundation.layout.BoxScope
@@ -20,6 +26,7 @@ import androidx.compose.foundation.layout.Row
20import androidx.compose.foundation.layout.Spacer
26import androidx.compose.foundation.layout.Spacer
21import androidx.compose.foundation.layout.fillMaxSize
27import androidx.compose.foundation.layout.fillMaxSize
22import androidx.compose.foundation.layout.fillMaxWidth
28import androidx.compose.foundation.layout.fillMaxWidth
29+import androidx.compose.foundation.layout.heightIn
23import androidx.compose.foundation.layout.PaddingValues
30import androidx.compose.foundation.layout.PaddingValues
24import androidx.compose.foundation.layout.WindowInsets
31import androidx.compose.foundation.layout.WindowInsets
25import androidx.compose.foundation.layout.ime
32import androidx.compose.foundation.layout.ime
@@ -36,8 +43,10 @@ import androidx.compose.foundation.text.KeyboardOptions
36import androidx.compose.material.icons.Icons
43import androidx.compose.material.icons.Icons
37import androidx.compose.material.icons.automirrored.filled.ArrowBack
44import androidx.compose.material.icons.automirrored.filled.ArrowBack
38import androidx.compose.material.icons.automirrored.filled.Send
45import androidx.compose.material.icons.automirrored.filled.Send
46+import androidx.compose.material.icons.filled.ChatBubbleOutline
39import androidx.compose.material.icons.filled.Close
47import androidx.compose.material.icons.filled.Close
40import androidx.compose.material.icons.filled.KeyboardArrowDown
48import androidx.compose.material.icons.filled.KeyboardArrowDown
49+import androidx.compose.material.icons.filled.KeyboardArrowUp
41import androidx.compose.material.icons.filled.Stop
50import androidx.compose.material.icons.filled.Stop
42import androidx.compose.material3.CircularProgressIndicator
51import androidx.compose.material3.CircularProgressIndicator
43import androidx.compose.material3.ExperimentalMaterial3Api
52import androidx.compose.material3.ExperimentalMaterial3Api
@@ -52,6 +61,7 @@ import androidx.compose.material3.OutlinedTextField
52import androidx.compose.material3.Scaffold
61import androidx.compose.material3.Scaffold
53import androidx.compose.material3.Surface
62import androidx.compose.material3.Surface
54import androidx.compose.material3.Text
63import androidx.compose.material3.Text
64+import androidx.compose.material3.TextButton
55import androidx.compose.material3.TopAppBar
65import androidx.compose.material3.TopAppBar
56import androidx.compose.runtime.Composable
66import androidx.compose.runtime.Composable
57import androidx.compose.runtime.LaunchedEffect
67import androidx.compose.runtime.LaunchedEffect
@@ -65,6 +75,8 @@ import androidx.compose.ui.Alignment
65import androidx.compose.ui.Modifier
75import androidx.compose.ui.Modifier
66import androidx.compose.ui.draw.clip
76import androidx.compose.ui.draw.clip
67import androidx.compose.ui.hapticfeedback.HapticFeedbackType
77import androidx.compose.ui.hapticfeedback.HapticFeedbackType
78+import androidx.compose.ui.platform.LocalClipboardManager
79+import androidx.compose.ui.platform.LocalContext
68import androidx.compose.ui.platform.LocalDensity
80import androidx.compose.ui.platform.LocalDensity
69import androidx.compose.ui.platform.LocalHapticFeedback
81import androidx.compose.ui.platform.LocalHapticFeedback
70import androidx.compose.ui.text.font.FontStyle
82import androidx.compose.ui.text.font.FontStyle
@@ -77,11 +89,12 @@ import soy.iko.opencode.core.Event
77import soy.iko.opencode.core.UiState
89import soy.iko.opencode.core.UiState
78import soy.iko.opencode.ui.components.MessageBubble
90import soy.iko.opencode.ui.components.MessageBubble
80−@OptIn(ExperimentalMaterial3Api::class)
92+@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
81@Composable
93@Composable
82fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
94fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
83 val listState = rememberLazyListState()
95 val listState = rememberLazyListState()
84 val coroutineScope = rememberCoroutineScope()
96 val coroutineScope = rememberCoroutineScope()
97+ val context = LocalContext.current
86 // Hide the jump-to-bottom FAB while the keyboard is up: it would otherwise
99 // Hide the jump-to-bottom FAB while the keyboard is up: it would otherwise
87 // sit over the top of the IME. Reading the ime inset bottom during composition
100 // 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) {
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 }
103 // Track how many messages the user has "seen" (i.e. was at the bottom for). While
122 // Track how many messages the user has "seen" (i.e. was at the bottom for). While
104 // they're scrolled up, any messages beyond that count are unseen — flag the FAB.
123 // they're scrolled up, any messages beyond that count are unseen — flag the FAB.
105 var lastSeenCount by remember { mutableStateOf(0) }
124 var lastSeenCount by remember { mutableStateOf(0) }
@@ -129,11 +148,20 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
129 topBar = {
148 topBar = {
130 TopAppBar(
149 TopAppBar(
131 title = {
150 title = {
151+ val title = state.currentSessionTitle.ifEmpty { "Chat" }
133− text = state.currentSessionTitle.ifEmpty { "Chat" },
153+ text = if (state.messages.isNotEmpty()) "$title (${state.messages.size})" else title,
134 maxLines = 1,
154 maxLines = 1,
135 overflow = TextOverflow.Ellipsis,
155 overflow = TextOverflow.Ellipsis,
156+ modifier = Modifier.combinedClickable(
136− modifier = Modifier.clickable { dispatch(Event.NavigateToSessions) },
157+ onClick = { dispatch(Event.NavigateToSessions) },
159+ state.currentSessionId?.let { id ->
160+ copyToClipboard(context, id)
161+ Toast.makeText(context, "Session ID copied", Toast.LENGTH_SHORT).show()
139 navigationIcon = {
167 navigationIcon = {
@@ -177,12 +205,24 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
178 Box(Modifier.fillMaxWidth().weight(1f)) {
206 Box(Modifier.fillMaxWidth().weight(1f)) {
179 if (state.messages.isEmpty() && !state.loading) {
207 if (state.messages.isEmpty() && !state.loading) {
181− text = "Send a message to start the conversation.",
182− style = MaterialTheme.typography.bodyMedium,
183− color = MaterialTheme.colorScheme.onSurfaceVariant,
184 modifier = Modifier.align(Alignment.Center).padding(32.dp),
209 modifier = Modifier.align(Alignment.Center).padding(32.dp),
210+ horizontalAlignment = Alignment.CenterHorizontally,
213+ Icons.Filled.ChatBubbleOutline,
214+ contentDescription = null,
215+ modifier = Modifier.size(48.dp),
216+ tint = MaterialTheme.colorScheme.primary.copy(alpha = 0.5f),
218+ Spacer(Modifier.size(16.dp))
220+ text = "Send a message to start the conversation.",
221+ style = MaterialTheme.typography.bodyMedium,
222+ color = MaterialTheme.colorScheme.onSurfaceVariant,
223+ textAlign = TextAlign.Center,
187 LazyColumn(
227 LazyColumn(
188 state = listState,
228 state = listState,
@@ -218,11 +258,25 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
218 onClick = {
258 onClick = {
219 coroutineScope.launch {
259 coroutineScope.launch {
220 if (state.messages.isNotEmpty()) {
260 if (state.messages.isNotEmpty()) {
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 })
221− listState.animateScrollToItem(state.messages.size - 1)
266+ state.messages.size - 1
267+ listState.animateScrollToItem(target)
273+ visible = showScrollTop,
275+ coroutineScope.launch {
276+ listState.animateScrollToItem(0)
@@ -262,6 +316,35 @@ private fun BoxScope.ScrollToBottomButton(visible: Boolean, showBadge: Boolean,
319+// Mirror of ScrollToBottomButton, pinned to the top-start corner. Same BoxScope
320+// extension trick so the top-level AnimatedVisibility overload resolves cleanly.
322+private fun BoxScope.ScrollToTopButton(visible: Boolean, onClick: () -> Unit) {
325+ modifier = Modifier.align(Alignment.TopStart),
326+ enter = scaleIn() + fadeIn(),
327+ exit = scaleOut() + fadeOut(),
329+ Box(Modifier.padding(16.dp)) {
330+ FloatingActionButton(
332+ modifier = Modifier.size(44.dp),
335+ Icons.Filled.KeyboardArrowUp,
336+ contentDescription = "Scroll to top",
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))
265private val dateSeparatorFormat = java.time.format.DateTimeFormatter.ofPattern("MMM d")
348private val dateSeparatorFormat = java.time.format.DateTimeFormatter.ofPattern("MMM d")
267/** Convert an epoch-millis timestamp to the local calendar day it falls on. */
350/** Convert an epoch-millis timestamp to the local calendar day it falls on. */
@@ -414,19 +497,56 @@ private fun ChatInputBar(
414 onStop: () -> Unit,
497 onStop: () -> Unit,
416 val haptic = LocalHapticFeedback.current
499 val haptic = LocalHapticFeedback.current
500+ val clipboardManager = LocalClipboardManager.current
417 Surface(tonalElevation = 3.dp) {
501 Surface(tonalElevation = 3.dp) {
419 modifier = Modifier.fillMaxWidth().imePadding().padding(horizontal = 12.dp, vertical = 8.dp),
503 modifier = Modifier.fillMaxWidth().imePadding().padding(horizontal = 12.dp, vertical = 8.dp),
506+ modifier = Modifier.fillMaxWidth(),
420 verticalAlignment = Alignment.Bottom,
507 verticalAlignment = Alignment.Bottom,
421 horizontalArrangement = Arrangement.spacedBy(8.dp),
508 horizontalArrangement = Arrangement.spacedBy(8.dp),
423 OutlinedTextField(
510 OutlinedTextField(
424 value = draft,
511 value = draft,
425 onValueChange = onDraftChange,
512 onValueChange = onDraftChange,
426− modifier = Modifier.weight(1f),
513+ modifier = Modifier.weight(1f).heightIn(min = 48.dp),
427 enabled = !loading,
514 enabled = !loading,
428 placeholder = { Text("Message opencode…") },
515 placeholder = { Text("Message opencode…") },
429 maxLines = 5,
516 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) {
522+ onClick = { onDraftChange("") },
523+ modifier = Modifier.size(20.dp),
527+ contentDescription = "Clear draft",
528+ modifier = Modifier.size(16.dp),
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) {
540+ text = "${draft.length} characters",
541+ style = MaterialTheme.typography.labelSmall,
542+ color = MaterialTheme.colorScheme.onSurfaceVariant,
543+ textAlign = TextAlign.End,
544+ modifier = Modifier.fillMaxWidth(),
430 keyboardOptions = KeyboardOptions(imeAction = ImeAction.Send),
550 keyboardOptions = KeyboardOptions(imeAction = ImeAction.Send),
431 keyboardActions = KeyboardActions(onSend = {
551 keyboardActions = KeyboardActions(onSend = {
432 if (!generating && !loading && draft.isNotBlank()) onSend()
552 if (!generating && !loading && draft.isNotBlank()) onSend()
@@ -454,5 +574,17 @@ private fun ChatInputBar(
577+ // With nothing typed yet, offer a one-tap way to drop in clipboard contents.
578+ if (draft.isEmpty() && !generating && !loading) {
581+ clipboardManager.getText()?.text?.let { onDraftChange(it) }
583+ contentPadding = PaddingValues(0.dp),