diff --git a/demo-artwork/changing-seasons.graphite b/demo-artwork/changing-seasons.graphite index a51f2d0eb8c..81e4745d3bc 100644 Binary files a/demo-artwork/changing-seasons.graphite and b/demo-artwork/changing-seasons.graphite differ diff --git a/demo-artwork/isometric-fountain.graphite b/demo-artwork/isometric-fountain.graphite index b2c770d0088..e78da9dfd79 100644 Binary files a/demo-artwork/isometric-fountain.graphite and b/demo-artwork/isometric-fountain.graphite differ diff --git a/demo-artwork/marbled-mandelbrot.graphite b/demo-artwork/marbled-mandelbrot.graphite index 382f81864fc..49df28f3884 100644 Binary files a/demo-artwork/marbled-mandelbrot.graphite and b/demo-artwork/marbled-mandelbrot.graphite differ diff --git a/demo-artwork/painted-dreams.graphite b/demo-artwork/painted-dreams.graphite index 65977039cf2..22b143b23f3 100644 Binary files a/demo-artwork/painted-dreams.graphite and b/demo-artwork/painted-dreams.graphite differ diff --git a/demo-artwork/parametric-dunescape.graphite b/demo-artwork/parametric-dunescape.graphite index 552891dcb80..cbba6e847c6 100644 Binary files a/demo-artwork/parametric-dunescape.graphite and b/demo-artwork/parametric-dunescape.graphite differ diff --git a/demo-artwork/procedural-string-lights.graphite b/demo-artwork/procedural-string-lights.graphite index 2ea1886d383..5c2797791b9 100644 Binary files a/demo-artwork/procedural-string-lights.graphite and b/demo-artwork/procedural-string-lights.graphite differ diff --git a/demo-artwork/red-dress.graphite b/demo-artwork/red-dress.graphite index 2240519d3d2..22c1e261221 100644 Binary files a/demo-artwork/red-dress.graphite and b/demo-artwork/red-dress.graphite differ diff --git a/demo-artwork/valley-of-spires.graphite b/demo-artwork/valley-of-spires.graphite index c57b7f57be1..53bd9620bd0 100644 Binary files a/demo-artwork/valley-of-spires.graphite and b/demo-artwork/valley-of-spires.graphite differ diff --git a/document/format/src/layers.rs b/document/format/src/layers.rs index 152383b5d2a..df7f6632388 100644 --- a/document/format/src/layers.rs +++ b/document/format/src/layers.rs @@ -55,9 +55,6 @@ mod tests { display_name: None, locked: false, pinned: false, - input_metadata: Vec::new(), - output_names: Vec::new(), - output_descriptions: Vec::new(), } } diff --git a/document/graph-storage/src/attributes/attr.rs b/document/graph-storage/src/attributes/attr.rs index 4fd1055cebd..58b6ef4e6f7 100644 --- a/document/graph-storage/src/attributes/attr.rs +++ b/document/graph-storage/src/attributes/attr.rs @@ -5,14 +5,6 @@ pub mod node { pub mod input { pub const IMPORT_TYPE: &str = "import_type"; - - pub mod ui { - pub const NAME: &str = "ui::name"; - pub const DESCRIPTION: &str = "ui::description"; - pub const WIDGET_OVERRIDE: &str = "ui::widget_override"; - /// Prefix for `InputPersistentMetadata::data` entries. Full key: `ui::data::`. - pub const DATA_PREFIX: &str = "ui::data::"; // TODO: Remove and make runtime strongly typed again - } } pub mod ui { @@ -21,8 +13,6 @@ pub mod node { pub const DISPLAY_NAME: &str = "ui::display_name"; pub const LOCKED: &str = "ui::locked"; pub const PINNED: &str = "ui::pinned"; - pub const OUTPUT_NAMES: &str = "ui::output_names"; - pub const OUTPUT_DESCRIPTIONS: &str = "ui::output_descriptions"; /// The built-in network definition a network was created from, stored on the network's attributes. See [`NetworkReference`](crate::NetworkReference). pub const REFERENCE: &str = "ui::reference"; } diff --git a/document/graph-storage/src/from_runtime.rs b/document/graph-storage/src/from_runtime.rs index 35bcd4a4fe0..8b6d17f02c8 100644 --- a/document/graph-storage/src/from_runtime.rs +++ b/document/graph-storage/src/from_runtime.rs @@ -360,10 +360,9 @@ fn convert_node( let timestamp = TimeStamp::ORIGIN; let mut inputs = Vec::with_capacity(doc_node.inputs.len()); - for (input_index, input) in doc_node.inputs.iter().enumerate() { + for input in &doc_node.inputs { let mut input_attrs = convert_input_attributes(input)?; input_attrs.set_floor(timestamp); - write_ui_input_attributes(&mut input_attrs, ctx.metadata, network, runtime_node_id, input_index, timestamp)?; inputs.push(InputSlot { input: convert_input(input)?, @@ -420,18 +419,6 @@ pub fn encode_node_ui_attributes( write_ui_attributes(attributes, metadata, network, runtime_node_id, timestamp) } -/// Public form of the per-input ui-attribute encoding, matching `encode_node_ui_attributes`. -pub fn encode_input_ui_attributes( - attributes: &mut crate::Attributes, - metadata: &M, - network: NetworkLocation, - runtime_node_id: RuntimeNodeId, - input_index: usize, - timestamp: TimeStamp, -) -> Result<(), ConversionError> { - write_ui_input_attributes(attributes, metadata, network, runtime_node_id, input_index, timestamp) -} - fn write_ui_attributes( attributes: &mut crate::Attributes, metadata: &M, @@ -462,21 +449,6 @@ fn write_ui_attributes( attributes.set(node::ui::DISPLAY_NAME, Value::Str(name.to_string()), timestamp); } - // One whole-vec attribute; per-slot LWW would be overkill for rename-on-output. - let output_names = metadata.output_names(network, runtime_node_id); - if !output_names.is_empty() { - attributes - .set_serialized(node::ui::OUTPUT_NAMES, &output_names, timestamp) - .map_err(map_serialization_error(node::ui::OUTPUT_NAMES))?; - } - - let output_descriptions = metadata.output_descriptions(network, runtime_node_id); - if output_descriptions.iter().any(|description| !description.is_empty()) { - attributes - .set_serialized(node::ui::OUTPUT_DESCRIPTIONS, &output_descriptions, timestamp) - .map_err(map_serialization_error(node::ui::OUTPUT_DESCRIPTIONS))?; - } - Ok(()) } @@ -531,33 +503,6 @@ fn write_network_interface_attributes(attributes Ok(()) } -/// Empty strings (the runtime's "unset" sentinel) and absent values are both skipped. -/// `input_data` entries each get their own `ui::input_data::` attribute for per-key LWW. -fn write_ui_input_attributes( - attributes: &mut crate::Attributes, - metadata: &M, - network: NetworkLocation, - runtime_node_id: RuntimeNodeId, - input_index: usize, - timestamp: TimeStamp, -) -> Result<(), ConversionError> { - let non_empty_string = |key: &'static str, value: Option<&str>, attributes: &mut crate::Attributes| { - if let Some(value) = value.filter(|s| !s.is_empty()) { - attributes.set(key, Value::Str(value.to_string()), timestamp); - } - }; - - non_empty_string(node::input::ui::NAME, metadata.input_name(network, runtime_node_id, input_index), attributes); - non_empty_string(node::input::ui::DESCRIPTION, metadata.input_description(network, runtime_node_id, input_index), attributes); - non_empty_string(node::input::ui::WIDGET_OVERRIDE, metadata.widget_override(network, runtime_node_id, input_index), attributes); - - for (sub_key, value) in metadata.input_data(network, runtime_node_id, input_index) { - attributes.set(&format!("{prefix}{sub_key}", prefix = node::input::ui::DATA_PREFIX), value, timestamp); - } - - Ok(()) -} - /// Converts one runtime input to storage form. pub fn convert_input(input: &GraphCraftNodeInput) -> Result { Ok(match input { diff --git a/document/graph-storage/src/lib.rs b/document/graph-storage/src/lib.rs index 2e2a05b44ff..c10cbf9a345 100644 --- a/document/graph-storage/src/lib.rs +++ b/document/graph-storage/src/lib.rs @@ -40,8 +40,8 @@ pub use value::{ValueError, from_value, to_value}; #[cfg(any(feature = "conversion", test))] pub use from_runtime::{ - EncodedDeclaration, RuntimeConversion, ScopedConversion, convert_input, convert_input_attributes, convert_resource_entry, decode_declaration, encode_declaration, encode_input_ui_attributes, - encode_node_ui_attributes, node_value_resource_refs, storage_location_id, storage_node_id, value_resource_ref, + EncodedDeclaration, RuntimeConversion, ScopedConversion, convert_input, convert_input_attributes, convert_resource_entry, decode_declaration, encode_declaration, encode_node_ui_attributes, + node_value_resource_refs, storage_location_id, storage_node_id, value_resource_ref, }; #[cfg(any(feature = "conversion", test))] pub use metadata_source::{InputMetadataEntry, NetworkMetadataEntry, NoMetadata, NodeMetadataEntry, NodeMetadataSource, Position}; diff --git a/document/graph-storage/src/metadata_source.rs b/document/graph-storage/src/metadata_source.rs index 0d2fea05604..dffd1d9a4bd 100644 --- a/document/graph-storage/src/metadata_source.rs +++ b/document/graph-storage/src/metadata_source.rs @@ -3,7 +3,7 @@ //! //! `network` is the location of the network holding the queried node, or of the queried network itself. -use std::collections::{BTreeMap, HashMap}; +use std::collections::BTreeMap; use core_types::uuid::NodeId as RuntimeNodeId; use graph_craft::document::NetworkLocation; @@ -21,11 +21,6 @@ pub struct NodeMetadataEntry { pub display_name: Option, pub locked: bool, pub pinned: bool, - /// Always sized to match the runtime node's `inputs.len()`; absent slots use `Default`. The rebuild - /// returns an error if this length does not match the node's input count. - pub input_metadata: Vec, - pub output_names: Vec, - pub output_descriptions: Vec, } /// Per-network metadata. Separate from `NodeMetadataEntry` since these are @@ -45,14 +40,13 @@ pub struct NetworkMetadataEntry { pub export_descriptions: Vec, } -/// Per-input editor metadata. Mirrors `InputPersistentMetadata` but wraps strings in `Option` so +/// Editor metadata for one import of a subgraph. Mirrors `InputPersistentMetadata` but wraps strings in `Option` so /// unset (`""` on the runtime side) is distinguishable from an explicit empty string. #[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)] pub struct InputMetadataEntry { pub input_name: Option, pub input_description: Option, pub widget_override: Option, - /// Reassembled from `ui::input_data::` attributes. pub input_data: BTreeMap, } @@ -74,29 +68,6 @@ pub trait NodeMetadataSource { fn pinned(&self, _network: NetworkLocation, _local_id: RuntimeNodeId) -> bool { false } - /// Empty vec = no overrides. Stored as a single `ui::output_names` attribute (whole-vec LWW). - fn output_names(&self, _network: NetworkLocation, _local_id: RuntimeNodeId) -> Vec { - Vec::new() - } - /// Indexed like `output_names`, where a missing or empty entry is no description. Stored as one `ui::output_descriptions` attribute. - fn output_descriptions(&self, _network: NetworkLocation, _local_id: RuntimeNodeId) -> Vec { - Vec::new() - } - - fn input_name(&self, _network: NetworkLocation, _local_id: RuntimeNodeId, _input_index: usize) -> Option<&str> { - None - } - fn input_description(&self, _network: NetworkLocation, _local_id: RuntimeNodeId, _input_index: usize) -> Option<&str> { - None - } - fn widget_override(&self, _network: NetworkLocation, _local_id: RuntimeNodeId, _input_index: usize) -> Option<&str> { - None - } - /// Returns owned to stay object-safe. Each entry is stored as `ui::input_data::` for per-key LWW. - fn input_data(&self, _network: NetworkLocation, _local_id: RuntimeNodeId, _input_index: usize) -> HashMap { - HashMap::new() - } - /// The definition the network was instantiated from, if it still names one. fn reference(&self, _network: NetworkLocation) -> Option { None diff --git a/document/graph-storage/src/to_runtime.rs b/document/graph-storage/src/to_runtime.rs index 7d1124a5dd0..fcdcf889c6e 100644 --- a/document/graph-storage/src/to_runtime.rs +++ b/document/graph-storage/src/to_runtime.rs @@ -1,5 +1,5 @@ use std::borrow::Cow; -use std::collections::{BTreeMap, HashMap}; +use std::collections::HashMap; use core_types::memo::MemoHash; use core_types::uuid::NodeId as RuntimeNodeId; @@ -10,7 +10,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use crate::attr::*; use crate::blob::{BlobBytes, decode_blob}; -use crate::metadata_source::{InputMetadataEntry, NetworkMetadataEntry, NodeMetadataEntry}; +use crate::metadata_source::{NetworkMetadataEntry, NodeMetadataEntry}; use crate::{BlobHash, Implementation, NetworkId, NetworkReference, Node, NodeId, NodeInput, Position, ProtoNode, ROOT_NETWORK, Registry, ResourceId, Value, from_value}; #[derive(Debug, thiserror::Error)] @@ -376,18 +376,12 @@ fn read_scope_injections(registry: &Registry, network_id: NetworkId, attributes: } /// Emitted for every node, even one without any `ui::*` attribute. -/// `input_metadata` is always sized to match `node.inputs.len()` for a strict slot-by-slot rebuild; -/// empty slots use `InputMetadataEntry::default()`. fn extract_ui_metadata(node: &crate::Node, network: NetworkLocation, local_id: RuntimeNodeId) -> NodeMetadataEntry { let position: Option = node.attributes.get_typed(node::ui::POSITION); let is_layer = node.attributes.get_or(node::ui::IS_LAYER, false); let display_name: Option = node.attributes.get_typed(node::ui::DISPLAY_NAME); let locked = node.attributes.get_or(node::ui::LOCKED, false); let pinned = node.attributes.get_or(node::ui::PINNED, false); - let output_names: Vec = node.attributes.get_or_default(node::ui::OUTPUT_NAMES); - let output_descriptions: Vec = node.attributes.get_or_default(node::ui::OUTPUT_DESCRIPTIONS); - - let input_metadata: Vec = node.inputs.iter().map(|slot| &slot.attributes).map(extract_input_metadata).collect(); NodeMetadataEntry { network, @@ -397,9 +391,6 @@ fn extract_ui_metadata(node: &crate::Node, network: NetworkLocation, local_id: R display_name, locked, pinned, - input_metadata, - output_names, - output_descriptions, } } @@ -429,21 +420,6 @@ fn extract_network_metadata(registry: &Registry, attributes: &crate::Attributes, } } -/// Reassembles `input_data` by scanning every attribute under `ui::input_data::` and stripping the prefix. -fn extract_input_metadata(attributes: &crate::Attributes) -> InputMetadataEntry { - let input_data: BTreeMap = attributes - .live() - .filter_map(|(key, value)| key.strip_prefix(node::input::ui::DATA_PREFIX).map(|sub_key| (sub_key.to_owned(), value.value.clone()))) - .collect(); - - InputMetadataEntry { - input_name: attributes.get_typed(node::input::ui::NAME), - input_description: attributes.get_typed(node::input::ui::DESCRIPTION), - widget_override: attributes.get_typed(node::input::ui::WIDGET_OVERRIDE), - input_data, - } -} - fn convert_node(context: &ConversionContext, node: &crate::Node) -> Result { let inputs = node .inputs diff --git a/editor/src/messages/portfolio/document/document_history.rs b/editor/src/messages/portfolio/document/document_history.rs index e4f41eb78cc..dbd35242c96 100644 --- a/editor/src/messages/portfolio/document/document_history.rs +++ b/editor/src/messages/portfolio/document/document_history.rs @@ -311,7 +311,7 @@ impl DocumentHistory { .registry() .to_runtime_with_full_metadata(&self.declarations, storage.blobs()) .map_err(|error| error.to_string()) - .and_then(|(network, node_entries, network_entries)| build_interface_from_storage(network, node_entries, network_entries).map_err(|error| error.to_string())) + .map(|(network, node_entries, network_entries)| build_interface_from_storage(network, node_entries, network_entries)) .inspect_err(|error| log::error!("Storage interface rebuild failed: {error}")) .ok() } diff --git a/editor/src/messages/portfolio/document/document_message_handler.rs b/editor/src/messages/portfolio/document/document_message_handler.rs index b5babf92b71..2e3bf15b05f 100644 --- a/editor/src/messages/portfolio/document/document_message_handler.rs +++ b/editor/src/messages/portfolio/document/document_message_handler.rs @@ -2780,8 +2780,6 @@ impl DocumentMessageHandler { document_message_handler.snapping_state.grid_visible = document_message_handler.snapping_state.grid_snapping; } - document_message_handler.network_interface.move_interface_metadata_into_networks(); - Ok(document_message_handler) } diff --git a/editor/src/messages/portfolio/document/node_graph/document_node_definitions.rs b/editor/src/messages/portfolio/document/node_graph/document_node_definitions.rs index f6807243588..02c3037f1d9 100644 --- a/editor/src/messages/portfolio/document/node_graph/document_node_definitions.rs +++ b/editor/src/messages/portfolio/document/node_graph/document_node_definitions.rs @@ -40,22 +40,7 @@ pub struct NodeInspectorContext<'a> { impl NodeInspectorContext<'_> { pub fn call_widget_override(&mut self, node_id: &NodeId, index: usize) -> Option> { let input_inspector_row = self.network_interface.persistent_input_metadata(node_id, index, self.selection_network_path)?; - - // The stored override wins, falling back to the definition's own so documents saved before it gained one still - // pick it up. Only `Custom` is recovered, since the other kinds also need input data stored at insertion time. - let widget_override = input_inspector_row.widget_override.clone().or_else(|| { - let implementation = self.network_interface.implementation(node_id, self.selection_network_path)?; - let DocumentNodeImplementation::ProtoNode(proto_node_identifier) = implementation else { - return None; - }; - - let metadata = registry::NODE_METADATA.lock().unwrap(); - let field = metadata.get(proto_node_identifier)?.fields.get(index)?; - match field.widget_override { - registry::RegistryWidgetOverride::Custom(name) => Some(name.to_string()), - _ => None, - } - })?; + let widget_override = input_inspector_row.widget_override.clone()?; let Some(widget_override_lambda) = INPUT_OVERRIDES.get(&widget_override) else { log::error!("Could not get widget override '{widget_override}' lambda in call_widget_override"); @@ -1385,7 +1370,23 @@ pub fn resolve_network_node_type(identifier: &str) -> Option<&'static DocumentNo } pub fn resolve_proto_node_type(identifier: ProtoNodeIdentifier) -> Option<&'static DocumentNodeDefinition> { - resolve_document_node_type(&DefinitionIdentifier::ProtoNode(identifier)) + resolve_proto_node_definition(&identifier) +} + +/// The proto node definitions keyed by their identifier, so a lookup can borrow it. +static PROTO_NODE_DEFINITIONS: once_cell::sync::Lazy> = once_cell::sync::Lazy::new(|| { + DOCUMENT_NODE_TYPES + .iter() + .filter_map(|(identifier, definition)| match identifier { + DefinitionIdentifier::ProtoNode(identifier) => Some((identifier.clone(), definition)), + _ => None, + }) + .collect() +}); + +/// Looks up a proto node's definition without taking its identifier, for reads frequent enough that a clone per call would show. +pub fn resolve_proto_node_definition(identifier: &ProtoNodeIdentifier) -> Option<&'static DocumentNodeDefinition> { + PROTO_NODE_DEFINITIONS.get(identifier).copied() } pub fn resolve_document_node_type(identifier: &DefinitionIdentifier) -> Option<&'static DocumentNodeDefinition> { diff --git a/editor/src/messages/portfolio/document/storage_tests/metadata_tests.rs b/editor/src/messages/portfolio/document/storage_tests/metadata_tests.rs index 1c140b3fa73..f9c6ab674d6 100644 --- a/editor/src/messages/portfolio/document/storage_tests/metadata_tests.rs +++ b/editor/src/messages/portfolio/document/storage_tests/metadata_tests.rs @@ -8,7 +8,7 @@ use std::collections::{BTreeSet, HashMap}; use document_graph_storage::{NodeMetadataSource, PeerId, Registry, to_value}; -use super::test_support::{located_node, network_locations, node_locations, node_paths, open_demo, open_demo_with_output_descriptions}; +use super::test_support::{located_node, network_locations, node_locations, open_demo, open_demo_with_named_subgraph_interfaces}; use crate::messages::portfolio::document::document_message_handler::DocumentMessageHandler; use crate::messages::portfolio::document::node_graph::document_node_definitions::{DefinitionIdentifier, resolve_document_node_type}; use crate::messages::portfolio::document::utility_types::network_interface::NodeLocator; @@ -30,7 +30,7 @@ fn normalize_output_descriptions(output_descriptions: Vec) -> Vec = source.input_data(network, local_id, index).into_iter().collect(); - - assert_eq!( - input_entry.input_name, expected_name, - "input_name mismatch at slot {index} for node {local_id:?} in network {network:?}" - ); - assert_eq!( - input_entry.input_description, expected_description, - "input_description mismatch at slot {index} for node {local_id:?} in network {network:?}" - ); - assert_eq!( - input_entry.widget_override, expected_widget, - "widget_override mismatch at slot {index} for node {local_id:?} in network {network:?}" - ); - assert_eq!( - input_entry.input_data, expected_data, - "input_data mismatch at slot {index} for node {local_id:?} in network {network:?}" - ); - } if entry.position.is_some() { checked_any_position = true; @@ -125,18 +77,15 @@ fn editor_metadata_round_trip_against_demo() { if entry.is_layer { checked_any_layer = true; } - if any_input_metadata_present { - checked_any_input_metadata = true; - } - if !expected_output_descriptions.is_empty() { - checked_any_output_description = true; - } } for entry in &network_entries { let network = &entry.network; let location = network; assert_eq!(entry.import_metadata, source.import_metadata(*location), "import_metadata mismatch for network {network:?}"); + if entry.import_metadata.iter().any(|input| input.input_name.is_some()) { + checked_any_input_metadata = true; + } assert_eq!(entry.export_names, source.export_names(*location), "export_names mismatch for network {network:?}"); let expected_export_descriptions = normalize_output_descriptions(source.export_descriptions(*location)); @@ -150,9 +99,7 @@ fn editor_metadata_round_trip_against_demo() { // just iterating empty metadata and proving nothing. assert!(checked_any_position, "demo artwork produced no positioned nodes: fixture is wrong or extraction is broken"); assert!(checked_any_layer, "demo artwork produced no layer nodes: fixture is wrong or extraction is broken"); - // Demo artworks always have some custom widget overrides / input names; if not, the - // per-input round-trip isn't actually being exercised here. - assert!(checked_any_input_metadata, "demo artwork produced no per-input metadata: fixture is wrong or extraction is broken"); + assert!(checked_any_input_metadata, "demo artwork produced no named imports: fixture is wrong or extraction is broken"); assert!(checked_any_output_description, "no output descriptions were round-tripped: fixture is wrong or extraction is broken"); } @@ -161,7 +108,7 @@ fn editor_metadata_round_trip_against_demo() { /// the original when read through `StorageMetadataView`. #[test] fn editor_interface_rebuild_round_trip() { - let document = open_demo_with_output_descriptions("changing-seasons.graphite"); + let document = open_demo_with_named_subgraph_interfaces("changing-seasons.graphite"); let original = &document.network_interface; let original_view = StorageMetadataView::new(original); @@ -172,7 +119,7 @@ fn editor_interface_rebuild_round_trip() { let registry = conversion.registry; let (rebuilt_network, node_entries, network_entries) = registry.to_runtime_with_full_metadata(&declarations, &blobs).expect("to_runtime_with_full_metadata failed"); - let rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries).expect("build_interface_from_storage failed"); + let rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries); let rebuilt_view = StorageMetadataView::new(&rebuilt); // Every node the original carried must also resolve identically through the rebuilt view. @@ -208,47 +155,12 @@ fn editor_interface_rebuild_round_trip() { "pinned mismatch for node {local_id:?} in network {network:?}" ); - // Per-input metadata: walk both interfaces' input slots in lockstep. let original_inputs = located_node(original, &network, local_id).map(|n| n.inputs.len()).unwrap_or(0); let rebuilt_inputs = located_node(&rebuilt, &network, local_id).map(|n| n.inputs.len()).unwrap_or(0); assert_eq!( rebuilt_inputs, original_inputs, "input count mismatch for node {local_id:?} in network {network:?}: rebuilt={rebuilt_inputs} original={original_inputs}" ); - - for index in 0..rebuilt_inputs { - assert_eq!( - rebuilt_view.input_name(network, local_id, index), - original_view.input_name(network, local_id, index), - "input_name mismatch at slot {index} for node {local_id:?} in network {network:?}" - ); - assert_eq!( - rebuilt_view.input_description(network, local_id, index), - original_view.input_description(network, local_id, index), - "input_description mismatch at slot {index} for node {local_id:?} in network {network:?}" - ); - assert_eq!( - rebuilt_view.widget_override(network, local_id, index), - original_view.widget_override(network, local_id, index), - "widget_override mismatch at slot {index} for node {local_id:?} in network {network:?}" - ); - assert_eq!( - rebuilt_view.input_data(network, local_id, index), - original_view.input_data(network, local_id, index), - "input_data mismatch at slot {index} for node {local_id:?} in network {network:?}" - ); - } - - assert_eq!( - rebuilt_view.output_names(network, local_id), - original_view.output_names(network, local_id), - "output_names mismatch for node {local_id:?} in network {network:?}" - ); - assert_eq!( - normalize_output_descriptions(rebuilt_view.output_descriptions(network, local_id)), - normalize_output_descriptions(original_view.output_descriptions(network, local_id)), - "output_descriptions mismatch for node {local_id:?} in network {network:?}" - ); } // Symmetric: every node in the original must also exist in the rebuilt interface. @@ -289,7 +201,7 @@ fn rebuilt_interface_keeps_storage_identities() { let registry = conversion.registry; let (rebuilt_network, node_entries, network_entries) = registry.to_runtime_with_full_metadata(&declarations, &blobs).expect("to_runtime_with_full_metadata failed"); - let rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries).expect("build_interface_from_storage failed"); + let rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries); let reconverted_network = rebuilt.document_network().clone(); let reconverted = Registry::convert_from_runtime(&reconverted_network, &StorageMetadataView::new(&rebuilt), &Default::default(), PeerId(9)).expect("re-convert from_runtime failed"); @@ -313,55 +225,43 @@ fn migrating_a_document_records_no_changes() { assert!(recorded.is_empty(), "migration left {} recorded changes behind", recorded.len()); } -/// The first Merge node in a document, as its network path and ID. -fn first_merge(document: &DocumentMessageHandler) -> (Vec, NodeId) { - let merge = Some(DefinitionIdentifier::Network("Merge".into())); - node_paths(&document.network_interface) - .into_iter() - .find(|(network_path, node_id)| document.network_interface.reference(node_id, network_path) == merge) - .expect("the demo artwork should have a Merge node") -} +/// A new Merge node given its own copy of the subgraph holding `output_descriptions`, after its document is saved and opened again. +async fn reopened_merge_with_output_descriptions(output_descriptions: Vec) -> (DocumentMessageHandler, NodeId) { + let mut editor = crate::test_utils::EditorTestUtils::create(); + editor.new_document().await; + let node_id = editor.create_node_by_name(DefinitionIdentifier::Network("Merge".into())).await; -/// Opening a document fills in the output descriptions of a node saved before its definition had any. -#[test] -fn opening_a_document_fills_output_descriptions_a_node_was_saved_without() { - let mut document = open_demo("changing-seasons.graphite"); - let (network_path, node_id) = first_merge(&document); - - // Saved as it would have been before Merge's definition described its output - let mut node = document.network_interface.node_mut(NodeLocator::new(node_id, &network_path)).expect("the Merge node should exist"); - node.set_output_descriptions(Vec::new()); + let document = editor.active_document_mut(); + let mut node = document.network_interface.node_interface_mut(NodeLocator::new(node_id, &[])).expect("the Merge node should exist"); + node.set_output_descriptions(output_descriptions); let content = document.serialize_document(); let reopened = load_legacy_document(content, &HashMapResourceStorage::new(), false).expect("the saved document should open"); + (reopened, node_id) +} + +/// Opening a document fills in the output descriptions of a node saved before its definition had any. +#[tokio::test] +async fn opening_a_document_fills_output_descriptions_a_node_was_saved_without() { + // Saved as it would have been before Merge's definition described its output + let (reopened, node_id) = reopened_merge_with_output_descriptions(Vec::new()).await; let definition_description = resolve_document_node_type(&DefinitionIdentifier::Network("Merge".into())) .and_then(|definition| definition.node_template.output_descriptions.first().cloned()) .filter(|description| !description.is_empty()) .expect("Merge's definition should describe its output"); assert_eq!( - reopened.network_interface.output_description(&node_id, 0, &network_path), + reopened.network_interface.output_description(&node_id, 0, &[]), definition_description, "the Merge node should be given its definition's output description" ); } /// An output description that was emptied stays empty when the document is saved and opened again, rather than being filled from the definition. -#[test] -fn an_emptied_output_description_stays_empty_after_reopening() { - let mut document = open_demo("changing-seasons.graphite"); - let (network_path, node_id) = first_merge(&document); - - let mut node = document.network_interface.node_mut(NodeLocator::new(node_id, &network_path)).expect("the Merge node should exist"); - node.set_output_descriptions(vec![String::new()]); - - let content = document.serialize_document(); - let reopened = load_legacy_document(content, &HashMapResourceStorage::new(), false).expect("the saved document should open"); - assert_eq!( - reopened.network_interface.output_description(&node_id, 0, &network_path), - "", - "the emptied description should stay empty" - ); +#[tokio::test] +async fn an_emptied_output_description_stays_empty_after_reopening() { + let (reopened, node_id) = reopened_merge_with_output_descriptions(vec![String::new()]).await; + assert_eq!(reopened.network_interface.output_description(&node_id, 0, &[]), "", "the emptied description should stay empty"); } /// The pinned display order is document state, so it survives a registry round trip. It names nodes and @@ -387,7 +287,7 @@ fn pinned_order_round_trips_through_the_registry() { let conversion = Registry::convert_from_runtime(&network, &view, &Default::default(), PeerId(0)).expect("convert_from_runtime failed"); let declarations = conversion.declarations; let (rebuilt_network, node_entries, network_entries) = conversion.registry.to_runtime_with_full_metadata(&declarations, &conversion.blob_bytes).expect("to_runtime failed"); - let rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries).expect("build_interface_from_storage failed"); + let rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries); let rebuilt_metadata = rebuilt.network_metadata(&[]).expect("the rebuilt document network should resolve"); assert_eq!( diff --git a/editor/src/messages/portfolio/document/storage_tests/round_trip_tests.rs b/editor/src/messages/portfolio/document/storage_tests/round_trip_tests.rs index 0a9d2c8a3c6..a50d51349b5 100644 --- a/editor/src/messages/portfolio/document/storage_tests/round_trip_tests.rs +++ b/editor/src/messages/portfolio/document/storage_tests/round_trip_tests.rs @@ -171,7 +171,7 @@ async fn edit_after_open_commits_cleanly() { let reopened = GddV1::open_in(working, layout).await.expect("open_in"); let declarations = reopened.declarations(&byte_store).await; let (rebuilt_network, node_entries, network_entries) = reopened.registry().to_runtime_with_full_metadata(&declarations, reopened.blobs()).expect("to_runtime"); - let rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries).expect("build_interface_from_storage"); + let rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries); // Install the rebuilt interface + reopened storage and finalize like the editor's open path does. { @@ -452,7 +452,7 @@ async fn per_network_navigation_round_trips_via_session_not_registry() { let reopened = GddV1::open_in(working, layout).await.expect("open_in"); let declarations = reopened.declarations(&byte_store).await; let (rebuilt_network, node_entries, network_entries) = reopened.registry().to_runtime_with_full_metadata(&declarations, reopened.blobs()).expect("to_runtime"); - let mut rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries.clone()).expect("build_interface_from_storage"); + let mut rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries.clone()); let reopened_ids = network_ids_from_entries(&network_entries); apply_network_view_settings(&mut rebuilt, &reopened_ids, reopened.network_view_settings()); diff --git a/editor/src/messages/portfolio/document/storage_tests/test_support.rs b/editor/src/messages/portfolio/document/storage_tests/test_support.rs index 1a79b492dc6..0c6d6fe50f0 100644 --- a/editor/src/messages/portfolio/document/storage_tests/test_support.rs +++ b/editor/src/messages/portfolio/document/storage_tests/test_support.rs @@ -30,8 +30,8 @@ pub fn open_demo(file_name: &str) -> DocumentMessageHandler { document.unwrap_or_else(|| panic!("Failed to open {path}")) } -/// Open a demo artwork and describe every subgraph node's outputs, which the demos predate, so a round trip has output descriptions to carry. -pub fn open_demo_with_output_descriptions(file_name: &str) -> DocumentMessageHandler { +/// Open a demo artwork, describe every subgraph node's outputs, and name one subgraph node's inputs, which the demos leave blank, so a round trip has an interface to carry. +pub fn open_demo_with_named_subgraph_interfaces(file_name: &str) -> DocumentMessageHandler { let mut document = open_demo(file_name); let interface = &mut document.network_interface; @@ -47,6 +47,22 @@ pub fn open_demo_with_output_descriptions(file_name: &str) -> DocumentMessageHan } } + // Built-in subgraphs take their interface from the catalog, so this forks one into a copy the document stores. + // It picks one sharing its catalog entry with another node, so the fork leaves no unused entry behind. + let root_nodes = &interface.document_network().nodes; + let shared_subgraph = root_nodes.iter().find_map(|(node_id, node)| { + let is_built_in = interface.is_read_only(&[*node_id]); + let is_shared = root_nodes.values().filter(|other| other.implementation == node.implementation).count() > 1; + (is_built_in && is_shared).then_some((*node_id, node.inputs.len())) + }); + if let Some((node_id, import_count)) = shared_subgraph + && let Some(mut node) = interface.node_interface_mut(NodeLocator::new(node_id, &[])) + { + for index in 0..import_count { + node.set_input_name(index, format!("Input {index} of node {node_id}")); + } + } + document } @@ -134,7 +150,7 @@ pub async fn round_trip_through_gdd(document: &DocumentMessageHandler) -> RoundT let declarations = reopened.declarations(&byte_store).await; let registry = reopened.registry().clone(); let (rebuilt_network, node_entries, network_entries) = registry.to_runtime_with_full_metadata(&declarations, reopened.blobs()).expect("to_runtime_with_full_metadata"); - let rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries).expect("build_interface_from_storage"); + let rebuilt = build_interface_from_storage(rebuilt_network, node_entries, network_entries); let view_settings = reopened.view_settings().clone(); RoundTrip { rebuilt, registry, view_settings } diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface.rs b/editor/src/messages/portfolio/document/utility_types/network_interface.rs index 7cc08fc5d42..c42c8845b62 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface.rs @@ -48,7 +48,7 @@ use crate::consts::{ EXPORTS_TO_RIGHT_EDGE_PIXEL_GAP, EXPORTS_TO_TOP_EDGE_PIXEL_GAP, GRID_SIZE, HALF_GRID_SIZE, IMPORTS_TO_LEFT_EDGE_PIXEL_GAP, IMPORTS_TO_TOP_EDGE_PIXEL_GAP, LAYER_INDENT_OFFSET, NODE_CHAIN_WIDTH, STACK_VERTICAL_GAP, }; -use crate::messages::portfolio::document::node_graph::document_node_definitions::{DefinitionIdentifier, resolve_document_node_type, resolve_proto_node_type}; +use crate::messages::portfolio::document::node_graph::document_node_definitions::{DefinitionIdentifier, resolve_document_node_type, resolve_proto_node_definition, resolve_proto_node_type}; use crate::messages::portfolio::document::node_graph::utility_types::{Direction, FrontendClickTargets, FrontendGraphDataType, FrontendGraphInput, FrontendGraphOutput}; use crate::messages::portfolio::document::utility_types::network_interface::resolved_types::ResolvedDocumentNodeTypes; use crate::messages::portfolio::document::utility_types::text_metrics::text_width; @@ -143,13 +143,7 @@ impl PartialEq for NodeNetworkInterface { } impl NodeNetworkInterface { - // TODO: Eventually remove this document upgrade code - /// Moves the interface metadata an older document stored on each instance node onto the network implementing it. - pub fn move_interface_metadata_into_networks(&mut self) { - self.migrate_metadata(NodeNetworkMetadata::move_interface_metadata_into_networks); - } - - /// Gives each entry under a catalog ID that differs from the shipped network a fresh ID. + /// Gives each entry under a catalog ID that differs from the shipped network a fresh ID, rekeying its nodes so they can't collide with the shipped network's. pub fn give_diverged_catalog_entries_new_ids(&mut self) { loop { let metadata_definitions = &self.network_metadata.persistent_metadata.definitions; @@ -160,7 +154,7 @@ impl NodeNetworkInterface { .filter(|(id, network)| { let Some(shipped) = catalog::entry(**id) else { return false }; let metadata = metadata_definitions.get(id).map(|metadata| &metadata.persistent_metadata); - shipped.network != **network || !metadata.is_some_and(|metadata| same_content(&shipped.metadata.persistent_metadata, metadata)) + shipped.network != **network || !metadata.is_some_and(|metadata| same_content_as_shipped(&shipped.metadata.persistent_metadata, metadata)) }) .map(|(id, _)| (*id, NetworkId::new())) .collect(); @@ -170,7 +164,8 @@ impl NodeNetworkInterface { self.migrate_graph(|network| { for (old, new) in &diverged { - if let Some(definition) = network.definitions.remove(old) { + if let Some(mut definition) = network.definitions.remove(old) { + definition.map_ids(|node_id| derived_ids::derived_node_id(*new, node_id)); network.definitions.insert(*new, definition); } } @@ -185,7 +180,9 @@ impl NodeNetworkInterface { }); self.migrate_metadata(|metadata, _| { for (old, new) in &diverged { - if let Some(definition) = metadata.persistent_metadata.definitions.remove(old) { + if let Some(mut definition) = metadata.persistent_metadata.definitions.remove(old) { + derived_ids::rekey_node_metadata(*new, &mut definition.persistent_metadata); + definition.transient_metadata = Default::default(); metadata.persistent_metadata.definitions.insert(*new, definition); } } @@ -193,6 +190,30 @@ impl NodeNetworkInterface { } } + /// Gives each catalog entry the shipped interface, which the catalog owns rather than the document. + pub fn adopt_catalog_interfaces(&mut self) { + let ids: Vec = self.document_network().definitions.keys().copied().collect(); + for id in ids { + self.adopt_catalog_interface(id); + } + } + + /// Gives the table entry `id` the shipped interface if it is a catalog entry, which storage keeps none of. + pub(crate) fn adopt_catalog_interface(&mut self, id: NetworkId) { + let Some(shipped) = catalog::entry(id) else { return }; + let shipped = shipped.metadata.persistent_metadata; + + self.migrate_metadata(|metadata, _| { + let Some(definition_metadata) = metadata.persistent_metadata.definitions.get_mut(&id) else { + return; + }; + let persistent = &mut definition_metadata.persistent_metadata; + persistent.import_metadata = shipped.import_metadata; + persistent.export_names = shipped.export_names; + persistent.export_descriptions = shipped.export_descriptions; + }); + } + // TODO: Eventually remove this document upgrade code /// Gives the nodes of each definition reusing a node ID from another network IDs derived from the definition, for a document from before node IDs were unique across it. /// Catalog entries keep their IDs. @@ -442,12 +463,11 @@ mod network_interface_tests { let mut definition_metadata = NodeNetworkMetadata::default(); definition_metadata.persistent_metadata.node_metadata.insert(NodeId(0), DocumentNodeMetadata::default()); - let mut instance_metadata = DocumentNodeMetadata::default(); - instance_metadata.persistent_metadata.input_metadata = vec![InputMetadata::default()]; - instance_metadata.persistent_metadata.output_names = vec![String::new()]; + definition_metadata.persistent_metadata.import_metadata = vec![InputMetadata::default()]; + definition_metadata.persistent_metadata.export_names = vec![String::new()]; let mut network_metadata = NodeNetworkMetadata::default(); for id in instances { - network_metadata.persistent_metadata.node_metadata.insert(id, instance_metadata.clone()); + network_metadata.persistent_metadata.node_metadata.insert(id, DocumentNodeMetadata::default()); } network_metadata.persistent_metadata.definitions.insert(definition, definition_metadata); @@ -462,6 +482,44 @@ mod network_interface_tests { assert_eq!(view.output_names(&NodeId(11)).unwrap(), ["Result".to_string()]); } + #[test] + fn a_proto_node_shows_its_definition_signature_over_a_stored_copy() { + use super::*; + + let definition = resolve_proto_node_type(graphene_std::vector_nodes::fill::IDENTIFIER).expect("Fill should have a definition"); + let mut template = definition.default_node_template(); + for input_metadata in &mut template.input_metadata { + input_metadata.persistent_metadata.input_name = "Stale name".into(); + input_metadata.persistent_metadata.input_description = "Stale description".into(); + } + + let mut interface = NodeNetworkInterface::default(); + interface.insert_node(NodeId(1), template, &[]); + + for (index, expected) in definition.node_template.input_metadata.iter().enumerate() { + let expected = &expected.persistent_metadata; + let (name, description) = interface.displayed_input_name_and_description(&NodeId(1), index, &[]); + + assert_eq!(description, expected.input_description, "input {index}"); + if !expected.input_name.is_empty() { + assert_eq!(name, expected.input_name, "input {index}"); + } + } + } + + #[test] + fn an_extract_node_shows_its_definition_signature() { + use super::*; + + let definition = resolve_document_node_type(&DefinitionIdentifier::Network("Extract".into())).expect("Extract should have a definition"); + let mut interface = NodeNetworkInterface::default(); + interface.insert_node(NodeId(1), definition.default_node_template(), &[]); + + let expected = &definition.node_template.input_metadata[0].persistent_metadata; + let shown = interface.displayed_input_name_and_description(&NodeId(1), 0, &[]); + assert_eq!(shown, (expected.input_name.clone(), expected.input_description.clone())); + } + #[test] fn interface_edits_through_one_instance_reach_every_instance() { use super::*; diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/apply.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/apply.rs index 694baf14556..309c3f80cf6 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/apply.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/apply.rs @@ -47,7 +47,7 @@ impl NodeNetworkInterface { EditorDelta::Graph(RuntimeDelta::SetInputs { network, node_id, inputs }) => { let Some(mut node) = self.node_mut_at(*network, *node_id) else { return }; - // The slots are what changed; their metadata follows in the paired delta. Resizing keeps the + // The slots are what changed; a table entry's metadata follows in the paired delta. Resizing keeps the // two arrays the same length in the meantime, which the rest of the interface assumes. let mut input_metadata = node.input_metadata().to_vec(); input_metadata.resize(inputs.len(), InputMetadata::default()); @@ -90,12 +90,6 @@ impl NodeNetworkInterface { } } - EditorDelta::NodeInputMetadata { network, node_id, input_metadata } => { - if let Some(mut node) = self.node_mut_at(*network, *node_id) { - node.set_input_metadata(input_metadata.clone()); - } - } - EditorDelta::NodeMetadata { network, node_id, change } => { let Some(mut node) = self.node_mut_at(*network, *node_id) else { return }; match change { @@ -103,10 +97,6 @@ impl NodeNetworkInterface { NodeMetadataChange::DisplayName(display_name) => node.set_display_name(display_name.clone()), NodeMetadataChange::Locked(locked) => node.set_locked(*locked), NodeMetadataChange::Pinned(pinned) => node.set_pinned(*pinned), - NodeMetadataChange::OutputNames(output_names) => node.set_output_names(output_names.clone()), - NodeMetadataChange::OutputDescriptions(output_descriptions) => node.set_output_descriptions(output_descriptions.clone()), - NodeMetadataChange::InputName { index, name } => node.set_input_name(*index, name.clone()), - NodeMetadataChange::WidgetOverride { index, widget_override } => node.set_widget_override(*index, widget_override.clone()), }; } diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/catalog.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/catalog.rs index 955990f6143..7be45e5f390 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/catalog.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/catalog.rs @@ -72,7 +72,7 @@ impl Catalog { fn catalog_id_of(&self, network: &NodeNetwork, metadata: &NodeNetworkPersistentMetadata) -> Option { let identifier = metadata.reference.as_deref()?; let (shipped_network, shipped_metadata) = self.inline.get(identifier)?; - (shipped_network == network && same_content(shipped_metadata, metadata)).then(|| network_id(identifier.as_bytes())) + (shipped_network == network && same_content_as_shipped(shipped_metadata, metadata)).then(|| network_id(identifier.as_bytes())) } } diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/catalog_tests.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/catalog_tests.rs index 6856f05978e..e7dcdd522c9 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/catalog_tests.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/catalog_tests.rs @@ -162,6 +162,92 @@ async fn an_entry_that_diverged_under_a_catalog_id_gets_a_fresh_id_on_open() { assert!(network.definitions[&renamed].exports.len() > catalog::entry(shipped).expect("shipped").network.exports.len()); } +#[tokio::test] +async fn an_entry_given_a_fresh_id_on_open_can_sit_beside_the_shipped_entry() { + let (mut editor, blends) = editor_with_blends(1).await; + let shipped = entry_of(&editor, blends[0]); + + let interface = &mut editor.active_document_mut().network_interface; + interface.migrate_graph(|network| { + let definition = network.definitions.get_mut(&shipped).expect("the shipped entry"); + let node = definition.nodes.values_mut().next().expect("the shipped entry should hold a node"); + node.visible = !node.visible; + }); + interface.give_diverged_catalog_entries_new_ids(); + + // A new node brings the shipped entry back, whose node IDs the renamed entry must no longer share + let template = resolve_network_node_type("Blend").expect("the catalog should hold Blend").default_node_template(); + interface.insert_node(NodeId::new(), template, &[]); + + assert_ne!(entry_of(&editor, blends[0]), shipped); + assert_eq!(editor.active_document().network_interface.validate_invariants(), Vec::::new()); +} + +#[tokio::test] +async fn a_catalog_entry_saved_with_an_outdated_interface_stays_shared_and_shows_the_shipped_one() { + let (mut editor, blends) = editor_with_blends(1).await; + let shipped = entry_of(&editor, blends[0]); + let shipped_name = catalog::entry(shipped).expect("shipped").metadata.persistent_metadata.import_metadata[0] + .persistent_metadata + .input_name + .clone(); + assert!(!shipped_name.is_empty(), "the shipped import should have a name for this to check anything"); + + let interface = &mut editor.active_document_mut().network_interface; + interface.migrate_metadata(|metadata, _| { + let entry = metadata.persistent_metadata.definitions.get_mut(&shipped).expect("the shipped entry"); + entry.persistent_metadata.import_metadata[0].persistent_metadata.input_name = "Outdated name".into(); + }); + interface.give_diverged_catalog_entries_new_ids(); + interface.adopt_catalog_interfaces(); + + assert_eq!(entry_of(&editor, blends[0]), shipped); + let (name, _) = editor.active_document().network_interface.displayed_input_name_and_description(&blends[0], 0, &[]); + assert_eq!(name, shipped_name); +} + +#[tokio::test] +async fn a_saved_document_leaves_the_catalog_interface_to_the_catalog() { + use super::storage_metadata::StorageMetadataView; + use document_graph_storage::NodeMetadataSource; + + let (editor, blends) = editor_with_blends(1).await; + let shipped = entry_of(&editor, blends[0]); + + let view = StorageMetadataView::new(&editor.active_document().network_interface); + assert!(view.import_metadata(Some(shipped)).is_empty()); + assert!(view.export_names(Some(shipped)).is_empty()); + assert!(view.export_descriptions(Some(shipped)).is_empty()); + + // The rebuild from storage, which undo and collaboration also go through, gives the interface back from the catalog + let reopened = round_trip_through_gdd(editor.active_document()).await.rebuilt; + let expected = editor.active_document().network_interface.displayed_input_name_and_description(&blends[0], 0, &[]); + assert_eq!(reopened.displayed_input_name_and_description(&blends[0], 0, &[]), expected); + assert!(reopened.validate_invariants().is_empty()); +} + +#[tokio::test] +async fn the_document_json_leaves_the_catalog_interface_to_the_catalog() { + let (editor, blends) = editor_with_blends(1).await; + let shipped = entry_of(&editor, blends[0]); + + let metadata = &editor.active_document().network_interface.document_network_metadata().persistent_metadata; + let json = serde_json::to_value(metadata).expect("the metadata should serialize"); + let reloaded: super::NodeNetworkPersistentMetadata = serde_json::from_value(json).expect("the metadata should deserialize"); + + let entry = &reloaded.definitions[&shipped].persistent_metadata; + assert!(entry.import_metadata.is_empty() && entry.export_names.is_empty() && entry.export_descriptions.is_empty()); +} + +#[tokio::test] +async fn a_copy_leaves_catalog_entries_to_the_catalog() { + let (editor, blends) = editor_with_blends(1).await; + let interface = &editor.active_document().network_interface; + + let template = interface.create_node_template(&blends[0], &[]).expect("the node should make a template"); + assert!(interface.copied_definitions([&template]).is_empty()); +} + #[tokio::test] async fn a_reopened_document_keeps_its_catalog_entries() { let (editor, blends) = editor_with_blends(1).await; @@ -253,3 +339,34 @@ async fn undoing_and_redoing_the_unlock_of_a_layer_keeps_its_nodes() { assert!(!editor.active_document().network_interface.is_read_only(&path)); assert!(editor.active_document().network_interface.validate_invariants().is_empty()); } + +/// A copied subgraph holding a built-in subgraph pastes into a document without that built-in, which the paste takes from the catalog. +#[tokio::test] +async fn a_subgraph_holding_a_catalog_node_pastes_into_a_document_without_it() { + let mut editor = EditorTestUtils::create(); + editor.new_document().await; + + let custom = NodeId::new(); + let blend = NodeId::new(); + let interface = &mut editor.active_document_mut().network_interface; + interface.insert_node(custom, resolve_network_node_type("Custom Node").expect("Custom Node").default_node_template(), &[]); + interface.insert_node(blend, resolve_network_node_type("Blend").expect("Blend").default_node_template(), &[custom]); + let DocumentNodeImplementation::Definition(shipped) = interface.document_node(&blend, &[custom]).expect("the Blend").implementation else { + panic!("the Blend should name a table entry") + }; + assert!(catalog::is_protected(shipped)); + + editor.handle_message(NodeGraphMessage::SelectedNodesSet { nodes: vec![custom] }).await; + let clipboard = editor.copy_to_clipboard(NodeGraphMessage::Copy).await.expect("copying should write the clipboard"); + + editor.new_document().await; + editor.paste_from_clipboard(clipboard).await; + + let interface = &editor.active_document().network_interface; + assert_eq!(interface.document_network().nodes.len(), 1, "the subgraph should be pasted"); + assert!( + interface.document_network().definitions.contains_key(&shipped), + "the paste should bring in the built-in subgraph from the catalog" + ); + assert!(interface.validate_invariants().is_empty()); +} diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/deserialization.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/deserialization.rs index 5f25fa99339..040f9320734 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/deserialization.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/deserialization.rs @@ -2,6 +2,7 @@ // This file holds the older serialized shapes of the network metadata, read once when a document is // opened and converted to the current form. +use super::types::fill_empty_interface; use crate::messages::portfolio::document::utility_types::network_interface::{DocumentNodePersistentMetadata, InputMetadata, InputPersistentMetadata, NodeNetworkMetadata, NodeTypePersistentMetadata}; use serde_json::Value; use std::collections::HashMap; @@ -139,12 +140,16 @@ struct DocumentNodePersistentMetadataStringReference { pub network_metadata: Option, } -impl From for DocumentNodePersistentMetadata { +impl From for DocumentNodePersistentMetadataInstanceInterface { fn from(mut old: DocumentNodePersistentMetadataStringReference) -> Self { - if let Some(metadata) = old.network_metadata.as_mut() { + // A node without a reference leaves the one its network holds + if let Some(metadata) = old.network_metadata.as_mut() + && old.reference.is_some() + { metadata.persistent_metadata.reference = old.reference; } - DocumentNodePersistentMetadata { + DocumentNodePersistentMetadataInstanceInterface { + storage_id: None, display_name: old.display_name, input_metadata: old.input_metadata, output_names: old.output_names, @@ -157,11 +162,49 @@ impl From for DocumentNodePersist } } +/// The shape from when each node stored a copy of its interface, which now belongs to its definition or the network implementing it. +#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] +#[serde(deny_unknown_fields)] +struct DocumentNodePersistentMetadataInstanceInterface { + /// An ID some of these documents stored for the storage format, which nothing reads. + #[serde(default, skip_serializing)] + pub storage_id: Option, + #[serde(default)] + pub display_name: String, + pub input_metadata: Vec, + pub output_names: Vec, + #[serde(default)] + pub output_descriptions: Vec, + #[serde(default)] + pub locked: bool, + #[serde(default)] + pub pinned: bool, + pub node_type_metadata: NodeTypePersistentMetadata, + pub network_metadata: Option, +} + +impl From for DocumentNodePersistentMetadata { + fn from(mut old: DocumentNodePersistentMetadataInstanceInterface) -> Self { + // An inline network takes the interface its node stored, while any other node's copy is dropped + if let Some(network_metadata) = old.network_metadata.as_mut() { + fill_empty_interface(&mut network_metadata.persistent_metadata, (old.input_metadata, old.output_names, old.output_descriptions)); + } + DocumentNodePersistentMetadata { + display_name: old.display_name, + locked: old.locked, + pinned: old.pinned, + node_type_metadata: old.node_type_metadata, + network_metadata: old.network_metadata, + } + } +} + #[derive(serde::Deserialize)] #[serde(untagged)] enum DocumentNodePersistentMetadataVersioned { // Newest first Current(DocumentNodePersistentMetadata), + InstanceInterface(DocumentNodePersistentMetadataInstanceInterface), StringReference(DocumentNodePersistentMetadataStringReference), HasPrimaryOutput(DocumentNodePersistentMetadataHasPrimaryOutput), PropertiesRow(DocumentNodePersistentMetadataPropertiesRow), @@ -178,12 +221,10 @@ where let versioned_document = serde_json::from_value::(value).map_err(serde::de::Error::custom)?; - let current: DocumentNodePersistentMetadata = match versioned_document { - DocumentNodePersistentMetadataVersioned::Current(v) => v, - DocumentNodePersistentMetadataVersioned::StringReference(v) => { - let v: DocumentNodePersistentMetadataStringReference = v; - v.into() - } + let instance_interface: DocumentNodePersistentMetadataInstanceInterface = match versioned_document { + DocumentNodePersistentMetadataVersioned::Current(v) => return Ok(v), + DocumentNodePersistentMetadataVersioned::InstanceInterface(v) => v, + DocumentNodePersistentMetadataVersioned::StringReference(v) => v.into(), DocumentNodePersistentMetadataVersioned::HasPrimaryOutput(v) => { let v: DocumentNodePersistentMetadataStringReference = v.into(); v.into() @@ -200,6 +241,71 @@ where v.into() } }; + let current: DocumentNodePersistentMetadata = instance_interface.into(); Ok(current) } + +#[cfg(test)] +mod tests { + use super::*; + + /// A node's metadata as a document saved it while each node stored a copy of its interface. + fn saved_with_interface(network_metadata: Option) -> Value { + serde_json::json!({ + "display_name": "", + "input_metadata": [{ "persistent_metadata": { "input_data": {}, "widget_override": null, "input_name": "Content", "input_description": "What is drawn." } }], + "output_names": ["Result"], + "output_descriptions": [], + "locked": false, + "pinned": false, + "node_type_metadata": NodeTypePersistentMetadata::default(), + "network_metadata": network_metadata, + }) + } + + #[test] + fn an_inline_network_takes_the_interface_its_node_stored() { + let metadata = deserialize_node_persistent_metadata(saved_with_interface(Some(NodeNetworkMetadata::default()))).unwrap(); + let network = metadata.network_metadata.expect("the network should be kept").persistent_metadata; + + assert_eq!(network.import_metadata[0].persistent_metadata.input_name, "Content"); + assert_eq!(network.import_metadata[0].persistent_metadata.input_description, "What is drawn."); + assert_eq!(network.export_names, ["Result"]); + } + + #[test] + fn a_node_without_a_network_opens_without_the_interface_it_stored() { + let metadata = deserialize_node_persistent_metadata(saved_with_interface(None)).unwrap(); + assert_eq!(metadata, DocumentNodePersistentMetadata::default()); + } + + #[test] + fn a_node_saved_with_a_storage_id_keeps_its_network_reference() { + let mut network_metadata = NodeNetworkMetadata::default(); + network_metadata.persistent_metadata.reference = Some("Merge".into()); + let mut saved = saved_with_interface(Some(network_metadata)); + saved["storage_id"] = 5.into(); + + let metadata = deserialize_node_persistent_metadata(saved).unwrap(); + let network = metadata.network_metadata.expect("the network should be kept").persistent_metadata; + assert_eq!(network.reference.as_deref(), Some("Merge")); + assert_eq!(network.import_metadata[0].persistent_metadata.input_name, "Content"); + } + + #[test] + fn a_node_copied_while_nodes_stored_their_interface_still_pastes() { + use super::super::NodeTemplate; + + let template = NodeTemplate { + display_name: "Copied".into(), + ..Default::default() + }; + let mut copied = serde_json::to_value(&template).unwrap(); + copied["persistent_node_metadata"] = saved_with_interface(None); + copied["persistent_node_metadata"]["display_name"] = "Copied".into(); + + let pasted: NodeTemplate = serde_json::from_value(copied).expect("the copied node should parse"); + assert_eq!(pasted.display_name, "Copied"); + } +} diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/editor_delta.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/editor_delta.rs index 2eb35530bb2..5d3fbf880a3 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/editor_delta.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/editor_delta.rs @@ -1,13 +1,12 @@ +use super::InputMetadata; use super::storage_metadata::{input_metadata_entry_from_runtime, position_from_runtime, versioned_reference}; use super::{DocumentNodePersistentMetadata, LayerPosition, NodePosition, NodeTypePersistentMetadata}; -use super::{InputMetadata, InputPersistentMetadata}; use document_graph_storage::attr::network as network_attr; use document_graph_storage::attr::node as node_attr; use document_graph_storage::from_runtime::{ConversionError, DeclarationBytes}; -use document_graph_storage::{AttributeDelta, AttributeValue, Attributes, Implementation, NoMetadata, NodeMetadataSource, Position, Registry, RegistryDelta, ScopedConversion, TimeStamp, Value}; +use document_graph_storage::{AttributeDelta, AttributeValue, Attributes, Implementation, NoMetadata, Position, Registry, RegistryDelta, ScopedConversion, TimeStamp, Value}; use document_graph_storage::{ - convert_input, convert_input_attributes, convert_resource_entry, encode_input_ui_attributes, encode_node_ui_attributes, node_value_resource_refs, storage_location_id, storage_node_id, - value_resource_ref, + convert_input, convert_input_attributes, convert_resource_entry, encode_node_ui_attributes, node_value_resource_refs, storage_location_id, storage_node_id, value_resource_ref, }; use graph_craft::application_io::resource::{ResourceId, ResourceRegistry}; use graph_craft::document::{NetworkLocation, NodeId, NodeNetwork}; @@ -48,13 +47,6 @@ pub enum EditorDelta { node_id: NodeId, change: NodeMetadataChange, }, - /// A node's whole input metadata array, paired with the `SetInputs` that changed the slots it - /// indexes. Whole-array for the same reason: an insert shifts every later entry. - NodeInputMetadata { - network: NetworkLocation, - node_id: NodeId, - input_metadata: Vec, - }, /// One field of an existing network's metadata. NetworkMetadata { network: NetworkLocation, @@ -73,18 +65,6 @@ pub enum NodeMetadataChange { DisplayName(String), Locked(bool), Pinned(bool), - /// The whole vec, since the encoding is one array-valued attribute rather than a slot each. - OutputNames(Vec), - /// The whole vec, for the same reason as `OutputNames`. - OutputDescriptions(Vec), - InputName { - index: usize, - name: String, - }, - WidgetOverride { - index: usize, - widget_override: Option, - }, } /// One field of a network's persistent metadata. The navigation transform and width are absent @@ -229,7 +209,6 @@ impl EditorDelta { } EditorDelta::Graph(RuntimeDelta::SetInputs { network: _, node_id, inputs }) => { - // The slots' `ui::*` attributes arrive with the paired input metadata delta let inputs = inputs .iter() .map(|input| { @@ -255,28 +234,16 @@ impl EditorDelta { ops.extend(node_metadata_ops(storage_node_id(*node_id), change)?); } - EditorDelta::NodeInputMetadata { network, node_id, input_metadata } => { - let location = network; - let global_id = storage_node_id(*node_id); - let source = InputMetadataSource(input_metadata); - let working_node = batch.node(global_id); - - for input_index in 0..input_metadata.len() { - let mut encoded = Attributes::new(); - encode_input_ui_attributes(&mut encoded, &source, *location, *node_id, input_index, TimeStamp::ORIGIN)?; - let current = working_node.and_then(|node| node.inputs().get(input_index)).map(|slot| &slot.attributes); - - for delta in ui_attribute_writes(current, &encoded) { - ops.push(RegistryDelta::ChangeNodeInputAttribute { - id: global_id, - index: input_index.try_into().map_err(|_| ConversionError::IndexOverflow(input_index))?, - delta, - }); - } + EditorDelta::NetworkMetadata { network, change } => { + // A catalog entry's interface is the catalog's, so storage keeps none of it + let interface_change = matches!( + change, + NetworkMetadataChange::ImportMetadata(_) | NetworkMetadataChange::ExportNames(_) | NetworkMetadataChange::ExportDescriptions(_) + ); + if interface_change && network.is_some_and(super::catalog::is_protected) { + return Ok(()); } - } - EditorDelta::NetworkMetadata { network, change } => { let delta = match change { NetworkMetadataChange::Reference(reference) => AttributeDelta { key: node_attr::ui::REFERENCE.to_string(), @@ -341,14 +308,6 @@ fn node_metadata_ops(global_id: document_graph_storage::NodeId, change: &NodeMet id: global_id, delta: AttributeDelta { key: key.to_string(), value }, }; - let input_attribute = |key: &str, index: usize, value: Option| { - Ok::<_, ConversionError>(RegistryDelta::ChangeNodeInputAttribute { - id: global_id, - index: index.try_into().map_err(|_| ConversionError::IndexOverflow(index))?, - delta: AttributeDelta { key: key.to_string(), value }, - }) - }; - // The empty string is the runtime's own sentinel for an unset name let flag = |value: bool| value.then_some(Value::Bool(true)); let non_empty = |value: &str| (!value.is_empty()).then(|| Value::Str(value.to_string())); @@ -363,20 +322,6 @@ fn node_metadata_ops(global_id: document_graph_storage::NodeId, change: &NodeMet NodeMetadataChange::DisplayName(display_name) => vec![node_attribute(node_attr::ui::DISPLAY_NAME, non_empty(display_name))], NodeMetadataChange::Locked(locked) => vec![node_attribute(node_attr::ui::LOCKED, flag(*locked))], NodeMetadataChange::Pinned(pinned) => vec![node_attribute(node_attr::ui::PINNED, flag(*pinned))], - NodeMetadataChange::OutputNames(output_names) => { - let value = (!output_names.is_empty()).then(|| serialize_attribute(node_attr::ui::OUTPUT_NAMES, output_names)).transpose()?; - vec![node_attribute(node_attr::ui::OUTPUT_NAMES, value)] - } - NodeMetadataChange::OutputDescriptions(output_descriptions) => { - let any_description = output_descriptions.iter().any(|description| !description.is_empty()); - let value = any_description.then(|| serialize_attribute(node_attr::ui::OUTPUT_DESCRIPTIONS, output_descriptions)).transpose()?; - vec![node_attribute(node_attr::ui::OUTPUT_DESCRIPTIONS, value)] - } - NodeMetadataChange::InputName { index, name } => vec![input_attribute(node_attr::input::ui::NAME, *index, non_empty(name))?], - NodeMetadataChange::WidgetOverride { index, widget_override } => { - let value = widget_override.as_deref().and_then(non_empty); - vec![input_attribute(node_attr::input::ui::WIDGET_OVERRIDE, *index, value)?] - } }) } @@ -459,9 +404,7 @@ fn stage_conversion( // wrote to its name, lock or pin. match batch.node(id) { Some(held) => { - // The swap did not write the slots' `ui::*` attributes, so they carry over to the slots that survive it with - // their stamps, tombstones and the held floor, losing to a concurrent write. The other attributes the new implementation - // lacks go by tombstone instead. + // The attributes the new implementation's slots lack go by tombstone, and the surviving slots keep the held floor let inputs = node .inputs() .iter() @@ -470,9 +413,7 @@ fn stage_conversion( let mut slot = slot.clone(); if let Some(previous) = held.inputs().get(index) { for (key, value) in previous.attributes.iter() { - if key.starts_with("ui::") { - slot.attributes.insert(key.clone(), value.clone()); - } else if !value.deleted && !slot.attributes.contains_key(key) { + if !value.deleted && !slot.attributes.contains_key(key) { slot.attributes.insert(key.clone(), AttributeValue::deleted(TimeStamp::ORIGIN)); } } @@ -567,19 +508,6 @@ fn construct_metadata_snapshot( ops.push(RegistryDelta::ChangeNodeAttribute { id: global_id, delta }); } - for input_index in 0..metadata.input_metadata.len() { - let mut encoded = Attributes::new(); - encode_input_ui_attributes(&mut encoded, &source, location, node_id, input_index, TimeStamp::ORIGIN)?; - let current = working_node.and_then(|node| node.inputs().get(input_index)).map(|slot| &slot.attributes); - for delta in ui_attribute_writes(current, &encoded) { - ops.push(RegistryDelta::ChangeNodeInputAttribute { - id: global_id, - index: input_index.try_into().map_err(|_| ConversionError::IndexOverflow(input_index))?, - delta, - }); - } - } - Ok(()) } @@ -839,37 +767,6 @@ fn network_resource_refs(network: &document_graph_storage::Network) -> impl Iter network.exports.iter().filter_map(|slot| slot.target.as_ref().and_then(value_resource_ref)) } -/// Serves one node's input metadata for encoding a [`EditorDelta::NodeInputMetadata`] delta. Only the -/// per-input accessors answer, since the node-level ones are never reached: the delta encodes through -/// `encode_input_ui_attributes` alone. -struct InputMetadataSource<'a>(&'a [InputMetadata]); - -impl InputMetadataSource<'_> { - fn persistent(&self, input_index: usize) -> Option<&InputPersistentMetadata> { - self.0.get(input_index).map(|metadata| &metadata.persistent_metadata) - } -} - -impl NodeMetadataSource for InputMetadataSource<'_> { - fn input_name(&self, _network: NetworkLocation, _local_id: NodeId, input_index: usize) -> Option<&str> { - Some(self.persistent(input_index)?.input_name.as_str()) - } - - fn input_description(&self, _network: NetworkLocation, _local_id: NodeId, input_index: usize) -> Option<&str> { - Some(self.persistent(input_index)?.input_description.as_str()) - } - - fn widget_override(&self, _network: NetworkLocation, _local_id: NodeId, input_index: usize) -> Option<&str> { - self.persistent(input_index)?.widget_override.as_deref() - } - - fn input_data(&self, _network: NetworkLocation, _local_id: NodeId, input_index: usize) -> HashMap { - self.persistent(input_index) - .map(|metadata| metadata.input_data.iter().map(|(key, value)| (key.clone(), value.clone().into())).collect()) - .unwrap_or_default() - } -} - /// Serves the metadata copy of the node `node_id` at `location` as the [`document_graph_storage::NodeMetadataSource`] for its own encoding. struct MetadataCopySource<'a> { location: NetworkLocation, @@ -913,37 +810,4 @@ impl document_graph_storage::NodeMetadataSource for MetadataCopySource<'_> { fn pinned(&self, network: NetworkLocation, node_id: NodeId) -> bool { self.metadata_for(network, node_id).is_some_and(|metadata| metadata.pinned) } - - fn output_names(&self, network: NetworkLocation, node_id: NodeId) -> Vec { - self.metadata_for(network, node_id).map(|metadata| metadata.output_names.clone()).unwrap_or_default() - } - - fn output_descriptions(&self, network: NetworkLocation, node_id: NodeId) -> Vec { - self.metadata_for(network, node_id).map(|metadata| metadata.output_descriptions.clone()).unwrap_or_default() - } - - fn input_name(&self, network: NetworkLocation, node_id: NodeId, input_index: usize) -> Option<&str> { - self.metadata_for(network, node_id) - .and_then(|metadata| metadata.input_metadata.get(input_index)) - .map(|input| input.persistent_metadata.input_name.as_str()) - } - - fn input_description(&self, network: NetworkLocation, node_id: NodeId, input_index: usize) -> Option<&str> { - self.metadata_for(network, node_id) - .and_then(|metadata| metadata.input_metadata.get(input_index)) - .map(|input| input.persistent_metadata.input_description.as_str()) - } - - fn widget_override(&self, network: NetworkLocation, node_id: NodeId, input_index: usize) -> Option<&str> { - self.metadata_for(network, node_id) - .and_then(|metadata| metadata.input_metadata.get(input_index)) - .and_then(|input| input.persistent_metadata.widget_override.as_deref()) - } - - fn input_data(&self, network: NetworkLocation, node_id: NodeId, input_index: usize) -> std::collections::HashMap { - self.metadata_for(network, node_id) - .and_then(|metadata| metadata.input_metadata.get(input_index)) - .map(|input| input.persistent_metadata.input_data.iter().map(|(key, value)| (key.clone(), value.clone().into())).collect()) - .unwrap_or_default() - } } diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/editor_delta_tests.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/editor_delta_tests.rs index a2724a4dd99..919db229a67 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/editor_delta_tests.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/editor_delta_tests.rs @@ -640,24 +640,17 @@ async fn swapping_an_implementation_leaves_the_node_in_place() { "the node's own metadata should not be restated by a swap that did not write it" ); - // The slots that survive the swap keep the names and descriptions the registry holds for them. - let Some((id, inputs)) = ops.iter().find_map(|op| match op { - RegistryDelta::SetNodeInputs { id, inputs } => Some((id, inputs)), + // The restated slots store no names or descriptions, which belong to the swapped-in definition + let Some(inputs) = ops.iter().find_map(|op| match op { + RegistryDelta::SetNodeInputs { inputs, .. } => Some(inputs), _ => None, }) else { panic!("the swap should restate the slots") }; - let held: Vec<_> = working.node_instances[id] - .inputs() - .iter() - .take(inputs.len()) - .flat_map(|slot| slot.attributes.live().filter(|(key, _)| key.starts_with("ui::"))) - .collect(); - assert!(!held.is_empty(), "the surviving slots should hold ui attributes for this to check anything"); + assert!(!inputs.is_empty(), "some slots should survive the swap for this to check anything"); for (index, slot) in inputs.iter().enumerate() { - for (key, value) in working.node_instances[id].inputs()[index].attributes.live().filter(|(key, _)| key.starts_with("ui::")) { - assert_eq!(slot.attributes.get(key), Some(value), "slot {index} lost {key}"); - } + let stored: Vec<_> = slot.attributes.live().map(|(key, _)| key).filter(|key| key.starts_with("ui::")).collect(); + assert!(stored.is_empty(), "slot {index} stores {stored:?}"); } } @@ -727,6 +720,22 @@ async fn assert_remote_reconcile_reproduces(edit: impl FnOnce(&mut EditorTestUti json_diff(&edited_metadata, &reconciled_metadata, "") ); + // The JSON leaves out each catalog entry's interface, which reconciling has to restore from the catalog + let catalog_interfaces = |interface: &super::NodeNetworkInterface| { + let definitions = &interface.document_network_metadata().persistent_metadata.definitions; + let mut interfaces: Vec<_> = definitions + .iter() + .filter(|(id, _)| super::catalog::is_protected(**id)) + .map(|(id, metadata)| { + let persistent = &metadata.persistent_metadata; + (*id, persistent.import_metadata.clone(), persistent.export_names.clone(), persistent.export_descriptions.clone()) + }) + .collect(); + interfaces.sort_by_key(|(id, ..)| *id); + interfaces + }; + assert_eq!(catalog_interfaces(&mirror), catalog_interfaces(edited), "reconciling did not restore the catalog interfaces"); + let converted = Registry::convert_from_runtime(mirror.document_network(), &StorageMetadataView::new(&mirror), &editor.active_document().resources.registry, PEER) .expect("the reconciled interface converts") .registry; @@ -984,8 +993,8 @@ async fn exports_of_a_subgraph_without_descriptions_emit_no_description_write() .filter(|delta| { matches!( delta, - EditorDelta::NodeMetadata { - change: NodeMetadataChange::OutputDescriptions(_), + EditorDelta::NetworkMetadata { + change: NetworkMetadataChange::ExportDescriptions(_), .. } ) @@ -1041,8 +1050,8 @@ async fn exports_past_the_end_of_the_description_list_emit_no_description_write( .filter(|delta| { matches!( delta, - EditorDelta::NodeMetadata { - change: NodeMetadataChange::OutputDescriptions(_), + EditorDelta::NetworkMetadata { + change: NetworkMetadataChange::ExportDescriptions(_), .. } ) @@ -1443,3 +1452,28 @@ async fn making_an_instance_unique_modifies_the_transaction() { assert!(interface.make_unique(©, &[])); assert_eq!(interface.transaction_status(), super::TransactionStatus::Modified); } + +/// A built-in subgraph's interface comes from the catalog, so inserting the first one writes none of it to storage. +#[tokio::test] +async fn inserting_a_catalog_node_stores_none_of_its_interface() { + use document_graph_storage::attr::network as network_attr; + + let mut editor = EditorTestUtils::create(); + editor.new_document().await; + let working = convert(&editor); + editor.active_document_mut().network_interface.discard_deltas(); + + let merge = editor.create_node_by_name(merge_definition()).await; + let emitted = committed_deltas(&mut editor); + let constructed = construct(&editor, &emitted, &working); + + let interface_keys = [network_attr::IMPORT_METADATA, network_attr::EXPORT_NAMES, network_attr::EXPORT_DESCRIPTIONS]; + let interface_writes: Vec<_> = constructed + .iter() + .filter(|op| matches!(op, RegistryDelta::ChangeNetworkAttribute { delta, .. } if interface_keys.contains(&delta.key.as_str()))) + .collect(); + assert!(interface_writes.is_empty(), "the catalog interface should not be stored: {interface_writes:#?}"); + + let (name, _) = editor.active_document().network_interface.displayed_input_name_and_description(&merge, 0, &[]); + assert!(!name.is_empty()); +} diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/frontend.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/frontend.rs index 1fdf0a03c01..bb600723bd0 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/frontend.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/frontend.rs @@ -118,17 +118,13 @@ impl NodeNetworkInterface { }) } - /// Returns the input name to display in the Inspector. If the name is empty then the type is used. + /// Returns the input name to display in the Inspector. If the input has no name then its type is used. pub fn displayed_input_name_and_description(&self, node_id: &NodeId, input_index: usize, network_path: &[NodeId]) -> (String, String) { - let Some(input_metadata) = self.persistent_input_metadata(node_id, input_index, network_path) else { - log::warn!("input metadata not found in displayed_input_name_and_description"); - return (String::new(), String::new()); - }; - let description = input_metadata.input_description.to_string(); - let name = if input_metadata.input_name.is_empty() { - self.input_type(&InputConnector::node_at_index(*node_id, input_index), network_path).resolved_type_node_string() - } else { - input_metadata.input_name.to_string() + let input_metadata = self.persistent_input_metadata(node_id, input_index, network_path); + let description = input_metadata.map(|input_metadata| input_metadata.input_description.to_string()).unwrap_or_default(); + let name = match input_metadata.map(|input_metadata| input_metadata.input_name.as_str()) { + Some(name) if !name.is_empty() => name.to_string(), + _ => self.input_type(&InputConnector::node_at_index(*node_id, input_index), network_path).resolved_type_node_string(), }; (name, description) } @@ -151,7 +147,7 @@ impl NodeNetworkInterface { established_type_name(&self.output_type(&OutputConnector::Import(import_index), network_path)).unwrap_or_else(|| format!("Import #{import_index}")) } - /// The description of a subgraph's import, which its encapsulating node stores for the input. + /// The description of a subgraph's import, from the interface of the network implementing it. pub fn import_description(&self, import_index: usize, network_path: &[NodeId]) -> String { let Some((encapsulating_node_id, encapsulating_path)) = network_path.split_last() else { return String::new(); diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/mutations.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/mutations.rs index 613d501cda5..b25abf5cbf0 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/mutations.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/mutations.rs @@ -466,7 +466,7 @@ impl NodeNetworkInterface { Some(old_inputs) } - /// Used when opening an old document to add the persistent metadata for each input if it doesn't exist, which is where the name/description are saved. + /// Used when opening an old document to give the interface of the subgraph a node names one metadata entry per input. pub fn validate_input_metadata(&mut self, node_id: &NodeId, node: &DocumentNode, network_path: &[NodeId]) { let number_of_inputs = node.inputs.len(); let definition = self.reference(node_id, network_path).as_ref().and_then(resolve_document_node_type); @@ -493,8 +493,8 @@ impl NodeNetworkInterface { node.resize_output_names(number_of_exports); } - /// Used when opening a `.graphite` document to update the descriptions each node stores a copy of: "TODO" input descriptions take the definition's - /// text (or become empty where the node no longer matches it), and a node saved without any output descriptions takes the definition's. + /// Used when opening a `.graphite` document to update the descriptions in the interface of the subgraph a node names: "TODO" input descriptions take + /// the definition's text (or become empty where the node no longer matches it), and a subgraph saved without any output descriptions takes the definition's. pub fn validate_descriptions(&mut self, node_id: &NodeId, node: &DocumentNode, network_path: &[NodeId]) { let definition_template = self .reference(node_id, network_path) @@ -1205,16 +1205,6 @@ impl NodeNetworkInterface { self.invalidate_node_appearance(node_id, network_path); } - /// Replaces the full list of output port names for a node. Used by document migrations that turn a single-output node - /// into a multi-output one, since the port labels are otherwise unnamed and fall back to the type name. - pub fn set_output_names(&mut self, node_id: &NodeId, output_names: Vec, network_path: &[NodeId]) { - let Some(mut node) = self.node_interface_mut(NodeLocator::new(*node_id, network_path)) else { - log::error!("Could not get node {node_id} in set_output_names"); - return; - }; - node.set_output_names(output_names); - } - pub fn set_import_export_name(&mut self, name: String, index: ImportOrExport, network_path: &[NodeId]) { let Some((encapsulating_node_id, encapsulating_network_path)) = network_path.split_last() else { log::error!("Could not get encapsulating network in set_import_export_name"); diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/queries.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/queries.rs index 9f5fd8f6e62..03514443c1d 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/queries.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/queries.rs @@ -489,8 +489,10 @@ impl NodeNetworkInterface { self.order_definitions_named_by(id, &mut visited, &mut ordered); } + // A catalog entry is the shipped network, which the paste takes from the catalog ordered .into_iter() + .filter(|id| !catalog::is_protected(*id)) .filter_map(|id| { Some(CopiedDefinition { id, diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/reconcile.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/reconcile.rs index 958f9fbfa04..6537695329c 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/reconcile.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/reconcile.rs @@ -6,10 +6,10 @@ use std::collections::{BTreeSet, HashMap}; use document_graph_storage::blob::BlobBytes; use document_graph_storage::to_runtime::ConversionError; use document_graph_storage::{Declarations, NetworkId as StorageNetworkId, NodeId as StorageNodeId, Registry, RuntimeProjection, Touched}; -use graph_craft::document::{DocumentNode, DocumentNodeImplementation, NetworkId, NetworkLocation, NodeId, NodeInput, NodeNetwork}; +use graph_craft::document::{DocumentNode, NetworkId, NetworkLocation, NodeId, NodeInput, NodeNetwork}; use graph_craft::runtime_delta::RuntimeDelta; -use super::storage_metadata::{InterfaceRebuildError, input_metadata_entry_to_runtime, node_metadata_from_projection}; +use super::storage_metadata::{input_metadata_entry_to_runtime, node_metadata_from_projection}; use super::{DocumentNodePersistentMetadata, EditorDelta, NetworkMetadataChange, NodeMetadataChange, NodeNetworkInterface}; #[derive(Debug, thiserror::Error)] @@ -24,8 +24,6 @@ pub enum ReconcileError { ScopeInjectionsChanged(NetworkLocation), #[error(transparent)] Conversion(ConversionError), - #[error(transparent)] - Metadata(#[from] InterfaceRebuildError), } impl From for ReconcileError { @@ -101,6 +99,7 @@ impl NodeNetworkInterface { ..Default::default() }; self.apply_reconciled(location, vec![EditorDelta::Graph(RuntimeDelta::AddNetwork { id, network: Box::new(added) })], reconciled); + self.adopt_catalog_interface(id); networks.insert(network); for node in projection.network_nodes(network) { @@ -144,7 +143,7 @@ impl NodeNetworkInterface { if current_metadata.pinned_node_order != target.metadata.pinned_order { deltas.push(change(NetworkMetadataChange::PinnedOrder(target.metadata.pinned_order.clone()))); } - // An empty list states nothing, since an older document keeps its interface on the instances + // An empty list states nothing, since a catalog entry's interface comes from the catalog rather than storage let import_metadata: Vec<_> = target.metadata.import_metadata.iter().cloned().map(input_metadata_entry_to_runtime).collect(); if !import_metadata.is_empty() && current_metadata.import_metadata != import_metadata { deltas.push(change(NetworkMetadataChange::ImportMetadata(import_metadata))); @@ -185,7 +184,7 @@ impl NodeNetworkInterface { if !self.holds_network(location) { return Err(ReconcileError::NetworkMissing(location)); } - let (node, metadata) = node_metadata_from_projection(projection.node(id)?)?; + let (node, metadata) = node_metadata_from_projection(projection.node(id)?); let deltas = vec![ EditorDelta::Graph(RuntimeDelta::AddNode { network: location, @@ -201,7 +200,7 @@ impl NodeNetworkInterface { self.apply_reconciled(location, deltas, reconciled); } (true, true) => { - let (node, metadata) = node_metadata_from_projection(projection.node(id)?)?; + let (node, metadata) = node_metadata_from_projection(projection.node(id)?); self.diff_node(location, local_id, node, metadata, reconciled); } } @@ -293,25 +292,13 @@ fn field_deltas( node_id: local_id, change, }; - let input_metadata = || EditorDelta::NodeInputMetadata { - network: location, - node_id: local_id, - input_metadata: target_metadata.input_metadata.clone(), - }; - - // An instance's input metadata belongs to its implementing network - let instance = matches!(target.implementation, DocumentNodeImplementation::Definition(_)); - - // Slots first, so the metadata indexing them finds the right length + // A node's input metadata belongs to what implements it, so only the slots themselves are reconciled here if current.inputs.len() != target.inputs.len() { deltas.push(EditorDelta::Graph(RuntimeDelta::SetInputs { network: location, node_id: local_id, inputs: target.inputs.clone(), })); - if !instance { - deltas.push(input_metadata()); - } } else { for (input_index, (held, wanted)) in current.inputs.iter().zip(&target.inputs).enumerate() { if held != wanted { @@ -323,9 +310,6 @@ fn field_deltas( })); } } - if !instance && current_metadata.input_metadata != target_metadata.input_metadata { - deltas.push(input_metadata()); - } } if current.visible != target.visible { @@ -355,12 +339,6 @@ fn field_deltas( if current_metadata.pinned != target_metadata.pinned { deltas.push(metadata(NodeMetadataChange::Pinned(target_metadata.pinned))); } - if current_metadata.output_names != target_metadata.output_names { - deltas.push(metadata(NodeMetadataChange::OutputNames(target_metadata.output_names.clone()))); - } - if current_metadata.output_descriptions != target_metadata.output_descriptions { - deltas.push(metadata(NodeMetadataChange::OutputDescriptions(target_metadata.output_descriptions.clone()))); - } deltas } diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/storage_metadata.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/storage_metadata.rs index 3bbc5d7e546..8dc566b781f 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/storage_metadata.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/storage_metadata.rs @@ -10,7 +10,7 @@ use std::collections::{BTreeMap, HashMap}; use document_graph_storage::attr::session; use document_graph_storage::{InputMetadataEntry, NetworkMetadataEntry, NetworkReference, NodeMetadataEntry, NodeMetadataSource, Position, Value, from_value, to_value}; use glam::IVec2; -use graph_craft::document::{DocumentNodeImplementation, NetworkLocation, NodeId, NodeNetwork}; +use graph_craft::document::{NetworkLocation, NodeId, NodeNetwork}; use graphene_std::vector::style::RenderMode; use super::{ @@ -22,13 +22,6 @@ use crate::messages::portfolio::document::overlays::utility_types::OverlaysVisib use crate::messages::portfolio::document::utility_types::misc::SnappingState; use crate::messages::portfolio::document::utility_types::nodes::CollapsedLayers; -/// Fired when the storage entry list disagrees with the converted `NodeNetwork`. -#[derive(Debug, thiserror::Error)] -pub enum InterfaceRebuildError { - #[error("input_metadata length {got} does not match inputs length {expected} for node {node:?} in network {network:?}")] - InputMetadataLengthMismatch { node: NodeId, network: NetworkLocation, expected: usize, got: usize }, -} - /// Per-peer view settings persisted in `session.json` under `ui::doc::*` (viewport view, render mode, /// overlay/ruler visibility, snapping, collapsed layers). Not part of the registry/CRDT/history; see /// [`DocumentSettings::to_view_map`]. @@ -62,13 +55,17 @@ impl StorageMetadataView<'_> { Some(&self.network_persistent(network)?.node_metadata.get(&local_id)?.persistent_metadata) } - fn input_persistent(&self, network: NetworkLocation, local_id: NodeId, input_index: usize) -> Option<&InputPersistentMetadata> { - Some(&self.persistent(network, local_id)?.input_metadata.get(input_index)?.persistent_metadata) - } - fn subnetwork_persistent(&self, network: NetworkLocation) -> Option<&NodeNetworkPersistentMetadata> { self.network_persistent(Some(network?)) } + + /// The metadata holding the subnetwork's interface, unless it is a catalog entry, whose interface the catalog owns. + fn owned_interface(&self, network: NetworkLocation) -> Option<&NodeNetworkPersistentMetadata> { + if network.is_some_and(super::catalog::is_protected) { + return None; + } + self.subnetwork_persistent(network) + } } impl NodeMetadataSource for StorageMetadataView<'_> { @@ -92,44 +89,18 @@ impl NodeMetadataSource for StorageMetadataView<'_> { self.persistent(network, local_id).is_some_and(|p| p.pinned) } - fn input_name(&self, network: NetworkLocation, local_id: NodeId, input_index: usize) -> Option<&str> { - Some(self.input_persistent(network, local_id, input_index)?.input_name.as_str()) - } - - fn input_description(&self, network: NetworkLocation, local_id: NodeId, input_index: usize) -> Option<&str> { - Some(self.input_persistent(network, local_id, input_index)?.input_description.as_str()) - } - - fn widget_override(&self, network: NetworkLocation, local_id: NodeId, input_index: usize) -> Option<&str> { - self.input_persistent(network, local_id, input_index)?.widget_override.as_deref() - } - - fn input_data(&self, network: NetworkLocation, local_id: NodeId, input_index: usize) -> HashMap { - self.input_persistent(network, local_id, input_index) - .map(|p| p.input_data.iter().map(|(key, value)| (key.clone(), value.clone().into())).collect()) - .unwrap_or_default() - } - - fn output_names(&self, network: NetworkLocation, local_id: NodeId) -> Vec { - self.persistent(network, local_id).map(|p| p.output_names.clone()).unwrap_or_default() - } - - fn output_descriptions(&self, network: NetworkLocation, local_id: NodeId) -> Vec { - self.persistent(network, local_id).map(|p| p.output_descriptions.clone()).unwrap_or_default() - } - fn import_metadata(&self, network: NetworkLocation) -> Vec { - self.subnetwork_persistent(network) + self.owned_interface(network) .map(|persistent| persistent.import_metadata.iter().map(input_metadata_entry_from_runtime).collect()) .unwrap_or_default() } fn export_names(&self, network: NetworkLocation) -> Vec { - self.subnetwork_persistent(network).map(|persistent| persistent.export_names.clone()).unwrap_or_default() + self.owned_interface(network).map(|persistent| persistent.export_names.clone()).unwrap_or_default() } fn export_descriptions(&self, network: NetworkLocation) -> Vec { - self.subnetwork_persistent(network).map(|persistent| persistent.export_descriptions.clone()).unwrap_or_default() + self.owned_interface(network).map(|persistent| persistent.export_descriptions.clone()).unwrap_or_default() } fn reference(&self, network: NetworkLocation) -> Option { @@ -206,17 +177,19 @@ pub fn position_to_runtime(position: Position, is_layer: bool) -> NodeTypePersis } /// Builds a `NodeNetworkInterface` from a `NodeNetwork` plus the metadata vecs `Registry::to_runtime_with_full_metadata` emits. -pub fn build_interface_from_storage(network: NodeNetwork, node_entries: Vec, network_entries: Vec) -> Result { +pub fn build_interface_from_storage(network: NodeNetwork, node_entries: Vec, network_entries: Vec) -> NodeNetworkInterface { let mut network_metadata = NodeNetworkMetadata::default(); seed_metadata_tree(&network, &mut network_metadata); - apply_entries_into_tree(&network, &mut network_metadata, node_entries)?; + apply_entries_into_tree(&mut network_metadata, node_entries); apply_network_entries_into_tree(&mut network_metadata, network_entries); - Ok(NodeNetworkInterface::from_trees(network, network_metadata)) + let mut interface = NodeNetworkInterface::from_trees(network, network_metadata); + interface.adopt_catalog_interfaces(); + interface } /// The node and its metadata from a single-node projection, patched like a whole rebuild's entry. -pub(super) fn node_metadata_from_projection(projected: document_graph_storage::ProjectedNode) -> Result<(graph_craft::document::DocumentNode, DocumentNodePersistentMetadata), InterfaceRebuildError> { +pub(super) fn node_metadata_from_projection(projected: document_graph_storage::ProjectedNode) -> (graph_craft::document::DocumentNode, DocumentNodePersistentMetadata) { let document_graph_storage::ProjectedNode { local_id, node, metadata: entry, .. } = projected; let entry = NodeMetadataEntry { network: None, ..entry }; @@ -225,16 +198,11 @@ pub(super) fn node_metadata_from_projection(projected: document_graph_storage::P scratch.nodes.insert(local_id, node); let mut metadata = NodeNetworkMetadata::default(); seed_metadata_tree(&scratch, &mut metadata); - apply_entries_into_tree(&scratch, &mut metadata, vec![entry])?; + apply_entries_into_tree(&mut metadata, vec![entry]); - let mut persistent = metadata.persistent_metadata.node_metadata.remove(&local_id).map(|node| node.persistent_metadata).unwrap_or_default(); + let persistent = metadata.persistent_metadata.node_metadata.remove(&local_id).map(|node| node.persistent_metadata).unwrap_or_default(); let node = scratch.nodes.remove(&local_id).expect("inserted above"); - if matches!(node.implementation, DocumentNodeImplementation::Definition(_)) { - persistent.input_metadata.clear(); - persistent.output_names.clear(); - persistent.output_descriptions.clear(); - } - Ok((node, persistent)) + (node, persistent) } /// Maps each runtime network location to its stable `NetworkId`, from the entries `to_runtime_with_full_metadata` returns. @@ -369,8 +337,7 @@ fn seed_network_metadata(network: &NodeNetwork, metadata: &mut NodeNetworkMetada /// Patches entries onto the metadata tree previously seeded by [`seed_metadata_tree`]. /// Entries with a stale `(network, local_id)` are skipped with a warning. -/// An `input_metadata` length mismatch returns an `InterfaceRebuildError`. -fn apply_entries_into_tree(network: &NodeNetwork, metadata: &mut NodeNetworkMetadata, entries: Vec) -> Result<(), InterfaceRebuildError> { +fn apply_entries_into_tree(metadata: &mut NodeNetworkMetadata, entries: Vec) { // Group entries by network so we do one nested_metadata_mut lookup per network. let mut by_network: HashMap> = HashMap::new(); for entry in entries { @@ -378,8 +345,6 @@ fn apply_entries_into_tree(network: &NodeNetwork, metadata: &mut NodeNetworkMeta } for (location, entries) in by_network { - let nested_network = network.located_network(location); - let Some(network_metadata) = metadata.nested_metadata_mut(location) else { log::warn!("apply_entries_into_tree: network at {location:?} not found, skipping {} entries", entries.len()); continue; @@ -405,33 +370,9 @@ fn apply_entries_into_tree(network: &NodeNetwork, metadata: &mut NodeNetworkMeta persistent.locked = entry.locked; persistent.pinned = entry.pinned; - // The converted runtime is the source of truth for input count; drift indicates a bug worth surfacing. - let expected_inputs = nested_network.and_then(|net| net.nodes.get(&entry.local_id)).map(|doc_node| doc_node.inputs.len()); - if let Some(expected) = expected_inputs - && expected != entry.input_metadata.len() - { - return Err(InterfaceRebuildError::InputMetadataLengthMismatch { - node: entry.local_id, - network: location, - expected, - got: entry.input_metadata.len(), - }); - } - - persistent.input_metadata = entry.input_metadata.into_iter().map(input_metadata_entry_to_runtime).collect(); - - if !entry.output_names.is_empty() { - persistent.output_names = entry.output_names; - } - if !entry.output_descriptions.is_empty() { - persistent.output_descriptions = entry.output_descriptions; - } - document_node_metadata.transient_metadata = DocumentNodeTransientMetadata::default(); } } - - Ok(()) } pub(super) fn input_metadata_entry_from_runtime(input_metadata: &InputMetadata) -> InputMetadataEntry { diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/store/cursor.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/store/cursor.rs index 40f3adf81b3..ce3d06883d3 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/store/cursor.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/store/cursor.rs @@ -71,17 +71,6 @@ impl<'m> InterfaceMut<'m> { output_descriptions: &mut metadata.export_descriptions, } } - - fn of_node(metadata: &'m mut DocumentNodePersistentMetadata, definition_metadata: &'m mut Option<&mut NodeNetworkPersistentMetadata>) -> Self { - if let Some(definition_metadata) = definition_metadata { - return Self::of_network(definition_metadata); - } - Self { - input_metadata: &mut metadata.input_metadata, - output_names: &mut metadata.output_names, - output_descriptions: &mut metadata.output_descriptions, - } - } } /// Where a node's writes record what they changed, carrying the node's address so each setter names @@ -109,14 +98,6 @@ impl NodeDeltas<'_> { self.deltas.push(EditorDelta::Graph(delta)); } - fn input_metadata(&mut self, input_metadata: Vec) { - self.deltas.push(EditorDelta::NodeInputMetadata { - network: self.network(), - node_id: self.node_id, - input_metadata, - }); - } - fn snapshot(&mut self, metadata: Box) { self.deltas.push(EditorDelta::NodeMetadataSnapshot { network: self.network(), @@ -252,28 +233,25 @@ impl NodeMut<'_> { changed } - pub(crate) fn interface(&mut self) -> InterfaceMut<'_> { - InterfaceMut::of_node(self.metadata, &mut self.definition_metadata) + /// The table entry the node's interface is stored in, or `None` for a node whose definition owns its interface. + pub(crate) fn interface(&mut self) -> Option> { + self.definition_metadata.as_deref_mut().map(InterfaceMut::of_network) } pub(crate) fn set_input_name(&mut self, input_index: usize, input_name: String) -> bool { - let Some(input_metadata) = self.interface().input_metadata.get_mut(input_index) else { + let Some(input_metadata) = self.interface().and_then(|interface| interface.input_metadata.get_mut(input_index)) else { return false; }; let changed = input_metadata.persistent_metadata.input_name != input_name; input_metadata.persistent_metadata.input_name = input_name; if changed { - let change = NodeMetadataChange::InputName { - index: input_index, - name: input_metadata.persistent_metadata.input_name.clone(), - }; - self.record_input_change(change); + self.emit_input_metadata(); } changed } pub(crate) fn set_input_description(&mut self, input_index: usize, input_description: String) -> bool { - let Some(input_metadata) = self.interface().input_metadata.get_mut(input_index) else { + let Some(input_metadata) = self.interface().and_then(|interface| interface.input_metadata.get_mut(input_index)) else { return false; }; let changed = input_metadata.persistent_metadata.input_description != input_description; @@ -287,23 +265,20 @@ impl NodeMut<'_> { /// The identifier of the widget override the Inspector uses for this input, or `None` for the /// widget generated from its type. pub(crate) fn set_widget_override(&mut self, input_index: usize, widget_override: Option) -> bool { - let Some(input_metadata) = self.interface().input_metadata.get_mut(input_index) else { + let Some(input_metadata) = self.interface().and_then(|interface| interface.input_metadata.get_mut(input_index)) else { return false; }; let changed = input_metadata.persistent_metadata.widget_override != widget_override; input_metadata.persistent_metadata.widget_override = widget_override; if changed { - let change = NodeMetadataChange::WidgetOverride { - index: input_index, - widget_override: input_metadata.persistent_metadata.widget_override.clone(), - }; - self.record_input_change(change); + self.emit_input_metadata(); } changed } pub(crate) fn set_output_name(&mut self, output_index: usize, output_name: String) -> bool { - let output_names = self.interface().output_names; + let Some(interface) = self.interface() else { return false }; + let output_names = interface.output_names; let Some(existing) = output_names.get_mut(output_index) else { return false }; let changed = *existing != output_name; *existing = output_name; @@ -314,21 +289,8 @@ impl NodeMut<'_> { changed } - /// The encapsulating node's names for every one of the network's exports, replaced together since - /// they are stored as one array. - pub(crate) fn set_output_names(&mut self, output_names: Vec) -> bool { - let existing = self.interface().output_names; - let changed = *existing != output_names; - *existing = output_names; - if changed { - let output_names = existing.clone(); - self.record_output_names(output_names); - } - changed - } - pub(crate) fn set_output_description(&mut self, output_index: usize, output_description: String) -> bool { - let interface = self.interface(); + let Some(interface) = self.interface() else { return false }; if output_index >= interface.output_names.len() { return false; } @@ -350,7 +312,8 @@ impl NodeMut<'_> { /// The encapsulating node's descriptions for the network's exports, replaced together since they are stored as one array. pub(crate) fn set_output_descriptions(&mut self, output_descriptions: Vec) -> bool { - let existing = self.interface().output_descriptions; + let Some(interface) = self.interface() else { return false }; + let existing = interface.output_descriptions; let changed = *existing != output_descriptions; *existing = output_descriptions; if changed { @@ -368,38 +331,27 @@ impl NodeMut<'_> { } } - fn record_input_change(&mut self, change: NodeMetadataChange) { - match self.interface_network() { - Some(_) => self.emit_input_metadata(), - None => self.deltas.metadata(change), - } - } - fn record_output_names(&mut self, output_names: Vec) { - match self.interface_network() { - Some(network) => self.deltas.owned_network(network, NetworkMetadataChange::ExportNames(output_names)), - None => self.deltas.metadata(NodeMetadataChange::OutputNames(output_names)), + if let Some(network) = self.interface_network() { + self.deltas.owned_network(network, NetworkMetadataChange::ExportNames(output_names)); } } fn record_output_descriptions(&mut self, output_descriptions: Vec) { - match self.interface_network() { - Some(network) => self.deltas.owned_network(network, NetworkMetadataChange::ExportDescriptions(output_descriptions)), - None => self.deltas.metadata(NodeMetadataChange::OutputDescriptions(output_descriptions)), + if let Some(network) = self.interface_network() { + self.deltas.owned_network(network, NetworkMetadataChange::ExportDescriptions(output_descriptions)); } } - /// The metadata array the node's inputs index, one entry per input. + /// The metadata array the node's inputs index, which only a node naming a table entry stores. pub(crate) fn input_metadata(&self) -> &[InputMetadata] { - if let Some(definition_metadata) = &self.definition_metadata { - return &definition_metadata.import_metadata; - } - &self.metadata.input_metadata + self.definition_metadata.as_ref().map_or(&[], |definition_metadata| &definition_metadata.import_metadata) } /// Grows `output_names` to one entry per export, which an older document may be missing. pub(crate) fn resize_output_names(&mut self, number_of_exports: usize) { - let output_names = self.interface().output_names; + let Some(interface) = self.interface() else { return }; + let output_names = interface.output_names; if output_names.len() == number_of_exports { return; } @@ -413,7 +365,8 @@ impl NodeMut<'_> { /// entry for the added index. Restores the parallel-array invariant for an older document. /// Entries past the last input are kept, since they can hold user-authored names and overrides. pub(crate) fn pad_input_metadata(&mut self, number_of_inputs: usize, defaults: impl Fn(usize) -> Option) { - let input_metadata = self.interface().input_metadata; + let Some(interface) = self.interface() else { return }; + let input_metadata = interface.input_metadata; if input_metadata.len() >= number_of_inputs { return; } @@ -424,20 +377,11 @@ impl NodeMut<'_> { } /// Swaps in a new implementation, which never carries an inline network since the store moves one into the definitions table first. - /// - /// A table entry keeps the interface it already has. Any other implementation takes over the node's previous interface. + /// The new implementation brings its own interface, from its definition or the table entry it names. pub(crate) fn replace_implementation(&mut self, implementation: DocumentNodeImplementation) { - let carried = (!matches!(implementation, DocumentNodeImplementation::Definition(_))).then(|| { - let previous = self.interface(); - (previous.input_metadata.clone(), previous.output_names.clone(), previous.output_descriptions.clone()) - }); - self.node.implementation = implementation; self.metadata.network_metadata = None; self.definition_metadata = None; - self.metadata.input_metadata.clear(); - self.metadata.output_names.clear(); - self.metadata.output_descriptions.clear(); let delta = RuntimeDelta::ReplaceNode { network: self.deltas.network(), @@ -445,12 +389,6 @@ impl NodeMut<'_> { node: Box::new(self.node.clone()), }; self.deltas.graph(delta); - - if let Some((input_metadata, output_names, output_descriptions)) = carried { - self.set_input_metadata(input_metadata); - self.set_output_names(output_names); - self.set_output_descriptions(output_descriptions); - } } /// Replaces the node's whole persistent metadata, including that of everything nested under it, @@ -462,15 +400,11 @@ impl NodeMut<'_> { self.deltas.snapshot(snapshot); } - /// Replaces the metadata array the node's inputs index, leaving the inputs themselves alone. - pub(crate) fn set_input_metadata(&mut self, input_metadata: Vec) { - *self.interface().input_metadata = input_metadata; - self.emit_input_metadata(); - } - - /// Swaps in new inputs together with their metadata, returning the inputs replaced. + /// Swaps in new inputs together with their metadata, returning the inputs replaced. The metadata is kept only where the node stores its interface. pub(crate) fn replace_inputs(&mut self, inputs: Vec, input_metadata: Vec) -> Vec { - *self.interface().input_metadata = input_metadata; + if let Some(interface) = self.interface() { + *interface.input_metadata = input_metadata; + } let previous = std::mem::replace(&mut self.node.inputs, inputs); self.emit_inputs(); @@ -511,11 +445,9 @@ impl NodeMut<'_> { } fn emit_input_metadata(&mut self) { + let Some(network) = self.interface_network() else { return }; let input_metadata = self.input_metadata().to_vec(); - match self.interface_network() { - Some(network) => self.deltas.owned_network(network, NetworkMetadataChange::ImportMetadata(input_metadata)), - None => self.deltas.input_metadata(input_metadata), - } + self.deltas.owned_network(network, NetworkMetadataChange::ImportMetadata(input_metadata)); } } diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/store/mod.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/store/mod.rs index 43fc059057c..78bfcf65d61 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/store/mod.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/store/mod.rs @@ -45,8 +45,7 @@ impl NodeNetworkInterface { /// Builds an interface around a freshly-assembled pair of trees, for a document arriving from /// storage or from an older format rather than being edited into existence. Nothing is recorded: the /// trees are the document, not a set of changes to it. - pub(super) fn from_trees(network: NodeNetwork, mut network_metadata: NodeNetworkMetadata) -> Self { - network_metadata.move_interface_metadata_into_networks(&network); + pub(super) fn from_trees(network: NodeNetwork, network_metadata: NodeNetworkMetadata) -> Self { Self { network: Guarded::new(MemoNetwork::new(network)), network_metadata: Guarded::new(network_metadata), @@ -408,17 +407,26 @@ impl NodeNetworkInterface { log::error!("Could not locate network {:?} in replace_node_implementation", locator.network_path); return; }; + let Ok(view) = self.view(locator.network_path) else { + log::error!("Could not view network {:?} in replace_node_implementation", locator.network_path); + return; + }; + + // Nothing goes into the definitions table for a node that isn't there + if view.node(&node_id).is_err() { + log::error!("Could not get node {node_id} in replace_node_implementation"); + return; + } if matches!(implementation, DocumentNodeImplementation::Network(_)) { - let Some(mut node) = self.node_mut_at(location, node_id) else { - log::error!("Could not get node {node_id} in replace_node_implementation"); - return; - }; - let previous = node.interface(); + // The network takes the interface the node had, whether its definition's or the one its table entry stored + let previous = ( + view.input_metadata(&node_id).map(<[_]>::to_vec).unwrap_or_default(), + view.output_names(&node_id).map(<[_]>::to_vec).unwrap_or_default(), + view.output_descriptions(&node_id).map(<[_]>::to_vec).unwrap_or_default(), + ); let interface = &mut network_metadata.get_or_insert_default().persistent_metadata; - interface.import_metadata = previous.input_metadata.clone(); - interface.export_names = previous.output_names.clone(); - interface.export_descriptions = previous.output_descriptions.clone(); + (interface.import_metadata, interface.export_names, interface.export_descriptions) = previous; self.lift_into_table(&mut implementation, &mut network_metadata); } @@ -519,11 +527,12 @@ impl NodeNetworkInterface { /// Nothing is added yet: each entry goes in once a node names it, so it lands in the same transaction as that node. /// /// Returns `None` for a list that names an entry before listing it, which no copy produces and which a cycle always does. + /// A catalog entry is never listed, since a paste takes it from the catalog. pub(crate) fn carry_definitions(&mut self, definitions: Vec) -> Option> { let mut listed = HashSet::new(); for definition in &definitions { let names_unlisted = definition.network.nodes.values().any(|node| match node.implementation { - DocumentNodeImplementation::Definition(named) => !listed.contains(&named) && !self.document_network().definitions.contains_key(&named), + DocumentNodeImplementation::Definition(named) => !listed.contains(&named) && !self.document_network().definitions.contains_key(&named) && !catalog::is_protected(named), _ => false, }); if names_unlisted { @@ -543,14 +552,17 @@ impl NodeNetworkInterface { } } - // A catalog ID always means the shipped network - let shipped = catalog::entry(id).map(|shipped| (shipped.network, shipped.metadata)); - let held = shipped.or_else(|| { - let network = self.document_network().definitions.get(&id)?.clone(); - let metadata = self.network_metadata.persistent_metadata.definitions.get(&id)?.clone(); - Some((network, metadata)) - }); - let diverged = held.is_some_and(|(held_network, held_metadata)| held_network != network || !same_content(&held_metadata.persistent_metadata, &metadata.persistent_metadata)); + // A catalog ID always means the shipped network, whose interface is the catalog's whatever the copy carries + let diverged = match catalog::entry(id) { + Some(shipped) => shipped.network != network || !same_content_as_shipped(&shipped.metadata.persistent_metadata, &metadata.persistent_metadata), + None => { + let held_network = self.document_network().definitions.get(&id); + let held_metadata = self.network_metadata.persistent_metadata.definitions.get(&id); + held_network + .zip(held_metadata) + .is_some_and(|(held_network, held_metadata)| *held_network != network || !same_content(&held_metadata.persistent_metadata, &metadata.persistent_metadata)) + } + }; let id = if diverged { let new_id = NetworkId::new(); rekey_nodes(new_id, &mut network, &mut metadata.persistent_metadata); @@ -589,12 +601,12 @@ impl NodeNetworkInterface { Some(unique) } - /// Adds `id` from the last paste or the catalog if the document lacks it. + /// Adds `id` from the catalog, or else the last paste, if the document lacks it. fn insert_missing_definition(&mut self, id: NetworkId) { if self.document_network().definitions.contains_key(&id) { return; } - let Some(CopiedDefinition { network, metadata, .. }) = self.carried_definitions.get(&id).cloned().or_else(|| catalog::entry(id)) else { + let Some(CopiedDefinition { network, metadata, .. }) = catalog::entry(id).or_else(|| self.carried_definitions.get(&id).cloned()) else { return; }; self.insert_definition(id, network, metadata); diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/template.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/template.rs index c5ed3bde941..f74cd798691 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/template.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/template.rs @@ -13,7 +13,7 @@ use graphene_std::Context; #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] #[serde(from = "NodeTemplateParts", into = "NodeTemplateParts")] pub struct NodeTemplate { - /// The graph data inputs to the node. Kept the same length as `input_metadata` by [`Self::normalize_input_metadata`] and [`Self::into_parts`]. + /// The graph data inputs to the node. pub inputs: Vec, /// Type of the argument which this node can be evaluated with. pub call_argument: Type, @@ -23,7 +23,8 @@ pub struct NodeTemplate { pub visible: bool, /// A name chosen by the user for this instance of the node. Empty indicates no given name, in which case the implementation name is displayed in italics. pub display_name: String, - /// Metadata to override the Inspector widgets for each input. Kept the same length as `inputs`. + /// Metadata to override the Inspector widgets for each input. This and the output names and descriptions are kept only by a definition + /// and an inline network, since any other node takes its interface from its definition or the table entry it names. pub input_metadata: Vec, pub output_names: Vec, /// Indexed like `output_names`, where a missing or empty entry means the output has no description. @@ -112,23 +113,20 @@ impl NodeTemplate { } = document_node; let DocumentNodePersistentMetadata { display_name, - mut input_metadata, - mut output_names, - mut output_descriptions, locked, pinned, node_type_metadata, network_metadata, } = persistent_node_metadata; + // Only an inline network carries its interface, which belongs to the definition or table entry of any other node + let (mut input_metadata, mut output_names, mut output_descriptions) = (Vec::new(), Vec::new(), Vec::new()); let implementation = match implementation { DocumentNodeImplementation::Network(network) => { let mut nested_persistent = network_metadata.map(|metadata| metadata.persistent_metadata).unwrap_or_default(); - if !nested_persistent.import_metadata.is_empty() || !nested_persistent.export_names.is_empty() { - input_metadata = std::mem::take(&mut nested_persistent.import_metadata); - output_names = std::mem::take(&mut nested_persistent.export_names); - output_descriptions = std::mem::take(&mut nested_persistent.export_descriptions); - } + input_metadata = std::mem::take(&mut nested_persistent.import_metadata); + output_names = std::mem::take(&mut nested_persistent.export_names); + output_descriptions = std::mem::take(&mut nested_persistent.export_descriptions); // Pair each nested node with its metadata by ID, recursively let mut nodes = HashMap::with_capacity(network.nodes.len()); @@ -185,20 +183,16 @@ impl NodeTemplate { let (implementation, mut network_metadata) = implementation.into_parts(); - // The stored metadata invariant requires exactly one input metadata entry per input - input_metadata.resize_with(inputs.len(), InputMetadata::default); - + // An inline network takes the template's interface, while any other node's belongs to its definition or the table entry it names if let Some(network_metadata) = &mut network_metadata { + // The stored metadata invariant requires exactly one input metadata entry per input + input_metadata.resize_with(inputs.len(), InputMetadata::default); + let persistent_metadata = &mut network_metadata.persistent_metadata; persistent_metadata.import_metadata = std::mem::take(&mut input_metadata); persistent_metadata.export_names = std::mem::take(&mut output_names); persistent_metadata.export_descriptions = std::mem::take(&mut output_descriptions); } - if matches!(implementation, DocumentNodeImplementation::Definition(_)) { - input_metadata.clear(); - output_names.clear(); - output_descriptions.clear(); - } let document_node = DocumentNode { inputs, @@ -210,9 +204,6 @@ impl NodeTemplate { }; let persistent_node_metadata = DocumentNodePersistentMetadata { display_name, - input_metadata, - output_names, - output_descriptions, locked, pinned, node_type_metadata, @@ -346,6 +337,7 @@ pub fn collect_template_resources(template: &NodeTemplate, out: &mut HashSet bool { + let content = |metadata: &NodeNetworkPersistentMetadata| { + let mut content = metadata.clone(); + clear_view_state(&mut content); + content.import_metadata.clear(); + content.export_names.clear(); + content.export_descriptions.clear(); + content + }; + content(shipped) == content(metadata) +} + fn clear_view_state(metadata: &mut NodeNetworkPersistentMetadata) { metadata.previewing = Previewing::No; metadata.navigation_metadata = NavigationMetadata::default(); @@ -575,22 +588,6 @@ impl NodeNetworkMetadata { } } - pub fn move_interface_metadata_into_networks(&mut self, document_network: &NodeNetwork) { - let mut instance_interfaces = Vec::new(); - move_interface_metadata(document_network, self, &mut instance_interfaces); - for (id, definition_metadata) in &mut self.persistent_metadata.definitions { - if let Some(definition) = document_network.definitions.get(id) { - move_interface_metadata(definition, definition_metadata, &mut instance_interfaces); - } - } - - for (id, interface) in instance_interfaces { - if let Some(definition_metadata) = self.persistent_metadata.definitions.get_mut(&id) { - fill_empty_interface(&mut definition_metadata.persistent_metadata, interface); - } - } - } - /// Moves the metadata of each network that [`NodeNetwork::lift_nested_networks`] lifted into the definitions table. pub fn lift_nested_metadata(&mut self, lifted: &[LiftedNetwork]) { for entry in lifted { @@ -630,35 +627,30 @@ impl NodeNetworkMetadata { } } -type InterfaceMetadata = (Vec, Vec, Vec); +/// Serializes the definitions table without each catalog entry's interface, which the catalog owns rather than the document. +fn serialize_definitions(definitions: &HashMap, serializer: S) -> Result { + use serde::ser::SerializeMap; -fn move_interface_metadata(network: &NodeNetwork, metadata: &mut NodeNetworkMetadata, instance_interfaces: &mut Vec<(NetworkId, InterfaceMetadata)>) { - for (node_id, node) in &network.nodes { - let Some(node_metadata) = metadata.persistent_metadata.node_metadata.get_mut(node_id) else { + let mut map = serializer.serialize_map(Some(definitions.len()))?; + for (id, metadata) in definitions { + if !super::catalog::is_protected(*id) { + map.serialize_entry(id, metadata)?; continue; - }; - let DocumentNodePersistentMetadata { - input_metadata, - output_names, - output_descriptions, - network_metadata, - .. - } = &mut node_metadata.persistent_metadata; - let mut take_interface = || (std::mem::take(input_metadata), std::mem::take(output_names), std::mem::take(output_descriptions)); - - match &node.implementation { - DocumentNodeImplementation::Network(nested) => { - let Some(nested_metadata) = network_metadata else { continue }; - fill_empty_interface(&mut nested_metadata.persistent_metadata, take_interface()); - move_interface_metadata(nested, nested_metadata, instance_interfaces); - } - DocumentNodeImplementation::Definition(id) => instance_interfaces.push((*id, take_interface())), - DocumentNodeImplementation::ProtoNode(_) | DocumentNodeImplementation::Extract => {} } + + let mut metadata = metadata.clone(); + let persistent = &mut metadata.persistent_metadata; + persistent.import_metadata.clear(); + persistent.export_names.clear(); + persistent.export_descriptions.clear(); + map.serialize_entry(id, &metadata)?; } + map.end() } -fn fill_empty_interface(network_metadata: &mut NodeNetworkPersistentMetadata, (import_metadata, export_names, export_descriptions): InterfaceMetadata) { +pub(super) type InterfaceMetadata = (Vec, Vec, Vec); + +pub(super) fn fill_empty_interface(network_metadata: &mut NodeNetworkPersistentMetadata, (import_metadata, export_names, export_descriptions): InterfaceMetadata) { if network_metadata.import_metadata.is_empty() { network_metadata.import_metadata = import_metadata; } @@ -693,7 +685,7 @@ pub struct NodeNetworkPersistentMetadata { /// Stack of selection snapshots for future history states. #[serde(skip)] pub selection_redo_history: VecDeque, - #[serde(default, skip_serializing_if = "HashMap::is_empty")] + #[serde(default, skip_serializing_if = "HashMap::is_empty", serialize_with = "serialize_definitions")] pub definitions: HashMap, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub import_metadata: Vec, @@ -942,9 +934,7 @@ pub enum WidgetOverride { // TODO: Custom deserialization/serialization to ensure number of input metadata rows matches number of node inputs #[derive(Debug, Clone, Default, PartialEq, serde::Serialize, serde::Deserialize)] pub struct InputPersistentMetadata { - /// A general datastore than can store key value pairs of any types for any input - /// Each instance of the input node needs to store its own data, since it can lose the reference to its - /// node definition if the node signature is modified by the user. For example adding/removing/renaming an import/export of a network node. + /// A general datastore that can store key value pairs of any types for any input. #[serde(serialize_with = "graphene_std::vector::serialize_hashmap_as_sorted_object")] pub input_data: HashMap, // An input can override a widget, which would otherwise be automatically generated from the type @@ -1028,13 +1018,6 @@ pub struct DocumentNodePersistentMetadata { /// A name chosen by the user for this instance of the node. Empty indicates no given name, in which case the implementation name is displayed to the user in italics. #[serde(default)] pub display_name: String, - /// Stores metadata to override the widgets in the Inspector for each input. These can either be generated automatically based on the type, or with a custom function. - /// Must match the length of node inputs - pub input_metadata: Vec, - pub output_names: Vec, - /// Indexed like `output_names`, where a missing or empty entry means the output has no description. - #[serde(default)] - pub output_descriptions: Vec, /// Represents the lock icon for locking/unlocking the node in the graph UI. When locked, a node cannot be moved in the graph UI. #[serde(default)] pub locked: bool, diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/validation.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/validation.rs index 254f3082602..7f8a16d8c86 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/validation.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/validation.rs @@ -183,30 +183,24 @@ fn validate_network(root: &mut DocumentRoot, network: &NodeNetwork, network_meta DocumentNodeImplementation::Definition(id) => document_metadata.persistent_metadata.definitions.get(id), _ => None, }; - let (input_metadata, output_names) = match implementation_metadata { - Some(network_metadata) => (&network_metadata.persistent_metadata.import_metadata, &network_metadata.persistent_metadata.export_names), - None => (&persistent.input_metadata, &persistent.output_names), - }; - if matches!(node.implementation, DocumentNodeImplementation::Network(_)) { violations.push(format!("Node {node_id} in network {path:?} implements an inline network, which belongs in the definitions table")); } - let stores_interface = !persistent.input_metadata.is_empty() || !persistent.output_names.is_empty() || !persistent.output_descriptions.is_empty(); - if implementation_metadata.is_some() && stores_interface { - violations.push(format!("Node {node_id} in network {path:?} stores interface metadata that belongs to the network implementing it")); - } - - // Input metadata is a parallel array to the node's inputs and the lengths must stay in sync - if input_metadata.len() != node.inputs.len() { - violations.push(format!( - "Node {node_id} in network {path:?} has {} inputs but {} input metadata entries", - node.inputs.len(), - input_metadata.len() - )); + // A table entry's import metadata is a parallel array to the inputs of each node naming it, so the lengths must stay in sync + if let Some(network_metadata) = implementation_metadata { + let import_metadata = &network_metadata.persistent_metadata.import_metadata; + if import_metadata.len() != node.inputs.len() { + violations.push(format!( + "Node {node_id} in network {path:?} has {} inputs but {} input metadata entries", + node.inputs.len(), + import_metadata.len() + )); + } } let validate_output_names = |export_count: usize, violations: &mut Vec| { + let output_names = implementation_metadata.map_or(&[][..], |network_metadata| &network_metadata.persistent_metadata.export_names); if output_names.len() != export_count { violations.push(format!("Node {node_id} in network {path:?} has {export_count} exports but {} output names", output_names.len())); } diff --git a/editor/src/messages/portfolio/document/utility_types/network_interface/view.rs b/editor/src/messages/portfolio/document/utility_types/network_interface/view.rs index be4cc393938..8ff53bd9f96 100644 --- a/editor/src/messages/portfolio/document/utility_types/network_interface/view.rs +++ b/editor/src/messages/portfolio/document/utility_types/network_interface/view.rs @@ -107,25 +107,42 @@ impl<'a, 'p> NetworkView<'a, 'p> { Ok(network_metadata.persistent_metadata.reference.clone().map(DefinitionIdentifier::Network)) } + /// The definition of a proto or Extract node `node_id`, which owns its signature rather than the node itself. + fn definition_template(&self, node_id: &NodeId) -> Result, NetworkError> { + let definition = match self.implementation(node_id)? { + DocumentNodeImplementation::ProtoNode(identifier) => resolve_proto_node_definition(identifier), + DocumentNodeImplementation::Extract => resolve_document_node_type(&DefinitionIdentifier::Network("Extract".into())), + _ => None, + }; + Ok(definition.map(|definition| &definition.node_template)) + } + + /// The node's input metadata, from its definition or else the network a subgraph names. pub fn input_metadata(&self, node_id: &NodeId) -> Result<&'a [InputMetadata], NetworkError> { - Ok(match self.implementation_metadata(node_id)? { - Some(network_metadata) => &network_metadata.persistent_metadata.import_metadata, - None => &self.node_metadata(node_id)?.persistent_metadata.input_metadata, - }) + if let Some(template) = self.definition_template(node_id)? { + return Ok(&template.input_metadata); + } + Ok(self + .implementation_metadata(node_id)? + .map_or(&[], |network_metadata| &network_metadata.persistent_metadata.import_metadata)) } pub fn output_names(&self, node_id: &NodeId) -> Result<&'a [String], NetworkError> { - Ok(match self.implementation_metadata(node_id)? { - Some(network_metadata) => &network_metadata.persistent_metadata.export_names, - None => &self.node_metadata(node_id)?.persistent_metadata.output_names, - }) + if let Some(template) = self.definition_template(node_id)? { + return Ok(&template.output_names); + } + Ok(self + .implementation_metadata(node_id)? + .map_or(&[], |network_metadata| &network_metadata.persistent_metadata.export_names)) } pub fn output_descriptions(&self, node_id: &NodeId) -> Result<&'a [String], NetworkError> { - Ok(match self.implementation_metadata(node_id)? { - Some(network_metadata) => &network_metadata.persistent_metadata.export_descriptions, - None => &self.node_metadata(node_id)?.persistent_metadata.output_descriptions, - }) + if let Some(template) = self.definition_template(node_id)? { + return Ok(&template.output_descriptions); + } + Ok(self + .implementation_metadata(node_id)? + .map_or(&[], |network_metadata| &network_metadata.persistent_metadata.export_descriptions)) } pub fn is_layer(&self, node_id: &NodeId) -> Result { diff --git a/editor/src/messages/portfolio/document_migration.rs b/editor/src/messages/portfolio/document_migration.rs index af2a48cfe91..48491e3f7a1 100644 --- a/editor/src/messages/portfolio/document_migration.rs +++ b/editor/src/messages/portfolio/document_migration.rs @@ -1019,7 +1019,6 @@ fn legacy_text_node_template() -> Option { let mut template = resolve_document_node_type(&DefinitionIdentifier::ProtoNode(graphene_std::text::text::IDENTIFIER))?.default_node_template(); template.implementation = NodeTemplateImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_std::text::TextNode")); template.inputs.push(NodeInput::value(TaggedValue::Bool(false), false)); - template.input_metadata.push(Default::default()); Some(template) } @@ -1227,16 +1226,9 @@ pub fn document_migration_upgrades(document: &mut DocumentMessageHandler, reset_ }) .collect::>(); for (node_id, network_path, cpu_identifier, inputs) in gpu_twins { - let input_metadata = document - .network_interface - .node_metadata(&node_id, &network_path) - .map(|metadata| metadata.persistent_metadata.input_metadata.iter().take(inputs.len()).cloned().collect()) - .unwrap_or_default(); - let mut node_template = NodeTemplate { implementation: NodeTemplateImplementation::ProtoNode(ProtoNodeIdentifier::with_owned_string(cpu_identifier)), inputs, - input_metadata, ..Default::default() }; document.network_interface.replace_implementation(&node_id, &network_path, &mut node_template); @@ -1279,6 +1271,9 @@ pub fn document_migration_upgrades(document: &mut DocumentMessageHandler, reset_ if *new == graphene_std::math_nodes::math_function::IDENTIFIER { reset_default_display_name(document, node_id, &network_path, "Math f(x)"); } + if *new == graphene_std::vector::weld::IDENTIFIER { + reset_default_display_name(document, node_id, &network_path, "Merge by Distance"); + } } } } @@ -1594,13 +1589,11 @@ pub fn document_migration_upgrades(document: &mut DocumentMessageHandler, reset_ let new_reference = DefinitionIdentifier::ProtoNode(new_identifier.clone()); let Some(definition) = resolve_document_node_type(&new_reference) else { continue }; let mut node_template = definition.default_node_template(); - let output_names = definition.node_template.output_names.clone(); document.network_interface.replace_implementation(node_id, network_path, &mut node_template); let Some(old_inputs) = document.network_interface.replace_inputs(node_id, network_path, &mut node_template) else { continue; }; - document.network_interface.set_output_names(node_id, output_names, network_path); reset_default_display_name(document, node_id, network_path, old_reference); if let Some(input) = old_inputs.first() { @@ -1655,13 +1648,11 @@ pub fn document_migration_upgrades(document: &mut DocumentMessageHandler, reset_ continue; }; let mut node_template = definition.default_node_template(); - let output_names = definition.node_template.output_names.clone(); document.network_interface.replace_implementation(node_id, network_path, &mut node_template); let Some(old_inputs) = document.network_interface.replace_inputs(node_id, network_path, &mut node_template) else { continue; }; - document.network_interface.set_output_names(node_id, output_names, network_path); reset_default_display_name(document, node_id, network_path, old_default_name); if let Some(input) = old_inputs.first() { @@ -1713,14 +1704,11 @@ pub fn document_migration_upgrades(document: &mut DocumentMessageHandler, reset_ let new_reference = DefinitionIdentifier::ProtoNode(graphene_std::vector::evaluate_path::IDENTIFIER); let Some(definition) = resolve_document_node_type(&new_reference) else { continue }; let mut node_template = definition.default_node_template(); - let output_names = definition.node_template.output_names.clone(); document.network_interface.replace_implementation(node_id, network_path, &mut node_template); let Some(old_inputs) = document.network_interface.replace_inputs(node_id, network_path, &mut node_template) else { continue; }; - // The old single-output nodes have no output names, so set them to match the new multi-output node's "Position", "Tangent", and "Normal" ports - document.network_interface.set_output_names(node_id, output_names, network_path); reset_default_display_name(document, node_id, network_path, if *is_tangent { "Tangent on Path" } else { "Position on Path" }); // Forward the shared inputs: content, progression, reverse, and parameterized distance @@ -2993,20 +2981,6 @@ fn migrate_node(node_id: &NodeId, node: &DocumentNode, network_path: &[NodeId], reset_default_display_name(document, node_id, network_path, "Merge by Distance"); } - // A Merge by Distance node saved with its "Algorithm" input takes Weld's input names and descriptions, keeping its inputs - if reference == DefinitionIdentifier::ProtoNode(graphene_std::vector::weld::IDENTIFIER) - && inputs_count == 3 - && document - .network_interface - .persistent_input_metadata(node_id, 2, network_path) - .is_some_and(|metadata| metadata.input_name == "Algorithm") - { - let mut node_template = resolve_document_node_type(&reference)?.default_node_template(); - node_template.inputs = node.inputs.clone(); - document.network_interface.replace_inputs(node_id, network_path, &mut node_template)?; - reset_default_display_name(document, node_id, network_path, "Merge by Distance"); - } - if reference == DefinitionIdentifier::ProtoNode(ProtoNodeIdentifier::new("graphene_core::vector::SpatialMergeByDistanceNode")) { let mut node_template = resolve_document_node_type(&DefinitionIdentifier::ProtoNode(graphene_std::vector::weld::IDENTIFIER))?.default_node_template(); document.network_interface.replace_implementation(node_id, network_path, &mut node_template); @@ -3035,26 +3009,12 @@ fn migrate_node(node_id: &NodeId, node: &DocumentNode, network_path: &[NodeId], reset_default_display_name(document, node_id, network_path, "Split Segments"); } - // Separate Subpaths and Close Path became Separate Paths and Close Paths, whose inputs take their current names and descriptions + // Separate Subpaths and Close Path became Separate Paths and Close Paths, which take their new titles let pluralized = [ (graphene_std::vector::separate_paths::IDENTIFIER, "Separate Subpaths"), (graphene_std::vector::close_paths::IDENTIFIER, "Close Path"), ]; if let Some((_, old_default_name)) = pluralized.iter().find(|(identifier, _)| reference == DefinitionIdentifier::ProtoNode(identifier.clone())) { - let mut node_template = resolve_document_node_type(&reference)?.default_node_template(); - let stored = document - .network_interface - .persistent_input_metadata(node_id, 0, network_path) - .map(|metadata| (metadata.input_name.clone(), metadata.input_description.clone())); - let current = node_template - .input_metadata - .first() - .map(|metadata| (metadata.persistent_metadata.input_name.clone(), metadata.persistent_metadata.input_description.clone())); - if stored != current { - node_template.inputs = node.inputs.clone(); - document.network_interface.replace_inputs(node_id, network_path, &mut node_template)?; - } - reset_default_display_name(document, node_id, network_path, old_default_name); } @@ -4485,7 +4445,6 @@ mod tests { let executor = || NodeInput::scope("graphene_std::platform_application_io::WgpuExecutorNode"); let legacy_node = |identifier: &'static str, inputs: Vec| NodeTemplate { implementation: NodeTemplateImplementation::ProtoNode(ProtoNodeIdentifier::new(identifier)), - input_metadata: inputs.iter().map(|_| ("Value", "").into()).collect(), inputs, ..Default::default() }; @@ -5252,21 +5211,15 @@ mod tests { let content = definition.default_node_template().inputs[0].clone(); let distance = NodeInput::value(TaggedValue::Number(2.), false); - // Older nodes stored their title and input names, with any "Algorithm" choice saved here as a placeholder method + // Older nodes stored their title, with any "Algorithm" choice saved here as a placeholder method let legacy_node = |identifier: &'static str, display_name: &str, placeholder_method: Option| { let mut inputs = vec![content.clone(), distance.clone()]; inputs.extend(placeholder_method.map(|method| NodeInput::value(TaggedValue::WeldMethod(method), false))); - let input_names: &[&str] = if placeholder_method.is_some() { - &["Content", "Distance", "Algorithm"] - } else { - &["Source", "Distance"] - }; NodeTemplate { implementation: NodeTemplateImplementation::ProtoNode(ProtoNodeIdentifier::new(identifier)), inputs, display_name: display_name.to_string(), - input_metadata: input_names.iter().map(|name| (*name, "").into()).collect(), ..Default::default() } }; @@ -5315,6 +5268,24 @@ mod tests { } } + // A Weld node titled the way Merge by Distance nodes defaulted to keeps its title when its document is opened again + #[test] + fn a_weld_node_titled_merge_by_distance_keeps_its_title() { + use crate::messages::portfolio::document_storage_io::load_legacy_document; + use graph_craft::application_io::resource::HashMapResourceStorage; + + let definition = resolve_document_node_type(&DefinitionIdentifier::ProtoNode(graphene_std::vector::weld::IDENTIFIER)).expect("the Weld definition should exist"); + let template = NodeTemplate { + display_name: "Merge by Distance".into(), + ..definition.default_node_template() + }; + let mut document = DocumentMessageHandler::default(); + document.network_interface.insert_node(NodeId(1), template, &[]); + + let opened = load_legacy_document(document.serialize_document(), &HashMapResourceStorage::new(), false).expect("the document should open"); + assert_eq!(opened.network_interface.display_name(&NodeId(1), &[]), "Merge by Distance"); + } + // Cut Segments nodes open as Unweld nodes splitting every anchor, still wired to the node feeding them #[test] fn cut_segments_nodes_open_as_unweld_nodes() { @@ -5333,7 +5304,6 @@ mod tests { implementation: NodeTemplateImplementation::ProtoNode(ProtoNodeIdentifier::new(identifier)), inputs: vec![content.clone()], display_name: display_name.to_string(), - input_metadata: vec![("Content", "").into()], ..Default::default() }; let (upstream, cut_segments, split_segments) = (NodeId(1), NodeId(2), NodeId(3)); @@ -5397,7 +5367,6 @@ mod tests { "Separate Paths", ["core_types::vector::SeparateSubpathsNode", "graphene_core::vector::SeparateSubpathsNode"], "Separate Subpaths", - "Content", ), ( graphene_std::vector::close_paths::IDENTIFIER, @@ -5405,11 +5374,10 @@ mod tests { "Close Paths", ["core_types::vector::ClosePathNode", "graphene_core::vector::ClosePathNode"], "Close Path", - "Source", ), ]; - for (identifier, identifier_name, title, [current_identifier, oldest_identifier], old_title, input_name) in renames { + for (identifier, identifier_name, title, [current_identifier, oldest_identifier], old_title) in renames { let definition = resolve_document_node_type(&DefinitionIdentifier::ProtoNode(identifier.clone())).expect("the renamed node's definition should exist"); // The node under its current identifier and its oldest one, both storing their old title @@ -5417,7 +5385,6 @@ mod tests { implementation: NodeTemplateImplementation::ProtoNode(ProtoNodeIdentifier::new(old_identifier)), inputs: definition.default_node_template().inputs, display_name: old_title.to_string(), - input_metadata: vec![(input_name, "").into()], ..Default::default() }; let (upstream, current, oldest) = (NodeId(1), NodeId(2), NodeId(3)); @@ -5566,7 +5533,6 @@ mod tests { let legacy_node = |identifier: &'static str, display_name: &str, inputs: Vec| NodeTemplate { implementation: NodeTemplateImplementation::ProtoNode(ProtoNodeIdentifier::new(identifier)), - input_metadata: inputs.iter().map(|_| ("Value", "").into()).collect(), inputs, display_name: display_name.to_string(), ..Default::default() @@ -5687,7 +5653,6 @@ mod tests { ]; NodeTemplate { implementation: NodeTemplateImplementation::ProtoNode(ProtoNodeIdentifier::new("core_types::vector::CopyToPointsNode")), - input_metadata: inputs.iter().map(|_| ("Value", "").into()).collect(), inputs, display_name: "Copy to Points".to_string(), ..Default::default() @@ -5969,7 +5934,6 @@ mod tests { ]; NodeTemplate { implementation: NodeTemplateImplementation::ProtoNode(ProtoNodeIdentifier::new("graphene_core::vector::AssignColorsNode")), - input_metadata: inputs.iter().map(|_| ("Value", "").into()).collect(), inputs, display_name: "Assign Colors".to_string(), ..Default::default() diff --git a/editor/src/messages/portfolio/document_storage_io.rs b/editor/src/messages/portfolio/document_storage_io.rs index 6337efa4bb9..354b0ad190f 100644 --- a/editor/src/messages/portfolio/document_storage_io.rs +++ b/editor/src/messages/portfolio/document_storage_io.rs @@ -365,13 +365,7 @@ async fn build_document_from_storage(gdd: GddV1, byte_store: &ResourcesHandle, d } }; let network_ids = network_ids_from_entries(&network_entries); - let mut interface = match build_interface_from_storage(network, node_entries, network_entries) { - Ok(interface) => interface, - Err(error) => { - log::error!("Opening document {document_id:?}: failed to build interface: {error}"); - return None; - } - }; + let mut interface = build_interface_from_storage(network, node_entries, network_entries); apply_network_view_settings(&mut interface, &network_ids, gdd.network_view_settings()); interface.give_diverged_catalog_entries_new_ids(); // TODO: Run document upgrades here once this format has a versioned migration @@ -451,6 +445,7 @@ pub(super) fn load_legacy_document(document_serialized_content: String, byte_sto } document.network_interface.give_diverged_catalog_entries_new_ids(); + document.network_interface.adopt_catalog_interfaces(); document.network_interface.give_reused_node_ids_new_ids(); document.network_interface.discard_deltas(); document.require_whole_document_stage();