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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STAGE_PAGE_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STAGE_PAGE_PATTERN_H 18 19 #include <functional> 20 21 #include "base/memory/referenced.h" 22 #include "base/utils/noncopyable.h" 23 #include "core/animation/animator_info.h" 24 #include "core/animation/page_transition_common.h" 25 #include "core/common/autofill/auto_fill_trigger_state_holder.h" 26 #include "core/components/theme/app_theme.h" 27 #include "core/components_ng/pattern/stage/content_root_pattern.h" 28 #include "core/components_ng/pattern/stage/page_event_hub.h" 29 #include "core/components_ng/pattern/stage/page_info.h" 30 #include "core/components_ng/pattern/stage/page_transition_effect.h" 31 #include "core/pipeline_ng/pipeline_context.h" 32 33 namespace OHOS::Ace::NG { 34 35 using SharedTransitionMap = std::unordered_map<ShareId, WeakPtr<FrameNode>>; 36 using JSAnimatorMap = std::unordered_map<std::string, RefPtr<Framework::AnimatorInfo>>; 37 38 using DynamicPageSizeCallback = std::function<void(const SizeF& size)>; 39 /** 40 * When the page triggers 'onHide' or 'onShow', the callback is called, 41 * and the input parameter is 'true' for onHide and false for onShow. 42 */ 43 using PageVisibilityChangeCallback = std::function<void(bool)>; 44 using OnNewParamCallback = std::function<void(const std::string&)>; 45 46 enum class RouterPageState { 47 ABOUT_TO_APPEAR = 0, 48 ABOUT_TO_DISAPPEAR = 1, 49 ON_PAGE_SHOW = 2, 50 ON_PAGE_HIDE = 3, 51 ON_BACK_PRESS = 4, 52 }; 53 54 // PagePattern is the base class for page root render node. 55 class ACE_FORCE_EXPORT PagePattern : public ContentRootPattern, public FocusView, public AutoFillTriggerStateHolder { 56 DECLARE_ACE_TYPE(PagePattern, ContentRootPattern, FocusView, AutoFillTriggerStateHolder); 57 58 public: PagePattern(const RefPtr<PageInfo> & pageInfo)59 explicit PagePattern(const RefPtr<PageInfo>& pageInfo) : pageInfo_(pageInfo) {} 60 ~PagePattern() override = default; 61 IsMeasureBoundary()62 bool IsMeasureBoundary() const override 63 { 64 return true; 65 } 66 IsAtomicNode()67 bool IsAtomicNode() const override 68 { 69 return false; 70 } 71 GetPageInfo()72 const RefPtr<PageInfo>& GetPageInfo() const 73 { 74 return pageInfo_; 75 } 76 GetPageUrl()77 std::string GetPageUrl() const 78 { 79 return pageInfo_ ? pageInfo_->GetPageUrl() : ""; 80 } 81 82 virtual void OnShow(bool isFromWindow = false); 83 84 virtual void OnHide(bool isFromWindow = false); 85 86 bool OnBackPressed(); 87 GetPageState()88 RouterPageState GetPageState() const 89 { 90 return state_; 91 } 92 SetOnPageShow(std::function<void ()> && onPageShow)93 void SetOnPageShow(std::function<void()>&& onPageShow) 94 { 95 onPageShow_ = std::move(onPageShow); 96 } 97 SetOnPageHide(std::function<void ()> && onPageHide)98 void SetOnPageHide(std::function<void()>&& onPageHide) 99 { 100 onPageHide_ = std::move(onPageHide); 101 } 102 SetOnBackPressed(std::function<bool ()> && OnBackPressed)103 void SetOnBackPressed(std::function<bool()>&& OnBackPressed) 104 { 105 onBackPressed_ = std::move(OnBackPressed); 106 } 107 SetOnNewParam(OnNewParamCallback && onNewParam)108 void SetOnNewParam(OnNewParamCallback&& onNewParam) 109 { 110 onNewParam_ = std::move(onNewParam); 111 } 112 SetPageTransitionFunc(std::function<void ()> && pageTransitionFunc)113 void SetPageTransitionFunc(std::function<void()>&& pageTransitionFunc) 114 { 115 pageTransitionFunc_ = std::move(pageTransitionFunc); 116 } 117 RegisterDumpInfoListener(const std::function<void (const std::vector<std::string> &)> && callback)118 void RegisterDumpInfoListener(const std::function<void(const std::vector<std::string>&)>&& callback) 119 { 120 dumpListener_ = std::move(callback); 121 } 122 FireDumpListener(const std::vector<std::string> & params)123 void FireDumpListener(const std::vector<std::string>& params) 124 { 125 CHECK_NULL_VOID(dumpListener_); 126 auto dumpListener = dumpListener_; 127 dumpListener(params); 128 } 129 130 // find pageTransition effect according to transition type 131 RefPtr<PageTransitionEffect> FindPageTransitionEffect(PageTransitionType type); 132 133 void ClearPageTransitionEffect(); 134 135 RefPtr<PageTransitionEffect> GetTopTransition() const; 136 137 void AddPageTransition(const RefPtr<PageTransitionEffect>& effect); 138 CreateEventHub()139 RefPtr<EventHub> CreateEventHub() override 140 { 141 return MakeRefPtr<PageEventHub>(); 142 } 143 GetFocusPattern()144 FocusPattern GetFocusPattern() const override 145 { 146 return { FocusType::SCOPE, true }; 147 } 148 GetRouteOfFirstScope()149 std::list<int32_t> GetRouteOfFirstScope() override 150 { 151 return { 0 }; 152 } 153 GetSharedTransitionMap()154 const SharedTransitionMap& GetSharedTransitionMap() const 155 { 156 return sharedTransitionMap_; 157 } 158 159 void AddJsAnimator(const std::string& animatorId, const RefPtr<Framework::AnimatorInfo>& animatorInfo); 160 RefPtr<Framework::AnimatorInfo> GetJsAnimator(const std::string& animatorId); 161 162 void BuildSharedTransitionMap(); 163 164 void ReloadPage(); 165 166 void SetFirstBuildCallback(std::function<void()>&& buildCallback); 167 SetPageInTransition(bool pageTransition)168 void SetPageInTransition(bool pageTransition) 169 { 170 isPageInTransition_ = pageTransition; 171 } 172 GetPageInTransition()173 bool GetPageInTransition() const 174 { 175 return isPageInTransition_; 176 } 177 178 // Mark current page node invisible in render tree. 179 void ProcessHideState(); 180 // Mark current page node visible in render tree. 181 void ProcessShowState(); 182 183 bool ProcessAutoSave(const std::function<void()>& onFinish = nullptr, 184 const std::function<void()>& onUIExtNodeBindingCompleted = nullptr); 185 MarkRenderDone()186 void MarkRenderDone() 187 { 188 isRenderDone_ = true; 189 } 190 191 void StopPageTransition(); 192 SetDynamicPageSizeCallback(DynamicPageSizeCallback && dynamicPageSizeCallback)193 void SetDynamicPageSizeCallback(DynamicPageSizeCallback&& dynamicPageSizeCallback) 194 { 195 dynamicPageSizeCallback_ = std::move(dynamicPageSizeCallback); 196 } 197 AddOnHiddenChange(int32_t id,PageVisibilityChangeCallback && onHiddenChange)198 void AddOnHiddenChange(int32_t id, PageVisibilityChangeCallback&& onHiddenChange) 199 { 200 onHiddenChange_[id] = std::move(onHiddenChange); 201 } 202 FireOnHiddenChange(bool flag)203 void FireOnHiddenChange(bool flag) 204 { 205 for (auto& onHiddenChangeInfo : onHiddenChange_) { 206 if (onHiddenChangeInfo.second) { 207 auto onHiddenChange = onHiddenChangeInfo.second; 208 onHiddenChange(flag); 209 } 210 } 211 } 212 UpdatePageParam()213 void UpdatePageParam() 214 { 215 auto host = GetHost(); 216 CHECK_NULL_VOID(host); 217 host->SetAccessibilityVisible(true); 218 host->SetJSViewActive(true); 219 } 220 RemoveOnHiddenChange(int32_t id)221 void RemoveOnHiddenChange(int32_t id) 222 { 223 auto iter = onHiddenChange_.find(id); 224 if (iter != onHiddenChange_.end()) { 225 onHiddenChange_.erase(iter); 226 } 227 } 228 CreateOverlayManager(bool isShow)229 void CreateOverlayManager(bool isShow) 230 { 231 if (!overlayManager_ && isShow) { 232 overlayManager_ = MakeRefPtr<OverlayManager>(GetHost()); 233 } 234 } 235 GetOverlayManager()236 const RefPtr<OverlayManager>& GetOverlayManager() 237 { 238 return overlayManager_; 239 } 240 DeleteOverlayManager()241 void DeleteOverlayManager() 242 { 243 overlayManager_.Reset(); 244 } 245 246 bool RemoveOverlay(); 247 void MarkDirtyOverlay(); 248 IsOnShow()249 bool IsOnShow() 250 { 251 return isOnShow_; 252 } 253 GetIsModalCovered()254 bool GetIsModalCovered() const 255 { 256 return isModalCovered_; 257 } 258 SetIsModalCovered(bool isModalCovered)259 void SetIsModalCovered(bool isModalCovered) 260 { 261 isModalCovered_ = isModalCovered; 262 } 263 SetPageVisibilityChangeCallback(PageVisibilityChangeCallback && callback)264 void SetPageVisibilityChangeCallback(PageVisibilityChangeCallback&& callback) 265 { 266 visibilityChangeCallback_ = std::move(callback); 267 } 268 SetAnimationId(int32_t animationId)269 void SetAnimationId(int32_t animationId) 270 { 271 animationId_ = animationId; 272 } 273 GetAnimationId()274 int32_t GetAnimationId() const 275 { 276 return animationId_; 277 } 278 279 void InitTransitionIn(const RefPtr<PageTransitionEffect>& effect, PageTransitionType type); 280 281 void InitTransitionOut(const RefPtr<PageTransitionEffect>& effect, PageTransitionType type); 282 283 void TransitionInFinish(const RefPtr<PageTransitionEffect>& effect, PageTransitionType type); 284 285 void TransitionOutFinish(const RefPtr<PageTransitionEffect>& effect, PageTransitionType type); 286 287 void FinishOutPage(const int32_t animationId, PageTransitionType type); 288 289 void FinishInPage(const int32_t animationId, PageTransitionType type); 290 291 RefPtr<PageTransitionEffect> GetDefaultPageTransition(PageTransitionType type); 292 293 void ResetPageTransitionEffect(); 294 295 void TriggerPageTransition(const std::function<void()>& onFinish, PageTransitionType type); 296 297 void OnDetachFromFrameNode(FrameNode* frameNode) override; 298 299 void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override; 300 SetIsNeedRemove(bool isNeedRemove)301 void SetIsNeedRemove(bool isNeedRemove) 302 { 303 isNeedRemove_ = isNeedRemove; 304 } 305 void CheckIsNeedForceExitWindow(bool result); 306 void RemoveJsChildImmediately(const RefPtr<FrameNode>& page, PageTransitionType transactionType); 307 308 bool IsNeedCallbackBackPressed(); 309 FireOnNewParam(const std::string & param)310 void FireOnNewParam(const std::string& param) 311 { 312 if (onNewParam_) { 313 onNewParam_(param); 314 } 315 } 316 317 protected: 318 void OnAttachToFrameNode() override; 319 void BeforeCreateLayoutWrapper() override; 320 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& wrapper, const DirtySwapConfig& config) override; 321 void BeforeSyncGeometryProperties(const DirtySwapConfig& config) override; 322 void FirePageTransitionFinish(); 323 324 void OnAttachToMainTree() override; 325 void OnDetachFromMainTree() override; 326 327 bool AvoidKeyboard() const override; AvoidTop()328 bool AvoidTop() const override 329 { 330 return true; 331 } AvoidBottom()332 bool AvoidBottom() const override 333 { 334 return true; 335 } 336 AvoidCutout()337 bool AvoidCutout() const override 338 { 339 return true; 340 } 341 342 void NotifyPerfMonitorPageMsg(const std::string& pageUrl, const std::string& bundleName); 343 344 RefPtr<PageTransitionEffect> GetPageTransitionEffect(const RefPtr<PageTransitionEffect>& transition); 345 346 void SlideTransitionEffect(const SlideEffect& effect, const RectF& rect, TranslateOptions& translate); 347 348 void UpdateDefaultEnterPopEffect(RefPtr<PageTransitionEffect>& effect, float statusHeight); 349 350 void UpdateEnterPushEffect(RefPtr<PageTransitionEffect>& effect, float statusHeight); 351 352 void UpdateExitPushEffect(RefPtr<PageTransitionEffect>& effect, float statusHeight); 353 354 void UpdateAnimationOption(const RefPtr<PageTransitionEffect>& transition, 355 RefPtr<PageTransitionEffect>& effect, AnimationOption& option, PageTransitionType type); 356 357 virtual void TriggerDefaultTransition(const std::function<void()>& onFinish, PageTransitionType type); 358 359 void MaskAnimation(const Color& initialBackgroundColor, const Color& backgroundColor); 360 361 void NotifyNavigationLifecycle(bool isShow, bool isFromWindow); 362 363 void RecordPageEvent(bool isShow); 364 365 RefPtr<PageInfo> pageInfo_; 366 RefPtr<OverlayManager> overlayManager_; 367 368 OnNewParamCallback onNewParam_; 369 std::function<void()> onPageShow_; 370 std::function<void()> onPageHide_; 371 std::function<bool()> onBackPressed_; 372 std::function<void()> pageTransitionFunc_; 373 std::function<void()> firstBuildCallback_; 374 std::unordered_map<int32_t, PageVisibilityChangeCallback> onHiddenChange_; 375 DynamicPageSizeCallback dynamicPageSizeCallback_; 376 PageVisibilityChangeCallback visibilityChangeCallback_; 377 std::shared_ptr<std::function<void()>> pageTransitionFinish_; 378 std::list<RefPtr<PageTransitionEffect>> pageTransitionEffects_; 379 std::function<void(const std::vector<std::string>&)> dumpListener_; 380 381 int32_t animationId_ = -1; 382 bool isOnShow_ = false; 383 bool isFirstLoad_ = true; 384 bool isPageInTransition_ = false; 385 bool isRenderDone_ = false; 386 bool isModalCovered_ = false; 387 bool isNeedRemove_ = false; 388 389 #if defined(ENABLE_SPLIT_MODE) 390 bool needFireObserver_ = true; 391 #endif 392 393 SharedTransitionMap sharedTransitionMap_; 394 JSAnimatorMap jsAnimatorMap_; 395 RouterPageState state_ = RouterPageState::ABOUT_TO_APPEAR; 396 std::shared_ptr<AnimationUtils::Animation> currCustomAnimation_; 397 398 ACE_DISALLOW_COPY_AND_MOVE(PagePattern); 399 }; 400 } // namespace OHOS::Ace::NG 401 402 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STAGE_PAGE_PATTERN_H 403