@@ -1,9 +1,5 @@
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
7import androidx.compose.animation.AnimatedVisibility
3import androidx.compose.animation.AnimatedVisibility
8import androidx.compose.animation.core.RepeatMode
4import androidx.compose.animation.core.RepeatMode
9import androidx.compose.animation.core.animateFloat
5import androidx.compose.animation.core.animateFloat
@@ -14,10 +10,8 @@ import androidx.compose.animation.fadeIn
14import androidx.compose.animation.fadeOut
10import androidx.compose.animation.fadeOut
15import androidx.compose.animation.scaleIn
11import androidx.compose.animation.scaleIn
16import androidx.compose.animation.scaleOut
12import androidx.compose.animation.scaleOut
17−import androidx.compose.foundation.ExperimentalFoundationApi
18import androidx.compose.foundation.background
13import androidx.compose.foundation.background
19import androidx.compose.foundation.clickable
14import androidx.compose.foundation.clickable
20−import androidx.compose.foundation.combinedClickable
21import androidx.compose.foundation.layout.Arrangement
15import androidx.compose.foundation.layout.Arrangement
22import androidx.compose.foundation.layout.Box
16import androidx.compose.foundation.layout.Box
23import androidx.compose.foundation.layout.BoxScope
17import androidx.compose.foundation.layout.BoxScope
@@ -26,7 +20,6 @@ import androidx.compose.foundation.layout.Row
26import androidx.compose.foundation.layout.Spacer
20import androidx.compose.foundation.layout.Spacer
27import androidx.compose.foundation.layout.fillMaxSize
21import androidx.compose.foundation.layout.fillMaxSize
28import androidx.compose.foundation.layout.fillMaxWidth
22import androidx.compose.foundation.layout.fillMaxWidth
29−import androidx.compose.foundation.layout.heightIn
30import androidx.compose.foundation.layout.PaddingValues
23import androidx.compose.foundation.layout.PaddingValues
31import androidx.compose.foundation.layout.WindowInsets
24import androidx.compose.foundation.layout.WindowInsets
32import androidx.compose.foundation.layout.ime
25import androidx.compose.foundation.layout.ime
@@ -43,10 +36,8 @@ import androidx.compose.foundation.text.KeyboardOptions
43import androidx.compose.material.icons.Icons
36import androidx.compose.material.icons.Icons
44import androidx.compose.material.icons.automirrored.filled.ArrowBack
37import androidx.compose.material.icons.automirrored.filled.ArrowBack
45import androidx.compose.material.icons.automirrored.filled.Send
38import androidx.compose.material.icons.automirrored.filled.Send
46−import androidx.compose.material.icons.filled.ChatBubbleOutline
47import androidx.compose.material.icons.filled.Close
39import androidx.compose.material.icons.filled.Close
48import androidx.compose.material.icons.filled.KeyboardArrowDown
40import androidx.compose.material.icons.filled.KeyboardArrowDown
49−import androidx.compose.material.icons.filled.KeyboardArrowUp
50import androidx.compose.material.icons.filled.Stop
41import androidx.compose.material.icons.filled.Stop
51import androidx.compose.material3.CircularProgressIndicator
42import androidx.compose.material3.CircularProgressIndicator
52import androidx.compose.material3.ExperimentalMaterial3Api
43import androidx.compose.material3.ExperimentalMaterial3Api
@@ -61,7 +52,6 @@ import androidx.compose.material3.OutlinedTextField
61import androidx.compose.material3.Scaffold
52import androidx.compose.material3.Scaffold
62import androidx.compose.material3.Surface
53import androidx.compose.material3.Surface
63import androidx.compose.material3.Text
54import androidx.compose.material3.Text
64−import androidx.compose.material3.TextButton
65import androidx.compose.material3.TopAppBar
55import androidx.compose.material3.TopAppBar
66import androidx.compose.runtime.Composable
56import androidx.compose.runtime.Composable
67import androidx.compose.runtime.LaunchedEffect
57import androidx.compose.runtime.LaunchedEffect
@@ -75,8 +65,6 @@ import androidx.compose.ui.Alignment
75import androidx.compose.ui.Modifier
65import androidx.compose.ui.Modifier
76import androidx.compose.ui.draw.clip
66import androidx.compose.ui.draw.clip
77import androidx.compose.ui.hapticfeedback.HapticFeedbackType
67import androidx.compose.ui.hapticfeedback.HapticFeedbackType
78−import androidx.compose.ui.platform.LocalClipboardManager
79−import androidx.compose.ui.platform.LocalContext
80import androidx.compose.ui.platform.LocalDensity
68import androidx.compose.ui.platform.LocalDensity
81import androidx.compose.ui.platform.LocalHapticFeedback
69import androidx.compose.ui.platform.LocalHapticFeedback
82import androidx.compose.ui.text.font.FontStyle
70import androidx.compose.ui.text.font.FontStyle
@@ -89,12 +77,11 @@ import soy.iko.opencode.core.Event
89import soy.iko.opencode.core.UiState
77import soy.iko.opencode.core.UiState
90import soy.iko.opencode.ui.components.MessageBubble
78import soy.iko.opencode.ui.components.MessageBubble
92−@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
80+@OptIn(ExperimentalMaterial3Api::class)
93@Composable
81@Composable
94fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
82fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
95 val listState = rememberLazyListState()
83 val listState = rememberLazyListState()
96 val coroutineScope = rememberCoroutineScope()
84 val coroutineScope = rememberCoroutineScope()
97− val context = LocalContext.current
99 // Hide the jump-to-bottom FAB while the keyboard is up: it would otherwise
86 // Hide the jump-to-bottom FAB while the keyboard is up: it would otherwise
100 // sit over the top of the IME. Reading the ime inset bottom during composition
87 // sit over the top of the IME. Reading the ime inset bottom during composition
@@ -113,12 +100,6 @@ 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 }
122 // Track how many messages the user has "seen" (i.e. was at the bottom for). While
103 // Track how many messages the user has "seen" (i.e. was at the bottom for). While
123 // they're scrolled up, any messages beyond that count are unseen — flag the FAB.
104 // they're scrolled up, any messages beyond that count are unseen — flag the FAB.
124 var lastSeenCount by remember { mutableStateOf(0) }
105 var lastSeenCount by remember { mutableStateOf(0) }
@@ -153,15 +134,7 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
153 text = if (state.messages.isNotEmpty()) "$title (${state.messages.size})" else title,
134 text = if (state.messages.isNotEmpty()) "$title (${state.messages.size})" else title,
154 maxLines = 1,
135 maxLines = 1,
155 overflow = TextOverflow.Ellipsis,
136 overflow = TextOverflow.Ellipsis,
156− modifier = Modifier.combinedClickable(
157− onClick = { dispatch(Event.NavigateToSessions) },
137+ modifier = Modifier.clickable { dispatch(Event.NavigateToSessions) },
159− state.currentSessionId?.let { id ->
160− copyToClipboard(context, id)
161− Toast.makeText(context, "Session ID copied", Toast.LENGTH_SHORT).show()
167 navigationIcon = {
140 navigationIcon = {
@@ -205,24 +178,12 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
206 Box(Modifier.fillMaxWidth().weight(1f)) {
179 Box(Modifier.fillMaxWidth().weight(1f)) {
207 if (state.messages.isEmpty() && !state.loading) {
180 if (state.messages.isEmpty() && !state.loading) {
182+ text = "Send a message to start the conversation.",
183+ style = MaterialTheme.typography.bodyMedium,
184+ color = MaterialTheme.colorScheme.onSurfaceVariant,
209 modifier = Modifier.align(Alignment.Center).padding(32.dp),
185 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,
227 LazyColumn(
188 LazyColumn(
228 state = listState,
189 state = listState,
@@ -258,25 +219,11 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
258 onClick = {
219 onClick = {
259 coroutineScope.launch {
220 coroutineScope.launch {
260 if (state.messages.isNotEmpty()) {
221 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 })
266− state.messages.size - 1
222+ listState.animateScrollToItem(state.messages.size - 1)
267− listState.animateScrollToItem(target)
273− visible = showScrollTop,
275− coroutineScope.launch {
276− listState.animateScrollToItem(0)
@@ -316,35 +263,6 @@ 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))
348private val dateSeparatorFormat = java.time.format.DateTimeFormatter.ofPattern("MMM d")
266private val dateSeparatorFormat = java.time.format.DateTimeFormatter.ofPattern("MMM d")
350/** Convert an epoch-millis timestamp to the local calendar day it falls on. */
268/** Convert an epoch-millis timestamp to the local calendar day it falls on. */
@@ -497,56 +415,19 @@ private fun ChatInputBar(
497 onStop: () -> Unit,
415 onStop: () -> Unit,
499 val haptic = LocalHapticFeedback.current
417 val haptic = LocalHapticFeedback.current
500− val clipboardManager = LocalClipboardManager.current
501 Surface(tonalElevation = 3.dp) {
418 Surface(tonalElevation = 3.dp) {
503− modifier = Modifier.fillMaxWidth().imePadding().padding(horizontal = 12.dp, vertical = 8.dp),
506− modifier = Modifier.fillMaxWidth(),
420+ modifier = Modifier.fillMaxWidth().imePadding().padding(horizontal = 12.dp, vertical = 8.dp),
507 verticalAlignment = Alignment.Bottom,
421 verticalAlignment = Alignment.Bottom,
508 horizontalArrangement = Arrangement.spacedBy(8.dp),
422 horizontalArrangement = Arrangement.spacedBy(8.dp),
510 OutlinedTextField(
424 OutlinedTextField(
511 value = draft,
425 value = draft,
512 onValueChange = onDraftChange,
426 onValueChange = onDraftChange,
513− modifier = Modifier.weight(1f).heightIn(min = 48.dp),
427+ modifier = Modifier.weight(1f),
514 enabled = !loading,
428 enabled = !loading,
515 placeholder = { Text("Message opencode…") },
429 placeholder = { Text("Message opencode…") },
516 maxLines = 5,
430 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(),
550 keyboardOptions = KeyboardOptions(imeAction = ImeAction.Send),
431 keyboardOptions = KeyboardOptions(imeAction = ImeAction.Send),
551 keyboardActions = KeyboardActions(onSend = {
432 keyboardActions = KeyboardActions(onSend = {
552 if (!generating && !loading && draft.isNotBlank()) onSend()
433 if (!generating && !loading && draft.isNotBlank()) onSend()
@@ -574,17 +455,5 @@ 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),