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