• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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/frame_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         return node;
34     }
35     node = MakeRefPtr<IfElseNode>(nodeId);
36     ElementRegister::GetInstance()->AddUINode(node);
37     return node;
38 }
39 
40 /*
41  * how if else works:
42  *
43  * the transpiler generated JS code is like this(simplified, but main points shown):
44  *
45  * this.observeComponentCreation(..., () => {
46  * If.create();
47  * if (condition1) {
48  *    this.ifElseBranchUpdateFunction(compilerGeneratedUniqueConstBranchId, () =>
49  *          code for children first render
50  *    })
51  * } else if (condition2) {
52  *    this.ifElseBranchUpdateFunction(compilerGeneratedUniqueConstBranchId, () =>
53  *          code for children first render
54  *    })
55  * } else {
56  *    // else added by the transpiler
57  *    If.setBranchId(compilerGeneratedUniqueConstBranchId)
58  * }
59  * If.pop()
60  *
61  * - if re-renders whenever one of the variables used in 'condition' during last render has changed.
62  *      - the dependent variables can change from one render of if to the next, depending if 'condition1' and
63  *        'condition2' bind to different variables.
64  *      - If can re-render, but the branch does not change, e.g. if 'condition1' is (this.i > 20)
65  *        then If will re-render whenever i changes.
66  *
67  * - eDSL to JS traspiler generates a unique id for each branch of the code inside.
68  *   Thereby the framework can detect that the branch has actually changed.
69  *   In this case ViewPU.ifElseBranchUpdateFunction will call IfElseNode::SetBranchId to upate
70  *   and it will execute the 2nd parameter lambda function to re-generate the children.
71  *
72  * - In case of if without else, or if ... else if ... , eDSL to JS traspiler generates
73  *   an extra else clause in which to set the branchId (calls IfElseNode::SetBranchId)
74  *
75  * - IfElseNode::FlushUpdateAndMarkDirty is called upon If.Pop()  FIXME
76  */
77 
SetBranchId(int32_t value,std::list<int32_t> & removedElmtId)78 void IfElseNode::SetBranchId(int32_t value, std::list<int32_t>& removedElmtId)
79 {
80     branchIdChanged_ = (branchId_ != value);
81     if (branchIdChanged_) {
82         // collect elmtIds of all children and their children up to CustomNode object
83         // these will be removed, but possibly with a delay if their is an animation
84         // list of elmtIds is sent back to calling TS ViewPU.ifElseBranchUpdateFunction()
85         CollectRemovedChildren(GetChildren(), removedElmtId);
86         Clean(false, true);
87         branchId_ = value;
88     }
89 }
90 
CollectRemovedChildren(const std::list<RefPtr<UINode>> & children,std::list<int32_t> & removedElmtId)91 void IfElseNode::CollectRemovedChildren(const std::list<RefPtr<UINode>>& children, std::list<int32_t>& removedElmtId)
92 {
93     for (auto const& child : children) {
94         CollectRemovedChild(child, removedElmtId);
95     }
96 }
97 
CollectRemovedChild(const RefPtr<UINode> & child,std::list<int32_t> & removedElmtId)98 void IfElseNode::CollectRemovedChild(const RefPtr<UINode>& child, std::list<int32_t>& removedElmtId)
99 {
100     removedElmtId.emplace_back(child->GetId());
101     if (child->GetTag() != V2::JS_VIEW_ETS_TAG) {
102         // add CustomNode but do not recurse into its children
103         CollectRemovedChildren(child->GetChildren(), removedElmtId);
104     }
105 }
106 
FlushUpdateAndMarkDirty()107 void IfElseNode::FlushUpdateAndMarkDirty()
108 {
109     if (branchIdChanged_) {
110         auto parent = GetParent();
111         while (parent) {
112             auto frameNode = AceType::DynamicCast<FrameNode>(parent);
113             if (frameNode) {
114                 frameNode->ChildrenUpdatedFrom(0);
115                 break;
116             }
117             parent = parent->GetParent();
118         }
119         // mark parent dirty to flush measure.
120         MarkNeedFrameFlushDirty(PROPERTY_UPDATE_BY_CHILD_REQUEST);
121     }
122     branchIdChanged_ = false;
123 }
124 
TryRetake(const std::string & id)125 bool IfElseNode::TryRetake(const std::string& id)
126 {
127     auto node = GetDisappearingChildById(id);
128     if (node) {
129         AddChild(node);
130         // for geometryTransition, let all reused children call UpdateGeometryTransition.
131         LayoutProperty::UpdateAllGeometryTransition(node);
132         return true;
133     }
134     return false;
135 }
136 
137 } // namespace OHOS::Ace::NG