Skip to content

Commit 0691ac6

Browse files
committed
Improve code structuring
1 parent ce94e4e commit 0691ac6

4 files changed

Lines changed: 44 additions & 53 deletions

File tree

editor/src/messages/portfolio/document/graph_operation/graph_operation_message_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,14 @@ fn import_usvg_node(
474474

475475
// Enable import mode: skips expensive is_acyclic checks and per-node cache invalidation
476476
// during wiring since we're building a known tree structure where cycles are impossible
477-
modify_inputs.import_mode = true;
477+
modify_inputs.import = true;
478478

479479
for child in group.children() {
480480
let extent = import_usvg_node_inner(modify_inputs, child, transform, NodeId::new(), layer, 0, graphite_gradient_stops, &mut group_extents_map);
481481
child_extents_svg_order.push(extent);
482482
}
483483

484-
modify_inputs.import_mode = false;
484+
modify_inputs.import = false;
485485
modify_inputs.layer_node = Some(layer);
486486

487487
// Rebuild the layer tree once now that all wiring is complete

editor/src/messages/portfolio/document/graph_operation/utility_types.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ pub struct ModifyInputsContext<'a> {
3333
pub responses: &'a mut VecDeque<Message>,
3434
// Cannot be LayerNodeIdentifier::ROOT_PARENT
3535
pub layer_node: Option<LayerNodeIdentifier>,
36-
/// When true, chain node wiring uses the lightweight import path that skips expensive `is_acyclic` checks and per-node cache invalidation.
37-
pub import_mode: bool,
36+
/// When true, uses lightweight import paths that skip expensive checks during bulk import.
37+
pub import: bool,
3838
}
3939

4040
impl<'a> ModifyInputsContext<'a> {
@@ -44,7 +44,7 @@ impl<'a> ModifyInputsContext<'a> {
4444
network_interface,
4545
responses,
4646
layer_node: None,
47-
import_mode: false,
47+
import: false,
4848
}
4949
}
5050

@@ -145,15 +145,6 @@ impl<'a> ModifyInputsContext<'a> {
145145
LayerNodeIdentifier::new(new_id, self.network_interface)
146146
}
147147

