nativeElements function

List<NativeNode> nativeElements(
  1. List<Map<String, Object?>> roots, {
  2. ({double h, double w})? screen,
  3. int max = 40,
  4. double scale = 1,
})

The labelled or tappable elements under roots whose center is on screen (points), at most max, with frames in points times scale.

Implementation

List<NativeNode> nativeElements(List<Map<String, Object?>> roots,
    {({double w, double h})? screen, int max = 40, double scale = 1}) {
  final out = <NativeNode>[];
  final seen = <String>{};
  void walk(Map<String, Object?> n, bool insideTappable) {
    if (out.length >= max || _isKeyboard(n)) return;
    final type = n['type'] as String? ?? '';
    final label = (n['label'] as String?) ?? (n['value'] as String?) ?? (n['placeholder'] as String?) ?? '';
    final tappable = _tappable.contains(type) || (type == 'image' && label.isNotEmpty);
    final f = n['frame'];
    if (f is List &&
        f.length == 4 &&
        (tappable || (type == 'text' && !insideTappable)) &&
        (tappable || label.isNotEmpty)) {
      final [x, y, w, h] = [for (final v in f) (v as num).toDouble()];
      final key = '$type|$label|${x.round()},${y.round()}';
      final onScreen =
          screen == null || (x + w / 2 >= 0 && y + h / 2 >= 0 && x + w / 2 <= screen.w && y + h / 2 <= screen.h);
      if (w > 0 && h > 0 && onScreen && seen.add(key)) {
        out.add((
          cls: type,
          label: label,
          clickable: tappable && n['enabled'] != false,
          x1: (x * scale).round(),
          y1: (y * scale).round(),
          x2: ((x + w) * scale).round(),
          y2: ((y + h) * scale).round(),
        ));
      }
    }
    for (final c in _children(n)) {
      walk(c, insideTappable || tappable);
    }
  }

  for (final r in roots) {
    walk(r, false);
  }
  return out;
}