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