From fbc2ea806da57af336643de4e088ea57db7f8a7a Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sun, 22 Nov 2026 15:39:54 +0100 Subject: [PATCH] Move node graph collision avoidance behind an off-by-default preference --- .../preferences_dialog_message_handler.rs | 25 ++++++++++++++++++- .../document/document_message_handler.rs | 2 ++ .../utility_types/network_interface.rs | 4 +++ .../characterization_tests.rs | 2 ++ .../network_interface/layout_pass.rs | 16 +++++++++--- .../preferences/preferences_message.rs | 3 +++ .../preferences_message_handler.rs | 7 ++++++ libraries/graph-layout/src/lib.rs | 20 ++++++++++++++- 8 files changed, 73 insertions(+), 6 deletions(-) diff --git a/editor/src/messages/dialog/preferences_dialog/preferences_dialog_message_handler.rs b/editor/src/messages/dialog/preferences_dialog/preferences_dialog_message_handler.rs index 2edeb78534..b9b66c2c52 100644 --- a/editor/src/messages/dialog/preferences_dialog/preferences_dialog_message_handler.rs +++ b/editor/src/messages/dialog/preferences_dialog/preferences_dialog_message_handler.rs @@ -297,7 +297,30 @@ impl PreferencesDialogMessageHandler { .widget_instance(), ]; - rows.extend_from_slice(&[header, node_graph_wires_label, graph_wire_style, brush_tool]); + let checkbox_id = CheckboxId::new(); + let collision_avoidance_description = " + Move nodes in the graph apart so they don't overlap, drawing their connections longer where needed.\n\ + \n\ + *Default: Off.* + " + .trim(); + let collision_avoidance = vec![ + Separator::new(SeparatorStyle::Unrelated).widget_instance(), + Separator::new(SeparatorStyle::Unrelated).widget_instance(), + CheckboxInput::new(preferences.node_graph_collision_avoidance) + .tooltip_label("Node Graph Collision Avoidance") + .tooltip_description(collision_avoidance_description) + .on_update(|checkbox_input: &CheckboxInput| PreferencesMessage::NodeGraphCollisionAvoidance { enabled: checkbox_input.checked }.into()) + .for_label(checkbox_id) + .widget_instance(), + TextLabel::new("Node Graph Collision Avoidance") + .tooltip_label("Node Graph Collision Avoidance") + .tooltip_description(collision_avoidance_description) + .for_checkbox(checkbox_id) + .widget_instance(), + ]; + + rows.extend_from_slice(&[header, node_graph_wires_label, graph_wire_style, collision_avoidance, brush_tool]); } // ========= diff --git a/editor/src/messages/portfolio/document/document_message_handler.rs b/editor/src/messages/portfolio/document/document_message_handler.rs index 4118681ec5..557390889c 100644 --- a/editor/src/messages/portfolio/document/document_message_handler.rs +++ b/editor/src/messages/portfolio/document/document_message_handler.rs @@ -284,6 +284,8 @@ impl MessageHandler> for DocumentMes fonts, } = context; + self.network_interface.set_collision_avoidance(preferences.node_graph_collision_avoidance); + match message { // Sub-messages DocumentMessage::Navigation(message) => { diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface.rs b/editor/src/messages/portfolio/document/utility_types/network_interface.rs index 2b10e12771..6491cfc074 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface.rs @@ -119,6 +119,9 @@ pub struct NodeNetworkInterface { /// The table entries the last paste carried, under the IDs they take here, added once a node names one. #[serde(skip)] carried_definitions: HashMap, + /// Whether the layout fits each node's upstream together, drawing connections longer so nodes don't overlap. + #[serde(skip)] + collision_avoidance: bool, } impl Clone for NodeNetworkInterface { @@ -135,6 +138,7 @@ impl Clone for NodeNetworkInterface { transaction_edited_nodes_changed: false, dots_that_lost_a_wire: HashMap::new(), carried_definitions: HashMap::new(), + collision_avoidance: self.collision_avoidance, } } } diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/characterization_tests.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/characterization_tests.rs index f281a1b174..7a59b905e2 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/characterization_tests.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/characterization_tests.rs @@ -3873,6 +3873,7 @@ async fn group_selected_layers(editor: &mut EditorTestUtils) { async fn grouping_the_top_layer_leaves_no_layers_overlapping_in_the_graph() { let mut editor = EditorTestUtils::create(); editor.new_document().await; + editor.handle_message(PreferencesMessage::NodeGraphCollisionAvoidance { enabled: true }).await; editor.draw_rect(0., 0., 100., 100.).await; editor.draw_rect(50., 50., 150., 150.).await; @@ -3886,6 +3887,7 @@ async fn grouping_the_top_layer_leaves_no_layers_overlapping_in_the_graph() { async fn grouping_a_group_moves_the_layer_below_it_past_all_of_its_contents() { let mut editor = EditorTestUtils::create(); editor.new_document().await; + editor.handle_message(PreferencesMessage::NodeGraphCollisionAvoidance { enabled: true }).await; editor.draw_rect(0., 0., 100., 100.).await; editor.draw_rect(50., 50., 150., 150.).await; let bottom = editor.active_document().metadata().all_layers().last().expect("the bottom rectangle should be a layer"); diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/layout_pass.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/layout_pass.rs index bd0f8d8de1..33e438e65f 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/layout_pass.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/layout_pass.rs @@ -1,10 +1,11 @@ use super::*; -use graph_layout::{Graph, Layout, Node, Placement, Wire, layout, reach}; +use graph_layout::{Graph, Layout, Node, Placement, Wire, layout, layout_as_stored, reach}; /// Where each node of a network is drawn and which layer's chain it is drawn in, as of one revision of the interface. #[derive(Debug, Clone)] pub struct NetworkLayout { revision: u64, + collision_avoidance: bool, node_ids: Vec, indices: HashMap, graph: Graph, @@ -63,11 +64,16 @@ impl NetworkLayout { } impl NodeNetworkInterface { - /// Reads the layout of the network at `network_path`, laying it out again first if anything was written since. + pub fn set_collision_avoidance(&mut self, collision_avoidance: bool) { + self.collision_avoidance = collision_avoidance; + } + + /// Reads the layout of the network at `network_path`, laying it out again first if anything was written or collision avoidance was toggled since. pub(crate) fn with_layout(&self, network_path: &[NodeId], read: impl FnOnce(&NetworkLayout) -> R) -> Option { let cache = &self.network_metadata(network_path)?.transient_metadata.layout; let revision = store::revision(); - if cache.with_loaded(|layout| layout.revision != revision) == Some(true) { + let outdated = |layout: &NetworkLayout| layout.revision != revision || layout.collision_avoidance != self.collision_avoidance; + if cache.with_loaded(outdated) == Some(true) { cache.unload(); } @@ -164,9 +170,11 @@ impl NodeNetworkInterface { .collect(); let graph = Graph { nodes, wires }; + let layout = if self.collision_avoidance { layout(&graph) } else { layout_as_stored(&graph) }; Some(NetworkLayout { revision, - layout: layout(&graph), + collision_avoidance: self.collision_avoidance, + layout, graph, node_ids, indices, diff --git a/editor/src/messages/preferences/preferences_message.rs b/editor/src/messages/preferences/preferences_message.rs index c29fa84bec..487850650f 100644 --- a/editor/src/messages/preferences/preferences_message.rs +++ b/editor/src/messages/preferences/preferences_message.rs @@ -24,6 +24,9 @@ pub enum PreferencesMessage { GraphWireStyle { style: GraphWireStyle, }, + NodeGraphCollisionAvoidance { + enabled: bool, + }, ViewportZoomWheelRate { rate: f64, }, diff --git a/editor/src/messages/preferences/preferences_message_handler.rs b/editor/src/messages/preferences/preferences_message_handler.rs index 95efb57b79..005003af55 100644 --- a/editor/src/messages/preferences/preferences_message_handler.rs +++ b/editor/src/messages/preferences/preferences_message_handler.rs @@ -18,6 +18,7 @@ pub struct PreferencesMessageHandler { pub zoom_with_scroll: bool, pub brush_tool: bool, pub graph_wire_style: GraphWireStyle, + pub node_graph_collision_avoidance: bool, pub viewport_zoom_wheel_rate: f64, pub ui_scale: f64, pub disable_ui_acceleration: bool, @@ -52,6 +53,7 @@ impl Default for PreferencesMessageHandler { zoom_with_scroll: matches!(MappingVariant::default(), MappingVariant::ZoomWithScroll), brush_tool: false, graph_wire_style: GraphWireStyle::default(), + node_graph_collision_avoidance: false, viewport_zoom_wheel_rate: VIEWPORT_ZOOM_WHEEL_RATE, ui_scale: UI_SCALE_DEFAULT, disable_ui_acceleration: true, // TODO: Set this back to false once we have ui acceleration working more reliably @@ -129,6 +131,11 @@ impl MessageHandler> for Prefe responses.add(NodeGraphMessage::UnloadWires); responses.add(NodeGraphMessage::SendWires); } + PreferencesMessage::NodeGraphCollisionAvoidance { enabled } => { + self.node_graph_collision_avoidance = enabled; + responses.add(NodeGraphMessage::UnloadWires); + responses.add(NodeGraphMessage::SendGraph); + } PreferencesMessage::ViewportZoomWheelRate { rate } => { self.viewport_zoom_wheel_rate = rate; } diff --git a/libraries/graph-layout/src/lib.rs b/libraries/graph-layout/src/lib.rs index 987712b267..b3efc22653 100644 --- a/libraries/graph-layout/src/lib.rs +++ b/libraries/graph-layout/src/lib.rs @@ -149,12 +149,21 @@ const HORIZONTAL_GAP: i32 = CHAIN_WIDTH - 5; /// Lays out the graph as a forest: each node goes to its highest reader as stored, each node's upstream is then fitted together /// below and left of it, and finally every node is placed from the left of its readers, level with its owner. pub fn layout(graph: &Graph) -> Layout { + lay_out(graph, true) +} + +/// Lays out the graph like `layout`, but draws every node where its stored offset places it, without fitting upstreams together. +pub fn layout_as_stored(graph: &Graph) -> Layout { + lay_out(graph, false) +} + +fn lay_out(graph: &Graph, fit_upstream: bool) -> Layout { let node_count = graph.nodes.len(); let readers = GroupedByNode::grouped(node_count, &graph.wires, |wire| wire.source); let inputs = GroupedByNode::grouped(node_count, &graph.wires, |wire| wire.target); let forest = Forest::grow(graph, &readers, &inputs); - let elongations = elongations(graph, &forest); + let elongations = if fit_upstream { elongations(graph, &forest) } else { vec![IVec2::ZERO; node_count] }; let mut positions = vec![IVec2::ZERO; node_count]; let mut anchors = vec![IVec2::ZERO; node_count]; @@ -590,6 +599,15 @@ mod tests { assert_eq!(layout.positions[5], layout.positions[4] - IVec2::new(CHAIN_WIDTH, 0)); } + #[test] + fn a_layer_laid_out_as_stored_stays_where_it_is_stored() { + let graph = stack_with_tall_content(1, 0); + let layout = layout_as_stored(&graph); + + assert_eq!(layout.positions[4], IVec2::new(30, 13)); + assert!(layout.elongations.iter().all(|elongation| *elongation == IVec2::ZERO)); + } + #[test] fn a_held_layer_stays_where_it_is_stored() { let mut graph = stack_with_tall_content(1, 0);