visibleText method

List<String> visibleText({
  1. int max = 15,
})

The text a person reads on screen, top to bottom and left to right, for error messages.

Implementation

List<String> visibleText({int max = 15}) {
  final List<({String text, Rect rect})> shown = [];
  for (final Element element in elements) {
    final String? text = _readableText(element);
    if (text == null || text.trim().isEmpty) continue;
    final Rect? rect = playbookVisibleRect(element);
    if (rect == null) continue;
    shown.add((text: text.replaceAll('\n', ' ').trim(), rect: rect));
  }
  shown.sort((a, b) {
    final int row = (a.rect.top - b.rect.top).abs() < 4
        ? 0
        : a.rect.top.compareTo(b.rect.top);
    return row != 0 ? row : a.rect.left.compareTo(b.rect.left);
  });
  final List<String> texts = [];
  for (final ({String text, Rect rect}) entry in shown) {
    if (!texts.contains(entry.text)) texts.add(entry.text);
    if (texts.length >= max) break;
  }
  return texts;
}