Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type" : "bugfix",
"description" : "Fix the issue that sometime images added via drag & drop are incorrectly routed to pinned context"
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ import software.aws.toolkits.jetbrains.services.codemodernizer.utils.isCodeTrans
import software.aws.toolkits.resources.message
import java.awt.datatransfer.DataFlavor
import java.awt.dnd.DropTarget
import java.awt.dnd.DropTargetDragEvent
import java.awt.dnd.DropTargetDropEvent
import java.awt.dnd.DropTargetEvent
import java.io.File
import java.util.concurrent.CompletableFuture
import javax.imageio.ImageIO.read
Expand Down Expand Up @@ -138,6 +140,15 @@ class AmazonQPanel(val project: Project, private val scope: CoroutineScope) : Di
// As an alternative, enabling the native drag in JCEF,
// and let the native handling the drop event, and update the UI through JS bridge.
val dropTarget = object : DropTarget() {
override fun dragEnter(dtde: DropTargetDragEvent) {
setDragAndDropOverlayVisible(browserInstance, true)
}
override fun dragOver(dtde: DropTargetDragEvent) {
setDragAndDropOverlayVisible(browserInstance, true)
}
override fun dragExit(dte: DropTargetEvent) {
setDragAndDropOverlayVisible(browserInstance, false)
}
override fun drop(dtde: DropTargetDropEvent) {
try {
dtde.acceptDrop(dtde.dropAction)
Expand Down Expand Up @@ -166,20 +177,16 @@ class AmazonQPanel(val project: Project, private val scope: CoroutineScope) : Di
validImages.subList(20, validImages.size).clear()
}

executeJavaScript(browserInstance, "window.resetTopBarClicked()")

setDragAndDropOverlayVisible(browserInstance, false)

val json = OBJECT_MAPPER.writeValueAsString(validImages)
browserInstance.jcefBrowser.cefBrowser.executeJavaScript(
"window.handleNativeDrop('$json')",
browserInstance.jcefBrowser.cefBrowser.url,
0
)
executeJavaScript(browserInstance, "window.handleNativeDrop('$json')")

if (errorMessages.isNotEmpty()) {
val errorJson = OBJECT_MAPPER.writeValueAsString(errorMessages)
browserInstance.jcefBrowser.cefBrowser.executeJavaScript(
"window.handleNativeNotify('$errorJson')",
browserInstance.jcefBrowser.cefBrowser.url,
0
)
executeJavaScript(browserInstance, "window.handleNativeNotify('$errorJson')")
}

dtde.dropComplete(true)
Expand Down Expand Up @@ -286,6 +293,22 @@ class AmazonQPanel(val project: Project, private val scope: CoroutineScope) : Di
}
}

private fun executeJavaScript(browserInstance: Browser, jsCommand: String) {
try {
browserInstance.jcefBrowser.cefBrowser.executeJavaScript(
jsCommand,
browserInstance.jcefBrowser.cefBrowser.url,
0
)
} catch (e: Exception) {
LOG.error { "Failed to execute JavaScript: $jsCommand - ${e.message}" }
}
}

private fun setDragAndDropOverlayVisible(browserInstance: Browser, visible: Boolean) {
executeJavaScript(browserInstance, "window.setDragAndDropVisible('$visible')")
}

private fun validateImageFile(file: File, allowedTypes: Set<String>, maxFileSize: Double, maxDimension: Int): String? {
val fileName = file.name
val ext = fileName.substringAfterLast('.', "").lowercase()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ class Browser(parent: Disposable, private val webUri: URI, val project: Project)
},
})
};

window.setDragAndDropVisible = function(visibility) {
const parsedVisibility = JSON.parse(visibility);
qChat.setDragOverlayVisible(qChat.getSelectedTabId(), parsedVisibility)
};

window.resetTopBarClicked = function() {
qChat.resetTopBarClicked(qChat.getSelectedTabId())
};
}
</script>
""".trimIndent()
Expand Down
Loading