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