1 /*
2  * Copyright (c) 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_CONTENT_SLOT_NODE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_CONTENT_SLOT_NODE_H
18 
19 #include <cstdint>
20 
21 #include "base/memory/referenced.h"
22 #include "base/utils/macros.h"
23 #include "base/utils/utils.h"
24 #include "core/components_ng/base/ui_node.h"
25 #include "core/components_ng/syntax/node_content.h"
26 #include "core/components_v2/inspector/inspector_constants.h"
27 
28 namespace OHOS::Ace::NG {
29 
30 class ACE_EXPORT ContentSlotNode : public UINode {
31     DECLARE_ACE_TYPE(ContentSlotNode, UINode);
32 
33 public:
34     static RefPtr<ContentSlotNode> GetOrCreateContentSlot(int32_t nodeId);
35 
ContentSlotNode(int32_t nodeId)36     explicit ContentSlotNode(int32_t nodeId) : UINode(V2::JS_NODE_SLOT_ETS_TAG, nodeId) {}
37     ~ContentSlotNode() override = default;
38 
IsAtomicNode()39     bool IsAtomicNode() const override
40     {
41         return true;
42     }
43 
AttachNodeContent(NodeContent * content)44     void AttachNodeContent(NodeContent* content)
45     {
46         CHECK_NULL_VOID(content);
47         // When content_ is held by other slot, the current slot cannot operate the slot held by this content_.
48         if (content_ && (RawPtr(content_) != content) && (content_->GetContentSlot().Upgrade() == this)) {
49             content_->DetachFromNode();
50         }
51         content_ = content;
52         content_->AttachToNode(this);
53     }
54 
DetachNodeContent()55     void DetachNodeContent()
56     {
57         if (content_) {
58             content_->DetachFromNode();
59         }
60         content_ = nullptr;
61     }
62 
OnAttachToMainTree(bool recursive)63     void OnAttachToMainTree(bool recursive) override
64     {
65         UINode::OnAttachToMainTree(recursive);
66         if (content_) {
67             content_->OnAttachToMainTree();
68         }
69     }
70 
71     void OnDetachFromMainTree(bool recursive, PipelineContext* context = nullptr) override
72     {
73         UINode::OnDetachFromMainTree(recursive, context);
74         if (content_) {
75             content_->OnDetachFromMainTree();
76         }
77     }
78 
79 private:
80     RefPtr<NodeContent> content_;
81 
82     ACE_DISALLOW_COPY_AND_MOVE(ContentSlotNode);
83 };
84 
85 } // namespace OHOS::Ace::NG
86 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_CONTENT_SLOT_NODE_H
87