|
| 1 | +package cc.unitmesh.devins.idea.toolwindow |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.layout.* |
| 5 | +import androidx.compose.foundation.lazy.LazyColumn |
| 6 | +import androidx.compose.foundation.lazy.items |
| 7 | +import androidx.compose.foundation.lazy.rememberLazyListState |
| 8 | +import androidx.compose.foundation.text.input.rememberTextFieldState |
| 9 | +import androidx.compose.runtime.* |
| 10 | +import androidx.compose.ui.Alignment |
| 11 | +import androidx.compose.ui.Modifier |
| 12 | +import androidx.compose.ui.input.key.* |
| 13 | +import androidx.compose.ui.text.font.FontWeight |
| 14 | +import androidx.compose.ui.unit.dp |
| 15 | +import androidx.compose.ui.unit.sp |
| 16 | +import kotlinx.coroutines.flow.distinctUntilChanged |
| 17 | +import org.jetbrains.jewel.foundation.theme.JewelTheme |
| 18 | +import org.jetbrains.jewel.ui.Orientation |
| 19 | +import org.jetbrains.jewel.ui.component.* |
| 20 | +import org.jetbrains.jewel.ui.theme.defaultBannerStyle |
| 21 | + |
| 22 | +/** |
| 23 | + * Main Compose application for AutoDev Chat. |
| 24 | + * |
| 25 | + * Uses Jewel theme for native IntelliJ IDEA integration. |
| 26 | + */ |
| 27 | +@Composable |
| 28 | +fun AutoDevChatApp(viewModel: AutoDevChatViewModel) { |
| 29 | + val messages by viewModel.chatMessages.collectAsState() |
| 30 | + val inputState by viewModel.inputState.collectAsState() |
| 31 | + val listState = rememberLazyListState() |
| 32 | + |
| 33 | + // Auto-scroll to bottom when new messages arrive |
| 34 | + LaunchedEffect(messages.size) { |
| 35 | + if (messages.isNotEmpty()) { |
| 36 | + listState.animateScrollToItem(messages.lastIndex) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + Column( |
| 41 | + modifier = Modifier |
| 42 | + .fillMaxSize() |
| 43 | + .background(JewelTheme.globalColors.panelBackground) |
| 44 | + ) { |
| 45 | + // Header |
| 46 | + ChatHeader( |
| 47 | + onNewConversation = { viewModel.onNewConversation() } |
| 48 | + ) |
| 49 | + |
| 50 | + Divider(Orientation.Horizontal, modifier = Modifier.fillMaxWidth().height(1.dp)) |
| 51 | + |
| 52 | + // Message list |
| 53 | + Box( |
| 54 | + modifier = Modifier |
| 55 | + .fillMaxWidth() |
| 56 | + .weight(1f) |
| 57 | + ) { |
| 58 | + if (messages.isEmpty()) { |
| 59 | + EmptyStateMessage() |
| 60 | + } else { |
| 61 | + LazyColumn( |
| 62 | + state = listState, |
| 63 | + modifier = Modifier.fillMaxSize(), |
| 64 | + contentPadding = PaddingValues(8.dp), |
| 65 | + verticalArrangement = Arrangement.spacedBy(4.dp) |
| 66 | + ) { |
| 67 | + items(messages, key = { it.id }) { message -> |
| 68 | + MessageBubble(message) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + Divider(Orientation.Horizontal, modifier = Modifier.fillMaxWidth().height(1.dp)) |
| 75 | + |
| 76 | + // Input area |
| 77 | + ChatInput( |
| 78 | + inputState = inputState, |
| 79 | + onInputChanged = { viewModel.onInputChanged(it) }, |
| 80 | + onSend = { viewModel.onSendMessage() }, |
| 81 | + onAbort = { viewModel.onAbortMessage() } |
| 82 | + ) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +@Composable |
| 87 | +private fun ChatHeader( |
| 88 | + onNewConversation: () -> Unit |
| 89 | +) { |
| 90 | + Row( |
| 91 | + modifier = Modifier |
| 92 | + .fillMaxWidth() |
| 93 | + .padding(8.dp), |
| 94 | + horizontalArrangement = Arrangement.SpaceBetween, |
| 95 | + verticalAlignment = Alignment.CenterVertically |
| 96 | + ) { |
| 97 | + Text( |
| 98 | + text = "AutoDev Chat", |
| 99 | + style = JewelTheme.defaultTextStyle.copy( |
| 100 | + fontWeight = FontWeight.Bold, |
| 101 | + fontSize = 14.sp |
| 102 | + ) |
| 103 | + ) |
| 104 | + |
| 105 | + IconButton(onClick = onNewConversation) { |
| 106 | + Text("+", style = JewelTheme.defaultTextStyle) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +@Composable |
| 112 | +private fun EmptyStateMessage() { |
| 113 | + Box( |
| 114 | + modifier = Modifier.fillMaxSize(), |
| 115 | + contentAlignment = Alignment.Center |
| 116 | + ) { |
| 117 | + Text( |
| 118 | + text = "Start a conversation with your AI Assistant!", |
| 119 | + style = JewelTheme.defaultTextStyle.copy( |
| 120 | + fontSize = 16.sp, |
| 121 | + color = JewelTheme.globalColors.text.info |
| 122 | + ) |
| 123 | + ) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +@Composable |
| 128 | +private fun MessageBubble(message: ChatMessage) { |
| 129 | + Row( |
| 130 | + modifier = Modifier.fillMaxWidth(), |
| 131 | + horizontalArrangement = if (message.isUser) Arrangement.End else Arrangement.Start |
| 132 | + ) { |
| 133 | + Box( |
| 134 | + modifier = Modifier |
| 135 | + .widthIn(max = 300.dp) |
| 136 | + .background( |
| 137 | + if (message.isUser) |
| 138 | + JewelTheme.defaultBannerStyle.information.colors.background.copy(alpha = 0.75f) |
| 139 | + else |
| 140 | + JewelTheme.globalColors.panelBackground |
| 141 | + ) |
| 142 | + .padding(8.dp) |
| 143 | + ) { |
| 144 | + Text( |
| 145 | + text = message.content, |
| 146 | + style = JewelTheme.defaultTextStyle |
| 147 | + ) |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +@Composable |
| 153 | +private fun ChatInput( |
| 154 | + inputState: MessageInputState, |
| 155 | + onInputChanged: (String) -> Unit, |
| 156 | + onSend: () -> Unit, |
| 157 | + onAbort: () -> Unit |
| 158 | +) { |
| 159 | + val textFieldState = rememberTextFieldState() |
| 160 | + val isSending = inputState is MessageInputState.Sending |
| 161 | + |
| 162 | + LaunchedEffect(Unit) { |
| 163 | + snapshotFlow { textFieldState.text.toString() } |
| 164 | + .distinctUntilChanged() |
| 165 | + .collect { onInputChanged(it) } |
| 166 | + } |
| 167 | + |
| 168 | + Row( |
| 169 | + modifier = Modifier |
| 170 | + .fillMaxWidth() |
| 171 | + .padding(8.dp), |
| 172 | + horizontalArrangement = Arrangement.spacedBy(8.dp), |
| 173 | + verticalAlignment = Alignment.CenterVertically |
| 174 | + ) { |
| 175 | + TextField( |
| 176 | + state = textFieldState, |
| 177 | + placeholder = { Text("Type your message...") }, |
| 178 | + modifier = Modifier |
| 179 | + .weight(1f) |
| 180 | + .onPreviewKeyEvent { keyEvent -> |
| 181 | + if (keyEvent.key == Key.Enter && keyEvent.type == KeyEventType.KeyDown && !isSending) { |
| 182 | + onSend() |
| 183 | + textFieldState.edit { replace(0, length, "") } |
| 184 | + true |
| 185 | + } else { |
| 186 | + false |
| 187 | + } |
| 188 | + }, |
| 189 | + enabled = !isSending |
| 190 | + ) |
| 191 | + |
| 192 | + if (isSending) { |
| 193 | + DefaultButton(onClick = onAbort) { |
| 194 | + Text("Stop") |
| 195 | + } |
| 196 | + } else { |
| 197 | + DefaultButton( |
| 198 | + onClick = { |
| 199 | + onSend() |
| 200 | + textFieldState.edit { replace(0, length, "") } |
| 201 | + }, |
| 202 | + enabled = inputState is MessageInputState.Enabled |
| 203 | + ) { |
| 204 | + Text("Send") |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | +} |
| 209 | + |
0 commit comments