|
| 1 | +// Copyright 2013 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 | +/// The results of diffing the current composition order with the active |
| 6 | +/// composition order. |
| 7 | +class ViewListDiffResult { |
| 8 | + /// Views which should be removed from the scene. |
| 9 | + final List<int> viewsToRemove; |
| 10 | + |
| 11 | + /// Views to add to the scene. |
| 12 | + final List<int> viewsToAdd; |
| 13 | + |
| 14 | + /// If `true`, [viewsToAdd] should be added at the beginning of the scene. |
| 15 | + /// Otherwise, they should be added at the end of the scene. |
| 16 | + final bool addToBeginning; |
| 17 | + |
| 18 | + /// If [addToBeginning] is `true`, then this is the id of the platform view |
| 19 | + /// to insert [viewsToAdd] before. |
| 20 | + /// |
| 21 | + /// `null` if [addToBeginning] is `false`. |
| 22 | + final int? viewToInsertBefore; |
| 23 | + |
| 24 | + const ViewListDiffResult( |
| 25 | + this.viewsToRemove, this.viewsToAdd, this.addToBeginning, |
| 26 | + {this.viewToInsertBefore}); |
| 27 | +} |
| 28 | + |
| 29 | +/// Diff the composition order with the active composition order. It is |
| 30 | +/// common for the composition order and active composition order to differ |
| 31 | +/// only slightly. |
| 32 | +/// |
| 33 | +/// Consider a scrolling list of platform views; from frame |
| 34 | +/// to frame the composition order will change in one of two ways, depending |
| 35 | +/// on which direction the list is scrolling. One or more views will be added |
| 36 | +/// to the beginning of the list, and one or more views will be removed from |
| 37 | +/// the end of the list, with the order of the unchanged middle views |
| 38 | +/// remaining the same. |
| 39 | +// TODO(hterkelsen): Refactor to use [longestIncreasingSubsequence] and logic |
| 40 | +// similar to `Surface._insertChildDomNodes` to efficiently handle more cases, |
| 41 | +// https://github.com/flutter/flutter/issues/89611. |
| 42 | +ViewListDiffResult? diffViewList(List<int> active, List<int> next) { |
| 43 | + if (active.isEmpty || next.isEmpty) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + // This is tried if the first element of the next list is contained in the |
| 48 | + // active list at `index`. If the active and next lists are in the expected |
| 49 | + // form, then we should be able to iterate from `index` to the end of the |
| 50 | + // active list where every element matches in the next list. |
| 51 | + ViewListDiffResult? lookForwards(int index) { |
| 52 | + for (int i = 0; i + index < active.length; i++) { |
| 53 | + if (active[i + index] != next[i]) { |
| 54 | + // An element in the next list didn't match. This isn't in the expected |
| 55 | + // form we can optimize. |
| 56 | + return null; |
| 57 | + } |
| 58 | + if (i == next.length - 1) { |
| 59 | + // The entire next list was contained in the active list. |
| 60 | + if (index == 0) { |
| 61 | + // If the first index of the next list is also the first index in the |
| 62 | + // active list, then the next list is the same as the active list with |
| 63 | + // views removed from the end. |
| 64 | + return ViewListDiffResult( |
| 65 | + active.sublist(i + 1), const <int>[], false); |
| 66 | + } else if (i + index == active.length - 1) { |
| 67 | + // If this is also the end of the active list, then the next list is |
| 68 | + // the same as the active list with some views removed from the |
| 69 | + // beginning. |
| 70 | + return ViewListDiffResult( |
| 71 | + active.sublist(0, index), const <int>[], false); |
| 72 | + } else { |
| 73 | + return null; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + // We reached the end of the active list but have not reached the end of the |
| 78 | + // next list. The lists are in the expected form. We should remove the |
| 79 | + // elements from `0` to `index` in the active list from the DOM and add the |
| 80 | + // elements from `active.length - index` (the entire active list minus the |
| 81 | + // number of new elements at the beginning) to the end of the next list to |
| 82 | + // the DOM at the end of the list of platform views. |
| 83 | + final List<int> viewsToRemove = active.sublist(0, index); |
| 84 | + final List<int> viewsToAdd = next.sublist(active.length - index); |
| 85 | + |
| 86 | + return ViewListDiffResult( |
| 87 | + viewsToRemove, |
| 88 | + viewsToAdd, |
| 89 | + false, |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + // This is tried if the last element of the next list is contained in the |
| 94 | + // active list at `index`. If the lists are in the expected form, we should be |
| 95 | + // able to iterate backwards from index to the beginning of the active list |
| 96 | + // and have every element match the corresponding element from the next list. |
| 97 | + ViewListDiffResult? lookBackwards(int index) { |
| 98 | + for (int i = 0; index - i >= 0; i++) { |
| 99 | + if (active[index - i] != next[next.length - 1 - i]) { |
| 100 | + // An element from the next list didn't match the coresponding element |
| 101 | + // from the active list. These lists aren't in the expected form. |
| 102 | + return null; |
| 103 | + } |
| 104 | + if (i == next.length - 1) { |
| 105 | + // The entire next list was contained in the active list. |
| 106 | + if (index == active.length - 1) { |
| 107 | + // If the last element of the next list is also the last element of |
| 108 | + // the active list, then the next list is just the active list with |
| 109 | + // some elements removed from the beginning. |
| 110 | + return ViewListDiffResult( |
| 111 | + active.sublist(0, active.length - i - 1), const <int>[], false); |
| 112 | + } else if (index == i) { |
| 113 | + // If we also reached the beginning of the active list, then the next |
| 114 | + // list is the same as the active list with some views removed from |
| 115 | + // the end. |
| 116 | + return ViewListDiffResult( |
| 117 | + active.sublist(index + 1), const <int>[], false); |
| 118 | + } else { |
| 119 | + return null; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // We reached the beginning of the active list but have not exhausted the |
| 125 | + // entire next list. The lists are in the expected form. We should remove |
| 126 | + // the elements from the end of the active list which come after the element |
| 127 | + // which matches the last index of the next list (everything after `index`). |
| 128 | + // We should add the elements from the next list which we didn't reach while |
| 129 | + // iterating above (the first `next.length - index` views). |
| 130 | + final List<int> viewsToRemove = active.sublist(index + 1); |
| 131 | + final List<int> viewsToAdd = next.sublist(0, next.length - 1 - index); |
| 132 | + |
| 133 | + return ViewListDiffResult( |
| 134 | + viewsToRemove, |
| 135 | + viewsToAdd, |
| 136 | + true, |
| 137 | + viewToInsertBefore: active.first, |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + // If the [active] and [next] lists are in the expected form described above, |
| 142 | + // then either the first or last element of [next] will be in [active]. |
| 143 | + final int firstIndex = active.indexOf(next.first); |
| 144 | + final int lastIndex = active.lastIndexOf(next.last); |
| 145 | + if (firstIndex != -1 && lastIndex != -1) { |
| 146 | + // Both the first element and the last element of the next list are in the |
| 147 | + // active list. Search in the direction that will result in the least |
| 148 | + // amount of deletions. |
| 149 | + if (firstIndex <= (active.length - lastIndex)) { |
| 150 | + return lookForwards(firstIndex); |
| 151 | + } else { |
| 152 | + return lookBackwards(lastIndex); |
| 153 | + } |
| 154 | + } else if (firstIndex != -1) { |
| 155 | + return lookForwards(firstIndex); |
| 156 | + } else if (lastIndex != -1) { |
| 157 | + return lookBackwards(lastIndex); |
| 158 | + } else { |
| 159 | + return null; |
| 160 | + } |
| 161 | +} |
0 commit comments