Skip to content

Commit f10b58a

Browse files
committed
Final code review pass
1 parent fa2cb39 commit f10b58a

File tree

13 files changed

+192
-182
lines changed

13 files changed

+192
-182
lines changed

editor/src/messages/portfolio/document/document_message_handler.rs

Lines changed: 77 additions & 73 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageData<'_>> for Gr
190190
insert_index,
191191
alias,
192192
} => {
193-
//trace!("Inserting new layer {id} as a child of {parent:?} at index {insert_index}");
194-
195193
let mut modify_inputs = ModifyInputsContext::new(document_network, document_metadata, node_graph, responses);
196194

197195
if let Some(layer) = modify_inputs.create_layer_with_insert_index(id, insert_index, parent) {
@@ -232,7 +230,7 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageData<'_>> for Gr
232230

233231
modify_inputs.responses.add(NodeGraphMessage::RunDocumentGraph);
234232
} else {
235-
error!("Create failed");
233+
error!("Creating new custom layer failed");
236234
}
237235

238236
load_network_structure(document_network, document_metadata, selected_nodes, collapsed);

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

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,8 @@ impl<'a> ModifyInputsContext<'a> {
135135
/// -> Layer3 input_index: 3
136136
/// if skip_layer_nodes == 3, return (Layer3, None, 0)
137137
pub fn get_post_node_with_index(network: &NodeNetwork, mut post_node_id: NodeId, insert_index: usize) -> (NodeId, Option<NodeId>, usize) {
138-
let mut post_node_input_index = 1; // Assume post node is a layer type.
139-
if post_node_id == NodeId(0) {
140-
post_node_input_index = 0; // Output node input index.
141-
}
138+
let mut post_node_input_index = if post_node_id == NodeId(0) { 0 } else { 1 };
139+
142140
// Skip layers based on skip_layer_nodes, which inserts the new layer at a certain index of the layer stack.
143141
let mut current_index = 0;
144142
loop {
@@ -159,13 +157,17 @@ impl<'a> ModifyInputsContext<'a> {
159157
if next_node_in_stack.is_layer {
160158
current_index += 1;
161159
}
160+
162161
post_node_id = next_node_in_stack_id;
163-
post_node_input_index = 0; //Input as a sibling to the Layer node above
162+
163+
// Input as a sibling to the Layer node above
164+
post_node_input_index = 0;
164165
} else {
165166
log::error!("Error creating layer: insert_index out of bounds");
166167
break;
167168
};
168169
}
170+
169171
// Move post_node to the end of the non layer chain that feeds into post_node, such that pre_node is the layer node at index 1 + insert_index
170172
let mut post_node = network.nodes.get(&post_node_id).expect("Post node should always exist");
171173
let mut pre_node_id = post_node
@@ -195,7 +197,7 @@ impl<'a> ModifyInputsContext<'a> {
195197
pub fn create_layer(&mut self, new_id: NodeId, output_node_id: NodeId, skip_layer_nodes: usize) -> Option<NodeId> {
196198
assert!(!self.document_network.nodes.contains_key(&new_id), "Creating already existing layer");
197199

198-
// Get the node which the new layer will output to (post node). First check if the output_node_id is the Output node, and set the output_node_id to the top most artboard,
200+
// Get the node which the new layer will output to (post node). First check if the output_node_id is the Output node, and set the output_node_id to the top-most artboard,
199201
// if there is one. Then skip layers based on skip_layer_nodes from the post_node.
200202
// TODO: Smarter placement of layers into artboards https://github.com/GraphiteEditor/Graphite/issues/1507
201203

@@ -210,25 +212,21 @@ impl<'a> ModifyInputsContext<'a> {
210212
}
211213
}
212214
let (post_node_id, pre_node_id, post_node_input_index) = Self::get_post_node_with_index(self.document_network, post_node_id, skip_layer_nodes);
215+
let new_layer_node = resolve_document_node_type("Merge").expect("Merge node").default_document_node();
213216
if let Some(pre_node_id) = pre_node_id {
214-
let new_layer_node = resolve_document_node_type("Merge").expect("Merge node").default_document_node();
215217
self.insert_between(
216218
new_id,
217219
new_layer_node,
218220
NodeInput::node(pre_node_id, 0),
219-
0, // Pre_node is a sibling so it connects to the first input.
221+
0, // pre_node is a sibling so it connects to the first input
220222
post_node_id,
221223
NodeInput::node(new_id, 0),
222224
post_node_input_index,
223225
IVec2::new(0, 3),
224226
);
225227
} else {
226-
let new_layer_node = resolve_document_node_type("Merge").expect("Merge node").default_document_node();
227-
if post_node_input_index == 1 {
228-
self.insert_node_before(new_id, post_node_id, post_node_input_index, new_layer_node, IVec2::new(-8, 3));
229-
} else {
230-
self.insert_node_before(new_id, post_node_id, post_node_input_index, new_layer_node, IVec2::new(0, 3));
231-
}
228+
let offset = if post_node_input_index == 1 { IVec2::new(-8, 3) } else { IVec2::new(0, 3) };
229+
self.insert_node_before(new_id, post_node_id, post_node_input_index, new_layer_node, offset);
232230
}
233231

234232
Some(new_id)
@@ -248,7 +246,6 @@ impl<'a> ModifyInputsContext<'a> {
248246
/// Creates an artboard that outputs to the output node.
249247
pub fn create_artboard(&mut self, new_id: NodeId, artboard: Artboard) -> Option<NodeId> {
250248
let output_node_id = self.document_network.original_outputs()[0].node_id;
251-
let mut shift = IVec2::new(0, 3);
252249

253250
let artboard_node = resolve_document_node_type("Artboard").expect("Node").to_document_node_default_inputs(
254251
[
@@ -277,18 +274,17 @@ impl<'a> ModifyInputsContext<'a> {
277274
output_node_id,
278275
NodeInput::node(new_id, 0),
279276
0,
280-
shift,
277+
IVec2::new(0, 3),
281278
)
282279
} else {
283-
shift = IVec2::new(-8, 3);
284-
self.insert_node_before(new_id, output_node_id, 0, artboard_node, shift)
280+
self.insert_node_before(new_id, output_node_id, 0, artboard_node, IVec2::new(-8, 3))
285281
};
286282

287283
if let Some(new_id) = created_node_id {
288284
let new_child = LayerNodeIdentifier::new_unchecked(new_id);
289285
LayerNodeIdentifier::ROOT.push_front_child(self.document_metadata, new_child);
290286
}
291-
//self.responses.add(NodeGraphMessage::RunDocumentGraph);
287+
292288
created_node_id
293289
}
294290
pub fn insert_vector_data(&mut self, subpaths: Vec<Subpath<ManipulatorGroupId>>, layer: NodeId) {
@@ -357,6 +353,7 @@ impl<'a> ModifyInputsContext<'a> {
357353
if shift_self {
358354
shift_nodes.insert(node_id);
359355
}
356+
360357
let mut stack = vec![node_id];
361358
while let Some(node_id) = stack.pop() {
362359
let Some(node) = self.document_network.nodes.get(&node_id) else { continue };
@@ -618,18 +615,20 @@ impl<'a> ModifyInputsContext<'a> {
618615

619616
pub fn resize_artboard(&mut self, location: IVec2, dimensions: IVec2) {
620617
self.modify_inputs("Artboard", false, |inputs, _node_id, _metadata| {
621-
let mut new_dimensions = dimensions;
622-
let mut new_location = location;
618+
let mut dimensions = dimensions;
619+
let mut location = location;
620+
623621
if dimensions.x < 0 {
624-
new_dimensions.x = -dimensions.x;
625-
new_location.x += dimensions.x;
622+
dimensions.x *= -1;
623+
location.x += dimensions.x;
626624
}
627625
if dimensions.y < 0 {
628-
new_dimensions.y = -dimensions.y;
629-
new_location.y += dimensions.y;
626+
dimensions.y *= -1;
627+
location.y += dimensions.y;
630628
}
631-
inputs[2] = NodeInput::value(TaggedValue::IVec2(new_location), false);
632-
inputs[3] = NodeInput::value(TaggedValue::IVec2(new_dimensions), false);
629+
630+
inputs[2] = NodeInput::value(TaggedValue::IVec2(location), false);
631+
inputs[3] = NodeInput::value(TaggedValue::IVec2(dimensions), false);
633632
});
634633
}
635634
}

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

Lines changed: 55 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,25 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
150150
continue;
151151
};
152152

153-
let node = document_network.nodes.get(&node_id).expect("node should always exist");
153+
let Some(node) = document_network.nodes.get(&node_id) else {
154+
continue;
155+
};
154156
let child_id = node.inputs.get(1).and_then(|input| if let NodeInput::Node { node_id, .. } = input { Some(node_id) } else { None });
155157
let Some(child_id) = child_id else {
156158
continue;
157159
};
158160

159161
let outward_links = document_network.collect_outwards_links();
160-
for (_node, upstream_id) in document_network.upstream_flow_back_from_nodes(vec![*child_id], graph_craft::document::FlowType::UpstreamFlow) {
162+
163+
for (_, upstream_id) in document_network.upstream_flow_back_from_nodes(vec![*child_id], graph_craft::document::FlowType::UpstreamFlow) {
161164
// TODO: move into a document_network function .is_sole_dependent. This function does a downstream traversal starting from the current node,
162-
// and only traverses for nodes that are not in the delete_nodes set. If all downstream nodes converge to some node in the delete_nodes set,
163-
// then it is a sole dependent. If the output node is eventually reached, then it is not a sole dependent. This means disconnected branches
164-
// that do not feed into the delete_nodes set or the output node will be deleted.
165+
// TODO: and only traverses for nodes that are not in the delete_nodes set. If all downstream nodes converge to some node in the delete_nodes set,
166+
// TODO: then it is a sole dependent. If the output node is eventually reached, then it is not a sole dependent. This means disconnected branches
167+
// TODO: that do not feed into the delete_nodes set or the output node will be deleted.
168+
165169
let mut stack = vec![upstream_id];
166170
let mut can_delete = true;
171+
167172
// TODO: Add iteration limit to force break in case of infinite while loop
168173
while let Some(current_node) = stack.pop() {
169174
if let Some(downstream_nodes) = outward_links.get(&current_node) {
@@ -176,7 +181,9 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
176181
// Continue traversing over the downstream sibling, which happens if the current node is a sibling to a node in node_ids
177182
else {
178183
for deleted_node_id in &node_ids {
179-
let output_node: &DocumentNode = document_network.nodes.get(&deleted_node_id).expect("node should always exist");
184+
let Some(output_node) = document_network.nodes.get(&deleted_node_id) else {
185+
continue;
186+
};
180187
let Some(input) = output_node.inputs.get(0) else {
181188
continue;
182189
};
@@ -191,26 +198,24 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
191198
}
192199
}
193200
}
201+
194202
if can_delete {
195203
delete_nodes.insert(upstream_id);
196204
}
197205
}
198206
}
207+
199208
for delete_node_id in delete_nodes {
200-
let delete_node = document_network.nodes.get(&delete_node_id).expect("node should always exist");
209+
let Some(delete_node) = document_network.nodes.get(&delete_node_id) else {
210+
continue;
211+
};
212+
201213
if delete_node.is_layer {
202214
// Delete node from document metadata
203215
let layer_node = LayerNodeIdentifier::new(delete_node_id, document_network);
204216
layer_node.delete(document_metadata);
205-
// Shift the position of the upstream sibling nodes for a deleted layer node. This ends up causing more positioning issues than it solves.
206-
// let shift = IVec2::new(0, -3);
207-
// let node_ids = document_network.upstream_flow_back_from_nodes(vec![delete_node_id], false).map(|(_, id)| id).collect::<Vec<_>>();
208-
209-
// for node_id in node_ids {
210-
// let Some(node) = document_network.nodes.get_mut(&node_id) else { continue };
211-
// node.metadata.position += shift;
212-
// }
213217
}
218+
214219
self.remove_node(document_network, selected_nodes, delete_node_id, responses, reconnect);
215220
}
216221

@@ -220,20 +225,18 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
220225
responses.add(NodeGraphMessage::RunDocumentGraph);
221226
}
222227
}
223-
// No need to call this since the metadata is already updated
224-
// load_network_structure(document_network, document_metadata, selected_nodes, collapsed);
225-
}
226228

227-
// Deletes selected_nodes. If reconnect is true, then all children nodes (secondary input) of the selected nodes are deleted and the siblings(primary input/output) are reconnected.
228-
// If reconnect is false, then only the selected nodes are deleted and not reconnected.
229+
// There is no need to call `load_network_structure()` since the metadata is already updated
230+
}
231+
// Deletes selected_nodes. If `reconnect` is true, then all children nodes (secondary input) of the selected nodes are deleted and the siblings (primary input/output) are reconnected.
232+
// If `reconnect` is false, then only the selected nodes are deleted and not reconnected.
229233
NodeGraphMessage::DeleteSelectedNodes { reconnect } => {
230234
responses.add(DocumentMessage::StartTransaction);
231235
responses.add(NodeGraphMessage::DeleteNodes {
232236
node_ids: selected_nodes.selected_nodes().copied().collect(),
233237
reconnect,
234238
});
235239
}
236-
237240
NodeGraphMessage::DisconnectNodes { node_id, input_index } => {
238241
let Some(network) = document_network.nested_network(&self.network) else {
239242
warn!("No network");
@@ -247,14 +250,16 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
247250
warn!("Node {} not in library", node.name);
248251
return;
249252
};
250-
251253
let Some((input_index, existing_input)) = node.inputs.iter().enumerate().filter(|(_, input)| input.is_exposed()).nth(input_index) else {
252254
return;
253255
};
256+
254257
let mut input = node_type.inputs[input_index].default.clone();
255258
if let NodeInput::Value { exposed, .. } = &mut input {
256259
*exposed = existing_input.is_exposed();
257260
}
261+
262+
responses.add(DocumentMessage::StartTransaction);
258263
responses.add(NodeGraphMessage::SetNodeInput { node_id, input_index, input });
259264

260265
if network.connected_to_output(node_id) {
@@ -343,7 +348,6 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
343348
}
344349

345350
responses.add(NodeGraphMessage::SetNodeInput { node_id, input_index, input });
346-
347351
responses.add(NodeGraphMessage::EnforceLayerHasNoMultiParams { node_id });
348352
responses.add(PropertiesPanelMessage::Refresh);
349353
responses.add(NodeGraphMessage::SendGraph);
@@ -382,6 +386,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
382386
error!("Failed to find input index {insert_node_input_index} on node {insert_node_id:#?}");
383387
return;
384388
};
389+
385390
responses.add(DocumentMessage::StartTransaction);
386391

387392
let post_input = NodeInput::node(insert_node_id, insert_node_output_index);
@@ -414,6 +419,7 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
414419
node.metadata.position += IVec2::new(displacement_x, displacement_y)
415420
}
416421
}
422+
417423
// Since document structure doesn't change, just update the nodes
418424
if graph_view_overlay_open {
419425
let links = Self::collect_links(network);
@@ -662,19 +668,20 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
662668
self.update_selection_action_buttons(document_network, document_metadata, selected_nodes, responses);
663669
}
664670
NodeGraphMessage::ToggleSelectedLayers => {
665-
if let Some(network) = document_network.nested_network_mut(&self.network) {
666-
for node_id in selected_nodes.selected_nodes() {
667-
if let Some(node) = network.nodes.get_mut(&node_id) {
668-
if node.has_primary_output {
669-
responses.add(NodeGraphMessage::SetToNodeOrLayer {
670-
node_id: *node_id,
671-
is_layer: !node.is_layer,
672-
});
673-
}
674-
if network.connected_to_output(*node_id) {
675-
responses.add(NodeGraphMessage::RunDocumentGraph);
676-
}
677-
}
671+
let Some(network) = document_network.nested_network_mut(&self.network) else { return };
672+
673+
for node_id in selected_nodes.selected_nodes() {
674+
let Some(node) = network.nodes.get_mut(&node_id) else { continue };
675+
676+
if node.has_primary_output {
677+
responses.add(NodeGraphMessage::SetToNodeOrLayer {
678+
node_id: *node_id,
679+
is_layer: !node.is_layer,
680+
});
681+
}
682+
683+
if network.connected_to_output(*node_id) {
684+
responses.add(NodeGraphMessage::RunDocumentGraph);
678685
}
679686
}
680687
}
@@ -683,13 +690,13 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
683690
return;
684691
}
685692

686-
if let Some(network) = document_network.nested_network_mut(&self.network) {
687-
if let Some(node) = network.nodes.get_mut(&node_id) {
688-
node.is_layer = is_layer;
689-
}
690-
responses.add(NodeGraphMessage::RunDocumentGraph);
691-
responses.add(DocumentMessage::DocumentStructureChanged);
693+
let Some(network) = document_network.nested_network_mut(&self.network) else { return };
694+
695+
if let Some(node) = network.nodes.get_mut(&node_id) {
696+
node.is_layer = is_layer;
692697
}
698+
responses.add(NodeGraphMessage::RunDocumentGraph);
699+
responses.add(DocumentMessage::DocumentStructureChanged);
693700
}
694701
NodeGraphMessage::SetName { node_id, name } => {
695702
responses.add(DocumentMessage::StartTransaction);
@@ -1136,27 +1143,17 @@ impl NodeGraphMessageHandler {
11361143
}
11371144

11381145
pub fn eligible_to_be_layer(&self, document_network: &NodeNetwork, node_id: NodeId) -> bool {
1139-
let Some(network) = document_network.nested_network(&self.network) else {
1140-
return false;
1141-
};
1142-
let Some(node) = network.nodes.get(&node_id) else {
1143-
return false;
1144-
};
1146+
let Some(network) = document_network.nested_network(&self.network) else { return false };
1147+
let Some(node) = network.nodes.get(&node_id) else { return false };
1148+
let Some(definition) = resolve_document_node_type(&node.name) else { return false };
11451149

1146-
let exposed_value_count = node
1147-
.inputs
1148-
.iter()
1149-
.filter(|input| if let NodeInput::Value { tagged_value: _, exposed } = input { *exposed } else { false })
1150-
.count();
1150+
let exposed_value_count = node.inputs.iter().filter(|input| if let NodeInput::Value { exposed, .. } = input { *exposed } else { false }).count();
11511151
let node_input_count = node.inputs.iter().filter(|input| if let NodeInput::Node { .. } = input { true } else { false }).count();
11521152
let input_count = node_input_count + exposed_value_count;
1153-
let Some(definition) = resolve_document_node_type(&node.name) else { return false };
11541153
let output_count = definition.outputs.len();
1154+
11551155
// TODO: Eventually allow nodes at the bottom of a stack to be layers, where `input_count` is 0
1156-
if !node.has_primary_output || output_count != 1 || input_count == 0 || input_count > 2 {
1157-
return false;
1158-
}
1159-
true
1156+
node.has_primary_output && output_count == 1 && (input_count == 1 || input_count == 2)
11601157
}
11611158

11621159
fn untitled_layer_label(node: &DocumentNode) -> String {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,9 +2422,8 @@ pub fn artboard_properties(document_node: &DocumentNode, node_id: NodeId, _conte
24222422
let location = vec2_widget(document_node, node_id, 2, "Location", "X", "Y", " px", None, add_blank_assist);
24232423
let dimensions = vec2_widget(document_node, node_id, 3, "Dimensions", "W", "H", " px", None, add_blank_assist);
24242424
let background = color_widget(document_node, node_id, 4, "Background", ColorButton::default().allow_none(false), true);
2425-
let clip = LayoutGroup::Row {
2426-
widgets: bool_widget(document_node, node_id, 5, "Clip", true),
2427-
};
2425+
let clip = bool_widget(document_node, node_id, 5, "Clip", true);
2426+
let clip = LayoutGroup::Row { widgets: clip };
24282427
vec![location, dimensions, background, clip]
24292428
}
24302429

0 commit comments

Comments
 (0)