Skip to content

Commit ae6665e

Browse files
committed
feat(gui): add key listener to AutoDevInputSection for better user experience #51
1 parent ccf3967 commit ae6665e

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import java.awt.Color
4646
import java.awt.Component
4747
import java.awt.Dimension
4848
import java.awt.Point
49+
import java.awt.event.KeyAdapter
50+
import java.awt.event.KeyEvent
4951
import java.awt.event.MouseAdapter
5052
import java.awt.event.MouseEvent
5153
import java.util.function.Supplier
@@ -179,22 +181,62 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
179181
}
180182

181183
private fun createPopup(): JBPopup {
182-
val devVariableList = AutoDevVariableList.from(CustomVariable.all()) { item ->
184+
val list: AutoDevVariableList = AutoDevVariableList.from(CustomVariable.all()) { item ->
183185
input.text += item.customVariable.variable
184186
this.popup?.cancel()
185187
}
186188

187-
devVariableList.selectedIndex = 0
189+
list.selectedIndex = 0
190+
191+
list.addKeyListener(object : KeyAdapter() {
192+
override fun keyPressed(e: KeyEvent?) {
193+
if (!hasPopup()) return
194+
195+
when (e?.keyCode) {
196+
KeyEvent.VK_ENTER -> {
197+
e.consume()
198+
val selectedItem = list.getSelectedValue()
199+
if (selectedItem != null) {
200+
text += "${selectedItem.customVariable.variable} "
201+
}
202+
this@AutoDevInputSection.popup?.cancel()
203+
}
204+
205+
KeyEvent.VK_DOWN -> {
206+
val selectedIndex = list.selectedIndex
207+
val itemsCount = list.getItemsCount()
208+
if (selectedIndex < itemsCount - 1) {
209+
list.setSelectedIndex(selectedIndex + 1)
210+
} else {
211+
list.setSelectedIndex(0)
212+
}
213+
}
214+
215+
KeyEvent.VK_UP -> {
216+
val selectedIndex = list.selectedIndex
217+
if (selectedIndex > 0) {
218+
list.setSelectedIndex(selectedIndex - 1)
219+
} else {
220+
list.setSelectedIndex(list.getItemsCount() - 1)
221+
}
222+
}
223+
}
224+
}
225+
})
188226

189227
val popups = JBPopupFactory.getInstance()
190-
.createComponentPopupBuilder(devVariableList, null)
191-
.setRequestFocus(false)
228+
.createComponentPopupBuilder(list, null)
229+
.setFocusable(true)
230+
.setRequestFocus(true)
192231
.setMinSize(Dimension(this@AutoDevInputSection.width, 0))
193232
.createPopup()
194233

195234
return popups
196235
}
197236

237+
fun hasPopup(): Boolean {
238+
return popup?.isVisible == true && popup?.isDisposed == false
239+
}
198240

199241
private fun loadRagApps(): List<CustomAgentConfig> {
200242
val ragsJsonConfig = project.customAgentSetting.ragsJsonConfig

src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevVariableList.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class VariableListCellRenderer : ListCellRenderer<AutoDevVariableListItemCompone
7373
}
7474
}
7575

76-
class AutoDevVariableListItemComponent(val customVariable: CustomVariable) : JPanel() {
76+
class AutoDevVariableListItemComponent(val customVariable: CustomVariable) : JPanel(BorderLayout()) {
7777
init {
7878
add(JLabel("$${customVariable.variable}"), BorderLayout.WEST)
7979
val label = JLabel(customVariable.description)

0 commit comments

Comments
 (0)