safeViewGroupPayloadIndex function
Clamps a ViewGroup tab index for indexing PageGroupState.pagePayloads and IndexedStack children. Call sites must use this when the global viewGroupNotifier may hold a stale index (for example after restoring from background storage when the menu definition has fewer tabs than before).
Implementation
int safeViewGroupPayloadIndex(int index, int payloadLength) {
if (payloadLength <= 0) return 0;
if (index < 0) return 0;
if (index >= payloadLength) return payloadLength - 1;
return index;
}