1 /*
2  * Copyright (c) 2021-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/popup/popup_component_v2.h"
17 
18 #include "core/components/button/button_component.h"
19 #include "core/components/common/layout/grid_system_manager.h"
20 #include "core/components/flex/flex_component.h"
21 #include "core/components/padding/padding_component.h"
22 #include "core/components/popup/popup_element_v2.h"
23 #include "core/components/text/text_component_v2.h"
24 
25 namespace OHOS::Ace {
26 namespace {
27 
28 constexpr Dimension DEFAULT_FONT_SIZE = 14.0_fp;
29 constexpr Dimension BUTTON_ZERO_PADDING = 0.0_vp;
30 constexpr Dimension BUTTON_ROW_PADDING = 4.0_vp;
31 constexpr Dimension MESSAGE_HORIZONTAL_PADDING = 16.0_vp;
32 constexpr Dimension MESSAGE_TOP_PADDING = 12.0_vp;
33 constexpr Dimension MESSAGE_ZERO_PADDING = 0.0_vp;
34 
35 constexpr Color POPUP_BUTTON_HOVER_COLOR = Color(0x19FFFFFF);
36 constexpr Color POPUP_BUTTON_CLICKED_COLOR = Color(0x26FFFFFF);
37 
38 } // namespace
39 
CreateElement()40 RefPtr<Element> PopupComponentV2::CreateElement()
41 {
42     return AceType::MakeRefPtr<PopupElementV2>(GetId());
43 }
44 
Initialization(const RefPtr<ThemeManager> & themeManager,const WeakPtr<PipelineContext> & context)45 void PopupComponentV2::Initialization(const RefPtr<ThemeManager>& themeManager, const WeakPtr<PipelineContext>& context)
46 {
47     if (hasInitialization_) {
48         return;
49     }
50 
51     if (!themeManager) {
52         LOGE("themeManager is null.");
53         return;
54     }
55 
56     themeManager_ = themeManager;
57     context_ = context;
58     auto popupTheme = themeManager_->GetTheme<PopupTheme>();
59     if (!popupTheme) {
60         LOGE("popupTheme is null.");
61         return;
62     }
63 
64     RefPtr<Component> child;
65     if (customComponent_) {
66         child = customComponent_;
67     } else if (primaryButtonProperties_.showButton || secondaryButtonProperties_.showButton) {
68         child = CreateChild();
69         GetPopupParam()->SetHasAction(true);
70     } else {
71         child = CreateMessage();
72     }
73 
74     auto box = CreateBox(popupTheme);
75     box->SetChild(child);
76     SetChild(box);
77     hasInitialization_ = true;
78 }
79 
CreateChild()80 const RefPtr<Component> PopupComponentV2::CreateChild()
81 {
82     RefPtr<ColumnComponent> child;
83     std::list<RefPtr<Component>> columnChildren;
84     columnChildren.emplace_back(SetPadding(CreateMessage(),
85         Edge(MESSAGE_HORIZONTAL_PADDING, MESSAGE_TOP_PADDING, MESSAGE_HORIZONTAL_PADDING, MESSAGE_ZERO_PADDING)));
86     columnChildren.emplace_back(SetPadding(
87         CreateButtons(), Edge(BUTTON_ROW_PADDING, BUTTON_ZERO_PADDING, BUTTON_ROW_PADDING, BUTTON_ROW_PADDING)));
88     child = AceType::MakeRefPtr<ColumnComponent>(FlexAlign::FLEX_START, FlexAlign::FLEX_END, columnChildren);
89     child->SetMainAxisSize(MainAxisSize::MIN);
90     child->SetCrossAxisSize(CrossAxisSize::MIN);
91     return child;
92 }
93 
CreateMessage()94 const RefPtr<Component> PopupComponentV2::CreateMessage()
95 {
96     auto text = AceType::MakeRefPtr<TextComponentV2>(message_);
97     auto textStyle = text->GetTextStyle();
98     textStyle.SetAllowScale(false);
99     textStyle.SetFontSize(DEFAULT_FONT_SIZE);
100     textStyle.SetTextColor(Color::WHITE);
101     text->SetTextStyle(std::move(textStyle));
102     return text;
103 }
104 
CreateButtons()105 const RefPtr<Component> PopupComponentV2::CreateButtons()
106 {
107     std::list<RefPtr<Component>> rowChildren;
108     rowChildren.emplace_back(SetPadding(CreateButton(primaryButtonProperties_),
109         Edge(BUTTON_ZERO_PADDING)));
110     rowChildren.emplace_back(SetPadding(CreateButton(secondaryButtonProperties_),
111         Edge(BUTTON_ROW_PADDING, BUTTON_ZERO_PADDING, BUTTON_ZERO_PADDING, BUTTON_ZERO_PADDING)));
112     auto row = AceType::MakeRefPtr<RowComponent>(FlexAlign::FLEX_END, FlexAlign::CENTER, rowChildren);
113     row->SetMainAxisSize(MainAxisSize::MIN);
114     auto box = AceType::MakeRefPtr<BoxComponent>();
115     box->SetChild(row);
116     return box;
117 }
118 
CreateButton(const ButtonProperties & buttonProperties)119 const RefPtr<Component> PopupComponentV2::CreateButton(const ButtonProperties& buttonProperties)
120 {
121     if (!buttonProperties.showButton) {
122         return nullptr;
123     }
124 
125     auto text = AceType::MakeRefPtr<TextComponent>(buttonProperties.value);
126     if (!themeManager_) {
127         LOGE("themeManager is null.");
128         return nullptr;
129     }
130 
131     auto buttonTheme = themeManager_->GetTheme<ButtonTheme>();
132     if (!buttonTheme) {
133         LOGE("buttonTheme is null.");
134         return nullptr;
135     }
136 
137     auto textStyle = text->GetTextStyle();
138     textStyle.SetAllowScale(false);
139     textStyle.SetFontSize(DEFAULT_FONT_SIZE);
140     textStyle.SetTextColor(Color::WHITE);
141     text->SetTextStyle(std::move(textStyle));
142     std::list<RefPtr<Component>> buttonChildren;
143     buttonChildren.emplace_back(SetPadding(text, buttonTheme->GetPadding()));
144     auto buttonComponent = AceType::MakeRefPtr<ButtonComponent>(buttonChildren);
145     buttonComponent->SetType(ButtonType::CAPSULE);
146     buttonComponent->SetDeclarativeFlag(true);
147     buttonComponent->SetHeight(buttonTheme->GetHeight());
148     buttonComponent->SetBackgroundColor(Color::TRANSPARENT);
149     buttonComponent->SetIsPopupButton(true);
150     buttonComponent->SetClickedColor(POPUP_BUTTON_CLICKED_COLOR);
151     buttonComponent->SetHoverColor(POPUP_BUTTON_HOVER_COLOR);
152     buttonComponent->SetMouseAnimationType(HoverAnimationType::NONE);
153     buttonComponent->SetClickFunction([action = buttonProperties.actionId, context = context_]() {
154         auto func = AceAsyncEvent<void()>::Create(action, context);
155         if (func) {
156             func();
157         }
158     });
159     return buttonComponent;
160 }
161 
SetPadding(const RefPtr<Component> & component,const Edge & edge)162 const RefPtr<Component> PopupComponentV2::SetPadding(const RefPtr<Component>& component, const Edge& edge)
163 {
164     auto paddingComponent = AceType::MakeRefPtr<PaddingComponent>();
165     paddingComponent->SetPadding(edge);
166     paddingComponent->SetChild(component);
167     return paddingComponent;
168 }
169 
CreateBox(const RefPtr<PopupTheme> & popupTheme)170 const RefPtr<BoxComponent> PopupComponentV2::CreateBox(const RefPtr<PopupTheme>& popupTheme)
171 {
172     auto box = AceType::MakeRefPtr<BoxComponent>();
173     auto decoration = box->GetBackDecoration();
174     if (!decoration) {
175         decoration = AceType::MakeRefPtr<Decoration>();
176         box->SetBackDecoration(decoration);
177     }
178     decoration->SetBorderRadius(popupTheme->GetRadius());
179 
180     if (!customComponent_ && !primaryButtonProperties_.showButton && !secondaryButtonProperties_.showButton) {
181         auto padding = popupTheme->GetPadding();
182         box->SetPadding(padding);
183         GetPopupParam()->SetPadding(padding);
184     }
185 
186     GetPopupParam()->SetBorder(decoration->GetBorder());
187     GetPopupParam()->SetTargetSpace(popupTheme->GetTargetSpace());
188     if (!GetPopupParam()->IsMaskColorSetted()) {
189         GetPopupParam()->SetMaskColor(popupTheme->GetMaskColor());
190     }
191     if (!GetPopupParam()->IsBackgroundColorSetted()) {
192         GetPopupParam()->SetBackgroundColor(popupTheme->GetBackgroundColor());
193     }
194     if (placementOnTop_) {
195         GetPopupParam()->SetPlacement(Placement::TOP);
196     }
197 
198     RefPtr<GridColumnInfo> columnInfo = GridSystemManager::GetInstance().GetInfoByType(GridColumnType::BUBBLE_TYPE);
199     if (columnInfo->GetParent()) {
200         columnInfo->GetParent()->BuildColumnWidth();
201     }
202     auto gridSizeType = GridSystemManager::GetInstance().GetCurrentSize();
203     double maxWidth = columnInfo->GetWidth(columnInfo->GetColumns(gridSizeType));
204     box->SetMaxWidth(Dimension(maxWidth, DimensionUnit::PX));
205 
206     return box;
207 }
208 
209 } // namespace OHOS::Ace