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/container_modal/render_container_modal.h" 17 18 #include "base/log/dump_log.h" 19 #include "base/log/event_report.h" 20 #include "core/components/common/layout/grid_system_manager.h" 21 #include "core/components/container_modal/container_modal_component.h" 22 #include "core/components/container_modal/container_modal_constants.h" 23 #include "core/components/image/image_component.h" 24 #include "core/components/text/text_component.h" 25 #include "core/components/theme/theme_manager.h" 26 #include "core/pipeline/base/render_node.h" 27 28 namespace OHOS::Ace { 29 Create()30RefPtr<RenderNode> RenderContainerModal::Create() 31 { 32 return AceType::MakeRefPtr<RenderContainerModal>(); 33 } 34 UpdateStyle(const RefPtr<Component> & component) const35void RenderContainerModal::UpdateStyle(const RefPtr<Component>& component) const 36 { 37 auto containerModal = AceType::DynamicCast<ContainerModalComponent>(component); 38 if (!containerModal) { 39 return; 40 } 41 auto themeManager = GetThemeManager(); 42 if (!themeManager) { 43 LOGE("get theme manager failed"); 44 return; 45 } 46 auto themeConstants = themeManager->GetThemeConstants(); 47 if (!themeConstants) { 48 LOGE("get theme constants failed"); 49 return; 50 } 51 auto context = GetContext().Upgrade(); 52 if (!context) { 53 LOGE("get pipeline context failed"); 54 return; 55 } 56 auto labelId = context->GetWindowManager()->GetAppLabelId(); 57 auto appLabelComponent = containerModal->GetTitleLabel(); 58 if (appLabelComponent && labelId != 0) { 59 auto appLabel = themeConstants->GetString(labelId); 60 appLabelComponent->SetData(appLabel); 61 } 62 63 auto iconId = context->GetWindowManager()->GetAppIconId(); 64 auto appIconComponent = containerModal->GetTitleIcon(); 65 if (appIconComponent && iconId != 0) { 66 auto appIconSrc = themeConstants->GetMediaPath(iconId); 67 appIconComponent->SetSrc(appIconSrc); 68 } 69 } 70 Update(const RefPtr<Component> & component)71void RenderContainerModal::Update(const RefPtr<Component>& component) 72 { 73 auto containerModal = AceType::DynamicCast<ContainerModalComponent>(component); 74 if (!containerModal) { 75 LOGE("containerModal update with nullptr"); 76 EventReport::SendRenderException(RenderExcepType::RENDER_COMPONENT_ERR); 77 return; 78 } 79 UpdateStyle(component); 80 MarkNeedLayout(); 81 } 82 ParseRequiredRenderNodes()83bool RenderContainerModal::ParseRequiredRenderNodes() 84 { 85 auto containerBox = AceType::DynamicCast<RenderBox>(GetFirstChild()); 86 if (!containerBox) { 87 LOGE("Container box render node is null!"); 88 return false; 89 } 90 containerBox_ = AceType::WeakClaim(AceType::RawPtr(containerBox)); 91 return true; 92 } 93 PerformLayout()94void RenderContainerModal::PerformLayout() 95 { 96 // Only 2 children are allowed in RenderContainerModal 97 if (GetChildren().empty()) { 98 LOGE("Children size wrong in container modal"); 99 return; 100 } 101 ParseRequiredRenderNodes(); 102 103 // ContainerModalComponent's size is as large as the root's. 104 auto maxSize = GetLayoutParam().GetMaxSize(); 105 auto columnInfo = GridSystemManager::GetInstance().GetInfoByType(GridColumnType::PANEL); 106 columnInfo->GetParent()->BuildColumnWidth(); 107 SetLayoutSize(maxSize); 108 109 ContainerBoxLayout(); 110 } 111 ContainerBoxLayout()112void RenderContainerModal::ContainerBoxLayout() 113 { 114 auto maxSize = GetLayoutParam().GetMaxSize(); 115 // layout container box 116 auto containerBox = containerBox_.Upgrade(); 117 if (!containerBox) { 118 LOGE("Container box render node is null, layout failed."); 119 return; 120 } 121 122 LayoutParam containerLayoutParam; 123 containerLayoutParam.SetMinSize(Size()); 124 containerLayoutParam.SetMaxSize(Size(maxSize.Width(), maxSize.Height())); 125 containerBox->Layout(containerLayoutParam); 126 containerBox->SetPosition(Offset(0.0, 0.0)); 127 } 128 129 } // namespace OHOS::Ace 130