• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     sharedTransitionElementMap_.erase(shareId);
56 }
57 
AddSharedTransition(const RefPtr<SharedTransitionElement> & shared)58 void PageElement::AddSharedTransition(const RefPtr<SharedTransitionElement>& shared)
59 {
60     if (!shared) {
61         LOGE("Add shared transition failed. element is null.");
62         return;
63     }
64     sharedTransitionElementMap_[shared->GetShareId()] = shared;
65 }
66 
GetSharedTransitionMap() const67 const PageElement::SharedTransitionMap& PageElement::GetSharedTransitionMap() const
68 {
69     return sharedTransitionElementMap_;
70 }
71 
SetHidden(bool hidden)72 void PageElement::SetHidden(bool hidden)
73 {
74     auto render = GetRenderNode();
75     if (render) {
76         auto parent = render->GetParent().Upgrade();
77         if (parent) {
78             parent->MarkNeedRender();
79         }
80         render->SetHidden(hidden);
81     }
82 
83     for (auto&& [id, callback] : hiddenCallbackMap_) {
84         callback(hidden);
85     }
86 }
87 
RemoveCardTransition(int32_t retakeId)88 void PageElement::RemoveCardTransition(int32_t retakeId)
89 {
90     cardTransitionMap_.erase(retakeId);
91 }
92 
AddCardTransition(const RefPtr<TransformElement> & transform)93 void PageElement::AddCardTransition(const RefPtr<TransformElement>& transform)
94 {
95     if (!transform) {
96         LOGE("Add transform transition failed. element is null.");
97         return;
98     }
99     cardTransitionMap_[transform->GetRetakeId()] = transform;
100 }
101 
GetCardTransitionMap() const102 const PageElement::CardTransitionMap& PageElement::GetCardTransitionMap() const
103 {
104     return cardTransitionMap_;
105 }
106 
AddGeometryTransition(const std::string & id,WeakPtr<BoxElement> & boxElement,AnimationOption & option)107 void PageElement::AddGeometryTransition(const std::string& id, WeakPtr<BoxElement>& boxElement, AnimationOption& option)
108 {
109     if (geometryTransitionMap_.find(id) == geometryTransitionMap_.end()) {
110         GeometryTransitionInfo sharedInfo;
111         sharedInfo.sharedAnimationOption = option;
112         sharedInfo.appearElement = boxElement;
113         sharedInfo.isNeedCreate = false;
114         geometryTransitionMap_.emplace(id, sharedInfo);
115     } else {
116         if (geometryTransitionMap_[id].appearElement != boxElement && !geometryTransitionMap_[id].isNeedCreate) {
117             geometryTransitionMap_[id].disappearElement = geometryTransitionMap_[id].appearElement;
118             geometryTransitionMap_[id].appearElement = boxElement;
119             geometryTransitionMap_[id].sharedAnimationOption = option;
120             geometryTransitionMap_[id].isNeedCreate = true;
121         }
122     }
123 }
124 
GetGeometryTransition() const125 const PageElement::GeometryTransitionMap& PageElement::GetGeometryTransition() const
126 {
127     return geometryTransitionMap_;
128 }
129 
RemoveGeometryTransition(const std::string & id)130 void PageElement::RemoveGeometryTransition(const std::string& id)
131 {
132     if (geometryTransitionMap_.find(id) != geometryTransitionMap_.end()) {
133         geometryTransitionMap_.erase(id);
134     }
135 }
136 
FinishCreateGeometryTransition(const std::string & id)137 void PageElement::FinishCreateGeometryTransition(const std::string& id)
138 {
139     geometryTransitionMap_[id].isNeedCreate = false;
140 }
141 
Dump()142 void PageElement::Dump()
143 {
144     for (const auto& item : geometryTransitionMap_) {
145         DumpLog::GetInstance().AddDesc(std::string("geometryTransitionID: ").append(item.first));
146         auto element = item.second.appearElement.Upgrade();
147         if (!element) {
148             continue;
149         }
150         std::string retakeId = std::to_string(element->GetRetakeId());
151         DumpLog::GetInstance().AddDesc(std::string("RetakeId: ").append(retakeId));
152     }
153 }
154 
GetComponentsCount()155 int32_t PageElement::GetComponentsCount()
156 {
157     int32_t result = 0;
158     std::queue<RefPtr<Element>> elements;
159     elements.push(AceType::Claim(this));
160     while (!elements.empty()) {
161         auto& element = elements.front();
162         auto inspectorElement = AceType::DynamicCast<V2::InspectorComposedElement>(element);
163         if (inspectorElement != nullptr) {
164             result++;
165         }
166         const auto& children = element->GetChildren();
167         for (const auto& child : children) {
168             elements.push(child);
169         }
170         elements.pop();
171     }
172 
173     return result;
174 }
175 
176 } // namespace OHOS::Ace