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/components/page/page_element.h"
17
18 #include "base/log/dump_log.h"
19 #include "core/common/frontend.h"
20 #include "core/common/text_field_manager.h"
21 #include "core/components/transform/transform_element.h"
22 #include "core/components_v2/inspector/inspector_composed_element.h"
23
24 namespace OHOS::Ace {
25
PageElement(int32_t pageId,const std::string & pageUrl,const ComposeId & id)26 PageElement::PageElement(int32_t pageId, const std::string& pageUrl, const ComposeId& id)
27 : ComposedElement(id), pageId_(pageId), pageUrl_(pageUrl)
28 {}
29
PageElement(int32_t pageId,const std::string & pageUrl,const ComposeId & cardComposeId,const ComposeId & id)30 PageElement::PageElement(
31 int32_t pageId, const std::string& pageUrl, const ComposeId& cardComposeId, const ComposeId& id)
32 : ComposedElement(id), pageId_(pageId), pageUrl_(pageUrl), cardComposeId_(cardComposeId)
33 {}
34
~PageElement()35 PageElement::~PageElement()
36 {
37 auto context = context_.Upgrade();
38 if (!context) {
39 return;
40 }
41 auto textFieldManager = context->GetTextFieldManager();
42 if (textFieldManager) {
43 textFieldManager->RemovePageId(pageId_);
44 }
45 }
46
RequestNextFocus(bool vertical,bool reverse,const Rect & rect)47 bool PageElement::RequestNextFocus(bool vertical, bool reverse, const Rect& rect)
48 {
49 // Do not precess logic.
50 return false;
51 }
52
RemoveSharedTransition(const ShareId & shareId)53 void PageElement::RemoveSharedTransition(const ShareId& shareId)
54 {
55 LOGD("Remove shared transition id:%{public}s", shareId.c_str());
56 sharedTransitionElementMap_.erase(shareId);
57 }
58
AddSharedTransition(const RefPtr<SharedTransitionElement> & shared)59 void PageElement::AddSharedTransition(const RefPtr<SharedTransitionElement>& shared)
60 {
61 if (!shared) {
62 LOGE("Add shared transition failed. element is null.");
63 return;
64 }
65 LOGD("Add shared transition element id:%{public}s", shared->GetShareId().c_str());
66 sharedTransitionElementMap_[shared->GetShareId()] = shared;
67 }
68
GetSharedTransitionMap() const69 const PageElement::SharedTransitionMap& PageElement::GetSharedTransitionMap() const
70 {
71 return sharedTransitionElementMap_;
72 }
73
SetHidden(bool hidden)74 void PageElement::SetHidden(bool hidden)
75 {
76 auto render = GetRenderNode();
77 if (render) {
78 auto parent = render->GetParent().Upgrade();
79 if (parent) {
80 parent->MarkNeedRender();
81 }
82 render->SetHidden(hidden);
83 }
84
85 for (auto&& [id, callback] : hiddenCallbackMap_) {
86 callback(hidden);
87 }
88 }
89
RemoveCardTransition(int32_t retakeId)90 void PageElement::RemoveCardTransition(int32_t retakeId)
91 {
92 cardTransitionMap_.erase(retakeId);
93 }
94
AddCardTransition(const RefPtr<TransformElement> & transform)95 void PageElement::AddCardTransition(const RefPtr<TransformElement>& transform)
96 {
97 if (!transform) {
98 LOGE("Add transform transition failed. element is null.");
99 return;
100 }
101 cardTransitionMap_[transform->GetRetakeId()] = transform;
102 }
103
GetCardTransitionMap() const104 const PageElement::CardTransitionMap& PageElement::GetCardTransitionMap() const
105 {
106 return cardTransitionMap_;
107 }
108
AddGeometryTransition(const std::string & id,WeakPtr<BoxElement> & boxElement,AnimationOption & option)109 void PageElement::AddGeometryTransition(const std::string& id, WeakPtr<BoxElement>& boxElement, AnimationOption& option)
110 {
111 if (geometryTransitionMap_.find(id) == geometryTransitionMap_.end()) {
112 GeometryTransitionInfo sharedInfo;
113 sharedInfo.sharedAnimationOption = option;
114 sharedInfo.appearElement = boxElement;
115 sharedInfo.isNeedCreate = false;
116 geometryTransitionMap_.emplace(id, sharedInfo);
117 } else {
118 if (geometryTransitionMap_[id].appearElement != boxElement && !geometryTransitionMap_[id].isNeedCreate) {
119 geometryTransitionMap_[id].disappearElement = geometryTransitionMap_[id].appearElement;
120 geometryTransitionMap_[id].appearElement = boxElement;
121 geometryTransitionMap_[id].sharedAnimationOption = option;
122 geometryTransitionMap_[id].isNeedCreate = true;
123 }
124 }
125 }
126
GetGeometryTransition() const127 const PageElement::GeometryTransitionMap& PageElement::GetGeometryTransition() const
128 {
129 return geometryTransitionMap_;
130 }
131
RemoveGeometryTransition(const std::string & id)132 void PageElement::RemoveGeometryTransition(const std::string& id)
133 {
134 if (geometryTransitionMap_.find(id) != geometryTransitionMap_.end()) {
135 geometryTransitionMap_.erase(id);
136 }
137 }
138
FinishCreateGeometryTransition(const std::string & id)139 void PageElement::FinishCreateGeometryTransition(const std::string& id)
140 {
141 geometryTransitionMap_[id].isNeedCreate = false;
142 }
143
Dump()144 void PageElement::Dump()
145 {
146 for (const auto& item : geometryTransitionMap_) {
147 DumpLog::GetInstance().AddDesc(std::string("geometryTransitionID: ").append(item.first));
148 auto element = item.second.appearElement.Upgrade();
149 if (!element) {
150 continue;
151 }
152 std::string retakeId = std::to_string(element->GetRetakeId());
153 DumpLog::GetInstance().AddDesc(std::string("RetakeId: ").append(retakeId));
154 }
155 }
156
GetComponentsCount()157 int32_t PageElement::GetComponentsCount()
158 {
159 int32_t result = 0;
160 std::queue<RefPtr<Element>> elements;
161 elements.push(AceType::Claim(this));
162 while (!elements.empty()) {
163 auto& element = elements.front();
164 auto inspectorElement = AceType::DynamicCast<V2::InspectorComposedElement>(element);
165 if (inspectorElement != nullptr) {
166 result++;
167 }
168 const auto& children = element->GetChildren();
169 for (const auto& child : children) {
170 elements.push(child);
171 }
172 elements.pop();
173 }
174
175 return result;
176 }
177
178 } // namespace OHOS::Ace