Skip to content

Commit 073ddf4

Browse files
committed
Enable the Data panel to display data from selected nodes living in subgraphs
1 parent e7f0703 commit 073ddf4

5 files changed

Lines changed: 98 additions & 42 deletions

File tree

editor/src/messages/portfolio/document/data_panel/data_panel_message_handler.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ pub struct DataPanelMessageContext<'a> {
2727
/// The data panel allows for graph data to be previewed.
2828
#[derive(Default, Debug, Clone, ExtractField)]
2929
pub struct DataPanelMessageHandler {
30-
introspected_node: Option<NodeId>,
30+
/// Full path from the root network to the introspected node, with the node itself as the last element.
31+
/// Empty when nothing is being introspected.
32+
introspected_node_path: Vec<NodeId>,
3133
introspected_data: Option<Arc<dyn Any + Send + Sync>>,
3234
element_path: Vec<PathStep>,
3335
active_vector_table_tab: VectorTableTab,
@@ -38,12 +40,12 @@ impl MessageHandler<DataPanelMessage, DataPanelMessageContext<'_>> for DataPanel
3840
fn process_message(&mut self, message: DataPanelMessage, responses: &mut VecDeque<Message>, context: DataPanelMessageContext) {
3941
match message {
4042
DataPanelMessage::UpdateLayout { mut inspect_result } => {
41-
self.introspected_node = Some(inspect_result.inspect_node);
4243
self.introspected_data = inspect_result.take_data();
44+
self.introspected_node_path = inspect_result.inspect_node_path;
4345
self.update_layout(responses, context);
4446
}
4547
DataPanelMessage::ClearLayout => {
46-
self.introspected_node = None;
48+
self.introspected_node_path.clear();
4749
self.introspected_data = None;
4850
self.element_path.clear();
4951
self.active_vector_table_tab = VectorTableTab::default();
@@ -93,8 +95,9 @@ impl DataPanelMessageHandler {
9395
let mut widgets = Vec::new();
9496

9597
// Selected layer/node name
96-
if let Some(node_id) = self.introspected_node {
97-
let is_layer = network_interface.is_layer(&node_id, &[]);
98+
if let Some((node_id, parent_path)) = self.introspected_node_path.split_last() {
99+
let node_id = *node_id;
100+
let is_layer = network_interface.is_layer(&node_id, parent_path);
98101

99102
widgets.extend([
100103
if is_layer {
@@ -103,7 +106,7 @@ impl DataPanelMessageHandler {
103106
IconLabel::new("Node").tooltip_description("Name of the selected node.").widget_instance()
104107
},
105108
Separator::new(SeparatorStyle::Related).widget_instance(),
106-
TextInput::new(network_interface.display_name(&node_id, &[]))
109+
TextInput::new(network_interface.display_name(&node_id, parent_path))
107110
.tooltip_description(if is_layer { "Name of the selected layer." } else { "Name of the selected node." })
108111
.on_update(move |text_input| {
109112
NodeGraphMessage::SetDisplayName {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,12 @@ impl DocumentMessageHandler {
16861686
self.network_interface.document_metadata()
16871687
}
16881688

1689+
/// Path to the subnetwork that the user's selection is currently scoped to.
1690+
/// Empty when the selection lives in the root document network.
1691+
pub fn selection_network_path(&self) -> &[NodeId] {
1692+
&self.selection_network_path
1693+
}
1694+
16891695
pub fn serialize_document(&self) -> String {
16901696
let val = serde_json::to_string(self);
16911697
// We fully expect the serialization to succeed

editor/src/messages/portfolio/portfolio_message_handler.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,22 +1868,31 @@ impl PortfolioMessageHandler {
18681868
}
18691869
}
18701870

1871-
/// Get the ID of the selected node that should be used as the current source for the Data panel.
1872-
pub fn node_to_inspect(&self) -> Option<NodeId> {
1871+
/// Returns the full path from the root network to the selected node that should drive the Data panel.
1872+
/// The last element is the node itself; preceding elements identify the nested subnetwork it lives in
1873+
/// so the Data panel can introspect nodes inside subgraphs. An empty `Vec` signals "nothing to inspect".
1874+
pub fn node_to_inspect(&self) -> Vec<NodeId> {
18731875
// Skip if the Data panel is not open
18741876
if !self.workspace_panel_layout.is_panel_visible(PanelType::Data) || self.workspace_panel_layout.focus_document {
1875-
return None;
1877+
return Vec::new();
18761878
}
18771879

1878-
let document = self.document(self.active_document_id?)?;
1879-
let selected_nodes = document.network_interface.selected_nodes().0;
1880+
let Some(document) = self.active_document_id.and_then(|id| self.document(id)) else {
1881+
return Vec::new();
1882+
};
1883+
let network_path = document.selection_network_path();
1884+
let Some(selected_nodes) = document.network_interface.selected_nodes_in_nested_network(network_path) else {
1885+
return Vec::new();
1886+
};
18801887

18811888
// Skip if there is not exactly one selected node
1882-
if selected_nodes.len() != 1 {
1883-
return None;
1884-
}
1889+
let [node_id] = selected_nodes.0.as_slice() else {
1890+
return Vec::new();
1891+
};
18851892

1886-
selected_nodes.first().copied()
1893+
let mut path = network_path.to_vec();
1894+
path.push(*node_id);
1895+
path
18871896
}
18881897

18891898
/// Remove a dockable panel type from whichever panel group currently contains it. Does not prune empty groups.

editor/src/node_graph_executor.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ pub struct NodeGraphExecutor {
5353
current_execution_id: u64,
5454
futures: VecDeque<(u64, ExecutionContext)>,
5555
node_graph_hash: u64,
56-
previous_node_to_inspect: Option<NodeId>,
56+
/// Full path from the root document network to the node currently being inspected by the Data panel, or empty if nothing is selected.
57+
/// The last element is the inspect target itself; preceding elements identify the nested subnetwork the node lives in,
58+
/// so the runtime can splice its monitor node alongside the target rather than only at the top level.
59+
/// Tracking the previously-sent value lets `update_node_graph` re-send the network when the inspection target changes.
60+
previous_node_to_inspect: Vec<NodeId>,
5761
}
5862

5963
#[derive(Debug, Clone)]
@@ -75,7 +79,7 @@ impl NodeGraphExecutor {
7579
runtime_io: NodeRuntimeIO::with_channels(request_sender, response_receiver),
7680
node_graph_hash: 0,
7781
current_execution_id: 0,
78-
previous_node_to_inspect: None,
82+
previous_node_to_inspect: Vec::new(),
7983
};
8084
(node_runtime, node_executor)
8185
}
@@ -109,18 +113,18 @@ impl NodeGraphExecutor {
109113
let instrumented = Instrumented::new(&mut network);
110114

111115
self.runtime_io
112-
.send(GraphRuntimeRequest::GraphUpdate(GraphUpdate { network, node_to_inspect: None }))
116+
.send(GraphRuntimeRequest::GraphUpdate(GraphUpdate { network, node_to_inspect: Vec::new() }))
113117
.map_err(|e| e.to_string())?;
114118
Ok(instrumented)
115119
}
116120

117121
/// Update the cached network if necessary.
118-
fn update_node_graph(&mut self, document: &mut DocumentMessageHandler, node_to_inspect: Option<NodeId>, ignore_hash: bool) -> Result<(), String> {
122+
fn update_node_graph(&mut self, document: &mut DocumentMessageHandler, node_to_inspect: Vec<NodeId>, ignore_hash: bool) -> Result<(), String> {
119123
let network_hash = document.network_interface.network_hash();
120124
// Refresh the graph when it changes or the inspect node changes
121125
if network_hash != self.node_graph_hash || self.previous_node_to_inspect != node_to_inspect || ignore_hash {
122126
let network = document.network_interface.document_network().clone();
123-
self.previous_node_to_inspect = node_to_inspect;
127+
self.previous_node_to_inspect.clone_from(&node_to_inspect);
124128
self.node_graph_hash = network_hash;
125129

126130
self.runtime_io
@@ -174,7 +178,7 @@ impl NodeGraphExecutor {
174178
viewport_resolution: UVec2,
175179
viewport_scale: f64,
176180
time: TimingInformation,
177-
node_to_inspect: Option<NodeId>,
181+
node_to_inspect: Vec<NodeId>,
178182
ignore_hash: bool,
179183
pointer: DVec2,
180184
) -> Result<Message, String> {
@@ -271,7 +275,7 @@ impl NodeGraphExecutor {
271275

272276
// Execute the node graph
273277
self.runtime_io
274-
.send(GraphRuntimeRequest::GraphUpdate(GraphUpdate { network, node_to_inspect: None }))
278+
.send(GraphRuntimeRequest::GraphUpdate(GraphUpdate { network, node_to_inspect: Vec::new() }))
275279
.map_err(|e| e.to_string())?;
276280
let execution_id = self.queue_execution(render_config);
277281
self.futures.push_back((
@@ -338,7 +342,7 @@ impl NodeGraphExecutor {
338342
});
339343

340344
// Update the Data panel on the frontend using the value of the inspect result.
341-
if let Some(inspect_result) = (self.previous_node_to_inspect.is_some()).then_some(inspect_result).flatten() {
345+
if let Some(inspect_result) = (!self.previous_node_to_inspect.is_empty()).then_some(inspect_result).flatten() {
342346
responses.add(DataPanelMessage::UpdateLayout { inspect_result });
343347
} else {
344348
responses.add(DataPanelMessage::ClearLayout);

editor/src/node_graph_executor/runtime.rs

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ pub enum GraphRuntimeRequest {
7676
#[derive(Debug, serde::Serialize, serde::Deserialize)]
7777
pub struct GraphUpdate {
7878
pub(super) network: NodeNetwork,
79-
/// The node that should be temporary inspected during execution
80-
pub(super) node_to_inspect: Option<NodeId>,
79+
/// Full path from the root network to the node that should be temporarily inspected during execution.
80+
/// The last element is the inspect target; preceding elements identify the nested subnetwork it lives in,
81+
/// so the runtime can splice its monitor node alongside the target instead of only at the top level.
82+
pub(super) node_to_inspect: Vec<NodeId>,
8183
}
8284

8385
#[derive(Default, Debug, Clone, serde::Serialize, serde::Deserialize)]
@@ -235,7 +237,7 @@ impl NodeRuntime {
235237
}
236238
GraphRuntimeRequest::GraphUpdate(GraphUpdate { mut network, node_to_inspect }) => {
237239
// Insert the monitor node to manage the inspection
238-
self.inspect_state = node_to_inspect.map(|inspect| InspectState::monitor_inspect_node(&mut network, inspect));
240+
self.inspect_state = InspectState::monitor_inspect_node(&mut network, &node_to_inspect);
239241

240242
self.old_graph = Some(network.clone());
241243

@@ -264,7 +266,7 @@ impl NodeRuntime {
264266
self.update_thumbnails = false;
265267

266268
// Resolve the result from the inspection by accessing the monitor node
267-
let inspect_result = self.inspect_state.and_then(|state| state.access(&self.executor));
269+
let inspect_result = self.inspect_state.as_ref().and_then(|state| state.access(&self.executor));
268270

269271
let (result, texture) = match result {
270272
Ok(TaggedValue::RenderOutput(RenderOutput {
@@ -408,7 +410,11 @@ impl NodeRuntime {
408410

409411
for monitor_node_path in &self.monitor_nodes {
410412
// Skip the inspect monitor node
411-
if self.inspect_state.is_some_and(|inspect_state| monitor_node_path.last().copied() == Some(inspect_state.monitor_node)) {
413+
if self
414+
.inspect_state
415+
.as_ref()
416+
.is_some_and(|inspect_state| monitor_node_path.last().copied() == Some(inspect_state.monitor_node))
417+
{
412418
continue;
413419
}
414420

@@ -540,16 +546,22 @@ pub async fn replace_application_io(application_io: PlatformApplicationIo) {
540546
}
541547

542548
/// Which node is inspected and which monitor node is used (if any) for the current execution
543-
#[derive(Debug, Clone, Copy)]
549+
#[derive(Debug, Clone)]
544550
struct InspectState {
545551
inspect_node: NodeId,
546552
monitor_node: NodeId,
553+
/// Path of the subnetwork the monitor was inserted into (i.e., the parent of `inspect_node`).
554+
/// Used to construct the full node path when introspecting the monitor's value.
555+
monitor_parent_path: Vec<NodeId>,
547556
}
548557
/// The resulting value from the temporary inspected during execution
549558
#[derive(Clone, Debug, Default)]
550559
pub struct InspectResult {
551560
introspected_data: Option<Arc<dyn std::any::Any + Send + Sync + 'static>>,
552-
pub inspect_node: NodeId,
561+
/// Full path from the root network to the inspected node, with the node itself as the last element.
562+
/// The parent slice (`split_last().1`) is the network the node lives in, which downstream consumers
563+
/// (e.g. the Data panel) need when looking the node up via `network_interface.is_layer(...)` etc.
564+
pub inspect_node_path: Vec<NodeId>,
553565
}
554566

555567
impl InspectResult {
@@ -561,17 +573,21 @@ impl InspectResult {
561573
// This is very ugly but is required to be inside a message
562574
impl PartialEq for InspectResult {
563575
fn eq(&self, other: &Self) -> bool {
564-
self.inspect_node == other.inspect_node
576+
self.inspect_node_path == other.inspect_node_path
565577
}
566578
}
567579

568580
impl InspectState {
569-
/// Insert the monitor node to manage the inspection
570-
pub fn monitor_inspect_node(network: &mut NodeNetwork, inspect_node: NodeId) -> Self {
581+
/// Insert the monitor node alongside the inspect node identified by `inspect_path` (full path from root, last element is the target).
582+
/// Returns `None` if the path is empty or doesn't resolve to a node inside a reachable subnetwork.
583+
pub fn monitor_inspect_node(network: &mut NodeNetwork, inspect_path: &[NodeId]) -> Option<Self> {
584+
let (inspect_node, parent_path) = inspect_path.split_last()?;
585+
let inspect_node = *inspect_node;
586+
let target_network = navigate_to_network_mut(network, parent_path)?;
571587
let monitor_id = NodeId::new();
572588

573589
// It is necessary to replace the inputs before inserting the monitor node to avoid changing the input of the new monitor node
574-
for input in network.nodes.values_mut().flat_map(|node| node.inputs.iter_mut()).chain(&mut network.exports) {
590+
for input in target_network.nodes.values_mut().flat_map(|node| node.inputs.iter_mut()).chain(&mut target_network.exports) {
575591
let NodeInput::Node { node_id, output_index, .. } = input else { continue };
576592
// We only care about the primary output of our inspect node
577593
if *output_index != 0 || *node_id != inspect_node {
@@ -588,21 +604,39 @@ impl InspectState {
588604
skip_deduplication: true,
589605
..Default::default()
590606
};
591-
network.nodes.insert(monitor_id, monitor_node);
607+
target_network.nodes.insert(monitor_id, monitor_node);
592608

593-
Self {
609+
Some(Self {
594610
inspect_node,
595611
monitor_node: monitor_id,
596-
}
612+
monitor_parent_path: parent_path.to_vec(),
613+
})
597614
}
598615
/// Resolve the result from the inspection by accessing the monitor node
599616
fn access(&self, executor: &DynamicExecutor) -> Option<InspectResult> {
600-
let introspected_data = executor.introspect(&[self.monitor_node]).inspect_err(|e| warn!("Failed to introspect monitor node {e}")).ok();
617+
// The executor's source map indexes by full path from root, so prepend the subnetwork path to the monitor ID.
618+
let mut monitor_path = self.monitor_parent_path.clone();
619+
monitor_path.push(self.monitor_node);
620+
let introspected_data = executor.introspect(&monitor_path).inspect_err(|e| warn!("Failed to introspect monitor node {e}")).ok();
601621
// TODO: Consider displaying the error instead of ignoring it
602622

603-
Some(InspectResult {
604-
inspect_node: self.inspect_node,
605-
introspected_data,
606-
})
623+
let mut inspect_node_path = self.monitor_parent_path.clone();
624+
inspect_node_path.push(self.inspect_node);
625+
Some(InspectResult { inspect_node_path, introspected_data })
626+
}
627+
}
628+
629+
/// Walks `network` down through `path`, returning a mutable reference to the nested `NodeNetwork`
630+
/// at the end. Each path element must name a `DocumentNode` whose implementation is `Network(...)`.
631+
/// Returns `None` if any step is missing or doesn't refer to a subnetwork.
632+
fn navigate_to_network_mut<'a>(network: &'a mut NodeNetwork, path: &[NodeId]) -> Option<&'a mut NodeNetwork> {
633+
let mut current = network;
634+
for node_id in path {
635+
let node = current.nodes.get_mut(node_id)?;
636+
current = match &mut node.implementation {
637+
DocumentNodeImplementation::Network(nested) => nested,
638+
_ => return None,
639+
};
607640
}
641+
Some(current)
608642
}

0 commit comments

Comments
 (0)