1 /* 2 * Copyright (c) 2021 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/root/render_root.h" 17 18 #include "root_component.h" 19 #include "base/log/log.h" 20 #include "core/animation/curve.h" 21 #include "core/animation/curve_animation.h" 22 #include "core/components/theme/app_theme.h" 23 24 namespace OHOS::Ace { 25 namespace { 26 IsShadowModal(const WeakPtr<PipelineContext> & contextWeak)27bool IsShadowModal(const WeakPtr<PipelineContext>& contextWeak) 28 { 29 auto context = contextWeak.Upgrade(); 30 if (!context) { 31 return false; 32 } 33 auto modal = context->GetWindowModal(); 34 return (modal == WindowModal::DIALOG_MODAL) || (modal == WindowModal::SEMI_MODAL) || 35 (modal == WindowModal::SEMI_MODAL_FULL_SCREEN); 36 } 37 38 } // namespace 39 RenderRoot()40RenderRoot::RenderRoot() : RenderNode(true) {} 41 Update(const RefPtr<Component> & component)42void RenderRoot::Update(const RefPtr<Component>& component) 43 { 44 if (!controller_ && IsShadowModal(context_)) { 45 controller_ = CREATE_ANIMATOR(GetContext()); 46 controller_->SetFillMode(FillMode::FORWARDS); 47 } 48 auto root = AceType::DynamicCast<RootComponent>(component); 49 if (root) { 50 isContextMenu_ = root->IsContextMenu(); 51 } 52 MarkNeedLayout(); 53 } 54 PerformLayout()55void RenderRoot::PerformLayout() 56 { 57 auto appTheme = GetTheme<AppTheme>(); 58 // Use theme background color to clear canvas. 59 if (appTheme && !isBgColorInit_) { 60 if (IsShadowModal(context_)) { 61 bgColor_ = appTheme->GetBackgroundColor(); 62 auto colorAnimation = 63 AceType::MakeRefPtr<CurveAnimation<Color>>(Color::TRANSPARENT, bgColor_, Curves::FAST_OUT_SLOW_IN); 64 bgColor_ = Color::TRANSPARENT; 65 colorAnimation->AddListener([weak = AceType::WeakClaim(this)](const Color& color) { 66 auto root = weak.Upgrade(); 67 if (!root) { 68 LOGE("Root transition failed. root is null."); 69 return; 70 } 71 if (!root->forceColor_) { 72 root->bgColor_ = color; 73 root->MarkNeedRender(); 74 } 75 }); 76 controller_->AddInterpolator(colorAnimation); 77 } 78 if (isContextMenu_) { 79 bgColor_ = Color::TRANSPARENT; 80 } 81 isBgColorInit_ = true; 82 } 83 viewPort_ = GetPaintRect().GetSize(); 84 LayoutParam innerLayoutParam; 85 innerLayoutParam.SetMaxSize(GetPaintRect().GetSize()); 86 87 for (auto& child : GetChildren()) { 88 child->Layout(innerLayoutParam); 89 child->SetPosition(Offset::Zero()); 90 } 91 } 92 DumpLayerTree()93void RenderRoot::DumpLayerTree() {} 94 AnimateToHide(int32_t duration)95void RenderRoot::AnimateToHide(int32_t duration) 96 { 97 if (controller_) { 98 controller_->SetDuration(duration); 99 controller_->Backward(); 100 } 101 } 102 AnimateToShow(int32_t duration)103void RenderRoot::AnimateToShow(int32_t duration) 104 { 105 if (controller_) { 106 controller_->SetDuration(duration); 107 controller_->Forward(); 108 } 109 } 110 SetDefaultBgColor(bool isTransparent)111void RenderRoot::SetDefaultBgColor(bool isTransparent) 112 { 113 auto appTheme = GetTheme<AppTheme>(); 114 if (!appTheme) { 115 return; 116 } 117 bgColor_ = isContextMenu_ || isTransparent ? Color::TRANSPARENT : appTheme->GetBackgroundColor(); 118 forceColor_ = false; 119 MarkNeedRender(); 120 } 121 SetBgColor(const Color & color)122void RenderRoot::SetBgColor(const Color& color) 123 { 124 bgColor_ = isContextMenu_ ? Color::TRANSPARENT : color; 125 forceColor_ = true; 126 MarkNeedRender(); 127 } 128 GetBgColor() const129Color RenderRoot::GetBgColor() const 130 { 131 return isContextMenu_ ? Color::TRANSPARENT : bgColor_; 132 } 133 } // namespace OHOS::Ace 134