firstAddressableId method

String? firstAddressableId()

Implementation

String? firstAddressableId() {
  // Two-pass: prefer a user-code leaf (createdByLocalProject=true) since
  // platform view plugin nodes (e.g. GoogleMap) fail geometry evals.
  // Fall back to any leaf with a live inspector handle if none found.
  SceneNode? bestUserLeaf;
  SceneNode? bestAnyNode;

  for (final n in root.walk().skip(1)) {
    if (n.isOffstage) continue;
    // Require a live inspector handle — empty inspectorId means setSelection
    // would fail, breaking any enricher that probes geometry or routes.
    if (n.inspectorId.isEmpty) continue;
    if (n.glintId == null || n.glintId!.isEmpty) continue;

    if (n.children.isEmpty && n.isPlatformView) continue;

    if (n.children.isEmpty) {
      if (n.createdByLocalProject) {
        bestUserLeaf ??= n;
      } else {
        bestAnyNode ??= n;
      }
    } else {
      if (n.createdByLocalProject) bestAnyNode ??= n;
    }
  }
  return (bestUserLeaf ?? bestAnyNode)?.glintId;
}