Skip to content

Commit 5ea09be

Browse files
committed
refactor(gui): move file-related UI components to a separate package
- Create a new 'file' package for file-related UI components - Move FilePresentation, FileSearchPopup, InputFileToolbar, RelatedFileListCellRenderer, and WorkspaceFilePanel to the new package- Update imports in AutoDevInputSection to use the new package - Make some minor UI adjustments (e.g., button border size)
1 parent 5139f8d commit 5ea09be

File tree

7 files changed

+55
-52
lines changed

7 files changed

+55
-52
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.AutoDevIcons
55
import cc.unitmesh.devti.agent.custom.model.CustomAgentConfig
66
import cc.unitmesh.devti.agent.custom.model.CustomAgentState
7+
import cc.unitmesh.devti.gui.chat.ui.file.InputFileToolbar
8+
import cc.unitmesh.devti.gui.chat.ui.file.RelatedFileListCellRenderer
9+
import cc.unitmesh.devti.gui.chat.ui.file.WorkspaceFilePanel
710
import cc.unitmesh.devti.gui.chat.ui.viewmodel.FileListViewModel
811
import cc.unitmesh.devti.llms.tokenizer.Tokenizer
912
import cc.unitmesh.devti.llms.tokenizer.TokenizerFactory
@@ -71,7 +74,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
7174
private val fileListViewModel = FileListViewModel(project)
7275
private val elementsList = JBList(fileListViewModel.getListModel())
7376

74-
private val workspacePanel: WorkspacePanel
77+
private val workspaceFilePanel: WorkspaceFilePanel
7578

7679
private val defaultRag: CustomAgentConfig = CustomAgentConfig("<Select Custom Agent>", "Normal")
7780
private var customAgent: ComboBox<CustomAgentConfig> = ComboBox(MutableCollectionComboBoxModel(listOf()))
@@ -88,7 +91,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
8891

