1 /*
2 * Copyright (c) 2021 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 "frameworks/bridge/common/dom/dom_popup.h"
17
18 #include "core/components/focus_collaboration/focus_collaboration_component.h"
19 #include "core/components/popup/popup_theme.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21
22 namespace OHOS::Ace::Framework {
23 namespace {
24
25 constexpr uint32_t MASK_COLOR_ALPHA = 0x4c000000; // UX standand, 30% opacity
26
27 } // namespace
28
DOMPopup(NodeId nodeId,const std::string & nodeName)29 DOMPopup::DOMPopup(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName)
30 {
31 popupChild_ = AceType::MakeRefPtr<PopupComponent>(std::to_string(nodeId), nodeName);
32 }
33
~DOMPopup()34 DOMPopup::~DOMPopup()
35 {
36 RemoveMarker();
37 }
38
InitializeStyle()39 void DOMPopup::InitializeStyle()
40 {
41 RefPtr<PopupTheme> theme = GetTheme<PopupTheme>();
42 if (!theme) {
43 return;
44 }
45 maskColor_ = theme->GetMaskColor();
46 popupChild_->GetPopupParam()->SetMaskColor(maskColor_);
47 backgroundColor_ = theme->GetBackgroundColor();
48 popupChild_->GetPopupParam()->SetBackgroundColor(backgroundColor_);
49
50 if (!declaration_) {
51 return;
52 }
53 auto& borderStyle = declaration_->MaybeResetStyle<CommonBorderStyle>(StyleTag::COMMON_BORDER_STYLE);
54 if (borderStyle.IsValid()) {
55 borderStyle.border.SetBorderRadius(theme->GetRadius());
56 }
57
58 auto& paddingStyle = declaration_->MaybeResetStyle<CommonPaddingStyle>(StyleTag::COMMON_PADDING_STYLE);
59 if (paddingStyle.IsValid()) {
60 paddingStyle.padding = theme->GetPadding();
61 }
62 declaration_->SetHasBoxStyle(true);
63 declaration_->SetHasDecorationStyle(true);
64 }
65
RemoveMarker()66 void DOMPopup::RemoveMarker()
67 {
68 BackEndEventManager<void(const ClickInfo&)>::GetInstance().RemoveBackEndEvent(clickMarker_);
69 }
70
ResetInitializedStyle()71 void DOMPopup::ResetInitializedStyle()
72 {
73 InitializeStyle();
74 }
75
OnChildNodeRemoved(const RefPtr<DOMNode> & child)76 void DOMPopup::OnChildNodeRemoved(const RefPtr<DOMNode>& child)
77 {
78 popupChild_->SetChild(nullptr);
79 }
80
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)81 bool DOMPopup::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
82 {
83 // static linear map must be sorted by key.
84 static const LinearMapNode<Placement> placeMap[] = {
85 { "bottom", Placement::BOTTOM },
86 { "bottomLeft", Placement::BOTTOM_LEFT },
87 { "bottomRight", Placement::BOTTOM_RIGHT },
88 { "left", Placement::LEFT },
89 { "right", Placement::RIGHT },
90 { "top", Placement::TOP },
91 { "topLeft", Placement::TOP_LEFT },
92 { "topRight", Placement::TOP_RIGHT },
93 };
94 if (attr.first == DOM_PLACEMENT) {
95 auto valueIt = BinarySearchFindIndex(placeMap, ArraySize(placeMap), attr.second.c_str());
96 if (valueIt != -1) {
97 popupChild_->GetPopupParam()->SetPlacement(placeMap[valueIt].value);
98 }
99 return true;
100 } else if (attr.first == DOM_ARROW_OFFSET) {
101 popupChild_->GetPopupParam()->SetArrowOffset(ParseDimension(attr.second));
102 return true;
103 } else if (attr.first == DOM_CLICKABLE) {
104 clickable_ = StringToBool(attr.second);
105 return true;
106 } else if (attr.first == DOM_KEEP_ALIVE) {
107 popupChild_->GetPopupParam()->SetHasAction(StringToBool(attr.second));
108 return true;
109 }
110 return false;
111 }
112
SetSpecializedStyle(const std::pair<std::string,std::string> & style)113 bool DOMPopup::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
114 {
115 if (style.first == DOM_BACKGROUND_COLOR || style.first == DOM_BACKGROUND || style.first == DOM_BACKGROUND_IMAGE) {
116 hasBackground_ = true;
117 }
118
119 // popup don't support position
120 if (style.first == DOM_POSITION || style.first == DOM_POSITION_LEFT || style.first == DOM_POSITION_RIGHT ||
121 style.first == DOM_POSITION_TOP || style.first == DOM_POSITION_BOTTOM) {
122 return true;
123 }
124
125 if (style.first == DOM_MASK_COLOR) {
126 maskColor_ = ParseColor(style.second, MASK_COLOR_ALPHA);
127 popupChild_->GetPopupParam()->SetMaskColor(maskColor_);
128 return true;
129 } else if (style.first == DOM_BACKGROUND_COLOR) {
130 backgroundColor_ = ParseColor(style.second);
131 popupChild_->GetPopupParam()->SetBackgroundColor(backgroundColor_);
132 return false;
133 } else {
134 return false;
135 }
136 }
137
AddSpecializedEvent(int32_t pageId,const std::string & event)138 bool DOMPopup::AddSpecializedEvent(int32_t pageId, const std::string& event)
139 {
140 if (event == DOM_VISIBILITY_CHANGE) {
141 visibilityChangeEventId_ = EventMarker(GetNodeIdForEvent(), event, pageId);
142 popupChild_->GetPopupParam()->SetOnVisibilityChange(visibilityChangeEventId_);
143 return true;
144 } else {
145 return false;
146 }
147 }
148
CallSpecializedMethod(const std::string & method,const std::string & args)149 void DOMPopup::CallSpecializedMethod(const std::string& method, const std::string& args)
150 {
151 auto popup = AceType::DynamicCast<PopupComponent>(popupChild_);
152 if (!popup) {
153 return;
154 }
155 auto controller = popup->GetPopupController();
156 if (!controller) {
157 return;
158 }
159 if (method == DOM_SHOW) {
160 controller->ShowPopup();
161 } else if (method == DOM_HIDE) {
162 controller->CancelPopup();
163 }
164 }
165
BindIdNode(const RefPtr<DOMNode> & idNode)166 void DOMPopup::BindIdNode(const RefPtr<DOMNode>& idNode)
167 {
168 if (!idNode) {
169 return;
170 }
171 if (!clickMarker_.IsEmpty()) {
172 RemoveMarker();
173 }
174 clickMarker_ = BackEndEventManager<void(const ClickInfo&)>::GetInstance().GetAvailableMarker();
175 popupChild_->GetPopupParam()->SetTargetId(idNode->GetRootComponent()->GetId());
176 popupChild_->GetPopupParam()->SetTargetMargin(idNode->GetBoxComponent()->GetMargin());
177
178 if (clickable_) {
179 BackEndEventManager<void(const ClickInfo&)>::GetInstance().BindBackendEvent(
180 clickMarker_, [weakChild = WeakPtr<PopupComponent>(popupChild_), weakNode = WeakPtr<DOMNode>(idNode)](
181 const ClickInfo& clickInfo) {
182 auto popupChild = weakChild.Upgrade();
183 auto idNode = weakNode.Upgrade();
184 if (popupChild && idNode) {
185 auto controller = popupChild->GetPopupController();
186 controller->ShowPopup();
187 }
188 });
189 idNode->SetOnClick(clickMarker_);
190 idNode->MarkNeedUpdate();
191 }
192 }
193
OnChildNodeAdded(const RefPtr<DOMNode> & child,int32_t slot)194 void DOMPopup::OnChildNodeAdded(const RefPtr<DOMNode>& child, int32_t slot)
195 {
196 if (!child) {
197 return;
198 }
199 if (lastComponent_) {
200 auto focusCollaboration = AceType::MakeRefPtr<FocusCollaborationComponent>();
201 focusCollaboration->InsertChild(0, child->GetRootComponent());
202 lastComponent_->SetChild(focusCollaboration);
203 }
204 }
205
PrepareSpecializedComponent()206 void DOMPopup::PrepareSpecializedComponent()
207 {
208 if (boxComponent_->GetBackDecoration()) {
209 auto boxBorder = boxComponent_->GetBackDecoration()->GetBorder();
210 popupChild_->GetPopupParam()->SetBorder(boxBorder);
211 Border border;
212 border.SetTopLeftRadius(boxBorder.TopLeftRadius());
213 border.SetTopRightRadius(boxBorder.TopRightRadius());
214 border.SetBottomLeftRadius(boxBorder.BottomLeftRadius());
215 border.SetBottomRightRadius(boxBorder.BottomRightRadius());
216 boxComponent_->GetBackDecoration()->SetBorder(border);
217 boxComponent_->GetBackDecoration()->SetBackgroundColor(Color::TRANSPARENT);
218 }
219 popupChild_->GetPopupParam()->SetIsShow(IsShow());
220 popupChild_->GetPopupParam()->SetPadding(boxComponent_->GetPadding());
221 popupChild_->GetPopupParam()->SetMargin(boxComponent_->GetMargin());
222 boxComponent_->SetMargin(Edge());
223 }
224
CompositeSpecializedComponent(const std::vector<RefPtr<SingleChild>> & components)225 RefPtr<Component> DOMPopup::CompositeSpecializedComponent(const std::vector<RefPtr<SingleChild>>& components)
226 {
227 lastComponent_ = components.back();
228 popupChild_->SetChild(AceType::DynamicCast<Component>(components.front()));
229 auto box = AceType::MakeRefPtr<BoxComponent>();
230 box->SetChild(popupChild_);
231 return box;
232 }
233
234 } // namespace OHOS::Ace::Framework
235