1 /*
2 * Copyright (c) 2021-2022 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.h"
17
18 #include <algorithm>
19
20 #include "core/common/ace_application_info.h"
21 #include "core/pipeline/base/render_component.h"
22 #include "core/pipeline/base/single_child.h"
23 #include "frameworks/core/pipeline/base/element_register.h"
24
25 namespace OHOS::Ace {
26
27 std::atomic<int32_t> Component::key_ = 1;
28
Component()29 Component::Component() : elmtId_(ElementRegister::UndefinedElementId)
30 {
31 SetRetakeId(key_++);
32 direction_ = AceApplicationInfo::GetInstance().IsRightToLeft() ? TextDirection::RTL : TextDirection::LTR;
33 }
34
35 Component::~Component() = default;
36
SetRetakeId(int32_t retakeId)37 void Component::SetRetakeId(int32_t retakeId)
38 {
39 retakeId_ = retakeId;
40 }
41
GetRetakeId() const42 int32_t Component::GetRetakeId() const
43 {
44 return retakeId_;
45 }
46
47 namespace {
48 template<typename T>
IsRenderComponent(const RefPtr<T> & component)49 inline bool IsRenderComponent(const RefPtr<T>& component)
50 {
51 return AceType::InstanceOf<RenderComponent>(component);
52 }
53 } // namespace
54
MergeRSNode(const std::vector<RefPtr<Component>> & components)55 void Component::MergeRSNode(const std::vector<RefPtr<Component>>& components)
56 {
57 if (components.empty()) {
58 return;
59 }
60 // locate first & last RenderComponent
61 auto head = std::find_if(components.begin(), components.end(), IsRenderComponent<Component>);
62 auto tail = std::find_if(components.rbegin(), components.rend(), IsRenderComponent<Component>);
63 if (head == components.end() || tail == components.rend()) {
64 return;
65 }
66 (*head)->isHeadComponent_ = true;
67 (*tail)->isTailComponent_ = true;
68 }
69
MergeRSNode(const std::vector<RefPtr<SingleChild>> & components)70 void Component::MergeRSNode(const std::vector<RefPtr<SingleChild>>& components)
71 {
72 if (components.empty()) {
73 return;
74 }
75 // locate first & last RenderComponent
76 auto head = std::find_if(components.begin(), components.end(), IsRenderComponent<SingleChild>);
77 auto tail = std::find_if(components.rbegin(), components.rend(), IsRenderComponent<SingleChild>);
78 if (head == components.end() || tail == components.rend()) {
79 return;
80 }
81 AceType::DynamicCast<Component>(*head)->isHeadComponent_ = true;
82 AceType::DynamicCast<Component>(*tail)->isTailComponent_ = true;
83 }
84
MergeRSNode(const std::vector<RefPtr<SingleChild>> & components,const RefPtr<Component> & mainComponent)85 void Component::MergeRSNode(const std::vector<RefPtr<SingleChild>>& components, const RefPtr<Component>& mainComponent)
86 {
87 auto head = std::find_if(components.begin(), components.end(), IsRenderComponent<SingleChild>);
88 auto tail = std::find_if(components.rbegin(), components.rend(), IsRenderComponent<SingleChild>);
89 if (components.empty() || head == components.end() || tail == components.rend()) {
90 return MergeRSNode(mainComponent);
91 }
92 // locate first & last RenderComponent
93 AceType::DynamicCast<Component>(*head)->isHeadComponent_ = true;
94 if (IsRenderComponent(mainComponent)) {
95 mainComponent->isTailComponent_ = true;
96 } else {
97 AceType::DynamicCast<Component>(*tail)->isTailComponent_ = true;
98 }
99 }
100
MergeRSNode(const RefPtr<Component> & head,const RefPtr<Component> & tail)101 void Component::MergeRSNode(const RefPtr<Component>& head, const RefPtr<Component>& tail)
102 {
103 if (!head || !tail) {
104 return;
105 }
106 head->isHeadComponent_ = true;
107 tail->isTailComponent_ = true;
108 }
109
MergeRSNode(const RefPtr<Component> & standaloneNode)110 void Component::MergeRSNode(const RefPtr<Component>& standaloneNode)
111 {
112 if (!standaloneNode || !AceType::InstanceOf<RenderComponent>(standaloneNode)) {
113 return;
114 }
115 standaloneNode->isHeadComponent_ = true;
116 standaloneNode->isTailComponent_ = true;
117 }
118
ExtendRSNode(const RefPtr<Component> & newHead,const RefPtr<Component> & prevHead)119 void Component::ExtendRSNode(const RefPtr<Component>& newHead, const RefPtr<Component>& prevHead)
120 {
121 if (!newHead || !prevHead) {
122 return;
123 }
124 newHead->isHeadComponent_ = prevHead->isHeadComponent_;
125 prevHead->isHeadComponent_ = false;
126 }
127
AssignUniqueElementId(int32_t elmtId)128 void Component::AssignUniqueElementId(int32_t elmtId)
129 {
130 elmtId_ = elmtId;
131 }
132
133 } // namespace OHOS::Ace
134