From df22d3ff08a24513bf700e32f53408a73a192d82 Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Wed, 18 Nov 2026 11:54:56 +0100 Subject: [PATCH] Close up the stack above a layer moved up against the layer above it --- .../characterization_tests.rs | 45 +++++++++++++++- .../utility_types/network_interface/layout.rs | 52 +++++++++++++++---- .../network_interface/layout_pass.rs | 20 +++++-- 3 files changed, 100 insertions(+), 17 deletions(-) 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 d71cf46447..f281a1b174 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 @@ -2,7 +2,7 @@ use super::{ ChainInsertionPoint, FlowType, GlideOffsets, ImportOrExport, InputConnector, NodeLocator, NodeNetworkInterface, NodeNetworkTemplate, NodePersistentMetadata, NodePosition, NodeTemplate, NodeTemplateImplementation, NodeTypePersistentMetadata, OutputConnector, Previewing, RootNode, TransactionStatus, }; -use crate::consts::{GRID_SIZE, WIRE_DRAG_NODE_EXPAND_DELAY_MILLISECONDS}; +use crate::consts::{GRID_SIZE, STACK_VERTICAL_GAP, WIRE_DRAG_NODE_EXPAND_DELAY_MILLISECONDS}; use crate::messages::input_mapper::utility_types::pointer::EditorPointerState; use crate::messages::portfolio::document::node_graph::document_node_definitions::resolve_proto_node_type; use crate::messages::portfolio::document::node_graph::utility_types::{ContextMenuData, Direction}; @@ -1077,6 +1077,49 @@ async fn move_layer_to_stack_builds_the_layer_stack() { assert_invariants(&editor, "after moving two layers into an artboard"); } +#[tokio::test] +async fn moving_a_closed_up_layer_up_closes_up_the_stack_above_it_and_then_moves_all_of_it() { + let mut editor = EditorTestUtils::create(); + editor.new_document().await; + + let artboard = NodeId::new(); + editor.handle_message(new_artboard_message(artboard)).await; + + let first = editor.create_node_by_name_at(merge_definition(), 0, 30).await; + let second = editor.create_node_by_name_at(merge_definition(), 0, 40).await; + let third = editor.create_node_by_name_at(merge_definition(), 0, 50).await; + + let network_interface = &mut editor.active_document_mut().network_interface; + let artboard_layer = LayerNodeIdentifier::new(artboard, network_interface); + for (insert_index, layer) in [first, second, third].into_iter().enumerate() { + network_interface.set_to_node_or_layer(&layer, &[], true); + network_interface.move_layer_to_stack(LayerNodeIdentifier::new(layer, network_interface), artboard_layer, insert_index, &[]); + } + + // Two rows open between the first and second layer, while the third is closed up under the second, and the first has room below the artboard + network_interface.shift_node(&first, IVec2::new(0, 2), &[]); + network_interface.shift_node(&second, IVec2::new(0, 2), &[]); + let positions = |network_interface: &NodeNetworkInterface| [first, second, third].map(|layer| network_interface.position(&layer, &[]).expect("every layer is drawn")); + let [first_start, second_start, third_start] = positions(network_interface); + assert_eq!(third_start.y - second_start.y, STACK_VERTICAL_GAP, "fixture is wrong"); + assert_eq!(second_start.y - first_start.y, STACK_VERTICAL_GAP + 2, "fixture is wrong"); + + network_interface.selected_nodes_mut(&[]).expect("the root network has a selection").set_selected_nodes(vec![third]); + let moved_up = |rows: i32, position: IVec2| position - IVec2::new(0, rows); + + // The second layer moves up in place of the third, which it carries, until it closes up under the first + for rows in 1..=2 { + network_interface.shift_selected_nodes(Direction::Up, false, &[]); + assert_eq!(positions(network_interface), [first_start, moved_up(rows, second_start), moved_up(rows, third_start)]); + } + + // Then the whole stack moves up from its top + network_interface.shift_selected_nodes(Direction::Up, false, &[]); + assert_eq!(positions(network_interface), [moved_up(1, first_start), moved_up(3, second_start), moved_up(3, third_start)]); + + assert_invariants(&editor, "after closing up a stack by moving its bottom layer up"); +} + #[tokio::test] async fn a_stack_closes_up_over_a_deleted_layer_and_a_nudge_moves_the_rest_one_row() { let mut editor = EditorTestUtils::create(); diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/layout.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/layout.rs index bbe458ade6..a4bc920cd2 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/layout.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/layout.rs @@ -487,13 +487,38 @@ impl NodeNetworkInterface { let mut shifted = HashSet::new(); for node_id in self.nodes_to_shift(node_ids, network_path) { // A stack keeps its layers aligned, so sideways it moves as a whole from its top layer - let node_id = if shift.x != 0 { self.stack_top(node_id, network_path) } else { node_id }; - if shifted.insert(node_id) { - self.shift_node(&node_id, shift, network_path); + let moved_node_id = match shift { + IVec2 { x: 0, y: -1 } => self.lowest_layer_with_room_above(node_id, network_path), + IVec2 { x: 0, .. } => node_id, + _ => self.stack_top(node_id, network_path), + }; + if !shifted.insert(moved_node_id) { + continue; } + + // A layer moved up in place of the dragged one is held along with it, so it keeps where the drag puts it + if moved_node_id != node_id && self.holds_nodes(network_path) { + self.hold_nodes([moved_node_id].into(), network_path); + } + self.shift_node(&moved_node_id, shift, network_path); } } + /// The first layer from `node_id` up its stack with room to move up before closing up under the layer above it, + /// or the top layer when the stack is closed up all the way. + fn lowest_layer_with_room_above(&self, node_id: NodeId, network_path: &[NodeId]) -> NodeId { + let mut layer = node_id; + let mut visited = HashSet::new(); + while visited.insert(layer) + && let Some(above) = self.layer_above(layer, network_path) + && let (Some(position), Some(above_position)) = (self.position(&layer, network_path), self.position(&above, network_path)) + && position.y - above_position.y <= STACK_VERTICAL_GAP + { + layer = above; + } + layer + } + /// The nodes a shift should move on their own, leaving out each one placed relative to another node being moved, /// which already carries it along. fn nodes_to_shift(&self, node_ids: HashSet, network_path: &[NodeId]) -> Vec { @@ -520,18 +545,23 @@ impl NodeNetworkInterface { fn stack_top(&self, node_id: NodeId, network_path: &[NodeId]) -> NodeId { let mut top = node_id; let mut visited = HashSet::new(); - while self.is_stack(&top, network_path) && visited.insert(top) { - let reader = self - .with_outward_wires(network_path, |outward_wires| { - outward_wires.get(&OutputConnector::primary_output(top)).and_then(|readers| readers.first()?.node_id()) - }) - .flatten(); - let Some(reader) = reader else { break }; - top = reader; + while visited.insert(top) + && let Some(above) = self.layer_above(top, network_path) + { + top = above; } top } + /// The layer `node_id` is stacked under, if it is stacked. + fn layer_above(&self, node_id: NodeId, network_path: &[NodeId]) -> Option { + if !self.is_stack(&node_id, network_path) { + return None; + } + self.with_outward_wires(network_path, |outward_wires| outward_wires.get(&OutputConnector::primary_output(node_id))?.first()?.node_id()) + .flatten() + } + /// Moves a node by `shift` from where it is drawn, carrying everything placed relative to it. pub fn shift_node(&mut self, node_id: &NodeId, shift: IVec2, network_path: &[NodeId]) { let Some(position) = self.position(node_id, network_path) else { 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 6d9be5b0e2..bd0f8d8de1 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 @@ -83,16 +83,16 @@ impl NodeNetworkInterface { .unwrap_or_default() } - /// Holds `node_ids` where the user moves them, so the layout draws their connections as stored until they are released. + /// Also holds `node_ids` where the user moves them, so the layout draws their connections as stored until they are released. /// Each node starting to be held, and each node it owns, is first stored where it is drawn, so holding moves nothing. pub fn hold_nodes(&mut self, node_ids: HashSet, network_path: &[NodeId]) { let Some(network_metadata) = self.network_metadata(network_path) else { return }; let held_nodes = &network_metadata.transient_metadata.held_nodes; - if *held_nodes == node_ids { + let newly_held: Vec = node_ids.difference(held_nodes).copied().collect(); + if newly_held.is_empty() { return; } - let newly_held: Vec = node_ids.difference(held_nodes).copied().collect(); let owned_by_newly_held: Vec = self .with_layout(network_path, |layout| { layout @@ -106,7 +106,7 @@ impl NodeNetworkInterface { let drawn_before = self.positions(newly_held.iter().chain(&owned_by_newly_held).copied(), network_path); let Some(transient) = self.network_transient_mut(network_path) else { return }; - transient.held_nodes = node_ids; + transient.held_nodes.extend(newly_held); transient.layout.unload(); // Each owner comes before the nodes it owns, so it is in place before they are stored from it @@ -118,7 +118,17 @@ impl NodeNetworkInterface { } pub fn release_held_nodes(&mut self, network_path: &[NodeId]) { - self.hold_nodes(HashSet::new(), network_path); + let Some(transient) = self.network_transient_mut(network_path) else { return }; + if !transient.held_nodes.is_empty() { + transient.held_nodes.clear(); + transient.layout.unload(); + } + } + + /// Whether the user is moving any nodes in the network. + pub(crate) fn holds_nodes(&self, network_path: &[NodeId]) -> bool { + self.network_metadata(network_path) + .is_some_and(|network_metadata| !network_metadata.transient_metadata.held_nodes.is_empty()) } fn lay_out(&self, network_path: &[NodeId], revision: u64) -> Option {