Skip to content

Commit 661a06d

Browse files
feat: add middle-click to remove files from context. (#1148)
* add middle-click to remove files from context. Previously when you wanted to remove files which were previously added to the chat context, you had to click the little "x" icon one by one. When you had several files to remove, this got tedious and difficult, especially since the file box varies in width, so the "x" is not in the same space. This change makes this operation easier by allowing you to middle-click anywhere on the file box to remove it. * fix: mouse actions when clicked outside of the tag label --------- Co-authored-by: Carl-Robert Linnupuu <carlrobertoh@gmail.com>
1 parent e578518 commit 661a06d

2 files changed

Lines changed: 26 additions & 22 deletions

File tree

src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/header/UserInputHeaderPanel.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -360,20 +360,21 @@ class UserInputHeaderPanel(
360360
}
361361

362362
private inner class TagPopupMenu : JBPopupMenu() {
363+
private fun resolveTagPanel(from: Component): TagPanel? = when (from) {
364+
is TagPanel -> from
365+
else -> SwingUtilities.getAncestorOfClass(TagPanel::class.java, from) as? TagPanel
366+
}
367+
363368
private val closeMenuItem =
364369
createPopupMenuItem(CodeGPTBundle.get("tagPopupMenuItem.close")) {
365-
val tagPanel = invoker as? TagPanel
366-
tagPanel?.let {
367-
if (it.tagDetails.isRemovable) {
368-
tagManager.remove(it.tagDetails)
369-
}
370+
resolveTagPanel(invoker)?.let {
371+
if (it.tagDetails.isRemovable) tagManager.remove(it.tagDetails)
370372
}
371373
}
372374

373375
private val closeOtherTagsMenuItem =
374376
createPopupMenuItem(CodeGPTBundle.get("tagPopupMenuItem.closeOthers")) {
375-
val tagPanel = invoker as? TagPanel
376-
tagPanel?.let { currentPanel ->
377+
resolveTagPanel(invoker)?.let { currentPanel ->
377378
val currentTag = currentPanel.tagDetails
378379
tagManager.getTags()
379380
.filter { it != currentTag && it.isRemovable }
@@ -411,8 +412,7 @@ class UserInputHeaderPanel(
411412
}
412413

413414
private fun closeTagsInRange(rangeSelector: (Array<Component>, Int) -> List<Component>) {
414-
val tagPanel = invoker as? TagPanel
415-
tagPanel?.let { currentPanel ->
415+
resolveTagPanel(invoker)?.let { currentPanel ->
416416
val components = this@UserInputHeaderPanel.components
417417
val currentIndex = components.indexOf(currentPanel)
418418

@@ -433,13 +433,7 @@ class UserInputHeaderPanel(
433433
}
434434

435435
override fun show(invoker: Component, x: Int, y: Int) {
436-
val tagPanel = when (invoker) {
437-
is TagPanel -> invoker
438-
else -> SwingUtilities.getAncestorOfClass(
439-
TagPanel::class.java,
440-
invoker
441-
) as? TagPanel
442-
} ?: return
436+
val tagPanel = resolveTagPanel(invoker) ?: return
443437
if (!tagPanel.isEnabled) return
444438
val components = this@UserInputHeaderPanel.components.filterIsInstance<TagPanel>()
445439
val currentIndex = components.indexOf(tagPanel)

src/main/kotlin/ee/carlrobert/codegpt/ui/textarea/header/tag/TagPanel.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,13 @@ abstract class TagPanel(
8787
label.inheritsPopupMenu = true
8888
closeButton.inheritsPopupMenu = true
8989

90-
label.addMouseListener(object : MouseAdapter() {
90+
val mouseAdapter = object : MouseAdapter() {
9191
override fun mousePressed(e: MouseEvent) {
92-
if (SwingUtilities.isLeftMouseButton(e)) {
93-
this@TagPanel.doClick()
94-
e.consume()
95-
}
92+
handleMousePress(e)
9693
}
97-
})
94+
}
95+
label.addMouseListener(mouseAdapter)
96+
addMouseListener(mouseAdapter)
9897

9998
addActionListener {
10099
if (isRevertingSelection) return@addActionListener
@@ -116,6 +115,17 @@ abstract class TagPanel(
116115
repaint()
117116
}
118117

118+
private fun handleMousePress(e: MouseEvent) {
119+
if (SwingUtilities.isLeftMouseButton(e)) {
120+
this@TagPanel.doClick()
121+
e.consume()
122+
}
123+
if (SwingUtilities.isMiddleMouseButton(e) && tagDetails.isRemovable) {
124+
onClose()
125+
e.consume()
126+
}
127+
}
128+
119129
private fun toProjectRelative(path: String): String? {
120130
val base = project.basePath ?: return path
121131
val baseTrim = base.trimEnd('/', '\\')

0 commit comments

Comments
 (0)