Skip to content

Commit 4a8fca0

Browse files
committed
fix(mcp): improve popup closing and UI refresh behavior
- Add explicit popup reference for proper closing on Apply/Cancel actions - Add tree repaint and panel revalidate calls to ensure UI updates correctly - Remove redundant comments and fix code formatting
1 parent 3e5a994 commit 4a8fca0

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

core/src/main/kotlin/cc/unitmesh/devti/mcp/ui/McpConfigPopup.kt

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ class McpConfigPopup {
7878
loadingPanel.add(JBScrollPane(tree), BorderLayout.CENTER)
7979
loadingPanel.preferredSize = Dimension(380, 350)
8080

81+
var currentPopup: com.intellij.openapi.ui.popup.JBPopup? = null
82+
8183
// Button panel
8284
val buttonPanel = JPanel().apply {
8385
layout = BoxLayout(this, BoxLayout.X_AXIS)
@@ -94,11 +96,15 @@ class McpConfigPopup {
9496
val applyButton = JButton("Apply").apply {
9597
addActionListener {
9698
saveSelectedTools(tree, configService)
97-
// Close popup - this will be handled by the popup framework
99+
currentPopup?.cancel()
98100
}
99101
}
100102

101-
val cancelButton = JButton("Cancel")
103+
val cancelButton = JButton("Cancel").apply {
104+
addActionListener {
105+
currentPopup?.cancel()
106+
}
107+
}
102108

103109
add(cancelButton)
104110
add(Box.createHorizontalStrut(8))
@@ -120,7 +126,7 @@ class McpConfigPopup {
120126
}
121127
})
122128

123-
val popup = JBPopupFactory.getInstance()
129+
currentPopup = JBPopupFactory.getInstance()
124130
.createComponentPopupBuilder(mainPanel, searchField)
125131
.setTitle("Configure MCP Tools")
126132
.setResizable(true)
@@ -129,9 +135,9 @@ class McpConfigPopup {
129135
.createPopup()
130136

131137
if (component != null) {
132-
popup.showUnderneathOf(component)
138+
currentPopup.showUnderneathOf(component)
133139
} else {
134-
popup.showCenteredInCurrentWindow(project)
140+
currentPopup.showCenteredInCurrentWindow(project)
135141
}
136142
}
137143

@@ -148,7 +154,7 @@ class McpConfigPopup {
148154

149155
loadToolsIntoTree(project, configService, rootNode, treeModel, tree, loadingPanel)
150156
}
151-
157+
152158
private fun loadToolsIntoTree(
153159
project: Project,
154160
configService: McpConfigService,
@@ -162,40 +168,43 @@ class McpConfigPopup {
162168
rootNode.add(loadingNode)
163169
treeModel.reload()
164170
expandAllNodes(tree)
165-
171+
166172
CoroutineScope(Dispatchers.IO).launch {
167173
try {
168174
val mcpServerConfig = project.customizeSetting.mcpServerConfig
169175
val allTools = configService.getAllAvailableTools(mcpServerConfig)
170176
val selectedTools = configService.getSelectedTools()
171-
177+
172178
invokeLater {
173179
rootNode.removeAllChildren()
174-
loadingPanel.stopLoading()
175-
180+
176181
allTools.forEach { (serverName, tools) ->
177182
val serverNode = ServerTreeNode(serverName)
178183
rootNode.add(serverNode)
179-
184+
180185
tools.forEach { tool ->
181186
val toolNode = ToolTreeNode(serverName, tool)
182187
val isSelected = selectedTools[serverName]?.contains(tool.name) == true
183188
toolNode.isChecked = isSelected
184189
serverNode.add(toolNode)
185190
}
186191
}
187-
192+
188193
treeModel.reload()
189194
expandAllNodes(tree)
195+
tree.repaint() // 添加显式重绘
196+
loadingPanel.stopLoading()
197+
loadingPanel.revalidate() // 确保布局更新
190198
}
191199
} catch (e: Exception) {
192200
invokeLater {
193201
rootNode.removeAllChildren()
194202
val errorNode = CheckedTreeNode("Error loading tools: ${e.message}")
195203
rootNode.add(errorNode)
196204
treeModel.reload()
197-
205+
tree.repaint() // 添加显式重绘
198206
loadingPanel.stopLoading()
207+
loadingPanel.revalidate() // 确保布局更新
199208
}
200209
}
201210
}
@@ -230,5 +239,7 @@ class McpConfigPopup {
230239
for (i in 0 until tree.rowCount) {
231240
tree.expandRow(i)
232241
}
242+
243+
tree.updateUI()
233244
}
234245
}

0 commit comments

Comments
 (0)