• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ClearChildren()58     void ClearChildren()
59     {
60         for (const auto& child : children_) {
61             child->SetParent(nullptr);
62         }
63         children_.clear();
64     }
65 
InsertChild(int32_t position,const RefPtr<Component> & child)66     virtual void InsertChild(int32_t position, const RefPtr<Component>& child)
67     {
68         if (!child) {
69             return;
70         }
71         child->SetParent(WeakClaim(this));
72         auto insertIter = children_.begin();
73         std::advance(insertIter, position);
74         children_.insert(insertIter, child);
75         OnChildInserted(child, position);
76     }
77 
AppendChild(const RefPtr<Component> & child)78     virtual void AppendChild(const RefPtr<Component>& child)
79     {
80         if (!child) {
81             return;
82         }
83         child->SetParent(WeakClaim(this));
84         children_.emplace_back(child);
85         OnChildAppended(child);
86     }
87 
AppendChildDirectly(const RefPtr<Component> & child)88     void AppendChildDirectly(const RefPtr<Component>& child)
89     {
90         if (!child) {
91             return;
92         }
93         child->SetParent(WeakClaim(this));
94         children_.emplace_back(child);
95     }
96 
RemoveChildDirectly(const RefPtr<Component> & child)97     virtual void RemoveChildDirectly(const RefPtr<Component>& child)
98     {
99         if (!child) {
100             return;
101         }
102 
103         child->SetParent(nullptr);
104         children_.pop_back();
105         return;
106     }
107 
RemoveChildByComposedId(const ComposeId & composeId)108     virtual void RemoveChildByComposedId(const ComposeId& composeId)
109     {
110         for (const auto& item : children_) {
111             auto compose = AceType::DynamicCast<ComposedComponent>(item);
112             if (compose && composeId == compose->GetId()) {
113                 auto child = compose->GetChild();
114                 if (child) {
115                     child->SetParent(nullptr);
116                 }
117                 children_.remove(item);
118                 return;
119             }
120         }
121     }
122 
RemoveChild(const RefPtr<Component> & child)123     virtual void RemoveChild(const RefPtr<Component>& child)
124     {
125         if (!child) {
126             return;
127         }
128         auto composedChild = AceType::DynamicCast<ComposedComponent>(child);
129         if (!composedChild) {
130             LOGW("get composed component failed");
131             return;
132         }
133         auto composeId = composedChild->GetId();
134         for (const auto& item : children_) {
135             auto compose = AceType::DynamicCast<ComposedComponent>(item);
136             if (compose && composeId == compose->GetId()) {
137                 child->SetParent(nullptr);
138                 children_.remove(item);
139                 return;
140             }
141         }
142     }
143 
SetUpdateType(UpdateType updateType)144     void SetUpdateType(UpdateType updateType) override
145     {
146         if (GetUpdateType() == updateType) {
147             return;
148         }
149         RenderComponent::SetUpdateType(updateType);
150         for (const auto& child : children_) {
151             child->SetUpdateType(updateType);
152         }
153     }
154 
SetDisabledStatus(bool disabledStatus)155     void SetDisabledStatus(bool disabledStatus) override
156     {
157         if (IsDisabledStatus() == disabledStatus) {
158             return;
159         }
160         RenderComponent::SetDisabledStatus(disabledStatus);
161         for (const auto& child : children_) {
162             child->SetDisabledStatus(disabledStatus);
163         }
164     }
165 
SetTextDirection(TextDirection direction)166     void SetTextDirection(TextDirection direction) override
167     {
168         RenderComponent::SetTextDirection(direction);
169         for (const auto& child : children_) {
170             child->SetTextDirection(direction);
171         }
172     }
173 
ReverseChildren()174     void ReverseChildren()
175     {
176         children_.reverse();
177     }
178 
179 private:
180     std::list<RefPtr<Component>> children_;
181 };
182 
183 } // namespace OHOS::Ace
184 
185 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_COMPONENT_GROUP_H
186