@@ -1,9 +1,5 @@
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
73import androidx.compose.animation.AnimatedVisibility
84import androidx.compose.animation.core.RepeatMode
95import androidx.compose.animation.core.animateFloat
@@ -14,10 +10,8 @@ import androidx.compose.animation.fadeIn
1410import androidx.compose.animation.fadeOut
1511import androidx.compose.animation.scaleIn
1612import androidx.compose.animation.scaleOut
17−import androidx.compose.foundation.ExperimentalFoundationApi
1813import androidx.compose.foundation.background
1914import androidx.compose.foundation.clickable
20−import androidx.compose.foundation.combinedClickable
2115import androidx.compose.foundation.layout.Arrangement
2216import androidx.compose.foundation.layout.Box
2317import androidx.compose.foundation.layout.BoxScope
@@ -26,7 +20,6 @@ import androidx.compose.foundation.layout.Row
2620import androidx.compose.foundation.layout.Spacer
2721import androidx.compose.foundation.layout.fillMaxSize
2822import androidx.compose.foundation.layout.fillMaxWidth
29−import androidx.compose.foundation.layout.heightIn
3023import androidx.compose.foundation.layout.PaddingValues
3124import androidx.compose.foundation.layout.WindowInsets
3225import androidx.compose.foundation.layout.ime
@@ -43,10 +36,8 @@ import androidx.compose.foundation.text.KeyboardOptions
4336import androidx.compose.material.icons.Icons
4437import androidx.compose.material.icons.automirrored.filled.ArrowBack
4538import androidx.compose.material.icons.automirrored.filled.Send
46−import androidx.compose.material.icons.filled.ChatBubbleOutline
4739import androidx.compose.material.icons.filled.Close
4840import androidx.compose.material.icons.filled.KeyboardArrowDown
49−import androidx.compose.material.icons.filled.KeyboardArrowUp
5041import androidx.compose.material.icons.filled.Stop
5142import androidx.compose.material3.CircularProgressIndicator
5243import androidx.compose.material3.ExperimentalMaterial3Api
@@ -61,7 +52,6 @@ import androidx.compose.material3.OutlinedTextField
6152import androidx.compose.material3.Scaffold
6253import androidx.compose.material3.Surface
6354import androidx.compose.material3.Text
64−import androidx.compose.material3.TextButton
6555import androidx.compose.material3.TopAppBar
6656import androidx.compose.runtime.Composable
6757import androidx.compose.runtime.LaunchedEffect
@@ -75,8 +65,6 @@ import androidx.compose.ui.Alignment
7565import androidx.compose.ui.Modifier
7666import androidx.compose.ui.draw.clip
7767import androidx.compose.ui.hapticfeedback.HapticFeedbackType
78−import androidx.compose.ui.platform.LocalClipboardManager
79−import androidx.compose.ui.platform.LocalContext
8068import androidx.compose.ui.platform.LocalDensity
8169import androidx.compose.ui.platform.LocalHapticFeedback
8270import androidx.compose.ui.text.font.FontStyle
@@ -89,12 +77,11 @@ import soy.iko.opencode.core.Event
8977import soy.iko.opencode.core.UiState
9078import soy.iko.opencode.ui.components.MessageBubble
9179
92−@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
80+@OptIn(ExperimentalMaterial3Api::class)
9381@Composable
9482fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
9583 val listState = rememberLazyListState()
9684 val coroutineScope = rememberCoroutineScope()
97− val context = LocalContext.current
9885
9986 // Hide the jump-to-bottom FAB while the keyboard is up: it would otherwise
10087 // 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) {
113100 }
114101 }
115102
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−
122103 // Track how many messages the user has "seen" (i.e. was at the bottom for). While
123104 // they're scrolled up, any messages beyond that count are unseen — flag the FAB.
124105 var lastSeenCount by remember { mutableStateOf(0) }
@@ -153,15 +134,7 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
153134 text = if (state.messages.isNotEmpty()) "$title (${state.messages.size})" else title,
154135 maxLines = 1,
155136 overflow = TextOverflow.Ellipsis,
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− ),
137+ modifier = Modifier.clickable { dispatch(Event.NavigateToSessions) },
165138 )
166139 },
167140 navigationIcon = {
@@ -205,24 +178,12 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
205178 }
206179 Box(Modifier.fillMaxWidth().weight(1f)) {
207180 if (state.messages.isEmpty() && !state.loading) {
208− Column(
181+ Text(
182+ text = "Send a message to start the conversation.",
183+ style = MaterialTheme.typography.bodyMedium,
184+ color = MaterialTheme.colorScheme.onSurfaceVariant,
209185 modifier = Modifier.align(Alignment.Center).padding(32.dp),
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− }
186+ )
226187 }
227188 LazyColumn(
228189 state = listState,
@@ -258,25 +219,11 @@ fun ChatScreen(state: UiState, dispatch: (Event) -> Unit) {
258219 onClick = {
259220 coroutineScope.launch {
260221 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 })
264− state.messages.size
265− else
266− state.messages.size - 1
267− listState.animateScrollToItem(target)
222+ listState.animateScrollToItem(state.messages.size - 1)
268223 }
269224 }
270225 },
271226 )
272− ScrollToTopButton(
273− visible = showScrollTop,
274− onClick = {
275− coroutineScope.launch {
276− listState.animateScrollToItem(0)
277− }
278− },
279− )
280227 }
281228 }
282229 }
@@ -316,35 +263,6 @@ private fun BoxScope.ScrollToBottomButton(visible: Boolean, showBadge: Boolean,
316263 }
317264}
318265
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−
348266private val dateSeparatorFormat = java.time.format.DateTimeFormatter.ofPattern("MMM d")
349267
350268/** Convert an epoch-millis timestamp to the local calendar day it falls on. */
@@ -497,56 +415,19 @@ private fun ChatInputBar(
497415 onStop: () -> Unit,
498416) {
499417 val haptic = LocalHapticFeedback.current
500− val clipboardManager = LocalClipboardManager.current
501418 Surface(tonalElevation = 3.dp) {
502− Column(
503− modifier = Modifier.fillMaxWidth().imePadding().padding(horizontal = 12.dp, vertical = 8.dp),
504− ) {
505419 Row(
506− modifier = Modifier.fillMaxWidth(),
420+ modifier = Modifier.fillMaxWidth().imePadding().padding(horizontal = 12.dp, vertical = 8.dp),
507421 verticalAlignment = Alignment.Bottom,
508422 horizontalArrangement = Arrangement.spacedBy(8.dp),
509423 ) {
510424 OutlinedTextField(
511425 value = draft,
512426 onValueChange = onDraftChange,
513− modifier = Modifier.weight(1f).heightIn(min = 48.dp),
427+ modifier = Modifier.weight(1f),
514428 enabled = !loading,
515429 placeholder = { Text("Message opencode…") },
516430 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− },
550431 keyboardOptions = KeyboardOptions(imeAction = ImeAction.Send),
551432 keyboardActions = KeyboardActions(onSend = {
552433 if (!generating && !loading && draft.isNotBlank()) onSend()
@@ -574,17 +455,5 @@ private fun ChatInputBar(
574455 }
575456 }
576457 }
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− }
589458 }
590459}