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/pattern/custom/custom_node.h"
17
18 #include "base/log/dump_log.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/components_ng/base/view_stack_processor.h"
21 #include "core/components_ng/pattern/custom/custom_node_pattern.h"
22 #include "core/components_v2/inspector/inspector_constants.h"
23 #include "core/pipeline/base/element_register.h"
24 #include "core/pipeline_ng/pipeline_context.h"
25 #include "core/pipeline_ng/ui_task_scheduler.h"
26
27 namespace OHOS::Ace::NG {
CreateCustomNode(int32_t nodeId,const std::string & viewKey)28 RefPtr<CustomNode> CustomNode::CreateCustomNode(int32_t nodeId, const std::string& viewKey)
29 {
30 auto node = MakeRefPtr<CustomNode>(nodeId, viewKey);
31 ElementRegister::GetInstance()->AddUINode(node);
32 return node;
33 }
34
CustomNode(int32_t nodeId,const std::string & viewKey)35 CustomNode::CustomNode(int32_t nodeId, const std::string& viewKey)
36 : UINode(V2::JS_VIEW_ETS_TAG, nodeId, MakeRefPtr<CustomNodePattern>()), viewKey_(viewKey)
37 {}
38
Build()39 void CustomNode::Build()
40 {
41 Render();
42 UINode::Build();
43 }
44
Render()45 void CustomNode::Render()
46 {
47 // NOTE: this function will be re-enter, we need backup needMarkParent_ first and restore it later.
48 bool needMarkParentBak = needMarkParent_;
49 needMarkParent_ = false;
50 if (renderFunction_) {
51 auto renderFunction = std::move(renderFunction_);
52 {
53 ACE_SCOPED_TRACE("CustomNode:OnAppear");
54 FireOnAppear();
55 }
56 {
57 ACE_SCOPED_TRACE("CustomNode:BuildItem %s", GetJSViewName().c_str());
58 // first create child node and wrapper.
59 ScopedViewStackProcessor scopedViewStackProcessor;
60 auto child = renderFunction();
61 if (child) {
62 child->MountToParent(Claim(this));
63 }
64 }
65 }
66 {
67 FireRecycleRenderFunc();
68 }
69 needMarkParent_ = needMarkParentBak;
70 }
71
72 // used in HotReload to update root view @Component
FlushReload()73 void CustomNode::FlushReload()
74 {
75 CHECK_NULL_VOID(completeReloadFunc_);
76 Clean();
77 renderFunction_ = completeReloadFunc_;
78 Render();
79 }
80
SetJSViewActive(bool active)81 void CustomNode::SetJSViewActive(bool active)
82 {
83 FireSetActiveFunc(active);
84 }
85
AdjustLayoutWrapperTree(const RefPtr<LayoutWrapperNode> & parent,bool forceMeasure,bool forceLayout)86 void CustomNode::AdjustLayoutWrapperTree(const RefPtr<LayoutWrapperNode>& parent, bool forceMeasure, bool forceLayout)
87 {
88 if (parent->GetHostTag() != V2::TAB_CONTENT_ITEM_ETS_TAG) {
89 Render();
90 UINode::AdjustLayoutWrapperTree(parent, forceMeasure, forceLayout);
91 return;
92 }
93
94 if (!renderFunction_ && !HasRecycleRenderFunc()) {
95 UINode::AdjustLayoutWrapperTree(parent, forceMeasure, forceLayout);
96 return;
97 }
98
99 parent->AppendChild(MakeRefPtr<LayoutWrapperNode>(
100 [weak = AceType::WeakClaim(this), forceMeasure, forceLayout](RefPtr<LayoutWrapperNode> layoutWrapper) {
101 auto customNode = weak.Upgrade();
102 CHECK_NULL_VOID(customNode);
103
104 customNode->Render();
105 if (customNode->GetChildren().empty()) {
106 return;
107 }
108 auto child = customNode->GetChildren().front();
109 while (!InstanceOf<FrameNode>(child)) {
110 auto custom = DynamicCast<CustomNode>(child);
111 if (custom) {
112 custom->Render();
113 }
114 auto children = child->GetChildren();
115 if (children.empty()) {
116 return;
117 }
118 child = children.front();
119 }
120 auto frameChild = DynamicCast<FrameNode>(child);
121 CHECK_NULL_VOID(frameChild);
122 frameChild->UpdateLayoutWrapper(layoutWrapper, forceMeasure, forceLayout);
123 }));
124 }
125
CreateLayoutWrapper(bool forceMeasure,bool forceLayout)126 RefPtr<LayoutWrapperNode> CustomNode::CreateLayoutWrapper(bool forceMeasure, bool forceLayout)
127 {
128 Build();
129 return UINode::CreateLayoutWrapper(forceMeasure, forceLayout);
130 }
131
MarkNeedSyncRenderTree(bool needRebuild)132 void CustomNode::MarkNeedSyncRenderTree(bool needRebuild)
133 {
134 if (needMarkParent_) {
135 UINode::MarkNeedSyncRenderTree(needRebuild);
136 }
137 }
138
GetFrameChildByIndex(uint32_t index,bool needBuild)139 RefPtr<UINode> CustomNode::GetFrameChildByIndex(uint32_t index, bool needBuild)
140 {
141 Render();
142 return UINode::GetFrameChildByIndex(index, needBuild);
143 }
144 } // namespace OHOS::Ace::NG
145