Skip to content

refactor: use breadth-first search in widget-filter #2481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions flutter/lib/src/screenshot/widget_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class WidgetFilter {
late WidgetFilterColorScheme _scheme;
late double _pixelRatio;
late Rect _bounds;
late List<Element> _visitList;
final _warnedWidgets = <int>{};

/// Used to test _obscureElementOrParent
Expand All @@ -34,11 +35,24 @@ class WidgetFilter {
assert(colorScheme.background.isOpaque);
assert(colorScheme.defaultMask.isOpaque);
assert(colorScheme.defaultTextMask.isOpaque);

// clear the output list
items.clear();
if (context is Element) {
_process(context);
} else {
context.visitChildElements(_process);

// Reset the list of elements we're going to process.
// Then do a breadth-first tree traversal on all the widgets.
// TODO benchmark performance compared to to DoubleLinkedQueue.
_visitList = [];
context.visitChildElements(_visitList.add);
while (_visitList.isNotEmpty) {
// Get a handle on the items we're supposed to process in this step.
// Then _visitList (which is updated in _process()) with a new instance.
final currentList = _visitList;
_visitList = [];

for (final element in currentList) {
_process(element);
}
}
}

Expand Down Expand Up @@ -66,7 +80,7 @@ class WidgetFilter {
break;
case SentryMaskingDecision.continueProcessing:
// If this element should not be obscured, visit and check its children.
element.visitChildElements(_process);
element.visitChildElements(_visitList.add);
break;
}
}
Expand Down
16 changes: 10 additions & 6 deletions flutter/test/screenshot/widget_filter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ void main() async {
bounds: defaultBounds,
colorScheme: colorScheme);
expect(sut.items.length, 6);
expect(boundsRect(sut.items[0]), '624x48');
expect(boundsRect(sut.items[1]), '169x20');
expect(boundsRect(sut.items[2]), '800x192');
expect(boundsRect(sut.items[3]), '800x24');
expect(boundsRect(sut.items[4]), '800x24');
expect(boundsRect(sut.items[5]), '50x20');
expect(
sut.items.map(boundsRect),
unorderedEquals([
'624x48',
'169x20',
'800x192',
'800x24',
'800x24',
'50x20',
]));
});
});

Expand Down
Loading