💄 loading icon, cancellation logic

This commit is contained in:
Nate Sesti 2024-03-31 23:58:13 -07:00
parent 0c2d8880dc
commit 21f5affbad
3 changed files with 46 additions and 23 deletions

View file

@ -31,7 +31,8 @@ class DiffStreamHandler(
private val textArea: JTextArea,
private val startLine: Int,
private val endLine: Int,
private val onClose: () -> Unit
private val onClose: () -> Unit,
private val onFinish: () -> Unit
) {
private val greenKey = run {
val attributes = TextAttributes().apply {
@ -66,7 +67,7 @@ class DiffStreamHandler(
private var changeCount: Int = 0
private var running: Boolean = false
init {
fun setup() {
// Highlight the range with unfinished color
for (i in startLine..endLine) {
val highlighter = editor.markupModel.addLineHighlighter(unfinishedKey, min(
@ -192,6 +193,8 @@ class DiffStreamHandler(
val parsed = Gson().fromJson(response, Map::class.java)
val done = parsed["done"] as? Boolean
if (done == true) {
onFinish()
ApplicationManager.getApplication().invokeLater {
// Clean up highlighters
if (currentLineHighlighter != null) {
@ -204,16 +207,6 @@ class DiffStreamHandler(
textArea.requestFocus()
}
editor.contentComponent.addKeyListener(object : java.awt.event.KeyAdapter() {
override fun keyPressed(e: KeyEvent) {
if (e.isMetaDown && e.isShiftDown && e.keyCode == KeyEvent.VK_ENTER) {
accept()
} else if (e.isMetaDown && e.isShiftDown && e.keyCode == KeyEvent.VK_BACK_SPACE) {
reject()
}
}
})
return@request
}
val data = parsed["content"] as Map<*, *>

View file

@ -8,6 +8,9 @@ class DiffStreamService {
private val handlers = mutableMapOf<Editor, DiffStreamHandler>()
fun register(handler: DiffStreamHandler, editor: Editor) {
if (handlers.containsKey(editor)) {
handlers[editor]?.reject()
}
handlers[editor] = handler
}

View file

@ -24,9 +24,7 @@ import org.jdesktop.swingx.border.DropShadowBorder
import java.awt.*
import java.awt.event.*
import java.awt.geom.RoundRectangle2D
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.JTextArea
import javax.swing.*
import javax.swing.border.EmptyBorder
import javax.swing.event.DocumentEvent
import javax.swing.event.DocumentListener
@ -82,19 +80,23 @@ class InlineEditAction : AnAction(), DumbAware {
val textArea = makeTextArea()
// Create diff stream handler
val diffStreamHandler = DiffStreamHandler(project, editor, textArea, startLineNum, endLineNum) {
val diffStreamHandler = DiffStreamHandler(project, editor, textArea, startLineNum, endLineNum, {
inlayRef.get().dispose()
}
}, {
customPanelRef.get().finish()
})
val diffStreamService = service<DiffStreamService>()
diffStreamService.register(diffStreamHandler, editor)
diffStreamHandler.setup()
fun onEnter() {
customPanelRef.get().enter()
diffStreamHandler.run(textArea.text, prefix, highlighted, suffix)
}
val panel = makePanel(customPanelRef, textArea, inlayRef, leftInset, {onEnter()}, {
diffStreamHandler.reject()
diffStreamService.reject(editor)
selectionModel.setSelection(start, end)
}, {
diffStreamService.accept(editor)
@ -174,7 +176,8 @@ class InlineEditAction : AnAction(), DumbAware {
val topPanel = ShadowPanel(MigLayout("wrap 1, insets 10 $leftInset 8 8, gap 0!")).apply {
val globalScheme = EditorColorsManager.getInstance().globalScheme
val defaultBackground = globalScheme.defaultBackground
background = defaultBackground
// background = defaultBackground
background = JBColor(0x20888888.toInt(), 0x20888888.toInt())
isOpaque = false
}
@ -235,6 +238,27 @@ class CustomPanel(layout: MigLayout, onEnter: () -> Unit, onCancel: () -> Unit,
}
private val subPanelB: JPanel = JPanel(MigLayout("insets 0, fillx")).apply {
// Get the global color scheme and default background color
val globalScheme = EditorColorsManager.getInstance().globalScheme
val defaultBackground = globalScheme.defaultBackground
val leftButton = CustomButton("Esc to cancel", {onCancel()}).apply {
foreground = Color(128, 128, 128, 200)
background = defaultBackground
}
val progressBar = JProgressBar()
progressBar.isIndeterminate = true
border = EmptyBorder(4, 8, 4, 8)
add(leftButton, "align left")
add(progressBar, "align right")
isOpaque = false
cursor = Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)
}
private val subPanelC: JPanel = JPanel(MigLayout("insets 0, fillx")).apply {
val leftLabel = JLabel("Enter follow-up instructions").apply {
foreground = Color(128, 128, 128, 200)
font = Font("Arial", Font.PLAIN, 11)
@ -272,6 +296,13 @@ class CustomPanel(layout: MigLayout, onEnter: () -> Unit, onCancel: () -> Unit,
repaint()
}
fun finish() {
remove(subPanelB)
add(subPanelC, "grow, gap 0!")
revalidate()
repaint()
}
override fun paintComponent(g: Graphics) {
val g2 = g as Graphics2D
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
@ -361,10 +392,6 @@ class CustomTextArea(rows: Int, columns: Int) : JXTextArea("") {
}
class ShadowPanel(layout: LayoutManager) : JXPanel(layout) {
init {
}
override fun getPreferredSize(): Dimension {
val prefSize = super.getPreferredSize()
val insets = getInsets()