nestKeptElements function
Nests kept nodes under their nearest kept Flutter ancestor.
Sibling / root order is visual reading order (top→bottom, then left→right), not Flutter's Element walk — Scaffold often visits body before the app bar, which would otherwise put the back button at the end of the tree.
Implementation
List<UiElement> nestKeptElements(
List<({Element element, UiElement ui})> kept,
) {
return RunnerBenchmark.sync('observer', 'nestKeptElements', () {
if (kept.isEmpty) return const [];
final elementToIndex = <Element, int>{
for (var i = 0; i < kept.length; i++) kept[i].element: i,
};
final childIndexes = List.generate(kept.length, (_) => <int>[]);
final isRoot = List<bool>.filled(kept.length, true);
for (var i = 0; i < kept.length; i++) {
kept[i].element.visitAncestorElements((ancestor) {
final parentIndex = elementToIndex[ancestor];
if (parentIndex == null) return true;
childIndexes[parentIndex].add(i);
isRoot[i] = false;
return false;
});
}
int compareVisual(int a, int b) {
final ba = kept[a].ui.bounds;
final bb = kept[b].ui.bounds;
if (ba == null && bb == null) return a.compareTo(b);
if (ba == null) return 1;
if (bb == null) return -1;
final topCmp = ba.top.compareTo(bb.top);
if (topCmp != 0) return topCmp;
final leftCmp = ba.left.compareTo(bb.left);
if (leftCmp != 0) return leftCmp;
return a.compareTo(b);
}
for (final kids in childIndexes) {
kids.sort(compareVisual);
}
UiElement build(int i) {
final children = [
for (final childIndex in childIndexes[i]) build(childIndex),
];
final ui = kept[i].ui;
if (children.isEmpty) return ui;
return ui.copyWith(children: children);
}
final roots = <int>[
for (var i = 0; i < kept.length; i++)
if (isRoot[i]) i,
]..sort(compareVisual);
return [for (final i in roots) build(i)];
});
}