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