148-
/// Dispatches to either the regular or import-optimized chain start function based on `import_mode`.
149-
fn chain_start(&mut self, node_id: &NodeId, layer: LayerNodeIdentifier) {
150-
if self.import_mode {
151-
self.network_interface.move_node_to_chain_start_for_import(node_id, layer, &[]);
152-
} else {
153-
self.network_interface.move_node_to_chain_start(node_id, layer, &[]);
154-
}
155-
}
156-
157148
pub fn insert_boolean_data(&mut self, operation: graphene_std::vector::misc::BooleanOperation, layer: LayerNodeIdentifier) {
158149
let boolean = resolve_network_node_type("Boolean Operation").expect("Boolean node does not exist").node_template_input_override([
159150
Some(NodeInput::value(TaggedValue::Graphic(Default::default()), true)),
@@ -162,7 +153,7 @@ impl<'a> ModifyInputsContext<'a> {
162153

163154
let boolean_id = NodeId::new();
164155
self.network_interface.insert_node(boolean_id, boolean, &[]);
165-
self.chain_start(&boolean_id, layer);
156+
self.network_interface.move_node_to_chain_start(&boolean_id, layer, &[], self.import);
166157
}
167158

168159
pub fn insert_vector(&mut self, subpaths: Vec<Subpath<PointId>>, layer: LayerNodeIdentifier, include_transform: bool, include_fill: bool, include_stroke: bool) {
@@ -173,13 +164,13 @@ impl<'a> ModifyInputsContext<'a> {
173164
.node_template_input_override([Some(NodeInput::value(TaggedValue::Vector(vector), false))]);
174165
let shape_id = NodeId::new();
175166
self.network_interface.insert_node(shape_id, shape, &[]);
176-
self.chain_start(&shape_id, layer);
167+
self.network_interface.move_node_to_chain_start(&shape_id, layer, &[], self.import);
177168

178169
if include_transform {
179170
let transform = resolve_network_node_type("Transform").expect("Transform node does not exist").default_node_template();
180171
let transform_id = NodeId::new();
181172
self.network_interface.insert_node(transform_id, transform, &[]);
182-
self.chain_start(&transform_id, layer);
173+
self.network_interface.move_node_to_chain_start(&transform_id, layer, &[], self.import);
183174
}
184175

185176
if include_stroke {
@@ -188,7 +179,7 @@ impl<'a> ModifyInputsContext<'a> {
188179
.default_node_template();
189180
let stroke_id = NodeId::new();
190181
self.network_interface.insert_node(stroke_id, stroke, &[]);
191-
self.chain_start(&stroke_id, layer);
182+
self.network_interface.move_node_to_chain_start(&stroke_id, layer, &[], self.import);
192183
}
193184

194185
if include_fill {
@@ -197,7 +188,7 @@ impl<'a> ModifyInputsContext<'a> {
197188
.default_node_template();
198189
let fill_id = NodeId::new();
199190
self.network_interface.insert_node(fill_id, fill, &[]);
200-
self.chain_start(&fill_id, layer);
191+
self.network_interface.move_node_to_chain_start(&fill_id, layer, &[], self.import);
201192
}
202193
}
203194

@@ -228,19 +219,19 @@ impl<'a> ModifyInputsContext<'a> {
228219

229220
let text_id = NodeId::new();
230221
self.network_interface.insert_node(text_id, text, &[]);
231-
self.chain_start(&text_id, layer);
222+
self.network_interface.move_node_to_chain_start(&text_id, layer, &[], self.import);
232223

233224
let transform_id = NodeId::new();
234225
self.network_interface.insert_node(transform_id, transform, &[]);
235-
self.chain_start(&transform_id, layer);
226+
self.network_interface.move_node_to_chain_start(&transform_id, layer, &[], self.import);
236227

237228
let stroke_id = NodeId::new();
238229
self.network_interface.insert_node(stroke_id, stroke, &[]);
239-
self.chain_start(&stroke_id, layer);
230+
self.network_interface.move_node_to_chain_start(&stroke_id, layer, &[], self.import);
240231

241232
let fill_id = NodeId::new();
242233
self.network_interface.insert_node(fill_id, fill, &[]);
243-
self.chain_start(&fill_id, layer);
234+
self.network_interface.move_node_to_chain_start(&fill_id, layer, &[], self.import);
244235
}
245236

246237
pub fn insert_image_data(&mut self, image_frame: Table<Raster<CPU>>, layer: LayerNodeIdentifier) {
@@ -251,11 +242,11 @@ impl<'a> ModifyInputsContext<'a> {
251242

252243
let image_id = NodeId::new();
253244
self.network_interface.insert_node(image_id, image, &[]);
254-
self.chain_start(&image_id, layer);
245+
self.network_interface.move_node_to_chain_start(&image_id, layer, &[], self.import);
255246

256247
let transform_id = NodeId::new();
257248
self.network_interface.insert_node(transform_id, transform, &[]);
258-
self.chain_start(&transform_id, layer);
249+
self.network_interface.move_node_to_chain_start(&transform_id, layer, &[], self.import);
259250
}
260251

261252
fn get_output_layer(&self) -> Option<LayerNodeIdentifier> {
@@ -340,12 +331,12 @@ impl<'a> ModifyInputsContext<'a> {
340331
};
341332
let node_id = NodeId::new();
342333
self.network_interface.insert_node(node_id, flatten_path_definition.default_node_template(), &[]);
343-
self.chain_start(&node_id, output_layer);
334+
self.network_interface.move_node_to_chain_start(&node_id, output_layer, &[], self.import);
344335
}
345336
}
346337
let node_id = NodeId::new();
347338
self.network_interface.insert_node(node_id, node_definition.default_node_template(), &[]);
348-
self.chain_start(&node_id, output_layer);
339+
self.network_interface.move_node_to_chain_start(&node_id, output_layer, &[], self.import);
349340
Some(node_id)
350341
}
351342

editor/src/messages/portfolio/document/node_graph/node_graph_message_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphMessageContext<'a>> for NodeG
717717
network_interface.move_layer_to_stack(layer, parent, insert_index, selection_network_path);
718718
}
719719
NodeGraphMessage::MoveNodeToChainStart { node_id, parent } => {
720-
network_interface.move_node_to_chain_start(&node_id, parent, selection_network_path);
720+
network_interface.move_node_to_chain_start(&node_id, parent, selection_network_path, false);
721721
}
722722
NodeGraphMessage::SetChainPosition { node_id } => {
723723
network_interface.set_chain_position(&node_id, selection_network_path);

editor/src/messages/portfolio/document/utility_types/network_interface.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5774,38 +5774,38 @@ impl NodeNetworkInterface {
57745774
self.create_wire(&upstream_output, &InputConnector::node(*node_id, insert_node_input_index), network_path);
57755775
}
57765776

5777-
// Moves a node and to the start of a layer chain (feeding into the secondary input of the layer)
5778-
pub fn move_node_to_chain_start(&mut self, node_id: &NodeId, parent: LayerNodeIdentifier, network_path: &[NodeId]) {
5779-
let Some(current_input) = self.input_from_connector(&InputConnector::node(parent.to_node(), 1), network_path) else {
5780-
log::error!("Could not get input for node {node_id}");
5781-
return;
5782-
};
5783-
if matches!(current_input, NodeInput::Value { .. }) {
5784-
self.create_wire(&OutputConnector::node(*node_id, 0), &InputConnector::node(parent.to_node(), 1), network_path);
5785-
self.set_chain_position(node_id, network_path);
5786-
} else {
5787-
// Insert the node in the gap and set the upstream to a chain
5788-
self.insert_node_between(node_id, &InputConnector::node(parent.to_node(), 1), 0, network_path);
5789-
self.force_set_upstream_to_chain(node_id, network_path);
5790-
}
5791-
}
5792-
5793-
/// Lightweight version of `move_node_to_chain_start` for bulk import. Uses `set_input_for_import`
5794-
/// to avoid the expensive `is_acyclic` check and other per-node side effects.
5795-
pub fn move_node_to_chain_start_for_import(&mut self, node_id: &NodeId, parent: LayerNodeIdentifier, network_path: &[NodeId]) {
5777+
/// Moves a node to the start of a layer chain (feeding into the secondary input of the layer).
5778+
/// When `import` is true, uses lightweight wiring that skips `is_acyclic` checks and per-node cache invalidation.
5779+
pub fn move_node_to_chain_start(&mut self, node_id: &NodeId, parent: LayerNodeIdentifier, network_path: &[NodeId], import: bool) {
57965780
let parent_input = InputConnector::node(parent.to_node(), 1);
57975781
let Some(current_input) = self.input_from_connector(&parent_input, network_path).cloned() else {
57985782
log::error!("Could not get input for node {node_id}");
57995783
return;
58005784
};
5801-
let node_output = NodeInput::node(*node_id, 0);
5785+
5786+
// Chain is empty: wire the node as the first (and only) entry in the chain
58025787
if matches!(current_input, NodeInput::Value { .. }) {
5803-
self.set_input_for_import(&parent_input, node_output, network_path);
5788+
// Wire: [parent] -> [new node]
5789+
if import {
5790+
self.set_input_for_import(&parent_input, NodeInput::node(*node_id, 0), network_path);
5791+
} else {
5792+
self.create_wire(&OutputConnector::node(*node_id, 0), &parent_input, network_path);
5793+
}
5794+
5795+
// Mark this lone node as chain-positioned
58045796
self.set_chain_position(node_id, network_path);
5805-
} else {
5806-
// Insert the node in the gap: wire node output to parent input, wire old upstream to node's input 0
5807-
self.set_input_for_import(&parent_input, node_output, network_path);
5808-
self.set_input_for_import(&InputConnector::node(*node_id, 0), current_input, network_path);
5797+
}
5798+
// Chain already has nodes: splice this node between the parent and the chain's existing final downstream node
5799+
else {
5800+
// Wire: [parent] -> [new node] -> [existing node]
5801+
if import {
5802+
self.set_input_for_import(&parent_input, NodeInput::node(*node_id, 0), network_path);
5803+
self.set_input_for_import(&InputConnector::node(*node_id, 0), current_input, network_path);
5804+
} else {
5805+
self.insert_node_between(node_id, &parent_input, 0, network_path);
5806+
}
5807+
5808+
// Ensure all upstream nodes from here are marked as chain-positioned
58095809
self.force_set_upstream_to_chain(node_id, network_path);
58105810
}
58115811
}

0 commit comments

Comments
 (0)