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