• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_ng/pattern/security_component/security_component_model_ng.h"
17 
18 #include "base/i18n/localization.h"
19 #include "base/log/ace_scoring_log.h"
20 #include "base/utils/utils.h"
21 #include "core/components/common/properties/text_style.h"
22 #include "core/components_ng/base/frame_node.h"
23 #include "core/components_ng/base/view_stack_processor.h"
24 #include "core/components_ng/pattern/button/button_layout_property.h"
25 #include "core/components_ng/pattern/button/button_pattern.h"
26 #include "core/components_ng/pattern/image/image_pattern.h"
27 #include "core/components_ng/pattern/security_component/security_component_pattern.h"
28 #include "core/components_ng/pattern/security_component/security_component_theme.h"
29 #include "core/components_ng/pattern/text/text_pattern.h"
30 #include "core/components_v2/inspector/inspector_constants.h"
31 #include "core/pipeline_ng/pipeline_context.h"
32 
33 namespace OHOS::Ace::NG {
GetTheme()34 RefPtr<SecurityComponentTheme> SecurityComponentModelNG::GetTheme()
35 {
36     auto pipeline = PipelineContext::GetCurrentContext();
37     CHECK_NULL_RETURN(pipeline, nullptr);
38     return pipeline->GetTheme<SecurityComponentTheme>();
39 }
40 
InitLayoutProperty(RefPtr<FrameNode> & node,int32_t text,int32_t icon,int32_t backgroundType)41 void SecurityComponentModelNG::InitLayoutProperty(RefPtr<FrameNode>& node, int32_t text, int32_t icon,
42     int32_t backgroundType)
43 {
44     auto property = node->GetLayoutProperty<SecurityComponentLayoutProperty>();
45     CHECK_NULL_VOID(property);
46     auto secCompTheme = GetTheme();
47     CHECK_NULL_VOID(secCompTheme);
48     property->UpdateSecurityComponentDescription(text);
49     property->UpdateIconStyle(icon);
50     property->UpdateBackgroundType(backgroundType);
51 
52     if ((text != static_cast<int32_t>(SecurityComponentDescription::TEXT_NULL)) &&
53         (icon != static_cast<int32_t>(SecurityComponentIconStyle::ICON_NULL))) {
54         property->UpdateTextIconSpace(secCompTheme->GetTextIconSpace());
55     } else {
56         property->UpdateTextIconSpace(Dimension(0.0F));
57     }
58 
59     if (backgroundType == BUTTON_TYPE_NULL) {
60         property->UpdateBackgroundLeftPadding(secCompTheme->GetPaddingWithoutBg());
61         property->UpdateBackgroundRightPadding(secCompTheme->GetPaddingWithoutBg());
62         property->UpdateBackgroundTopPadding(secCompTheme->GetPaddingWithoutBg());
63         property->UpdateBackgroundBottomPadding(secCompTheme->GetPaddingWithoutBg());
64     } else {
65         property->UpdateBackgroundLeftPadding(secCompTheme->GetBackgroundLeftPadding());
66         property->UpdateBackgroundRightPadding(secCompTheme->GetBackgroundRightPadding());
67         property->UpdateBackgroundTopPadding(secCompTheme->GetBackgroundTopPadding());
68         property->UpdateBackgroundBottomPadding(secCompTheme->GetBackgroundBottomPadding());
69     }
70 
71     property->UpdateTextIconLayoutDirection(SecurityComponentLayoutDirection::HORIZONTAL);
72 }
73 
CreateCommon(const std::string & tag,int32_t text,int32_t icon,int32_t backgroundType,const std::function<RefPtr<Pattern> (void)> & patternCreator)74 void SecurityComponentModelNG::CreateCommon(const std::string& tag, int32_t text, int32_t icon,
75     int32_t backgroundType, const std::function<RefPtr<Pattern>(void)>& patternCreator)
76 {
77     auto stack = ViewStackProcessor::GetInstance();
78     auto nodeId = stack->ClaimNodeId();
79     auto frameNode = FrameNode::GetOrCreateFrameNode(tag, nodeId, patternCreator);
80     CHECK_NULL_VOID(frameNode);
81 
82     if (frameNode->GetChildren().empty()) {
83         bool isButtonVisible = (backgroundType != BUTTON_TYPE_NULL);
84         auto buttonNode = FrameNode::CreateFrameNode(
85             V2::BUTTON_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
86             AceType::MakeRefPtr<ButtonPattern>());
87         buttonNode->SetInternal();
88 
89         if (isButtonVisible) {
90             SetDefaultBackgroundButton(buttonNode, backgroundType);
91         } else {
92             SetInvisibleBackgroundButton(buttonNode);
93         }
94         frameNode->AddChild(buttonNode);
95 
96         if (icon != static_cast<int32_t>(SecurityComponentIconStyle::ICON_NULL)) {
97             auto imageIcon = FrameNode::CreateFrameNode(
98                 V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<ImagePattern>());
99             imageIcon->SetInternal();
100             InternalResource::ResourceId iconId;
101             if (GetIconResource(icon, iconId)) {
102                 SetDefaultIconStyle(imageIcon, iconId, isButtonVisible);
103             }
104             frameNode->AddChild(imageIcon);
105         }
106 
107         if (text != static_cast<int32_t>(SecurityComponentDescription::TEXT_NULL)) {
108             auto textNode = FrameNode::CreateFrameNode(
109                 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
110             textNode->SetInternal();
111             std::string textStr = "";
112             GetTextResource(text, textStr);
113             SetDefaultTextStyle(textNode, textStr, isButtonVisible);
114             frameNode->AddChild(textNode);
115         }
116         InitLayoutProperty(frameNode, text, icon, backgroundType);
117     }
118     auto property = frameNode->GetLayoutProperty<SecurityComponentLayoutProperty>();
119     CHECK_NULL_VOID(property);
120     property->UpdatePropertyChangeFlag(PROPERTY_UPDATE_MEASURE);
121     stack->Push(frameNode);
122 }
123 
SetDefaultTextStyle(const RefPtr<FrameNode> & textNode,const std::string & text,bool isButtonVisible)124 void SecurityComponentModelNG::SetDefaultTextStyle(const RefPtr<FrameNode>& textNode, const std::string& text,
125     bool isButtonVisible)
126 {
127     auto secCompTheme = GetTheme();
128     CHECK_NULL_VOID(secCompTheme);
129     auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
130     CHECK_NULL_VOID(textLayoutProperty);
131     textLayoutProperty->UpdateContent(text);
132     textLayoutProperty->UpdateMaxLines(1);
133     textLayoutProperty->UpdateFontSize(secCompTheme->GetFontSize());
134     textLayoutProperty->UpdateItalicFontStyle(Ace::FontStyle::NORMAL);
135     textLayoutProperty->UpdateFontWeight(FontWeight::MEDIUM);
136     std::vector<std::string> defaultFontFamily = { "HarmonyOS Sans" };
137     textLayoutProperty->UpdateFontFamily(defaultFontFamily);
138 
139     if (isButtonVisible) {
140         textLayoutProperty->UpdateTextColor(secCompTheme->GetFontColor());
141     } else {
142         textLayoutProperty->UpdateTextColor(secCompTheme->GetFontColorNoBg());
143     }
144 }
145 
SetDefaultIconStyle(const RefPtr<FrameNode> & imageNode,InternalResource::ResourceId id,bool isButtonVisible)146 void SecurityComponentModelNG::SetDefaultIconStyle(const RefPtr<FrameNode>& imageNode, InternalResource::ResourceId id,
147     bool isButtonVisible)
148 {
149     auto secCompTheme = GetTheme();
150     CHECK_NULL_VOID(secCompTheme);
151     ImageSourceInfo imageSourceInfo;
152     imageSourceInfo.SetResourceId(id);
153     if (isButtonVisible) {
154         imageSourceInfo.SetFillColor(secCompTheme->GetIconColor());
155     } else {
156         imageSourceInfo.SetFillColor(secCompTheme->GetIconColorNoBg());
157     }
158 
159     auto iconProp = imageNode->GetLayoutProperty<ImageLayoutProperty>();
160     CHECK_NULL_VOID(iconProp);
161     iconProp->UpdateImageSourceInfo(imageSourceInfo);
162     iconProp->UpdateUserDefinedIdealSize(
163         CalcSize(NG::CalcLength(secCompTheme->GetIconSize()), NG::CalcLength(secCompTheme->GetIconSize())));
164 }
165 
SetDefaultBackgroundButton(const RefPtr<FrameNode> & buttonNode,int32_t type)166 void SecurityComponentModelNG::SetDefaultBackgroundButton(const RefPtr<FrameNode>& buttonNode,
167     int32_t type)
168 {
169     auto buttonLayoutProperty = buttonNode->GetLayoutProperty<ButtonLayoutProperty>();
170     CHECK_NULL_VOID(buttonLayoutProperty);
171     const auto& renderContext = buttonNode->GetRenderContext();
172     CHECK_NULL_VOID(renderContext);
173     auto secCompTheme = GetTheme();
174     CHECK_NULL_VOID(secCompTheme);
175 
176     BorderColorProperty borderColor;
177     borderColor.SetColor(secCompTheme->GetBorderColor());
178     renderContext->UpdateBorderColor(borderColor);
179     BorderWidthProperty widthProp;
180     widthProp.SetBorderWidth(secCompTheme->GetBorderWidth());
181     buttonLayoutProperty->UpdateBorderWidth(widthProp);
182     BorderStyleProperty style;
183     style.SetBorderStyle(BorderStyle::NONE);
184     renderContext->UpdateBorderStyle(style);
185     auto buttonRadius = secCompTheme->GetBorderRadius();
186     buttonLayoutProperty->UpdateBorderRadius(BorderRadiusProperty(buttonRadius));
187     renderContext->UpdateBackgroundColor(secCompTheme->GetBackgroundColor());
188     buttonLayoutProperty->UpdateType(static_cast<ButtonType>(type));
189 }
190 
SetInvisibleBackgroundButton(const RefPtr<FrameNode> & buttonNode)191 void SecurityComponentModelNG::SetInvisibleBackgroundButton(const RefPtr<FrameNode>& buttonNode)
192 {
193     auto buttonLayoutProperty = buttonNode->GetLayoutProperty<ButtonLayoutProperty>();
194     CHECK_NULL_VOID(buttonLayoutProperty);
195     const auto& renderContext = buttonNode->GetRenderContext();
196     CHECK_NULL_VOID(renderContext);
197     renderContext->UpdateBackgroundColor(Color::TRANSPARENT);
198     buttonLayoutProperty->UpdateType(ButtonType::NORMAL);
199 }
200 
201 template<typename T>
GetChildLayoutProprty(const std::string & tag)202 RefPtr<T> GetChildLayoutProprty(const std::string& tag)
203 {
204     auto node = GetCurSecCompChildNode(tag);
205     CHECK_NULL_RETURN(node, nullptr);
206     return node->GetLayoutProperty<T>();
207 }
208 
IsBackgroundVisible()209 bool SecurityComponentModelNG::IsBackgroundVisible()
210 {
211     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
212     CHECK_NULL_RETURN(frameNode, false);
213     auto prop = frameNode->GetLayoutProperty<SecurityComponentLayoutProperty>();
214     if (prop) {
215         return (prop->GetBackgroundType() != BUTTON_TYPE_NULL);
216     }
217     return false;
218 }
219 
SetIconSize(const Dimension & value)220 void SecurityComponentModelNG::SetIconSize(const Dimension& value)
221 {
222     auto iconProp = GetChildLayoutProprty<ImageLayoutProperty>(V2::IMAGE_ETS_TAG);
223     CHECK_NULL_VOID(iconProp);
224     iconProp->UpdateUserDefinedIdealSize(CalcSize(NG::CalcLength(value), NG::CalcLength(value)));
225 }
226 
SetIconColor(const Color & value)227 void SecurityComponentModelNG::SetIconColor(const Color& value)
228 {
229     auto iconProp = GetChildLayoutProprty<ImageLayoutProperty>(V2::IMAGE_ETS_TAG);
230     CHECK_NULL_VOID(iconProp);
231     auto iconSrcInfo = iconProp->GetImageSourceInfo().value();
232     iconSrcInfo.SetFillColor(value);
233     iconProp->UpdateImageSourceInfo(iconSrcInfo);
234 }
235 
SetFontSize(const Dimension & value)236 void SecurityComponentModelNG::SetFontSize(const Dimension& value)
237 {
238     auto textProp = GetChildLayoutProprty<TextLayoutProperty>(V2::TEXT_ETS_TAG);
239     CHECK_NULL_VOID(textProp);
240     textProp->UpdateFontSize(value);
241 }
242 
SetFontStyle(const Ace::FontStyle & value)243 void SecurityComponentModelNG::SetFontStyle(const Ace::FontStyle& value)
244 {
245     auto textProp = GetChildLayoutProprty<TextLayoutProperty>(V2::TEXT_ETS_TAG);
246     CHECK_NULL_VOID(textProp);
247     textProp->UpdateItalicFontStyle(value);
248 }
249 
SetFontWeight(const FontWeight & value)250 void SecurityComponentModelNG::SetFontWeight(const FontWeight& value)
251 {
252     auto textProp = GetChildLayoutProprty<TextLayoutProperty>(V2::TEXT_ETS_TAG);
253     CHECK_NULL_VOID(textProp);
254     textProp->UpdateFontWeight(value);
255 }
256 
SetFontFamily(const std::vector<std::string> & fontFamilies)257 void SecurityComponentModelNG::SetFontFamily(const std::vector<std::string>& fontFamilies)
258 {
259     auto textProp = GetChildLayoutProprty<TextLayoutProperty>(V2::TEXT_ETS_TAG);
260     CHECK_NULL_VOID(textProp);
261     textProp->UpdateFontFamily(fontFamilies);
262 }
263 
SetFontColor(const Color & value)264 void SecurityComponentModelNG::SetFontColor(const Color& value)
265 {
266     auto textProp = GetChildLayoutProprty<TextLayoutProperty>(V2::TEXT_ETS_TAG);
267     CHECK_NULL_VOID(textProp);
268     textProp->UpdateTextColor(value);
269 }
270 
SetBackgroundColor(const Color & value)271 void SecurityComponentModelNG::SetBackgroundColor(const Color& value)
272 {
273     if (!IsBackgroundVisible()) {
274         LOGW("background is not exist");
275         return;
276     }
277     auto bgNode = GetCurSecCompChildNode(V2::BUTTON_ETS_TAG);
278     CHECK_NULL_VOID(bgNode);
279     const auto& renderContext = bgNode->GetRenderContext();
280     CHECK_NULL_VOID(renderContext);
281     renderContext->UpdateBackgroundColor(value);
282 }
283 
SetBackgroundBorderWidth(const Dimension & value)284 void SecurityComponentModelNG::SetBackgroundBorderWidth(const Dimension& value)
285 {
286     if (!IsBackgroundVisible()) {
287         LOGW("background is not exist");
288         return;
289     }
290 
291     auto bgProp = GetChildLayoutProprty<ButtonLayoutProperty>(V2::BUTTON_ETS_TAG);
292     CHECK_NULL_VOID(bgProp);
293     BorderWidthProperty widthProp;
294     widthProp.SetBorderWidth(value);
295     bgProp->UpdateBorderWidth(widthProp);
296 }
297 
SetBackgroundBorderColor(const Color & value)298 void SecurityComponentModelNG::SetBackgroundBorderColor(const Color& value)
299 {
300     if (!IsBackgroundVisible()) {
301         LOGW("background is not exist");
302         return;
303     }
304 
305     auto bgNode = GetCurSecCompChildNode(V2::BUTTON_ETS_TAG);
306     CHECK_NULL_VOID(bgNode);
307     const auto& renderContext = bgNode->GetRenderContext();
308     CHECK_NULL_VOID(renderContext);
309     BorderColorProperty borderColor;
310     borderColor.SetColor(value);
311     renderContext->UpdateBorderColor(borderColor);
312 }
313 
SetBackgroundBorderStyle(const BorderStyle & value)314 void SecurityComponentModelNG::SetBackgroundBorderStyle(const BorderStyle& value)
315 {
316     if (!IsBackgroundVisible()) {
317         LOGW("background is not exist");
318         return;
319     }
320 
321     auto bgNode = GetCurSecCompChildNode(V2::BUTTON_ETS_TAG);
322     CHECK_NULL_VOID(bgNode);
323     const auto& renderContext = bgNode->GetRenderContext();
324     CHECK_NULL_VOID(renderContext);
325     BorderStyleProperty style;
326     style.SetBorderStyle(value);
327     renderContext->UpdateBorderStyle(style);
328 }
329 
SetBackgroundBorderRadius(const Dimension & value)330 void SecurityComponentModelNG::SetBackgroundBorderRadius(const Dimension& value)
331 {
332     if (!IsBackgroundVisible()) {
333         LOGW("background is not exist");
334         return;
335     }
336     auto bgProp = GetChildLayoutProprty<ButtonLayoutProperty>(V2::BUTTON_ETS_TAG);
337     CHECK_NULL_VOID(bgProp);
338     bgProp->UpdateBorderRadius(BorderRadiusProperty(value));
339 }
340 
SetBackgroundPadding(const std::optional<Dimension> & left,const std::optional<Dimension> & right,const std::optional<Dimension> & top,const std::optional<Dimension> & bottom)341 void SecurityComponentModelNG::SetBackgroundPadding(const std::optional<Dimension>& left,
342     const std::optional<Dimension>& right, const std::optional<Dimension>& top,
343     const std::optional<Dimension>& bottom)
344 {
345     if (!IsBackgroundVisible()) {
346         LOGW("Can not set background padding without background");
347         return;
348     }
349 
350     auto secCompTheme = GetTheme();
351     CHECK_NULL_VOID(secCompTheme);
352     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty,
353         BackgroundLeftPadding, left.value_or(secCompTheme->GetBackgroundLeftPadding()));
354     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty,
355         BackgroundRightPadding, right.value_or(secCompTheme->GetBackgroundRightPadding()));
356     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty,
357         BackgroundTopPadding, top.value_or(secCompTheme->GetBackgroundTopPadding()));
358     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty,
359         BackgroundBottomPadding, bottom.value_or(secCompTheme->GetBackgroundBottomPadding()));
360 }
361 
SetBackgroundPadding(const std::optional<Dimension> & padding)362 void SecurityComponentModelNG::SetBackgroundPadding(const std::optional<Dimension>& padding)
363 {
364     SetBackgroundPadding(padding, padding, padding, padding);
365 }
366 
SetTextIconSpace(const Dimension & value)367 void SecurityComponentModelNG::SetTextIconSpace(const Dimension& value)
368 {
369     if ((GetCurSecCompChildNode(V2::TEXT_ETS_TAG) == nullptr) ||
370         (GetCurSecCompChildNode(V2::IMAGE_ETS_TAG) == nullptr)) {
371         LOGW("Can not set text icon padding without text and icon");
372         return;
373     }
374     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty, TextIconSpace, value);
375 }
376 
SetTextIconLayoutDirection(const SecurityComponentLayoutDirection & value)377 void SecurityComponentModelNG::SetTextIconLayoutDirection(const SecurityComponentLayoutDirection& value)
378 {
379     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty, TextIconLayoutDirection, value);
380 }
381 } // namespace OHOS::Ace::NG
382