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