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