Skip to content

Commit 6109e0e

Browse files
authored
Add GraphPosition parameter to onConnectEnd callback (#22)
1 parent d741657 commit 6109e0e

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

packages/vyuh_node_flow/lib/graph/api/connection_api.dart

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,8 +1103,13 @@ extension ConnectionApi<T> on NodeFlowController<T> {
11031103
required String targetPortId,
11041104
}) {
11051105
final temp = interaction.temporaryConnection.value;
1106+
1107+
final eventPosition = interaction.pointerPosition == null
1108+
? GraphPosition.zero
1109+
: viewport.toGraph(interaction.pointerPosition!);
1110+
11061111
if (temp == null) {
1107-
events.connection?.onConnectEnd?.call(null, null);
1112+
events.connection?.onConnectEnd?.call(null, null, eventPosition);
11081113
return null;
11091114
}
11101115

@@ -1213,10 +1218,16 @@ extension ConnectionApi<T> on NodeFlowController<T> {
12131218

12141219
// Fire connection end event with the target node and port that the user dropped on
12151220
final droppedOnNode = _nodes[targetNodeId];
1221+
12161222
final droppedOnPort = droppedOnNode?.allPorts
12171223
.where((p) => p.id == targetPortId)
12181224
.firstOrNull;
1219-
events.connection?.onConnectEnd?.call(droppedOnNode, droppedOnPort);
1225+
1226+
events.connection?.onConnectEnd?.call(
1227+
droppedOnNode,
1228+
droppedOnPort,
1229+
eventPosition,
1230+
);
12201231

12211232
return createdConnection;
12221233
}
@@ -1228,6 +1239,7 @@ extension ConnectionApi<T> on NodeFlowController<T> {
12281239
void cancelConnectionDrag() {
12291240
// Reset highlighted port before canceling
12301241
final temp = interaction.temporaryConnection.value;
1242+
12311243
if (temp != null &&
12321244
temp.targetNodeId != null &&
12331245
temp.targetPortId != null) {
@@ -1245,7 +1257,10 @@ extension ConnectionApi<T> on NodeFlowController<T> {
12451257
interaction.cancelConnection();
12461258

12471259
// Note: Canvas unlocking is now handled by DragSession
1248-
1249-
events.connection?.onConnectEnd?.call(null, null);
1260+
events.connection?.onConnectEnd?.call(
1261+
null,
1262+
null,
1263+
temp == null ? GraphPosition.zero : GraphPosition(temp.currentPoint),
1264+
);
12501265
}
12511266
}

packages/vyuh_node_flow/lib/graph/node_flow_events.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,13 @@ class ConnectionEvents<T> {
282282
///
283283
/// If [targetNode] and [targetPort] are non-null, a connection was successfully
284284
/// created to that target. If both are null, the connection was cancelled.
285-
final void Function(Node<T>? targetNode, Port? targetPort)? onConnectEnd;
285+
/// [position] provides the graph coordinates where the event occurred.
286+
final void Function(
287+
Node<T>? targetNode,
288+
Port? targetPort,
289+
GraphPosition position,
290+
)?
291+
onConnectEnd;
286292

287293
/// Validation callback before starting a connection from a port
288294
/// Return ConnectionValidationResult with allowed: false to prevent connection start
@@ -307,7 +313,12 @@ class ConnectionEvents<T> {
307313
void Function(Connection connection, ScreenPosition screenPosition)?
308314
onContextMenu,
309315
void Function(Node<T> sourceNode, Port sourcePort)? onConnectStart,
310-
void Function(Node<T>? targetNode, Port? targetPort)? onConnectEnd,
316+
void Function(
317+
Node<T>? targetNode,
318+
Port? targetPort,
319+
GraphPosition position,
320+
)?
321+
onConnectEnd,
311322
ConnectionValidationResult Function(ConnectionStartContext<T> context)?
312323
onBeforeStart,
313324
ConnectionValidationResult Function(ConnectionCompleteContext<T> context)?

packages/vyuh_node_flow/test/behavior/connections/connection_creation_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ void main() {
592592
controller.internalUpdateEvents(
593593
NodeFlowEvents<String>(
594594
connection: ConnectionEvents(
595-
onConnectEnd: (node, port) {
595+
onConnectEnd: (node, port, _) {
596596
endTargetNode = node;
597597
endTargetPort = port;
598598
},
@@ -685,7 +685,7 @@ void main() {
685685
controller.internalUpdateEvents(
686686
NodeFlowEvents<String>(
687687
connection: ConnectionEvents(
688-
onConnectEnd: (node, port) {
688+
onConnectEnd: (node, port, _) {
689689
callbackFired = true;
690690
endTargetNode = node;
691691
endTargetPort = port;

0 commit comments

Comments
 (0)