Files
Ely-GPUI-Components/src/canvas/wiring.rs
T
ZacharyZhang-NYandClaude Opus 5.5 f3822a8d98 Canvas · Graphs and boards: node graph, flow chart, mind map, whiteboard
NodeGraph joins Nodes by wires between typed Ports: a title drags a
node, a drag out of an output lands on an input of the same kind, an
input takes one wire, and a press on a wired input lifts its wire.
Links join shapes on the plane and follow them, straight or at right
angles, with an arrow and words; the Connector tool draws them and the
Note tool drops a sticky note. A double press with Select writes on a
shape. FlowChart draws Steps as ellipses, boxes and diamonds with their
words. MindMap lays topics out from a root, half to each side, and asks
the owner to add, remove and rename by key. Whiteboard keeps its own
tool, view and selection over a board it hands back after each change.
Painting moves to canvas::paint; a press on the tool layer goes through
canvas::press, and the layer focuses itself and asks to delete.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 08:22:54 -04:00

99 lines
3.2 KiB
Rust

use std::rc::Rc;
use gpui::{App, Bounds, Entity, EntityId, Pixels, SharedString, Window};
use super::graph::{Edge, Node, Socket, fits, socket_at};
pub(super) type OnNudge = Rc<dyn Fn(&SharedString, (f32, f32), &mut Window, &mut App)>;
pub(super) type OnEdge = Rc<dyn Fn(&Edge, &mut Window, &mut App)>;
/// A socket's reach for a press or a release, in view pixels.
pub(super) const REACH: f32 = 10.0;
/// What a press on the graph set going.
#[derive(Clone, Debug, Default)]
pub(super) enum Pull {
#[default]
Idle,
Moving {
key: SharedString,
from: (f32, f32),
to: (f32, f32),
},
Wiring {
from: Socket,
lifted: Option<Edge>,
to: (f32, f32),
},
}
/// The graph's own state: the pull under way and the box it covers.
#[derive(Default)]
pub(super) struct Hold {
pub(super) pull: Pull,
pub(super) bounds: Bounds<Pixels>,
}
/// A drag on a graph, and whose graph it is.
pub(super) struct Pulling {
pub(super) owner: EntityId,
}
/// What a finished pull asks the owner, given the graph as it was.
pub(super) struct Ending {
pub(super) nodes: Rc<Vec<Node>>,
pub(super) edges: Rc<Vec<Edge>>,
pub(super) zoom: f32,
pub(super) on_move: Option<OnNudge>,
pub(super) on_connect: Option<OnEdge>,
pub(super) on_disconnect: Option<OnEdge>,
}
impl Ending {
pub(super) fn run(&self, hold: &Entity<Hold>, window: &mut Window, cx: &mut App) {
let pull = hold.update(cx, |hold, cx| {
cx.notify();
std::mem::take(&mut hold.pull)
});
match pull {
Pull::Moving { key, from, to } => {
let by = (to.0 - from.0, to.1 - from.1);
if by != (0.0, 0.0) {
log::info!("node graph: move {key} by {by:?}");
if let Some(on_move) = &self.on_move {
on_move(&key, by, window, cx);
}
}
}
Pull::Wiring { from, lifted, to } => {
let target =
socket_at(&self.nodes, to, REACH / self.zoom).filter(|to| fits(&from, to));
let made = target.map(|to| {
Edge::new((from.node.clone(), from.port.clone()), (to.node, to.port))
});
if made.is_some() && made == lifted {
return;
}
let replaced = made.as_ref().and_then(|made| {
self.edges
.iter()
.find(|edge| edge.to == made.to && Some(*edge) != lifted.as_ref())
});
for gone in lifted.iter().chain(replaced) {
log::info!("node graph: unwire {gone:?}");
if let Some(on_disconnect) = &self.on_disconnect {
on_disconnect(gone, window, cx);
}
}
if let Some(made) = &made {
log::info!("node graph: wire {made:?}");
if let Some(on_connect) = &self.on_connect {
on_connect(made, window, cx);
}
}
}
Pull::Idle => {}
}
}
}