isNestedActionableElement function

bool isNestedActionableElement(
  1. Element element
)

When a keyed Ensemble wrapper is typed generically, prefer the logical control it owns (Checkbox → checkbox, TextField → textInput, …).

Also refines ancestor-derived card/button when the keyed host wraps a specific control — so testId on a Checkbox inside a tappable FlexRow is not reported as a list-row card. The FlexRow itself stays a separate card observe row when it is also kept. Nested actionable / chrome controls kept under a tappable card/row ancestor.

Without this, anything under a FlexRow/GestureDetector primary was dropped — so agents never saw info icons, nested IconButtons, or unkeyed checkboxes inside settings rows. Excludes structural wrappers and leaves under a more specific host (Icon under IconButton, etc.). Nav chevrons are kept so the observe tree mirrors what is on screen.

Implementation

/// Nested actionable / chrome controls kept under a tappable card/row ancestor.
///
/// Without this, anything under a FlexRow/GestureDetector primary was dropped —
/// so agents never saw info icons, nested IconButtons, or unkeyed checkboxes
/// inside settings rows. Excludes structural wrappers and leaves under a more
/// specific host (Icon under IconButton, etc.). Nav chevrons are kept so the
/// observe tree mirrors what is on screen.
bool isNestedActionableElement(Element element) {
  if (!hasPrimaryControlAncestor(element)) return false;

  final widget = element.widget;

  // Specific interactive hosts nested in a row/card.
  if (_isSpecificNestedActionHost(widget)) {
    // KeyedSubtree(testId) → Checkbox: the keyed host is already kept as this
    // control. Keeping the leaf too duplicates id/type in the tree + overlay.
    if (_isRedundantLeafUnderKeyedControlWrapper(element)) return false;
    return true;
  }
  if (widget is EditableText) return false;

  if (_isGenericTapTarget(widget)) {
    // Inside Checkbox/TextField/IconButton — the host is the action, not this.
    if (_hasSpecificControlAncestor(element)) return false;
    // Nested under another nested action host — keep outermost only.
    if (_hasNestedActionTapWrapperAncestor(element)) return false;
    // KeyedSubtree(back_button) → InkWell: shell already observes as icon.
    if (isRedundantLeafUnderKeyedIconShell(element)) return false;
    if (!_genericTapTargetIsEnabled(widget)) return false;

    final bounds = boundsFor(element);
    if (bounds == null) return false;
    // Full-row / card-sized wrappers are the parent primary, not a nested action.
    if (_looksLikeCardHitTarget(bounds) || _looksLikeListRowHitTarget(bounds)) {
      return false;
    }

    final text = _longestTextDescendant(element);
    final substantialText = text != null && text.trim().length > 2;

    // Compact icon / glyph chrome (info, overflow, close, chevron, …).
    if (_looksLikeCompactIconHitTarget(element)) {
      if (substantialText) return false;
      return _hasIconDescendant(element) ||
          _mediaTypeFromDescendant(element) != null ||
          (text != null && text.trim().isNotEmpty);
    }

    // Smaller nested CTA / link inside a card (not the whole row).
    if (substantialText && bounds.width <= 220 && bounds.height <= 56) {
      return true;
    }
    return false;
  }

  // Plain Icon / ImageIcon chrome on a settings row (may or may not be wrapped).
  // Icon-only compact hosts already exclude leaves via `_isUnderCompactIconControl`.
  // Caption+icon Rows (WifiCard eye) keep the leaf so agents can tap the glyph.
  if (widget is Icon) {
    if (_isUnderCompactIconControl(element)) return false;
    if (_hasSpecificControlAncestor(element)) return false;
    if (_hasIconButtonAncestor(element)) return false;
    if (!_looksLikeCompactIconHitTarget(element)) return false;
    return true;
  }
  if (widget is ImageIcon) {
    if (_isUnderCompactIconControl(element)) return false;
    if (_hasSpecificControlAncestor(element)) return false;
    if (_hasIconButtonAncestor(element)) return false;
    if (!_looksLikeCompactIconHitTarget(element)) return false;
    return true;
  }
  return false;
}