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 #include "core/pipeline/base/component_group_element.h"
17
18 #include "core/pipeline/base/component_group.h"
19
20 namespace OHOS::Ace {
21
Create()22 RefPtr<Element> ComponentGroupElement::Create()
23 {
24 return AceType::MakeRefPtr<ComponentGroupElement>();
25 }
26
PerformBuild()27 void ComponentGroupElement::PerformBuild()
28 {
29 RefPtr<ComponentGroup> componentGroup = AceType::DynamicCast<ComponentGroup>(component_);
30 if (!componentGroup) {
31 LOGW("Should be component group, but %s", AceType::TypeName(component_));
32 return;
33 }
34
35 auto context = context_.Upgrade();
36 if (!context) {
37 LOGW("Pipeline context is invalid");
38 return;
39 }
40
41 const auto& newComponents = componentGroup->GetChildren();
42 if (context->GetIsDeclarative() && componentGroup->GetUpdateType() != UpdateType::REBUILD) {
43 UpdateChildrenForDeclarative(newComponents);
44 } else {
45 UpdateChildren(newComponents);
46 }
47 }
48
UpdateChildren(const std::list<RefPtr<Component>> & newComponents)49 void ComponentGroupElement::UpdateChildren(const std::list<RefPtr<Component>>& newComponents)
50 {
51 auto itChild = children_.begin();
52 auto itChildEnd = children_.end();
53 auto itComponent = newComponents.begin();
54 auto itComponentEnd = newComponents.end();
55
56 while (itChild != itChildEnd && itComponent != itComponentEnd) {
57 const auto& child = *itChild;
58 const auto& component = *itComponent;
59 if (child->NeedUpdateWithComponent(component)) {
60 if (!child->CanUpdate(component)) {
61 break;
62 }
63 UpdateChild(child, component);
64 }
65 ++itChild;
66 ++itComponent;
67 }
68
69 // children_ will be modified during UpdateChild.(some items will be removed from children_)
70 while (itChild != itChildEnd) {
71 UpdateChild(*(itChild++), nullptr);
72 }
73
74 while (itComponent != itComponentEnd) {
75 UpdateChild(nullptr, *(itComponent++));
76 }
77 }
78
UpdateChildrenForDeclarative(const std::list<RefPtr<Component>> & newComponents)79 void ComponentGroupElement::UpdateChildrenForDeclarative(const std::list<RefPtr<Component>>& newComponents)
80 {
81 int32_t slot = 0;
82 int32_t renderSlot = 0;
83 #if defined(PREVIEW)
84 if (newComponents.empty()) {
85 auto itChild = children_.begin();
86 auto itChildEnd = children_.end();
87 while (itChild != itChildEnd) {
88 UpdateChild(*(itChild++), nullptr);
89 }
90 }
91 #endif
92 if (children_.empty()) {
93 for (const auto& component : newComponents) {
94 auto newChild = UpdateChildWithSlot(nullptr, component, slot++, renderSlot);
95 if (newChild) {
96 renderSlot += newChild->CountRenderNode();
97 }
98 }
99 return;
100 }
101
102 // For declarative frontend, the component tree is very stable,
103 // so size of children MUST be matched between elements and components
104 if (children_.size() != newComponents.size()) {
105 LOGW("Size of old children and new components are mismatched");
106 return;
107 }
108 auto itChild = children_.begin();
109 for (const auto& component : newComponents) {
110 auto newChild = UpdateChildWithSlot(*(itChild++), component, slot++, renderSlot);
111 renderSlot += newChild->CountRenderNode();
112 }
113 }
114
115 } // namespace OHOS::Ace
116