Skip to content

Commit 61950ac

Browse files
committed
feat(gui): refactor MessageView layout and add toolbar
- Restructured layout to use BorderLayout for better organization - Added toolbar with author label and copy action - Implemented createToolbar() and createToolbarActions() functions - Moved copy action implementation to new location - Updated NormalChatCodingPanel to use runInEdt when adding MessageView
1 parent 6e18ba8 commit 61950ac

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/NormalChatCodingPanel.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ class NormalChatCodingPanel(private val chatCodingService: ChatCodingService, va
244244

245245
private suspend fun updateMessageInUi(content: Flow<String>): String {
246246
val messageView = MessageView(chatCodingService.project, "", ChatRole.Assistant, "")
247-
myList.add(messageView)
247+
runInEdt {
248+
myList.add(messageView)
249+
}
248250

249251
val startTime = System.currentTimeMillis()
250252
var text = ""

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/view/MessageView.kt

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import com.intellij.openapi.project.Project
2121
import com.intellij.openapi.ui.DialogPanel
2222
import com.intellij.ui.components.JBPanel
2323
import com.intellij.ui.components.panels.VerticalLayout
24+
import com.intellij.ui.components.panels.Wrapper
2425
import com.intellij.ui.dsl.builder.panel
2526
import com.intellij.util.ui.JBFont
2627
import com.intellij.util.ui.JBUI
28+
import com.intellij.util.ui.UIUtil
2729
import java.awt.BorderLayout
2830
import java.awt.Toolkit
2931
import java.awt.datatransfer.StringSelection
@@ -50,30 +52,20 @@ class MessageView(val project: Project, val message: String, val role: ChatRole,
5052
isDoubleBuffered = true
5153
isOpaque = true
5254

53-
val authorLabel = JLabel()
54-
authorLabel.setFont(JBFont.h4())
55-
authorLabel.setText(
56-
when (role) {
57-
ChatRole.System -> "System"
58-
ChatRole.Assistant -> "Assistant"
59-
ChatRole.User -> "User"
60-
}
61-
)
55+
layout = BorderLayout(0, 0)
6256

63-
layout = BorderLayout(JBUI.scale(4), 0)
57+
val centerPanel = JPanel(BorderLayout())
6458

65-
val centerPanel = JPanel(VerticalLayout(JBUI.scale(4)))
66-
centerPanel.add(authorLabel)
59+
val toolbarPanel = createToolbar()
60+
centerPanel.add(toolbarPanel, BorderLayout.NORTH)
6761

68-
val toolbar = createViewActionGroup().component
6962
runInEdt {
70-
centerPanel.add(toolbar)
7163
if (role == ChatRole.User) {
7264
myList.add(createSingleTextView(project, message))
7365
}
7466
}
7567

76-
centerPanel.add(myList)
68+
centerPanel.add(myList, BorderLayout.CENTER)
7769
add(centerPanel, BorderLayout.CENTER)
7870

7971
ApplicationManager.getApplication().invokeLater {
@@ -82,6 +74,44 @@ class MessageView(val project: Project, val message: String, val role: ChatRole,
8274
}
8375
}
8476

77+
private fun createToolbar(): JPanel {
78+
val authorLabel = JLabel().apply {
79+
font = JBFont.h4()
80+
text = when (role) {
81+
ChatRole.System -> "System"
82+
ChatRole.Assistant -> "Assistant"
83+
ChatRole.User -> "User"
84+
}
85+
border = JBUI.Borders.empty(0, 10)
86+
}
87+
88+
val actionGroup = DefaultActionGroup(createToolbarActions())
89+
val toolbar = ActionManager.getInstance()
90+
.createActionToolbar("MessageViewToolbar", actionGroup, true).apply {
91+
this.targetComponent = this@MessageView
92+
}
93+
94+
val toolbarPanel = JPanel(BorderLayout()).apply {
95+
add(authorLabel, BorderLayout.WEST)
96+
add(toolbar.component, BorderLayout.EAST)
97+
}
98+
99+
toolbarPanel.border = JBUI.Borders.customLine(UIUtil.getBoundsColor(), 0, 0, 1, 0)
100+
return toolbarPanel
101+
}
102+
103+
private fun createToolbarActions(): List<AnAction> {
104+
val copyAction = object : AnAction("Copy", "Copy text", AllIcons.Actions.Copy) {
105+
override fun actionPerformed(e: AnActionEvent) {
106+
val clipboard = Toolkit.getDefaultToolkit().systemClipboard
107+
val selection = StringSelection(displayText)
108+
clipboard.setContents(selection, null)
109+
}
110+
}
111+
112+
return listOf(copyAction)
113+
}
114+
85115
fun onFinish(text: String) {
86116
displayText = text
87117
runInEdt {
@@ -148,24 +178,6 @@ class MessageView(val project: Project, val message: String, val role: ChatRole,
148178
}
149179
}
150180

151-
152-
fun createViewActionGroup(): ActionToolbar {
153-
val copyAction = object : AnAction("Copy", "Copy text", AllIcons.Actions.Copy) {
154-
override fun actionPerformed(e: AnActionEvent) {
155-
val clipboard = Toolkit.getDefaultToolkit().systemClipboard
156-
val selection = StringSelection(displayText)
157-
clipboard.setContents(selection, null)
158-
}
159-
}
160-
161-
val actionGroup = DefaultActionGroup(listOf(copyAction))
162-
val rightToolbar = ActionManager.getInstance()
163-
.createActionToolbar("AutoDevCopyView", actionGroup, true)
164-
165-
rightToolbar.targetComponent = this
166-
return rightToolbar
167-
}
168-
169181
companion object {
170182
fun createSingleTextView(project: Project, text: String, language: String = "markdown"): DialogPanel {
171183
val codeBlockViewer = CodeHighlightSketch(project, text, CodeFence.findLanguage(language)).apply {

0 commit comments

Comments
 (0)