mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-10-01 11:38:12 +08:00
Fix Document > Clear Artboards so it doesn't also clear everything else (#2177)
* Clear Artboards, Closes #2015 * Memory optmization * - Fix transactions - Fix node positions - Optimize nodes' deletion * no message * Fix layers shift to wrong positions * fix bugs * Code review --------- Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
co-authored by
Keavon Chambers
parent
a0f8f89e71
commit
618190d6e4
@@ -272,7 +272,6 @@ impl MessageHandler<DocumentMessage, DocumentMessageData<'_>> for DocumentMessag
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
DocumentMessage::ClearArtboards => {
|
DocumentMessage::ClearArtboards => {
|
||||||
responses.add(DocumentMessage::AddTransaction);
|
|
||||||
responses.add(GraphOperationMessage::ClearArtboards);
|
responses.add(GraphOperationMessage::ClearArtboards);
|
||||||
}
|
}
|
||||||
DocumentMessage::ClearLayersPanel => {
|
DocumentMessage::ClearLayersPanel => {
|
||||||
@@ -778,6 +777,7 @@ impl MessageHandler<DocumentMessage, DocumentMessageData<'_>> for DocumentMessag
|
|||||||
responses.add(NodeGraphMessage::SetDisplayName {
|
responses.add(NodeGraphMessage::SetDisplayName {
|
||||||
node_id: layer.to_node(),
|
node_id: layer.to_node(),
|
||||||
alias: name,
|
alias: name,
|
||||||
|
skip_adding_history_step: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if let Some((parent, insert_index)) = parent_and_insert_index {
|
if let Some((parent, insert_index)) = parent_and_insert_index {
|
||||||
@@ -822,6 +822,7 @@ impl MessageHandler<DocumentMessage, DocumentMessageData<'_>> for DocumentMessag
|
|||||||
responses.add(NodeGraphMessage::SetDisplayName {
|
responses.add(NodeGraphMessage::SetDisplayName {
|
||||||
node_id: layer.to_node(),
|
node_id: layer.to_node(),
|
||||||
alias: name,
|
alias: name,
|
||||||
|
skip_adding_history_step: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if let Some((parent, insert_index)) = parent_and_insert_index {
|
if let Some((parent, insert_index)) = parent_and_insert_index {
|
||||||
|
|||||||
+87
-5
@@ -1,5 +1,6 @@
|
|||||||
use super::transform_utils;
|
use super::transform_utils;
|
||||||
use super::utility_types::ModifyInputsContext;
|
use super::utility_types::ModifyInputsContext;
|
||||||
|
use crate::messages::portfolio::document::graph_operation::utility_types::TransformIn;
|
||||||
use crate::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier;
|
use crate::messages::portfolio::document::utility_types::document_metadata::LayerNodeIdentifier;
|
||||||
use crate::messages::portfolio::document::utility_types::network_interface::{InputConnector, NodeNetworkInterface, OutputConnector};
|
use crate::messages::portfolio::document::utility_types::network_interface::{InputConnector, NodeNetworkInterface, OutputConnector};
|
||||||
use crate::messages::portfolio::document::utility_types::nodes::CollapsedLayers;
|
use crate::messages::portfolio::document::utility_types::nodes::CollapsedLayers;
|
||||||
@@ -14,6 +15,13 @@ use graphene_std::vector::convert_usvg_path;
|
|||||||
|
|
||||||
use glam::{DAffine2, DVec2};
|
use glam::{DAffine2, DVec2};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct ArtboardInfo {
|
||||||
|
input_node: NodeInput,
|
||||||
|
output_nodes: Vec<InputConnector>,
|
||||||
|
merge_node: NodeId,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct GraphOperationMessageData<'a> {
|
pub struct GraphOperationMessageData<'a> {
|
||||||
pub network_interface: &'a mut NodeNetworkInterface,
|
pub network_interface: &'a mut NodeNetworkInterface,
|
||||||
pub collapsed: &'a mut CollapsedLayers,
|
pub collapsed: &'a mut CollapsedLayers,
|
||||||
@@ -108,7 +116,7 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageData<'_>> for Gr
|
|||||||
network_interface.move_layer_to_stack(artboard_layer, LayerNodeIdentifier::ROOT_PARENT, 0, &[]);
|
network_interface.move_layer_to_stack(artboard_layer, LayerNodeIdentifier::ROOT_PARENT, 0, &[]);
|
||||||
|
|
||||||
// If there is a non artboard feeding into the primary input of the artboard, move it to the secondary input
|
// If there is a non artboard feeding into the primary input of the artboard, move it to the secondary input
|
||||||
let Some(artboard) = network_interface.network(&[]).unwrap().nodes.get(&id) else {
|
let Some(artboard) = network_interface.network(&[]).and_then(|network| network.nodes.get(&id)) else {
|
||||||
log::error!("Artboard not created");
|
log::error!("Artboard not created");
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@@ -191,13 +199,87 @@ impl MessageHandler<GraphOperationMessage, GraphOperationMessageData<'_>> for Gr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
GraphOperationMessage::ClearArtboards => {
|
GraphOperationMessage::ClearArtboards => {
|
||||||
|
if network_interface.all_artboards().is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
responses.add(DocumentMessage::AddTransaction);
|
||||||
|
responses.add(NodeGraphMessage::DeleteNodes {
|
||||||
|
node_ids: network_interface.all_artboards().iter().map(|layer_node| layer_node.to_node()).collect(),
|
||||||
|
delete_children: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut artboard_data: HashMap<NodeId, ArtboardInfo> = HashMap::new();
|
||||||
|
|
||||||
|
// Go through all artboards and create merge nodes
|
||||||
for artboard in network_interface.all_artboards() {
|
for artboard in network_interface.all_artboards() {
|
||||||
responses.add(NodeGraphMessage::DeleteNodes {
|
let node_id = NodeId::new();
|
||||||
node_ids: vec![artboard.to_node()],
|
let Some(document_node) = network_interface.network(&[]).and_then(|network| network.nodes.get(&artboard.to_node())) else {
|
||||||
delete_children: false,
|
log::error!("Artboard not created");
|
||||||
|
responses.add(DocumentMessage::AbortTransaction);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
artboard_data.insert(
|
||||||
|
artboard.to_node(),
|
||||||
|
ArtboardInfo {
|
||||||
|
input_node: NodeInput::node(document_node.inputs[1].as_node().unwrap_or_default(), 0),
|
||||||
|
output_nodes: network_interface
|
||||||
|
.outward_wires(&[])
|
||||||
|
.and_then(|outward_wires| outward_wires.get(&OutputConnector::node(artboard.to_node(), 0)))
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_default(),
|
||||||
|
merge_node: node_id,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut modify_inputs = ModifyInputsContext::new(network_interface, responses);
|
||||||
|
modify_inputs.create_layer(node_id);
|
||||||
|
|
||||||
|
responses.add(NodeGraphMessage::SetDisplayName {
|
||||||
|
node_id,
|
||||||
|
alias: network_interface.frontend_display_name(&artboard.to_node(), &[]),
|
||||||
|
skip_adding_history_step: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Shift node positions in the graph
|
||||||
|
let (x, y) = network_interface.position(&artboard.to_node(), &[]).unwrap_or_default().into();
|
||||||
|
responses.add(NodeGraphMessage::ShiftNodePosition { node_id, x, y });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Go through all artboards and connect them to the merge nodes
|
||||||
|
for artboard in &artboard_data {
|
||||||
|
// Modify downstream connections
|
||||||
|
responses.add(NodeGraphMessage::SetInput {
|
||||||
|
input_connector: InputConnector::node(artboard.1.merge_node, 1),
|
||||||
|
input: NodeInput::node(artboard.1.input_node.as_node().unwrap_or_default(), 0),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Modify upstream connections
|
||||||
|
for outward_wire in &artboard.1.output_nodes {
|
||||||
|
let input = NodeInput::node(artboard_data[artboard.0].merge_node, 0);
|
||||||
|
let input_connector = if let Some(artboard_info) = artboard_data.get(&outward_wire.node_id().unwrap_or_default()) {
|
||||||
|
InputConnector::node(artboard_info.merge_node, outward_wire.input_index())
|
||||||
|
} else {
|
||||||
|
*outward_wire
|
||||||
|
};
|
||||||
|
responses.add(NodeGraphMessage::SetInput { input_connector, input });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reposition merge nodes
|
||||||
|
let offset = network_interface
|
||||||
|
.document_metadata()
|
||||||
|
.bounding_box_document(LayerNodeIdentifier::new_unchecked(*artboard.0))
|
||||||
|
.map(|p| p[0])
|
||||||
|
.unwrap_or_default();
|
||||||
|
responses.add(GraphOperationMessage::TransformChange {
|
||||||
|
layer: LayerNodeIdentifier::new_unchecked(artboard.1.merge_node),
|
||||||
|
transform: DAffine2::from_translation(offset),
|
||||||
|
transform_in: TransformIn::Local,
|
||||||
|
skip_rerender: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// TODO: Replace deleted artboards with merge nodes
|
|
||||||
responses.add(NodeGraphMessage::RunDocumentGraph);
|
responses.add(NodeGraphMessage::RunDocumentGraph);
|
||||||
responses.add(NodeGraphMessage::SelectedNodesUpdated);
|
responses.add(NodeGraphMessage::SelectedNodesUpdated);
|
||||||
responses.add(NodeGraphMessage::SendGraph);
|
responses.add(NodeGraphMessage::SendGraph);
|
||||||
|
|||||||
@@ -138,6 +138,7 @@ pub enum NodeGraphMessage {
|
|||||||
SetDisplayName {
|
SetDisplayName {
|
||||||
node_id: NodeId,
|
node_id: NodeId,
|
||||||
alias: String,
|
alias: String,
|
||||||
|
skip_adding_history_step: bool,
|
||||||
},
|
},
|
||||||
SetDisplayNameImpl {
|
SetDisplayNameImpl {
|
||||||
node_id: NodeId,
|
node_id: NodeId,
|
||||||
|
|||||||
@@ -1450,11 +1450,19 @@ impl<'a> MessageHandler<NodeGraphMessage, NodeGraphHandlerData<'a>> for NodeGrap
|
|||||||
responses.add(NodeGraphMessage::RunDocumentGraph);
|
responses.add(NodeGraphMessage::RunDocumentGraph);
|
||||||
responses.add(NodeGraphMessage::SendGraph);
|
responses.add(NodeGraphMessage::SendGraph);
|
||||||
}
|
}
|
||||||
NodeGraphMessage::SetDisplayName { node_id, alias } => {
|
NodeGraphMessage::SetDisplayName {
|
||||||
responses.add(DocumentMessage::StartTransaction);
|
node_id,
|
||||||
|
alias,
|
||||||
|
skip_adding_history_step,
|
||||||
|
} => {
|
||||||
|
if !skip_adding_history_step {
|
||||||
|
responses.add(DocumentMessage::StartTransaction);
|
||||||
|
}
|
||||||
responses.add(NodeGraphMessage::SetDisplayNameImpl { node_id, alias });
|
responses.add(NodeGraphMessage::SetDisplayNameImpl { node_id, alias });
|
||||||
// Does not add a history step if the name was not changed
|
if !skip_adding_history_step {
|
||||||
responses.add(DocumentMessage::EndTransaction);
|
// Does not add a history step if the name was not changed
|
||||||
|
responses.add(DocumentMessage::EndTransaction);
|
||||||
|
}
|
||||||
responses.add(DocumentMessage::RenderRulers);
|
responses.add(DocumentMessage::RenderRulers);
|
||||||
responses.add(DocumentMessage::RenderScrollbars);
|
responses.add(DocumentMessage::RenderScrollbars);
|
||||||
responses.add(NodeGraphMessage::SendGraph);
|
responses.add(NodeGraphMessage::SendGraph);
|
||||||
@@ -2023,6 +2031,7 @@ impl NodeGraphMessageHandler {
|
|||||||
NodeGraphMessage::SetDisplayName {
|
NodeGraphMessage::SetDisplayName {
|
||||||
node_id: layer,
|
node_id: layer,
|
||||||
alias: text_input.value.clone(),
|
alias: text_input.value.clone(),
|
||||||
|
skip_adding_history_step: false,
|
||||||
}
|
}
|
||||||
.into()
|
.into()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -552,6 +552,7 @@ impl EditorHandle {
|
|||||||
let message = NodeGraphMessage::SetDisplayName {
|
let message = NodeGraphMessage::SetDisplayName {
|
||||||
node_id: layer.to_node(),
|
node_id: layer.to_node(),
|
||||||
alias: name,
|
alias: name,
|
||||||
|
skip_adding_history_step: false,
|
||||||
};
|
};
|
||||||
self.dispatch(message);
|
self.dispatch(message);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user