Merge pull request #36 from setzer22/feature/disconnect_on_delete

Send disconnect event on node delete
This commit is contained in:
setzer22 2022-06-08 10:01:32 +02:00 committed by GitHub
commit 316baf91c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 8 deletions

View File

@ -174,6 +174,10 @@ where
/* Handle responses from drawing nodes */
// Some responses generate additional responses when processed. These
// are stored here to report them back to the user.
let mut extra_responses: Vec<NodeResponse<UserResponse>> = Vec::new();
for response in delayed_responses.iter().copied() {
match response {
NodeResponse::ConnectEventStarted(node_id, port) => {
@ -205,7 +209,14 @@ where
self.selected_node = Some(node_id);
}
NodeResponse::DeleteNode(node_id) => {
self.graph.remove_node(node_id);
let removed = self.graph.remove_node(node_id);
extra_responses.extend(
removed
.into_iter()
.map(|x| x.0)
.into_iter()
.map(NodeResponse::DisconnectEvent),
);
self.node_positions.remove(node_id);
// Make sure to not leave references to old nodes hanging
if self.selected_node.map(|x| x == node_id).unwrap_or(false) {
@ -238,6 +249,11 @@ where
}
}
// Push any responses that were generated during response handling.
// These are only informative for the end-user and need no special
// treatment here.
delayed_responses.extend(extra_responses);
/* Mouse input handling */
// This locks the context, so don't hold on to it for too long.

View File

@ -63,18 +63,36 @@ impl<NodeData, DataType, ValueType> Graph<NodeData, DataType, ValueType> {
output_id
}
pub fn remove_node(&mut self, node_id: NodeId) {
self.connections
.retain(|i, o| !(self.outputs[*o].node == node_id || self.inputs[i].node == node_id));
let inputs: SVec<_> = self[node_id].input_ids().collect();
for input in inputs {
/// Removes a node from the graph with given `node_id`. This also removes
/// any incoming or outgoing connections from that node
///
/// This function returns the list of connections that has been removed
/// after deleting this node as input-output pairs. Note that one of the two
/// ids in the pair (the one on `node_id`'s end) will be invalid after
/// calling this function.
pub fn remove_node(&mut self, node_id: NodeId) -> Vec<(InputId, OutputId)> {
let mut disconnect_events = vec![];
self.connections.retain(|i, o| {
if self.outputs[*o].node == node_id || self.inputs[i].node == node_id {
disconnect_events.push((i, *o));
false
} else {
true
}
});
// NOTE: Collect is needed because we can't borrow the input ids while
// we remove them inside the loop.
for input in self[node_id].input_ids().collect::<SVec<_>>() {
self.inputs.remove(input);
}
let outputs: SVec<_> = self[node_id].output_ids().collect();
for output in outputs {
for output in self[node_id].output_ids().collect::<SVec<_>>() {
self.outputs.remove(output);
}
self.nodes.remove(node_id);
disconnect_events
}
pub fn remove_connection(&mut self, input_id: InputId) -> Option<OutputId> {