"Fossies" - the Fresh Open Source Software Archive

Member "flutter-3.7.0/packages/flutter_test/lib/src/all_elements.dart" (24 Jan 2023, 3153 Bytes) of package /linux/misc/flutter-3.7.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 import 'package:flutter/foundation.dart';
    6 import 'package:flutter/widgets.dart';
    7 
    8 /// Provides an iterable that efficiently returns all the elements
    9 /// rooted at the given element. See [CachingIterable] for details.
   10 ///
   11 /// This method must be called again if the tree changes. You cannot
   12 /// call this function once, then reuse the iterable after having
   13 /// changed the state of the tree, because the iterable returned by
   14 /// this function caches the results and only walks the tree once.
   15 ///
   16 /// The same applies to any iterable obtained indirectly through this
   17 /// one, for example the results of calling `where` on this iterable
   18 /// are also cached.
   19 Iterable<Element> collectAllElementsFrom(
   20   Element rootElement, {
   21   required bool skipOffstage,
   22 }) {
   23   return CachingIterable<Element>(_DepthFirstChildIterator(rootElement, skipOffstage));
   24 }
   25 
   26 /// Provides a recursive, efficient, depth first search of an element tree.
   27 ///
   28 /// [Element.visitChildren] does not guarantee order, but does guarantee stable
   29 /// order. This iterator also guarantees stable order, and iterates in a left
   30 /// to right order:
   31 ///
   32 ///       1
   33 ///     /   \
   34 ///    2     3
   35 ///   / \   / \
   36 ///  4   5 6   7
   37 ///
   38 /// Will iterate in order 2, 4, 5, 3, 6, 7.
   39 ///
   40 /// Performance is important here because this method is on the critical path
   41 /// for flutter_driver and package:integration_test performance tests.
   42 /// Performance is measured in the all_elements_bench microbenchmark.
   43 /// Any changes to this implementation should check the before and after numbers
   44 /// on that benchmark to avoid regressions in general performance test overhead.
   45 ///
   46 /// If we could use RTL order, we could save on performance, but numerous tests
   47 /// have been written (and developers clearly expect) that LTR order will be
   48 /// respected.
   49 class _DepthFirstChildIterator implements Iterator<Element> {
   50   _DepthFirstChildIterator(Element rootElement, this.skipOffstage) {
   51     _fillChildren(rootElement);
   52   }
   53 
   54   final bool skipOffstage;
   55 
   56   late Element _current;
   57 
   58   final List<Element> _stack = <Element>[];
   59 
   60   @override
   61   Element get current => _current;
   62 
   63   @override
   64   bool moveNext() {
   65     if (_stack.isEmpty) {
   66       return false;
   67     }
   68 
   69     _current = _stack.removeLast();
   70     _fillChildren(_current);
   71 
   72     return true;
   73   }
   74 
   75   void _fillChildren(Element element) {
   76     assert(element != null);
   77     // If we did not have to follow LTR order and could instead use RTL,
   78     // we could avoid reversing this and the operation would be measurably
   79     // faster. Unfortunately, a lot of tests depend on LTR order.
   80     final List<Element> reversed = <Element>[];
   81     if (skipOffstage) {
   82       element.debugVisitOnstageChildren(reversed.add);
   83     } else {
   84       element.visitChildren(reversed.add);
   85     }
   86     // This is faster than _stack.addAll(reversed.reversed), presumably since
   87     // we don't actually care about maintaining an iteration pointer.
   88     while (reversed.isNotEmpty) {
   89       _stack.add(reversed.removeLast());
   90     }
   91   }
   92 }