Skip to content
Open
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
Expand Up @@ -80,6 +80,7 @@ fun BoardCanvas(
onDetachConnection: (Connection, Boolean, Offset) -> Unit,
onConnectionDrag: (Offset) -> Unit,
onConnectionDrop: (Boolean) -> Unit,
onConnectionCancel: () -> Unit,
onMoveConnectionFirst: (Connection) -> Unit,
onMoveConnectionLast: (Connection) -> Unit,
selectedNodeIds: Set<Long>,
Expand Down Expand Up @@ -173,6 +174,7 @@ fun BoardCanvas(
onZoom = onZoom,
onConnectionDrag = onConnectionDrag,
onConnectionDrop = onConnectionDrop,
onConnectionCancel = onConnectionCancel,
onDeleteConnection = onDeleteConnection,
onDetachConnection = onDetachConnection
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import org.wip.plugintoolkit.api.isCompatibleWith
import org.wip.plugintoolkit.core.notification.NotificationService
import org.wip.plugintoolkit.core.theme.ToolkitTheme
import org.wip.plugintoolkit.features.flows.model.Flow
import org.wip.plugintoolkit.features.flows.model.Connection
import org.wip.plugintoolkit.features.flows.model.Node
import org.wip.plugintoolkit.features.flows.model.NodeSerializationUtils
import org.wip.plugintoolkit.features.flows.viewmodel.FlowEditorViewModel
Expand Down Expand Up @@ -154,6 +155,7 @@ fun FlowEditorView(
var dragStartPosition by remember { mutableStateOf(Offset.Zero) }
var dragGrabOffset by remember { mutableStateOf(Offset.Zero) }
var draggingNodeScale by remember { mutableStateOf(1f) }
var connectionBeingRewired by remember { mutableStateOf<Connection?>(null) }

val handlePaletteClick = { paletteNode: PaletteNode ->
val dropPos = (Offset(boardSize.width / 2f, boardSize.height / 2f) - state.offset) / state.scale
Expand Down Expand Up @@ -213,7 +215,7 @@ fun FlowEditorView(
onBoardSizeChanged = { boardSize = it },
onDeleteConnection = { viewModel.onEvent(FlowEvent.DeleteConnection(it)) },
onDetachConnection = { connection, isSource, offset ->
viewModel.onEvent(FlowEvent.DeleteConnection(connection))
connectionBeingRewired = connection
if (isSource) {
connectionStartNodeId = connection.targetNodeId
connectionStartPortId = connection.targetPortId
Expand Down Expand Up @@ -245,17 +247,38 @@ fun FlowEditorView(
val targetPortId =
if (connectionStartIsOutput) highlightedPortId!! else connectionStartPortId!!

val original = connectionBeingRewired
viewModel.onEvent(
FlowEvent.TryConnectPorts(
sourceNodeId,
sourcePortId,
targetNodeId,
targetPortId,
isShiftPressed
)
if (original != null) {
FlowEvent.RewireConnection(
original,
sourceNodeId,
sourcePortId,
targetNodeId,
targetPortId,
isShiftPressed
)
} else {
FlowEvent.TryConnectPorts(
sourceNodeId,
sourcePortId,
targetNodeId,
targetPortId,
isShiftPressed
)
}
)
}
isDrawingConnection = false
connectionBeingRewired = null
connectionStartNodeId = null
connectionStartPortId = null
highlightedPortId = null
highlightedNodeId = null
},
onConnectionCancel = {
isDrawingConnection = false
connectionBeingRewired = null
connectionStartNodeId = null
connectionStartPortId = null
highlightedPortId = null
Expand Down Expand Up @@ -708,7 +731,8 @@ fun FlowEditorView(
pendingConn.sourceNodeId,
pendingConn.sourcePortId,
pendingConn.targetNodeId,
pendingConn.targetPortId
pendingConn.targetPortId,
pendingConn.originalConnection
)
)
viewModel.onEvent(FlowEvent.CancelPendingConnection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fun RenderTestBoardCanvas(
onDetachConnection = { _, _, _ -> },
onConnectionDrag = {},
onConnectionDrop = {},
onConnectionCancel = {},
onMoveConnectionFirst = {},
onMoveConnectionLast = {},
selectedNodeIds = selectedNodeIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package org.wip.plugintoolkit.features.flows.ui.canvas

import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.geometry.Offset
Expand Down Expand Up @@ -145,6 +148,7 @@ fun Modifier.boardSelectionBoxGesture(
)
}

@Composable
fun Modifier.boardPointerEventGesture(
interactionState: BoardInteractionState,
isDrawingConnection: Boolean,
Expand All @@ -156,9 +160,17 @@ fun Modifier.boardPointerEventGesture(
onZoom: (Float, Offset, Boolean) -> Unit,
onConnectionDrag: (Offset) -> Unit,
onConnectionDrop: (Boolean) -> Unit,
onConnectionCancel: () -> Unit,
onDeleteConnection: (Connection) -> Unit,
onDetachConnection: (Connection, Boolean, Offset) -> Unit
): Modifier = this.pointerInput(connections, nodes, scale, offset, isDrawingConnection) {
): Modifier {
val currentIsDrawingConnection by rememberUpdatedState(isDrawingConnection)
val currentOnConnectionDrag by rememberUpdatedState(onConnectionDrag)
val currentOnConnectionDrop by rememberUpdatedState(onConnectionDrop)
val currentOnConnectionCancel by rememberUpdatedState(onConnectionCancel)
val currentOnDetachConnection by rememberUpdatedState(onDetachConnection)

return this.pointerInput(connections, nodes, scale, offset) {
awaitPointerEventScope {
while (true) {
val event = awaitPointerEvent()
Expand All @@ -173,9 +185,9 @@ fun Modifier.boardPointerEventGesture(
}
} else if (event.type == PointerEventType.Move) {
interactionState.lastPointerPosition = position
if (isDrawingConnection) {
if (currentIsDrawingConnection) {
val boardPos = (position - offset) / scale
onConnectionDrag(boardPos)
currentOnConnectionDrag(boardPos)
}

var bestConnection: Connection? = null
Expand Down Expand Up @@ -241,26 +253,33 @@ fun Modifier.boardPointerEventGesture(
} else if (event.keyboardModifiers.isCtrlPressed) {
interactionState.hoveredConnection?.let { conn ->
val isSrc = interactionState.hoveredConnectionIsSource ?: false
onDetachConnection(conn, isSrc, position)
currentOnDetachConnection(conn, isSrc, position)

event.changes.forEach { it.consume() }

while (true) {
val dragEvent = awaitPointerEvent()
if (dragEvent.type == PointerEventType.Move) {
val screenPos = dragEvent.changes.firstOrNull()?.position ?: Offset.Zero
interactionState.lastPointerPosition = screenPos
val boardPos = (screenPos - offset) / scale
onConnectionDrag(boardPos)
dragEvent.changes.forEach { it.consume() }
} else if (dragEvent.type == PointerEventType.Release) {
onConnectionDrop(dragEvent.keyboardModifiers.isShiftPressed)
break
var completed = false
try {
while (true) {
val dragEvent = awaitPointerEvent()
if (dragEvent.type == PointerEventType.Move) {
val screenPos = dragEvent.changes.firstOrNull()?.position ?: Offset.Zero
interactionState.lastPointerPosition = screenPos
val boardPos = (screenPos - offset) / scale
currentOnConnectionDrag(boardPos)
dragEvent.changes.forEach { it.consume() }
} else if (dragEvent.type == PointerEventType.Release) {
currentOnConnectionDrop(dragEvent.keyboardModifiers.isShiftPressed)
completed = true
break
}
}
} finally {
if (!completed) currentOnConnectionCancel()
}
}
}
}
}
}
}
}
Loading