1 /*
2 * Copyright (c) 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_ng/pattern/stage/stage_manager.h"
17
18 #include "base/geometry/ng/size_t.h"
19 #include "base/memory/referenced.h"
20 #include "base/utils/utils.h"
21 #include "core/animation/page_transition_common.h"
22 #include "core/common/container.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components_ng/base/frame_node.h"
25 #include "core/components_ng/base/ui_node.h"
26 #include "core/components_ng/manager/shared_overlay/shared_overlay_manager.h"
27 #include "core/components_ng/pattern/overlay/overlay_manager.h"
28 #include "core/components_ng/pattern/stage/page_pattern.h"
29 #include "core/components_ng/pattern/stage/stage_pattern.h"
30 #include "core/components_ng/property/property.h"
31 #include "core/components_v2/inspector/inspector_constants.h"
32 #include "core/pipeline_ng/pipeline_context.h"
33 #include "core/pipeline_ng/ui_task_scheduler.h"
34
35 namespace OHOS::Ace::NG {
36
37 namespace {
FirePageTransition(const RefPtr<FrameNode> & page,PageTransitionType transitionType)38 void FirePageTransition(const RefPtr<FrameNode>& page, PageTransitionType transitionType)
39 {
40 CHECK_NULL_VOID(page);
41 auto pagePattern = page->GetPattern<PagePattern>();
42 CHECK_NULL_VOID(pagePattern);
43 page->GetEventHub<EventHub>()->SetEnabled(false);
44 pagePattern->SetPageInTransition(true);
45 if (transitionType == PageTransitionType::EXIT_PUSH || transitionType == PageTransitionType::EXIT_POP) {
46 pagePattern->TriggerPageTransition(
47 transitionType, [weak = WeakPtr<FrameNode>(page), transitionType, instanceId = Container::CurrentId()]() {
48 ContainerScope scope(instanceId);
49 LOGI("pageTransition exit finish");
50 auto page = weak.Upgrade();
51 CHECK_NULL_VOID(page);
52 auto context = PipelineContext::GetCurrentContext();
53 CHECK_NULL_VOID(context);
54 context->SetIsNeedShowFocus(false);
55 auto pageFocusHub = page->GetFocusHub();
56 CHECK_NULL_VOID(pageFocusHub);
57 pageFocusHub->SetParentFocusable(false);
58 pageFocusHub->LostFocus();
59 if (transitionType == PageTransitionType::EXIT_POP && page->GetParent()) {
60 auto stageNode = page->GetParent();
61 stageNode->RemoveChild(page);
62 stageNode->RebuildRenderContextTree();
63 context->RequestFrame();
64 return;
65 }
66 page->GetEventHub<EventHub>()->SetEnabled(true);
67 auto pattern = page->GetPattern<PagePattern>();
68 CHECK_NULL_VOID(pattern);
69 pattern->SetPageInTransition(false);
70 pattern->ProcessHideState();
71 });
72 return;
73 }
74 pagePattern->TriggerPageTransition(
75 transitionType, [weak = WeakPtr<FrameNode>(page), instanceId = Container::CurrentId()]() {
76 ContainerScope scope(instanceId);
77 LOGI("pageTransition in finish");
78 auto page = weak.Upgrade();
79 CHECK_NULL_VOID(page);
80 page->GetEventHub<EventHub>()->SetEnabled(true);
81 auto pattern = page->GetPattern<PagePattern>();
82 CHECK_NULL_VOID(pattern);
83 pattern->SetPageInTransition(false);
84
85 auto pageFocusHub = page->GetFocusHub();
86 CHECK_NULL_VOID(pageFocusHub);
87 pageFocusHub->SetParentFocusable(true);
88 pageFocusHub->RequestFocus();
89 auto context = PipelineContext::GetCurrentContext();
90 CHECK_NULL_VOID(context);
91 context->SetIsNeedShowFocus(false);
92 });
93 }
94 } // namespace
95
StartTransition(const RefPtr<FrameNode> & srcPage,const RefPtr<FrameNode> & destPage,RouteType type)96 void StageManager::StartTransition(const RefPtr<FrameNode>& srcPage, const RefPtr<FrameNode>& destPage, RouteType type)
97 {
98 auto pipeline = PipelineContext::GetCurrentContext();
99 CHECK_NULL_VOID(pipeline);
100 auto sharedManager = pipeline->GetSharedOverlayManager();
101 CHECK_NULL_VOID(sharedManager);
102 sharedManager->StartSharedTransition(srcPage, destPage);
103 srcPageNode_ = srcPage;
104 destPageNode_ = destPage;
105 if (type == RouteType::PUSH) {
106 FirePageTransition(srcPage, PageTransitionType::EXIT_PUSH);
107 FirePageTransition(destPage, PageTransitionType::ENTER_PUSH);
108 } else if (type == RouteType::POP) {
109 FirePageTransition(srcPage, PageTransitionType::EXIT_POP);
110 FirePageTransition(destPage, PageTransitionType::ENTER_POP);
111 }
112 }
113
StageManager(const RefPtr<FrameNode> & stage)114 StageManager::StageManager(const RefPtr<FrameNode>& stage) : stageNode_(stage)
115 {
116 CHECK_NULL_VOID(stageNode_);
117 stagePattern_ = DynamicCast<StagePattern>(stageNode_->GetPattern());
118 }
119
StopPageTransition()120 void StageManager::StopPageTransition()
121 {
122 auto srcNode = srcPageNode_.Upgrade();
123 if (srcNode) {
124 auto pattern = srcNode->GetPattern<PagePattern>();
125 pattern->StopPageTransition();
126 srcPageNode_ = nullptr;
127 }
128 auto destNode = destPageNode_.Upgrade();
129 if (destNode) {
130 auto pattern = destNode->GetPattern<PagePattern>();
131 pattern->StopPageTransition();
132 destPageNode_ = nullptr;
133 }
134 }
135
PushPage(const RefPtr<FrameNode> & node,bool needHideLast,bool needTransition)136 bool StageManager::PushPage(const RefPtr<FrameNode>& node, bool needHideLast, bool needTransition)
137 {
138 CHECK_NULL_RETURN(stageNode_, false);
139 CHECK_NULL_RETURN(node, false);
140 auto pipeline = AceType::DynamicCast<NG::PipelineContext>(PipelineBase::GetCurrentContext());
141 CHECK_NULL_RETURN(pipeline, false);
142 StopPageTransition();
143
144 const auto& children = stageNode_->GetChildren();
145 RefPtr<FrameNode> outPageNode;
146 needTransition &= !children.empty();
147 if (needTransition) {
148 pipeline->FlushPipelineImmediately();
149 }
150 if (!children.empty() && needHideLast) {
151 FirePageHide(children.back(), needTransition ? PageTransitionType::EXIT_PUSH : PageTransitionType::NONE);
152 outPageNode = AceType::DynamicCast<FrameNode>(children.back());
153 }
154 auto rect = stageNode_->GetGeometryNode()->GetFrameRect();
155 rect.SetOffset({});
156 node->GetRenderContext()->SyncGeometryProperties(rect);
157 // mount to parent and mark build render tree.
158 node->MountToParent(stageNode_);
159 // then build the total child.
160 node->Build();
161 stageNode_->RebuildRenderContextTree();
162 FirePageShow(node, needTransition ? PageTransitionType::ENTER_PUSH : PageTransitionType::NONE);
163
164 auto pagePattern = node->GetPattern<PagePattern>();
165 CHECK_NULL_RETURN(pagePattern, false);
166 stagePattern_->currentPageIndex_ = pagePattern->GetPageInfo()->GetPageId();
167 if (needTransition) {
168 pipeline->AddAfterLayoutTask([weakStage = WeakClaim(this), weakIn = WeakPtr<FrameNode>(node),
169 weakOut = WeakPtr<FrameNode>(outPageNode)]() {
170 auto stage = weakStage.Upgrade();
171 CHECK_NULL_VOID(stage);
172 auto inPageNode = weakIn.Upgrade();
173 auto outPageNode = weakOut.Upgrade();
174 stage->StartTransition(outPageNode, inPageNode, RouteType::PUSH);
175 });
176 }
177
178 // flush layout task.
179 if (!stageNode_->GetGeometryNode()->GetMarginFrameSize().IsPositive()) {
180 // in first load case, wait for window size.
181 LOGI("waiting for window size");
182 return true;
183 }
184 stageNode_->MarkDirtyNode(PROPERTY_UPDATE_MEASURE);
185 return true;
186 }
187
PopPage(bool needShowNext,bool needTransition)188 bool StageManager::PopPage(bool needShowNext, bool needTransition)
189 {
190 auto pipeline = PipelineContext::GetCurrentContext();
191 CHECK_NULL_RETURN(pipeline, false);
192 CHECK_NULL_RETURN(stageNode_, false);
193 StopPageTransition();
194 const auto& children = stageNode_->GetChildren();
195 if (children.empty()) {
196 LOGE("fail to pop page due to children is null");
197 return false;
198 }
199 auto pageNode = children.back();
200 const size_t transitionPageSize = 2;
201 needTransition &= (children.size() >= transitionPageSize);
202 if (needTransition) {
203 pipeline->FlushPipelineImmediately();
204 }
205 FirePageHide(pageNode, needTransition ? PageTransitionType::EXIT_POP : PageTransitionType::NONE);
206
207 RefPtr<FrameNode> inPageNode;
208 if (needShowNext && children.size() >= transitionPageSize) {
209 auto newPageNode = *(++children.rbegin());
210 FirePageShow(newPageNode, needTransition ? PageTransitionType::ENTER_POP : PageTransitionType::NONE);
211 inPageNode = AceType::DynamicCast<FrameNode>(newPageNode);
212 }
213
214 auto outPageNode = AceType::DynamicCast<FrameNode>(pageNode);
215 if (needTransition) {
216 StartTransition(outPageNode, inPageNode, RouteType::POP);
217 return true;
218 }
219 stageNode_->RemoveChild(pageNode);
220 pageNode->SetChildrenInDestroying();
221 stageNode_->RebuildRenderContextTree();
222 pipeline->RequestFrame();
223 return true;
224 }
225
PopPageToIndex(int32_t index,bool needShowNext,bool needTransition)226 bool StageManager::PopPageToIndex(int32_t index, bool needShowNext, bool needTransition)
227 {
228 auto pipeline = PipelineContext::GetCurrentContext();
229 CHECK_NULL_RETURN(pipeline, false);
230 CHECK_NULL_RETURN(stageNode_, false);
231 StopPageTransition();
232 const auto& children = stageNode_->GetChildren();
233 if (children.empty()) {
234 LOGE("fail to pop page due to children is null");
235 return false;
236 }
237 int32_t popSize = static_cast<int32_t>(children.size()) - index - 1;
238 if (popSize < 0) {
239 LOGE("fail to pop page due to index is out of range");
240 return false;
241 }
242 if (popSize == 0) {
243 LOGD("already here");
244 return true;
245 }
246
247 if (needTransition) {
248 pipeline->FlushPipelineImmediately();
249 }
250 bool firstPageTransition = true;
251 auto outPageNode = AceType::DynamicCast<FrameNode>(children.back());
252 auto iter = children.rbegin();
253 for (int32_t current = 0; current < popSize; ++current) {
254 auto pageNode = *iter;
255 FirePageHide(
256 pageNode, firstPageTransition && needTransition ? PageTransitionType::EXIT_POP : PageTransitionType::NONE);
257 firstPageTransition = false;
258 ++iter;
259 }
260
261 RefPtr<FrameNode> inPageNode;
262 if (needShowNext) {
263 const auto& newPageNode = *iter;
264 FirePageShow(newPageNode, needTransition ? PageTransitionType::ENTER_POP : PageTransitionType::NONE);
265 inPageNode = AceType::DynamicCast<FrameNode>(newPageNode);
266 }
267
268 if (needTransition) {
269 // from the penultimate node, (popSize - 1) nodes are deleted.
270 // the last node will be deleted after pageTransition
271 iter = children.rbegin();
272 ++iter;
273 for (int32_t current = 1; current < popSize; ++current) {
274 auto pageNode = *(iter++);
275 stageNode_->RemoveChild(pageNode);
276 }
277 stageNode_->RebuildRenderContextTree();
278 StartTransition(outPageNode, inPageNode, RouteType::POP);
279 return true;
280 }
281 for (int32_t current = 0; current < popSize; ++current) {
282 auto pageNode = children.back();
283 stageNode_->RemoveChild(pageNode);
284 }
285 stageNode_->RebuildRenderContextTree();
286 pipeline->RequestFrame();
287 return true;
288 }
289
CleanPageStack()290 bool StageManager::CleanPageStack()
291 {
292 auto pipeline = PipelineContext::GetCurrentContext();
293 CHECK_NULL_RETURN(pipeline, false);
294 CHECK_NULL_RETURN(stageNode_, false);
295 const auto& children = stageNode_->GetChildren();
296 if (children.size() <= 1) {
297 LOGE("fail to clean page stack due to children size is illegal");
298 return false;
299 }
300 auto popSize = static_cast<int32_t>(children.size() - 1);
301 for (int32_t count = 1; count <= popSize; ++count) {
302 auto pageNode = children.front();
303 stageNode_->RemoveChild(pageNode);
304 }
305 stageNode_->RebuildRenderContextTree();
306 pipeline->RequestFrame();
307 return true;
308 }
309
MovePageToFront(const RefPtr<FrameNode> & node,bool needHideLast,bool needTransition)310 bool StageManager::MovePageToFront(const RefPtr<FrameNode>& node, bool needHideLast, bool needTransition)
311 {
312 auto pipeline = PipelineContext::GetCurrentContext();
313 CHECK_NULL_RETURN(pipeline, false);
314 CHECK_NULL_RETURN(stageNode_, false);
315 StopPageTransition();
316 const auto& children = stageNode_->GetChildren();
317 if (children.empty()) {
318 LOGE("child is empty");
319 return false;
320 }
321 const auto& lastPage = children.back();
322 if (lastPage == node) {
323 LOGD("page already on the top");
324 return true;
325 }
326 if (needTransition) {
327 pipeline->FlushPipelineImmediately();
328 }
329 if (needHideLast) {
330 FirePageHide(lastPage, needTransition ? PageTransitionType::EXIT_PUSH : PageTransitionType::NONE);
331 }
332 node->MovePosition(static_cast<int32_t>(stageNode_->GetChildren().size() - 1));
333 node->GetRenderContext()->ResetPageTransitionEffect();
334 FirePageShow(node, needTransition ? PageTransitionType::ENTER_PUSH : PageTransitionType::NONE);
335
336 stageNode_->RebuildRenderContextTree();
337 if (needTransition) {
338 auto outPageNode = AceType::DynamicCast<FrameNode>(lastPage);
339 StartTransition(outPageNode, node, RouteType::PUSH);
340 }
341 pipeline->RequestFrame();
342 return true;
343 }
344
FirePageHide(const RefPtr<UINode> & node,PageTransitionType transitionType)345 void StageManager::FirePageHide(const RefPtr<UINode>& node, PageTransitionType transitionType)
346 {
347 auto pageNode = DynamicCast<FrameNode>(node);
348 CHECK_NULL_VOID(pageNode);
349 auto pagePattern = pageNode->GetPattern<PagePattern>();
350 CHECK_NULL_VOID(pagePattern);
351 pagePattern->OnHide();
352 if (transitionType == PageTransitionType::NONE) {
353 // If there is a page transition, this function should execute after page transition,
354 // otherwise the page will not be visible
355 pagePattern->ProcessHideState();
356 }
357
358 auto pageFocusHub = pageNode->GetFocusHub();
359 CHECK_NULL_VOID(pageFocusHub);
360 pageFocusHub->SetParentFocusable(false);
361
362 auto context = PipelineContext::GetCurrentContext();
363 CHECK_NULL_VOID_NOLOG(context);
364 context->SetIsNeedShowFocus(false);
365 }
366
FirePageShow(const RefPtr<UINode> & node,PageTransitionType transitionType)367 void StageManager::FirePageShow(const RefPtr<UINode>& node, PageTransitionType transitionType)
368 {
369 auto pageNode = DynamicCast<FrameNode>(node);
370 CHECK_NULL_VOID(pageNode);
371 auto pagePattern = pageNode->GetPattern<PagePattern>();
372 CHECK_NULL_VOID(pagePattern);
373 pagePattern->OnShow();
374 // With or without a page transition, we need to make the coming page visible first
375 pagePattern->ProcessShowState();
376
377 auto pageFocusHub = pageNode->GetFocusHub();
378 CHECK_NULL_VOID(pageFocusHub);
379 pageFocusHub->SetParentFocusable(true);
380 pageFocusHub->RequestFocus();
381
382 auto context = PipelineContext::GetCurrentContext();
383 CHECK_NULL_VOID_NOLOG(context);
384 context->SetIsNeedShowFocus(false);
385 }
386
GetLastPage()387 RefPtr<FrameNode> StageManager::GetLastPage()
388 {
389 CHECK_NULL_RETURN(stageNode_, nullptr);
390 const auto& children = stageNode_->GetChildren();
391 if (children.empty()) {
392 LOGE("fail to return page due to children is null");
393 return nullptr;
394 }
395 return DynamicCast<FrameNode>(children.back());
396 }
397
ReloadStage()398 void StageManager::ReloadStage()
399 {
400 CHECK_NULL_VOID(stageNode_);
401 const auto& children = stageNode_->GetChildren();
402 for (const auto& child : children) {
403 auto frameNode = DynamicCast<FrameNode>(child);
404 if (!frameNode) {
405 continue;
406 }
407 auto pagePattern = frameNode->GetPattern<PagePattern>();
408 if (!pagePattern) {
409 continue;
410 }
411 pagePattern->ReloadPage();
412 }
413 }
414
415 } // namespace OHOS::Ace::NG
416