1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "core/components_ng/syntax/if_else_node.h"
17
18 #include <list>
19 #include <type_traits>
20
21 #include "base/log/ace_trace.h"
22 #include "core/components_ng/base/ui_node.h"
23 #include "core/components_ng/layout/layout_property.h"
24 #include "core/components_ng/property/property.h"
25 #include "core/pipeline/base/element_register.h"
26
27 namespace OHOS::Ace::NG {
28
GetOrCreateIfElseNode(int32_t nodeId)29 RefPtr<IfElseNode> IfElseNode::GetOrCreateIfElseNode(int32_t nodeId)
30 {
31 auto node = ElementRegister::GetInstance()->GetSpecificItemById<IfElseNode>(nodeId);
32 if (node) {
33 LOGD("Returning existing IfElseNode with nodeId %{public}d", nodeId);
34 return node;
35 }
36 node = MakeRefPtr<IfElseNode>(nodeId);
37 ElementRegister::GetInstance()->AddUINode(node);
38 LOGD("Created new IfElseNode with nodeId %{public}d", nodeId);
39 return node;
40 }
41
42 /*
43 * how if else works:
44 *
45 * the transpiler generated JS code is like this(simplified, but main points shown):
46 *
47 * this.observeComponentCreation(..., () => {
48 * If.create();
49 * if (condition1) {
50 * this.ifElseBranchUpdateFunction(compilerGeneratedUniqueConstBranchId, () =>
51 * code for children first render
52 * })
53 * } else if (condition2) {
54 * this.ifElseBranchUpdateFunction(compilerGeneratedUniqueConstBranchId, () =>
55 * code for children first render
56 * })
57 * } else {
58 * // else added by the transpiler
59 * If.setBranchId(compilerGeneratedUniqueConstBranchId)
60 * }
61 * If.pop()
62 *
63 * - if re-renders whenever one of the variables used in 'condition' during last render has changed.
64 * - the dependent variables can change from one render of if to the next, depending if 'condition1' and
65 * 'condition2' bind to different variables.
66 * - If can re-render, but the branch does not change, e.g. if 'condition1' is (this.i > 20)
67 * then If will re-render whenever i changes.
68 *
69 * - eDSL to JS traspiler generates a unique id for each branch of the code inside.
70 * Thereby the framework can detect that the branch has actually changed.
71 * In this case ViewPU.ifElseBranchUpdateFunction will call IfElseNode::SetBranchId to upate
72 * and it will execute the 2nd parameter lambda function to re-generate the children.
73 *
74 * - In case of if without else, or if ... else if ... , eDSL to JS traspiler generates
75 * an extra else clause in which to set the branchId (calls IfElseNode::SetBranchId)
76 *
77 * - IfElseNode::FlushUpdateAndMarkDirty is called upon If.Pop() FIXME
78 */
79
SetBranchId(int32_t value,std::list<int32_t> & removedElmtId)80 void IfElseNode::SetBranchId(int32_t value, std::list<int32_t>& removedElmtId)
81 {
82 branchIdChanged_ = (branchId_ != value);
83 if (branchIdChanged_) {
84 LOGD("%{public}s id %{public}d, branchId changed to %{public}d", AceType::TypeName(this), GetId(), value);
85 // collect elmtIds of all children and their children up to CustomNode object
86 // these will be removed, but possibly with a delay if their is an animation
87 // list of elmtIds is sent back to calling TS ViewPU.ifElseBranchUpdateFunction()
88 CollectRemovedChildren(GetChildren(), removedElmtId);
89 Clean(false, true);
90 branchId_ = value;
91 }
92 }
93
CollectRemovedChildren(const std::list<RefPtr<UINode>> & children,std::list<int32_t> & removedElmtId)94 void IfElseNode::CollectRemovedChildren(const std::list<RefPtr<UINode>>& children, std::list<int32_t>& removedElmtId)
95 {
96 for (auto const& child : children) {
97 CollectRemovedChild(child, removedElmtId);
98 }
99 }
100
CollectRemovedChild(const RefPtr<UINode> & child,std::list<int32_t> & removedElmtId)101 void IfElseNode::CollectRemovedChild(const RefPtr<UINode>& child, std::list<int32_t>& removedElmtId)
102 {
103 LOGD("add %{public}s elmtId %{public}d", child->GetTag().c_str(), (int32_t)child->GetId());
104 removedElmtId.emplace_back(child->GetId());
105 if (child->GetTag() != V2::JS_VIEW_ETS_TAG) {
106 // add CustomNode but do not recurse into its children
107 CollectRemovedChildren(child->GetChildren(), removedElmtId);
108 }
109 }
110
FlushUpdateAndMarkDirty()111 void IfElseNode::FlushUpdateAndMarkDirty()
112 {
113 if (branchIdChanged_) {
114 LOGD("%{public}s id %{public}d, branchId changed, resetting with %{public}d new children",
115 AceType::TypeName(this), GetId(), static_cast<int32_t>(GetChildren().size()));
116 // mark parent dirty to flush measure.
117 MarkNeedFrameFlushDirty(PROPERTY_UPDATE_BY_CHILD_REQUEST);
118 }
119 branchIdChanged_ = false;
120 }
121
TryRetake(const std::string & id)122 bool IfElseNode::TryRetake(const std::string& id)
123 {
124 auto node = GetDisappearingChildById(id);
125 if (node) {
126 AddChild(node);
127 // for geometryTransition, let all reused children call UpdateGeometryTransition.
128 LayoutProperty::UpdateAllGeometryTransition(node);
129 return true;
130 }
131 return false;
132 }
133
134 } // namespace OHOS::Ace::NG