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