Move node graph collision avoidance behind an off-by-default preference

This commit is contained in:
Dennis Kobert committed 2026-11-22 15:39:54 +01:00
1 parent df22d3ff08
commit fbc2ea806d
8 files changed
+73 -6

No files matched your search

@@ -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]);
}
// =========
@@ -284,6 +284,8 @@ impl MessageHandler<DocumentMessage, DocumentMessageContext<'_>> for DocumentMes
fonts,
} = context;
self.network_interface.set_collision_avoidance(preferences.node_graph_collision_avoidance);
match message {
// Sub-messages
DocumentMessage::Navigation(message) => {
@@ -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<NetworkId, CopiedDefinition>,
/// 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,
}
}
}
@@ -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");
@@ -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<NodeId>,
indices: HashMap<NodeId, u32>,
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<R>(&self, network_path: &[NodeId], read: impl FnOnce(&NetworkLayout) -> R) -> Option<R> {
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,
@@ -24,6 +24,9 @@ pub enum PreferencesMessage {
GraphWireStyle {
style: GraphWireStyle,
},
NodeGraphCollisionAvoidance {
enabled: bool,
},
ViewportZoomWheelRate {
rate: f64,
},
@@ -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<PreferencesMessage, PreferencesMessageContext<'_>> 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;
}
+19 -1
View File
@@ -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);