mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
[PyCharm] PY-80171 Jupyter DND emergency fix - pressing up/down key during drag cancels the operation instead of freezed preview popup
GitOrigin-RevId: e2e5da860d896214cc200a7315d0ea57da166095
This commit is contained in:
committed by
intellij-monorepo-bot
parent
d5bc3fd128
commit
fb8e3edefb
@@ -14,6 +14,9 @@ import java.awt.Point
|
|||||||
import java.awt.event.MouseEvent
|
import java.awt.event.MouseEvent
|
||||||
import javax.swing.JComponent
|
import javax.swing.JComponent
|
||||||
import kotlin.text.lines
|
import kotlin.text.lines
|
||||||
|
import java.awt.KeyboardFocusManager
|
||||||
|
import java.awt.KeyEventDispatcher
|
||||||
|
import java.awt.event.KeyEvent
|
||||||
|
|
||||||
class EditorCellDragAssistant(
|
class EditorCellDragAssistant(
|
||||||
private val editor: EditorImpl,
|
private val editor: EditorImpl,
|
||||||
@@ -36,10 +39,52 @@ class EditorCellDragAssistant(
|
|||||||
private var outputInitialStates: MutableMap<Int, Boolean> = mutableMapOf()
|
private var outputInitialStates: MutableMap<Int, Boolean> = mutableMapOf()
|
||||||
|
|
||||||
private val inlayManager = NotebookCellInlayManager.get(editor)
|
private val inlayManager = NotebookCellInlayManager.get(editor)
|
||||||
|
private var keyEventDispatcher: KeyEventDispatcher? = null
|
||||||
|
|
||||||
fun initDrag(e: MouseEvent) {
|
fun initDrag(e: MouseEvent) {
|
||||||
isDragging = true
|
isDragging = true
|
||||||
dragStartPoint = e.locationOnScreen
|
dragStartPoint = e.locationOnScreen
|
||||||
|
attachKeyEventDispatcher()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun attachKeyEventDispatcher() {
|
||||||
|
if (keyEventDispatcher == null) {
|
||||||
|
keyEventDispatcher = KeyEventDispatcher { keyEvent ->
|
||||||
|
if (isDragging) {
|
||||||
|
when (keyEvent.id) {
|
||||||
|
KeyEvent.KEY_PRESSED -> {
|
||||||
|
handleKeyPressedDuringDrag(keyEvent)
|
||||||
|
return@KeyEventDispatcher false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
KeyboardFocusManager.getCurrentKeyboardFocusManager()
|
||||||
|
.addKeyEventDispatcher(keyEventDispatcher)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleKeyPressedDuringDrag(keyEvent: KeyEvent) {
|
||||||
|
when (keyEvent.keyCode) {
|
||||||
|
KeyEvent.VK_ESCAPE -> cancelDrag()
|
||||||
|
KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT -> cancelDrag()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun cancelDrag() {
|
||||||
|
deleteDragPreview()
|
||||||
|
clearDragState()
|
||||||
|
unfoldCellIfNeeded()
|
||||||
|
removeKeyEventDispatcher()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun removeKeyEventDispatcher() {
|
||||||
|
keyEventDispatcher?.let {
|
||||||
|
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(it)
|
||||||
|
keyEventDispatcher = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateDragOperation(e: MouseEvent) {
|
fun updateDragOperation(e: MouseEvent) {
|
||||||
@@ -58,6 +103,8 @@ class EditorCellDragAssistant(
|
|||||||
|
|
||||||
fun finishDrag(e: MouseEvent) {
|
fun finishDrag(e: MouseEvent) {
|
||||||
deleteDragPreview()
|
deleteDragPreview()
|
||||||
|
removeKeyEventDispatcher()
|
||||||
|
|
||||||
if (!isDragging || dragStartPoint == null) {
|
if (!isDragging || dragStartPoint == null) {
|
||||||
isDragging = false
|
isDragging = false
|
||||||
return
|
return
|
||||||
@@ -112,7 +159,7 @@ class EditorCellDragAssistant(
|
|||||||
|
|
||||||
private fun foldDraggedCell() {
|
private fun foldDraggedCell() {
|
||||||
inputFoldedState = cellInput.folded
|
inputFoldedState = cellInput.folded
|
||||||
if (inputFoldedState == false) foldInput()
|
if (!inputFoldedState) foldInput()
|
||||||
|
|
||||||
cellInput.cell.view?.outputs?.outputs?.forEachIndexed { index, output ->
|
cellInput.cell.view?.outputs?.outputs?.forEachIndexed { index, output ->
|
||||||
outputInitialStates[index] = output.collapsed
|
outputInitialStates[index] = output.collapsed
|
||||||
@@ -122,8 +169,8 @@ class EditorCellDragAssistant(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun unfoldCellIfNeeded() {
|
private fun unfoldCellIfNeeded() {
|
||||||
if (wasFolded == false) return
|
if (!wasFolded) return
|
||||||
if (inputFoldedState == false) unfoldInput()
|
if (!inputFoldedState) unfoldInput()
|
||||||
|
|
||||||
cellInput.cell.view?.outputs?.outputs?.forEachIndexed { index, output ->
|
cellInput.cell.view?.outputs?.outputs?.forEachIndexed { index, output ->
|
||||||
output.collapsed = outputInitialStates[index] == true
|
output.collapsed = outputInitialStates[index] == true
|
||||||
@@ -164,6 +211,7 @@ class EditorCellDragAssistant(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun dispose() {
|
override fun dispose() {
|
||||||
|
removeKeyEventDispatcher()
|
||||||
deleteDropIndicator()
|
deleteDropIndicator()
|
||||||
deleteDragPreview()
|
deleteDragPreview()
|
||||||
unfoldCellIfNeeded()
|
unfoldCellIfNeeded()
|
||||||
@@ -181,4 +229,4 @@ class EditorCellDragAssistant(
|
|||||||
private const val MINIMAL_DRAG_DISTANCE = 8
|
private const val MINIMAL_DRAG_DISTANCE = 8
|
||||||
private const val MAX_PREVIEW_TEXT_LENGTH = 20
|
private const val MAX_PREVIEW_TEXT_LENGTH = 20
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user