1 /*
2 * Copyright (c) 2024-2025 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/node_content.h"
17
18 #include "core/common/builder_util.h"
19
20 namespace OHOS::Ace::NG {
21
AttachToNode(UINode * node)22 void NodeContent::AttachToNode(UINode* node)
23 {
24 auto slot = nodeSlot_.Upgrade();
25 if (slot && (slot != node)) {
26 DetachFromNode();
27 }
28 nodeSlot_ = WeakClaim(node);
29 std::list<RefPtr<NG::UINode>> nodes;
30 for (const auto& child : children_) {
31 node->AddChild(child);
32 BuilderUtils::GetBuilderNodes(child, nodes);
33 }
34 BuilderUtils::AddBuilderToParent(Claim(node), nodes);
35 node->MarkNeedFrameFlushDirty(PROPERTY_UPDATE_BY_CHILD_REQUEST);
36 if (node->IsOnMainTree()) {
37 OnAttachToMainTree();
38 }
39 }
40
DetachFromNode()41 void NodeContent::DetachFromNode()
42 {
43 auto slot = nodeSlot_.Upgrade();
44 nodeSlot_.Reset();
45 if (slot) {
46 children_ = slot->GetChildren();
47 slot->Clean();
48 std::list<RefPtr<NG::UINode>> nodes;
49 for (const auto& child : children_) {
50 BuilderUtils::GetBuilderNodes(child, nodes);
51 }
52 BuilderUtils::RemoveBuilderFromParent(slot, nodes);
53 if (slot->IsOnMainTree()) {
54 OnDetachFromMainTree();
55 }
56 }
57 }
58
AddNode(UINode * node,int32_t position)59 void NodeContent::AddNode(UINode* node, int32_t position)
60 {
61 auto slot = nodeSlot_.Upgrade();
62 auto child = Claim(node);
63 if (slot) {
64 slot->AddChild(child, position);
65 BuilderUtils::AddBuilderToParent(slot, child);
66 slot->MarkNeedFrameFlushDirty(PROPERTY_UPDATE_BY_CHILD_REQUEST);
67 }
68 auto it = std::find(children_.begin(), children_.end(), child);
69 if (it != children_.end()) {
70 return;
71 }
72
73 it = children_.begin();
74 std::advance(it, position);
75 children_.insert(it, child);
76 }
77
RemoveNode(UINode * node)78 void NodeContent::RemoveNode(UINode* node)
79 {
80 auto nodeRef = Claim(node);
81 auto slot = nodeSlot_.Upgrade();
82 if (slot) {
83 slot->RemoveChild(nodeRef);
84 BuilderUtils::RemoveBuilderFromParent(slot, nodeRef);
85 slot->MarkNeedFrameFlushDirty(PROPERTY_UPDATE_BY_CHILD_REQUEST);
86 }
87 auto it = std::find(children_.begin(), children_.end(), nodeRef);
88 if (it == children_.end()) {
89 return;
90 }
91 children_.erase(it);
92 }
93
OnAttachToMainTree()94 void NodeContent::OnAttachToMainTree()
95 {
96 if (onMainTree_) {
97 return;
98 }
99 onMainTree_ = true;
100 if (onAttachCallback_) {
101 onAttachCallback_();
102 }
103 }
104
OnDetachFromMainTree()105 void NodeContent::OnDetachFromMainTree()
106 {
107 if (!onMainTree_) {
108 return;
109 }
110 onMainTree_ = false;
111 if (onDetachCallback_) {
112 onDetachCallback_();
113 }
114 }
115
116 } // namespace OHOS::Ace::NG
117