1 /*
2  * Copyright (c) 2021 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_BASE_COMPONENT_GROUP_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_COMPONENT_GROUP_H
18 
19 #include <list>
20 
21 #include "core/pipeline/base/component_group_element.h"
22 #include "core/pipeline/base/composed_component.h"
23 #include "core/pipeline/base/render_component.h"
24 
25 namespace OHOS::Ace {
26 
27 class ComponentGroup : public RenderComponent {
28     DECLARE_ACE_TYPE(ComponentGroup, RenderComponent);
29 
30 public:
31     ComponentGroup() = default;
ComponentGroup(const std::list<RefPtr<Component>> & children)32     explicit ComponentGroup(const std::list<RefPtr<Component>>& children) : children_(children)
33     {
34         InitChildren();
35     }
36     ~ComponentGroup() override = default;
37 
OnChildInserted(const RefPtr<Component> & child,int32_t position)38     virtual void OnChildInserted(const RefPtr<Component>& child, int32_t position) {}
OnChildAppended(const RefPtr<Component> & child)39     virtual void OnChildAppended(const RefPtr<Component>& child) {}
40 
CreateElement()41     RefPtr<Element> CreateElement() override
42     {
43         return AceType::MakeRefPtr<ComponentGroupElement>();
44     }
45 
InitChildren()46     void InitChildren()
47     {
48         for (const auto& child : children_) {
49             child->SetParent(WeakClaim(this));
50         }
51     }
52 
GetChildren()53     const std::list<RefPtr<Component>>& GetChildren() const
54     {
55         return children_;
56     }
57 
GetSizeOfChildren()58     auto GetSizeOfChildren() const
59     {
60         return children_.size();
61     }
62 
ClearChildren()63     void ClearChildren()
64     {
65         for (const auto& child : children_) {
66             child->SetParent(nullptr);
67         }
68         children_.clear();
69     }
70 
InsertChild(int32_t position,const RefPtr<Component> & child)71     virtual void InsertChild(int32_t position, const RefPtr<Component>& child)
72     {
73         if (!child) {
74             return;
75         }
76         child->SetParent(WeakClaim(this));
77         auto insertIter = children_.begin();
78         std::advance(insertIter, position);
79         children_.insert(insertIter, child);
80         OnChildInserted(child, position);
81     }
82 
AppendChild(const RefPtr<Component> & child)83     virtual void AppendChild(const RefPtr<Component>& child)
84     {
85         if (!child) {
86             return;
87         }
88         child->SetParent(WeakClaim(this));
89         children_.emplace_back(child);
90         OnChildAppended(child);
91     }
92 
AppendChildDirectly(const RefPtr<Component> & child)93     void AppendChildDirectly(const RefPtr<Component>& child)
94     {
95         if (!child) {
96             return;
97         }
98         child->SetParent(WeakClaim(this));
99         children_.emplace_back(child);
100     }
101 
RemoveChildDirectly(const RefPtr<Component> & child)102     virtual void RemoveChildDirectly(const RefPtr<Component>& child)
103     {
104         if (!child) {
105             return;
106         }
107 
108         child->SetParent(nullptr);
109         children_.pop_back();
110         return;
111     }
112 
RemoveChildByComposedId(const ComposeId & composeId)113     virtual void RemoveChildByComposedId(const ComposeId& composeId)
114     {
115         for (const auto& item : children_) {
116             auto compose = AceType::DynamicCast<ComposedComponent>(item);
117             if (compose && composeId == compose->GetId()) {
118                 auto child = compose->GetChild();
119                 if (child) {
120                     child->SetParent(nullptr);
121                 }
122                 children_.remove(item);
123                 return;
124             }
125         }
126     }
127 
RemoveChild(const RefPtr<Component> & child)128     virtual void RemoveChild(const RefPtr<Component>& child)
129     {
130         if (!child) {
131             return;
132         }
133         auto composedChild = AceType::DynamicCast<ComposedComponent>(child);
134         if (!composedChild) {
135             LOGW("get composed component failed");
136             return;
137         }
138         auto composeId = composedChild->GetId();
139         for (const auto& item : children_) {
140             auto compose = AceType::DynamicCast<ComposedComponent>(item);
141             if (compose && composeId == compose->GetId()) {
142                 child->SetParent(nullptr);
143                 children_.remove(item);
144                 return;
145             }
146         }
147     }
148 
SetUpdateType(UpdateType updateType)149     void SetUpdateType(UpdateType updateType) override
150     {
151         if (GetUpdateType() == updateType) {
152             return;
153         }
154         RenderComponent::SetUpdateType(updateType);
155         for (const auto& child : children_) {
156             child->SetUpdateType(updateType);
157         }
158     }
159 
SetDisabledStatus(bool disabledStatus)160     void SetDisabledStatus(bool disabledStatus) override
161     {
162         if (IsDisabledStatus() == disabledStatus) {
163             return;
164         }
165         RenderComponent::SetDisabledStatus(disabledStatus);
166         for (const auto& child : children_) {
167             child->SetDisabledStatus(disabledStatus);
168         }
169     }
170 
SetTextDirection(TextDirection direction)171     void SetTextDirection(TextDirection direction) override
172     {
173         RenderComponent::SetTextDirection(direction);
174         for (const auto& child : children_) {
175             child->SetTextDirection(direction);
176         }
177     }
178 
ReverseChildren()179     void ReverseChildren()
180     {
181         children_.reverse();
182     }
183 
184 private:
185     std::list<RefPtr<Component>> children_;
186 };
187 
188 } // namespace OHOS::Ace
189 
190 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_COMPONENT_GROUP_H
191