Skip to main content

ferrum_flow/plugins/node/
mod.rs

1mod command;
2mod interaction;
3
4pub use command::DragNodesCommand;
5use gpui::{Element, ParentElement, div};
6pub use interaction::NodeInteractionPlugin;
7
8use crate::{RenderContext, plugin::Plugin};
9
10pub struct NodePlugin {}
11
12impl NodePlugin {
13    pub fn new() -> Self {
14        Self {}
15    }
16}
17
18impl Plugin for NodePlugin {
19    fn name(&self) -> &'static str {
20        "node"
21    }
22    fn setup(&mut self, _ctx: &mut crate::plugin::InitPluginContext) {}
23    fn on_event(
24        &mut self,
25        _event: &crate::plugin::FlowEvent,
26        _context: &mut crate::plugin::PluginContext,
27    ) -> crate::plugin::EventResult {
28        crate::plugin::EventResult::Continue
29    }
30    fn priority(&self) -> i32 {
31        60
32    }
33    fn render_layer(&self) -> crate::plugin::RenderLayer {
34        crate::plugin::RenderLayer::Nodes
35    }
36    fn render(&mut self, ctx: &mut RenderContext) -> Option<gpui::AnyElement> {
37        let node_ids: Vec<_> = ctx
38            .graph
39            .node_order()
40            .iter()
41            .filter(|node_id| ctx.is_node_visible(node_id))
42            .cloned()
43            .collect();
44
45        ctx.cache_port_offset_with_nodes(&node_ids);
46
47        let list = node_ids.iter().filter_map(|node_id| {
48            let node = ctx.graph.nodes().get(node_id)?;
49            let render = ctx.renderers.get(&node.node_type);
50
51            let node_render = render.render(node, ctx);
52
53            let ports = ctx
54                .graph
55                .ports
56                .iter()
57                .filter(|(_, port)| port.node_id == *node_id)
58                .filter_map(|(_, port)| render.port_render(node, port, ctx));
59
60            Some(div().child(node_render).children(ports))
61        });
62
63        Some(div().children(list).into_any())
64    }
65}