Skip to content

Commit 1887f00

Browse files
committed
feat: update port interaction logic
1 parent bf798d5 commit 1887f00

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

crates/core/src/plugin.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,22 @@ impl<'a> PluginContext<'a> {
514514
Some(self.renderers.get(node.renderer_key()))
515515
}
516516

517+
/// World-space offset from the node's top-left ([`Node::point`]) to the port anchor used for
518+
/// edge wiring (same as [`NodeRenderer::port_offset`]).
519+
///
520+
/// `graph` must contain `node` and that node's ports (a scratch graph is fine) so multi-port
521+
/// spacing matches runtime layout.
522+
pub(crate) fn port_world_offset_relative(
523+
&self,
524+
graph: &Graph,
525+
node: &Node,
526+
port: &Port,
527+
) -> Point<Pixels> {
528+
self.renderers
529+
.get(node.renderer_key())
530+
.port_offset(node, port, graph)
531+
}
532+
517533
pub fn get_node_mut(&mut self, id: &NodeId) -> Option<&mut Node> {
518534
self.graph.get_node_mut(id)
519535
}

crates/core/src/plugins/port/interaction.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{collections::HashSet, sync::Arc};
33
use gpui::{Bounds, Element, MouseButton, Pixels, Point, canvas, px, rgb};
44

55
use crate::{
6-
DefaultEdgeValidator, EdgeValidator, PortId, PortKind, PortPosition, ToastMessage,
6+
DefaultEdgeValidator, EdgeValidator, Graph, PortId, PortKind, PortPosition, ToastMessage,
77
canvas::Interaction,
88
plugin::{FlowEvent, InputEvent, Plugin, RenderContext},
99
plugins::port::{edge_bezier, filled_disc_path, port_screen_big_bounds, port_screen_bounds},
@@ -75,17 +75,31 @@ impl PortInteractionPlugin {
7575
return;
7676
};
7777

78-
let x: f32 = p.end_world.x.into();
79-
let y: f32 = p.end_world.y.into();
80-
8178
let mut builder = ctx.create_node("");
82-
builder = builder.position(x, y);
8379
builder = match source.kind() {
8480
PortKind::Output => builder.input(),
8581
PortKind::Input => builder.output(),
8682
};
8783

88-
let (new_node, new_ports, _) = builder.build_raw();
84+
let (mut new_node, new_ports, _) = builder.build_raw();
85+
86+
let Some(connect_port) = (match source.kind() {
87+
PortKind::Output => new_ports.iter().find(|p| p.kind() == PortKind::Input),
88+
PortKind::Input => new_ports.iter().find(|p| p.kind() == PortKind::Output),
89+
}) else {
90+
return;
91+
};
92+
93+
let mut scratch = Graph::new();
94+
scratch.add_node(new_node.clone());
95+
for port in &new_ports {
96+
scratch.add_port(port.clone());
97+
}
98+
let offset = ctx.port_world_offset_relative(&scratch, &new_node, connect_port);
99+
new_node.set_position_with_point(Point::new(
100+
p.end_world.x - offset.x,
101+
p.end_world.y - offset.y,
102+
));
89103

90104
let edge = match source.kind() {
91105
PortKind::Output => {

0 commit comments

Comments
 (0)