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 /* how if else works
41  * the transpiler generated JS code is like this(simplified, but main points shown)
42  * this.observeComponentCreation(..., () => {
43  * If.create();
44  * if (condition1) {
45  *    this.ifElseBranchUpdateFunction(compilerGeneratedUniqueConstBranchId, () =>
46  *          code for children first render
47  *    })
48  * } else if (condition2) {
49  *    this.ifElseBranchUpdateFunction(compilerGeneratedUniqueConstBranchId, () =>
50  *          code for children first render
51  *    })
52  * } else {
53  *    // else added by the transpiler
54  *    If.setBranchId(compilerGeneratedUniqueConstBranchId)
55  * }
56  * If.pop()
57  * - if re-renders whenever one of the variables used in 'condition' during last render has changed.
58  *      - the dependent variables can change from one render of if to the next, depending if 'condition1' and
59  *        'condition2' bind to different variables.
60  *      - If can re-render, but the branch does not change, e.g. if 'condition1' is (this.i > 20)
61  *        then If will re-render whenever i changes.
62  * - eDSL to JS traspiler generates a unique id for each branch of the code inside.
63  *   Thereby the framework can detect that the branch has actually changed.
64  *   In this case ViewPU.ifElseBranchUpdateFunction will call IfElseNode::SetBranchId to upate
65  *   and it will execute the 2nd parameter lambda function to re-generate the children.
66  * - In case of if without else, or if ... else if ... , eDSL to JS traspiler generates
67  *   an extra else clause in which to set the branchId (calls IfElseNode::SetBranchId)
68  * - IfElseNode::FlushUpdateAndMarkDirty is called upon If.Pop()
69  */
SetBranchId(int32_t value,std::list<int32_t> & removedElmtId)70 void IfElseNode::SetBranchId(int32_t value, std::list<int32_t>& removedElmtId)
71 {
72     branchIdChanged_ = (branchId_ != value);
73     TAG_LOGD(AceLogTag::ACE_IF, "id: %{public}d, branchIdChanged_: %{public}d", GetId(), branchIdChanged_);
74     if (branchIdChanged_) {
75         // collect elmtIds of all children and their children up to CustomNode object
76         // these will be removed, but possibly with a delay if their is an animation
77         // list of elmtIds is sent back to calling TS ViewPU.ifElseBranchUpdateFunction()
78         Clean(false, true, branchId_);
79         CollectRemovedChildren(GetChildren(), removedElmtId, true);
80         branchId_ = value;
81     }
82 }
83 
FlushUpdateAndMarkDirty()84 void IfElseNode::FlushUpdateAndMarkDirty()
85 {
86     if (branchIdChanged_) {
87         auto parent = GetParent();
88         while (parent) {
89             auto frameNode = AceType::DynamicCast<FrameNode>(parent);
90             if (frameNode) {
91                 frameNode->ChildrenUpdatedFrom(0);
92                 break;
93             }
94             parent = parent->GetParent();
95         }
96         // mark parent dirty to flush measure.
97         MarkNeedFrameFlushDirty(PROPERTY_UPDATE_BY_CHILD_REQUEST);
98     }
99     branchIdChanged_ = false;
100 }
101 
TryRetake(const std::string & id)102 bool IfElseNode::TryRetake(const std::string& id)
103 {
104     auto node = GetDisappearingChildById(id, branchId_);
105     if (node) {
106         ACE_SCOPED_TRACE("IfElse TryRetake validate.");
107         AddChild(node);
108         // for geometryTransition, let all reused children call UpdateGeometryTransition.
109         LayoutProperty::UpdateAllGeometryTransition(node);
110         return true;
111     }
112     return false;
113 }
114 
115 } // namespace OHOS::Ace::NG
116