restoreExpansionSnapshot method
- ExpansionSnapshot snapshot
Restore a previously captured ExpansionSnapshot on this graph.
Reconciles the current state (which may already have the top module partially expanded from the adapter builder) to match the snapshot.
Implementation
void restoreExpansionSnapshot(ExpansionSnapshot snapshot) {
// First, capture what the fresh adapter already has.
final freshExpanded = expandedNodeIds;
final freshPartials = <String, Set<String>>{};
void collectFreshPartials(LayoutNode n) {
if (n.isPartiallyExpanded) {
freshPartials[n.id] = Set<String>.from(n.partialChildIds!);
}
n.children.forEach(collectFreshPartials);
if (n.hiddenChildren != null) {
n.hiddenChildren!.forEach(collectFreshPartials);
}
}
collectFreshPartials(root);
// Collapse fully-expanded nodes that shouldn't be.
for (final id in freshExpanded) {
if (!snapshot.fullyExpanded.contains(id) &&
!snapshot.partials.containsKey(id)) {
toggleNode(id);
}
}
// Expand nodes that should be fully expanded but aren't.
for (final id in snapshot.fullyExpanded) {
if (!freshExpanded.contains(id)) {
toggleNode(id);
}
}
// Restore partial expansions.
for (final entry in snapshot.partials.entries) {
final node = nodeMap[entry.key];
if (node == null) {
continue;
}
// If the node is already partially expanded from the fresh adapter,
// clear it and reapply the snapshot's partial set.
node
..partialChildIds = entry.value.childIds
..partialHyperedgeIds = entry.value.edgeIds;
}
// Clear any fresh partial expansions not in the snapshot.
for (final id in freshPartials.keys) {
if (!snapshot.partials.containsKey(id) &&
!snapshot.fullyExpanded.contains(id)) {
nodeMap[id]?.partialChildIds = null;
nodeMap[id]?.partialHyperedgeIds = null;
}
}
}