|
17 | 17 | namespace facebook { |
18 | 18 | namespace react { |
19 | 19 |
|
20 | | -/* |
21 | | - * `shadowNode` might not be the newest revision of `ShadowNodeFamily`. |
22 | | - * This function looks at `parentNode`'s children and finds one that belongs |
23 | | - * to the same family as `shadowNode`. |
24 | | - */ |
25 | | -static ShadowNode const *findNewestChildInParent( |
26 | | - ShadowNode const &parentNode, |
27 | | - ShadowNode const &shadowNode) { |
28 | | - for (auto const &child : parentNode.getChildren()) { |
29 | | - if (ShadowNode::sameFamily(*child, shadowNode)) { |
30 | | - return child.get(); |
| 20 | +LayoutMetrics LayoutableShadowNode::computeRelativeLayoutMetrics( |
| 21 | + ShadowNodeFamily const &descendantNodeFamily, |
| 22 | + LayoutableShadowNode const &ancestorNode, |
| 23 | + LayoutInspectingPolicy policy) { |
| 24 | + if (&descendantNodeFamily == &ancestorNode.getFamily()) { |
| 25 | + // Layout metrics of a node computed relatively to the same node are equal |
| 26 | + // to `transform`-ed layout metrics of the node with zero `origin`. |
| 27 | + auto layoutMetrics = ancestorNode.getLayoutMetrics(); |
| 28 | + if (policy.includeTransform) { |
| 29 | + layoutMetrics.frame = layoutMetrics.frame * ancestorNode.getTransform(); |
31 | 30 | } |
| 31 | + layoutMetrics.frame.origin = {0, 0}; |
| 32 | + return layoutMetrics; |
| 33 | + } |
| 34 | + |
| 35 | + auto ancestors = descendantNodeFamily.getAncestors(ancestorNode); |
| 36 | + |
| 37 | + if (ancestors.size() == 0) { |
| 38 | + // Specified nodes do not form an ancestor-descender relationship |
| 39 | + // in the same tree. Aborting. |
| 40 | + return EmptyLayoutMetrics; |
32 | 41 | } |
33 | | - return nullptr; |
34 | | -} |
35 | 42 |
|
36 | | -static LayoutMetrics calculateOffsetForLayoutMetrics( |
37 | | - LayoutMetrics layoutMetrics, |
38 | | - ShadowNode::AncestorList const &ancestors, |
39 | | - LayoutableShadowNode::LayoutInspectingPolicy const &policy) { |
40 | | - // `AncestorList` starts from the given ancestor node and ends with the parent |
41 | | - // node. We iterate from parent node (reverse iteration) and stop before the |
42 | | - // given ancestor (rend() - 1). |
43 | | - for (auto it = ancestors.rbegin(); it != ancestors.rend() - 1; ++it) { |
44 | | - auto ¤tShadowNode = it->first.get(); |
45 | | - |
46 | | - if (currentShadowNode.getTraits().check( |
47 | | - ShadowNodeTraits::Trait::RootNodeKind)) { |
| 43 | + // Step 1. |
| 44 | + // Creating a list of nodes that form a chain from the descender node to |
| 45 | + // ancestor node inclusively. |
| 46 | + auto shadowNodeList = better::small_vector<ShadowNode const *, 16>{}; |
| 47 | + |
| 48 | + // Finding the measured node. |
| 49 | + // The last element in the `AncestorList` is a pair of a parent of the node |
| 50 | + // and an index of this node in the parent's children list. |
| 51 | + auto &pair = ancestors.at(ancestors.size() - 1); |
| 52 | + auto descendantNode = pair.first.get().getChildren().at(pair.second).get(); |
| 53 | + |
| 54 | + // Putting the node inside the list. |
| 55 | + // Even if this is a node with a `RootNodeKind` trait, we don't treat it as |
| 56 | + // root because we measure it from an outside tree perspective. |
| 57 | + shadowNodeList.push_back(descendantNode); |
| 58 | + |
| 59 | + for (auto it = ancestors.rbegin(); it != ancestors.rend(); it++) { |
| 60 | + auto &shadowNode = it->first.get(); |
| 61 | + |
| 62 | + shadowNodeList.push_back(&shadowNode); |
| 63 | + |
| 64 | + if (shadowNode.getTraits().check(ShadowNodeTraits::Trait::RootNodeKind)) { |
| 65 | + // If this is a node with a `RootNodeKind` trait, we need to stop right |
| 66 | + // there. |
48 | 67 | break; |
49 | 68 | } |
| 69 | + } |
| 70 | + |
| 71 | + // Step 2. |
| 72 | + // Computing the initial size of the measured node. |
| 73 | + auto descendantLayoutableNode = |
| 74 | + traitCast<LayoutableShadowNode const *>(descendantNode); |
50 | 75 |
|
51 | | - auto layoutableCurrentShadowNode = |
52 | | - dynamic_cast<LayoutableShadowNode const *>(¤tShadowNode); |
| 76 | + if (!descendantLayoutableNode) { |
| 77 | + return EmptyLayoutMetrics; |
| 78 | + } |
53 | 79 |
|
54 | | - if (!layoutableCurrentShadowNode) { |
| 80 | + auto layoutMetrics = descendantLayoutableNode->getLayoutMetrics(); |
| 81 | + auto &resultFrame = layoutMetrics.frame; |
| 82 | + resultFrame.origin = {0, 0}; |
| 83 | + |
| 84 | + // Step 3. |
| 85 | + // Iterating on a list of nodes computing compound offset. |
| 86 | + auto size = shadowNodeList.size(); |
| 87 | + for (int i = 0; i < size; i++) { |
| 88 | + auto currentShadowNode = |
| 89 | + traitCast<LayoutableShadowNode const *>(shadowNodeList.at(i)); |
| 90 | + |
| 91 | + if (!currentShadowNode) { |
55 | 92 | return EmptyLayoutMetrics; |
56 | 93 | } |
57 | 94 |
|
58 | | - auto frame = layoutableCurrentShadowNode->getLayoutMetrics().frame; |
| 95 | + auto currentFrame = currentShadowNode->getLayoutMetrics().frame; |
| 96 | + if (i == size - 1) { |
| 97 | + // If it's the last element, its origin is irrelevant. |
| 98 | + currentFrame.origin = {0, 0}; |
| 99 | + } |
59 | 100 |
|
60 | 101 | if (policy.includeTransform) { |
61 | | - layoutMetrics.frame.size = layoutMetrics.frame.size * |
62 | | - layoutableCurrentShadowNode->getTransform(); |
63 | | - frame = frame * layoutableCurrentShadowNode->getTransform(); |
| 102 | + resultFrame.size = resultFrame.size * currentShadowNode->getTransform(); |
| 103 | + currentFrame = currentFrame * currentShadowNode->getTransform(); |
64 | 104 | } |
65 | 105 |
|
66 | | - layoutMetrics.frame.origin += frame.origin; |
| 106 | + resultFrame.origin += currentFrame.origin; |
67 | 107 | } |
| 108 | + |
68 | 109 | return layoutMetrics; |
69 | 110 | } |
70 | 111 |
|
@@ -109,37 +150,8 @@ Transform LayoutableShadowNode::getTransform() const { |
109 | 150 | LayoutMetrics LayoutableShadowNode::getRelativeLayoutMetrics( |
110 | 151 | LayoutableShadowNode const &ancestorLayoutableShadowNode, |
111 | 152 | LayoutInspectingPolicy policy) const { |
112 | | - auto &ancestorShadowNode = |
113 | | - dynamic_cast<ShadowNode const &>(ancestorLayoutableShadowNode); |
114 | | - auto &shadowNode = dynamic_cast<ShadowNode const &>(*this); |
115 | | - |
116 | | - if (ShadowNode::sameFamily(shadowNode, ancestorShadowNode)) { |
117 | | - auto layoutMetrics = getLayoutMetrics(); |
118 | | - layoutMetrics.frame.origin = {0, 0}; |
119 | | - return layoutMetrics; |
120 | | - } |
121 | | - |
122 | | - auto ancestors = shadowNode.getFamily().getAncestors(ancestorShadowNode); |
123 | | - |
124 | | - if (ancestors.empty()) { |
125 | | - return EmptyLayoutMetrics; |
126 | | - } |
127 | | - |
128 | | - auto newestChild = |
129 | | - findNewestChildInParent(ancestors.rbegin()->first.get(), shadowNode); |
130 | | - |
131 | | - if (!newestChild) { |
132 | | - return EmptyLayoutMetrics; |
133 | | - } |
134 | | - |
135 | | - auto layoutableNewestChild = |
136 | | - dynamic_cast<LayoutableShadowNode const *>(newestChild); |
137 | | - auto layoutMetrics = layoutableNewestChild->getLayoutMetrics(); |
138 | | - if (policy.includeTransform) { |
139 | | - layoutMetrics.frame = |
140 | | - layoutMetrics.frame * layoutableNewestChild->getTransform(); |
141 | | - } |
142 | | - return calculateOffsetForLayoutMetrics(layoutMetrics, ancestors, policy); |
| 153 | + return computeRelativeLayoutMetrics( |
| 154 | + getFamily(), ancestorLayoutableShadowNode, policy); |
143 | 155 | } |
144 | 156 |
|
145 | 157 | LayoutableShadowNode::UnsharedList |
|
0 commit comments