8992
var text: String
9093
get() {
91-
val files = workspacePanel.getAllFilesFormat()
94+
val files = workspaceFilePanel.getAllFilesFormat()
9295
return input.text + "\n" + files
9396
}
9497
set(text) {
@@ -98,7 +101,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
98101

99102
init {
100103
input = AutoDevInput(project, listOf(), disposable, this)
101-
workspacePanel = WorkspacePanel(project, input)
104+
workspaceFilePanel = WorkspaceFilePanel(project)
102105

103106
setupElementsList()
104107
val sendButtonPresentation = Presentation(AutoDevBundle.message("chat.panel.send"))
@@ -169,8 +172,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
169172
inputPanel.add(input, BorderLayout.CENTER)
170173
inputPanel.addToBottom(layoutPanel)
171174

172-
// Add workspace panel to the input panel
173-
inputPanel.addToTop(workspacePanel)
175+
inputPanel.addToTop(workspaceFilePanel)
174176

175177
val scrollPane = JBScrollPane(elementsList)
176178
scrollPane.verticalScrollBarPolicy = JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED
@@ -255,7 +257,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
255257
val actionType = fileListViewModel.determineFileAction(wrapper, e.point, cellBounds)
256258
val actionPerformed = fileListViewModel.handleFileAction(wrapper, actionType) { vfile, relativePath ->
257259
if (relativePath != null) {
258-
workspacePanel.addFileToWorkspace(vfile)
260+
workspaceFilePanel.addFileToWorkspace(vfile)
259261
ApplicationManager.getApplication().invokeLater {
260262
if (!vfile.isValid) return@invokeLater
261263
val psiFile = PsiManager.getInstance(project).findFile(vfile) ?: return@invokeLater
@@ -376,7 +378,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
376378

377379
customAgent.selectedItem = defaultRag
378380
text = ""
379-
workspacePanel.clear()
381+
workspaceFilePanel.clear()
380382
}
381383

382384
fun moveCursorToStart() {

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/FilePresentation.kt renamed to core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/file/FilePresentation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cc.unitmesh.devti.gui.chat.ui
1+
package cc.unitmesh.devti.gui.chat.ui.file
22

33
import com.intellij.icons.AllIcons
44
import com.intellij.openapi.project.Project

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/FileSearchPopup.kt renamed to core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/file/FileSearchPopup.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cc.unitmesh.devti.gui.chat.ui
1+
package cc.unitmesh.devti.gui.chat.ui.file
22

33
import cc.unitmesh.devti.util.canBeAdded
44
import com.intellij.openapi.fileEditor.impl.EditorHistoryManager
@@ -143,7 +143,7 @@ class FileSearchPopup(
143143
infoPanel.isOpaque = false
144144

145145
val fileLabel = JBLabel(value.name, value.icon, JBLabel.LEFT)
146-
fileLabel.border = JBUI.Borders.emptyRight(8)
146+
fileLabel.border = JBUI.Borders.emptyRight(4)
147147

148148
val relativePath = value.presentablePath
149149
val pathLabel = JBLabel(" - $relativePath", JBLabel.LEFT)

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/InputFileToolbar.kt renamed to core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/file/InputFileToolbar.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package cc.unitmesh.devti.gui.chat.ui
1+
package cc.unitmesh.devti.gui.chat.ui.file
22

33
import cc.unitmesh.devti.AutoDevBundle
4+
import cc.unitmesh.devti.gui.chat.ui.AutoDevInput
5+
import cc.unitmesh.devti.gui.chat.ui.AutoDevInputSection
6+
import cc.unitmesh.devti.gui.chat.ui.mediumFontFunction
47
import cc.unitmesh.devti.gui.chat.ui.viewmodel.FileListViewModel
5-
import com.intellij.openapi.fileEditor.FileEditorManager
68
import com.intellij.openapi.project.Project
79
import com.intellij.ui.components.JBLabel
810
import com.intellij.ui.components.labels.LinkLabel
@@ -12,6 +14,7 @@ import javax.swing.Box
1214
import javax.swing.JToolBar
1315
import java.awt.Component
1416
import java.awt.Container
17+
import kotlin.collections.forEach
1518

1619
object InputFileToolbar {
1720
fun createToolbar(project: Project, viewModel: FileListViewModel, input: AutoDevInput): JToolBar {
@@ -25,11 +28,11 @@ object InputFileToolbar {
2528

2629
toolbar.add(Box.createHorizontalGlue())
2730

28-
val findWorkspacePanel: () -> WorkspacePanel? = lambda@{
31+
val findWorkspaceFilePanel: () -> WorkspaceFilePanel? = lambda@{
2932
var component: Component? = input.parent
3033
while (component != null) {
3134
if (component is AutoDevInputSection) {
32-
val workspace = component.findComponentOfType(WorkspacePanel::class.java)
35+
val workspace = component.findComponentOfType(WorkspaceFilePanel::class.java)
3336
if (workspace != null) {
3437
return@lambda workspace
3538
}
@@ -43,7 +46,7 @@ object InputFileToolbar {
4346
val recentFiles = LinkLabel(AutoDevBundle.message("chat.panel.add.openFiles"), null) { _: LinkLabel<Unit>, _: Unit? ->
4447
val addedFiles = viewModel.addRecentlyOpenedFiles()
4548

46-
val workspace = findWorkspacePanel()
49+
val workspace = findWorkspaceFilePanel()
4750
if (workspace != null) {
4851
addedFiles.forEach { file ->
4952
workspace.addFileToWorkspace(file.virtualFile)
@@ -66,7 +69,7 @@ object InputFileToolbar {
6669

6770
val clearAll = LinkLabel(AutoDevBundle.message("chat.panel.clear.all"), null) { _: LinkLabel<Unit>, _: Unit? ->
6871
viewModel.clearAllFiles()
69-
findWorkspacePanel()?.clear()
72+
findWorkspaceFilePanel()?.clear()
7073
}
7174

7275
clearAll.mediumFontFunction()

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/RelatedFileListCellRenderer.kt renamed to core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/file/RelatedFileListCellRenderer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cc.unitmesh.devti.gui.chat.ui
1+
package cc.unitmesh.devti.gui.chat.ui.file
22

33
import com.intellij.icons.AllIcons
44
import com.intellij.openapi.project.Project

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/WorkspacePanel.kt renamed to core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/file/WorkspaceFilePanel.kt

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cc.unitmesh.devti.gui.chat.ui
1+
package cc.unitmesh.devti.gui.chat.ui.file
22

33
import com.intellij.icons.AllIcons
44
import com.intellij.openapi.fileChooser.FileChooser
@@ -13,39 +13,37 @@ import java.awt.event.MouseAdapter
1313
import java.awt.event.MouseEvent
1414
import javax.swing.*
1515

16-
class WorkspacePanel(
17-
private val project: Project,
18-
private val input: AutoDevInput
19-
) : JPanel(BorderLayout()) {
16+
class WorkspaceFilePanel(private val project: Project) : JPanel(BorderLayout()) {
2017
private val workspaceFiles = mutableListOf<FilePresentation>()
2118
private val filesPanel = JPanel(WrapLayout(FlowLayout.LEFT, 2, 2))
22-
19+
2320
init {
2421
border = JBUI.Borders.empty()
2522

2623
filesPanel.isOpaque = false
2724
filesPanel.add(createAddButton())
28-
25+
2926
add(filesPanel, BorderLayout.NORTH)
3027
isOpaque = false
3128
}
3229

3330
private fun createAddButton(): JBLabel {
34-
val addButton = JBLabel(AllIcons.General.Add)
35-
addButton.cursor = Cursor(Cursor.HAND_CURSOR)
36-
addButton.toolTipText = "Add files to workspace"
37-
addButton.border = JBUI.Borders.empty(2, 4)
38-
addButton.background = JBColor(0xEDF4FE, 0x313741)
39-
addButton.isOpaque = true
40-
41-
addButton.addMouseListener(object : MouseAdapter() {
31+
val button = JBLabel(AllIcons.General.Add)
32+
button.cursor = Cursor(Cursor.HAND_CURSOR)
33+
button.toolTipText = "Add files to workspace"
34+
button.border = JBUI.Borders.empty(2)
35+
button.background = JBColor(0xEDF4FE, 0x313741)
36+
button.isOpaque = true
37+
38+
button.addMouseListener(object : MouseAdapter() {
4239
override fun mouseClicked(e: MouseEvent) {
43-
showFileSearchPopup(this@WorkspacePanel)
40+
showFileSearchPopup(this@WorkspaceFilePanel)
4441
}
4542
})
46-
return addButton
43+
44+
return button
4745
}
48-
46+
4947
private fun showFileSearchPopup(component: JComponent) {
5048
val popup = FileSearchPopup(project) { files ->
5149
for (file in files) {
@@ -54,52 +52,52 @@ class WorkspacePanel(
5452
}
5553
popup.show(component)
5654
}
57-
55+
5856
private fun addFile() {
5957
val descriptor = FileChooserDescriptor(true, true, false, false, false, true)
6058
.withTitle("Select Files for Workspace")
6159
.withDescription("Choose files to add to your workspace")
62-
60+
6361
FileChooser.chooseFiles(descriptor, project, null) { files ->
6462
for (file in files) {
6563
addFileToWorkspace(file)
6664
}
6765
}
6866
}
69-
67+
7068
fun addFileToWorkspace(file: VirtualFile) {
7169
val filePresentation = FilePresentation.from(project, file)
7270
if (workspaceFiles.none { it.virtualFile == file }) {
7371
workspaceFiles.add(filePresentation)
7472
updateFilesPanel()
7573
}
7674
}
77-
75+
7876
private fun updateFilesPanel() {
7977
filesPanel.removeAll()
8078
filesPanel.add(createAddButton())
81-
79+
8280
for (filePresentation in workspaceFiles) {
83-
val fileLabel = FileItemPanel(project, filePresentation) {
81+
val fileLabel = FileItemPanel(project, filePresentation) {
8482
removeFile(filePresentation)
8583
}
8684
filesPanel.add(fileLabel)
8785
}
88-
86+
8987
filesPanel.revalidate()
9088
filesPanel.repaint()
9189
}
92-
90+
9391
private fun removeFile(filePresentation: FilePresentation) {
9492
workspaceFiles.remove(filePresentation)
9593
updateFilesPanel()
9694
}
97-
95+
9896
fun clear() {
9997
workspaceFiles.clear()
10098
updateFilesPanel()
10199
}
102-
100+
103101
fun getAllFiles(): List<FilePresentation> {
104102
return workspaceFiles.toList()
105103
}
@@ -123,20 +121,20 @@ class FileItemPanel(
123121
)
124122
background = JBColor(0xEDF4FE, 0x313741)
125123
isOpaque = true
126-
124+
127125
val fileLabel = JBLabel(filePresentation.name, filePresentation.icon, JBLabel.LEFT)
128-
126+
129127
val removeLabel = JBLabel(AllIcons.Actions.Close)
130128
removeLabel.cursor = Cursor(Cursor.HAND_CURSOR)
131129
removeLabel.addMouseListener(object : MouseAdapter() {
132130
override fun mouseClicked(e: MouseEvent) {
133131
onRemove()
134132
}
135133
})
136-
134+
137135
add(fileLabel)
138136
add(removeLabel)
139-
137+
140138
this.border = JBUI.Borders.empty(2)
141139
}
142140
}
@@ -179,7 +177,7 @@ class WrapLayout : FlowLayout {
179177
synchronized(target.treeLock) {
180178
// Each row must fit within the target container width
181179
var targetWidth = target.width
182-
180+
183181
if (targetWidth == 0) {
184182
targetWidth = Int.MAX_VALUE
185183
}
@@ -200,7 +198,7 @@ class WrapLayout : FlowLayout {
200198
val m = target.getComponent(i)
201199
if (m.isVisible) {
202200
val d = if (preferred) m.preferredSize else m.minimumSize
203-
201+
204202
// If this component doesn't fit in the current row, start a new row
205203
if (rowWidth + d.width > maxWidth && rowWidth > 0) {
206204
dim.width = maxWidth.coerceAtLeast(rowWidth)

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/viewmodel/FileListViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package cc.unitmesh.devti.gui.chat.ui.viewmodel
22

3-
import cc.unitmesh.devti.gui.chat.ui.FilePresentation
3+
import cc.unitmesh.devti.gui.chat.ui.file.FilePresentation
44
import cc.unitmesh.devti.util.isInProject
55
import com.intellij.diff.editor.DiffVirtualFileBase
66
import com.intellij.openapi.Disposable

0 commit comments

Comments
 (0)