• 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(Dimension(0.0));
55     }
56 
57     if (backgroundType == BUTTON_TYPE_NULL) {
58         property->UpdateBackgroundLeftPadding(secCompTheme->GetPaddingWithoutBg());
59         property->UpdateBackgroundRightPadding(secCompTheme->GetPaddingWithoutBg());
60         property->UpdateBackgroundTopPadding(secCompTheme->GetPaddingWithoutBg());
61         property->UpdateBackgroundBottomPadding(secCompTheme->GetPaddingWithoutBg());
62     }
63 
64     property->UpdateTextIconLayoutDirection(SecurityComponentLayoutDirection::HORIZONTAL);
65 }
66 
CreateNode(const std::string & tag,int32_t nodeId,SecurityComponentElementStyle & style,const std::function<RefPtr<Pattern> (void)> & patternCreator)67 RefPtr<FrameNode> SecurityComponentModelNG::CreateNode(const std::string& tag, int32_t nodeId,
68     SecurityComponentElementStyle& style,
69     const std::function<RefPtr<Pattern>(void)>& patternCreator)
70 {
71     ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", tag.c_str(), nodeId);
72     auto frameNode = FrameNode::GetOrCreateFrameNode(tag, nodeId, patternCreator);
73     CHECK_NULL_RETURN(frameNode, nullptr);
74 
75     if (frameNode->GetChildren().empty()) {
76         bool isButtonVisible = (style.backgroundType != BUTTON_TYPE_NULL);
77         auto buttonNode = FrameNode::CreateFrameNode(
78             V2::BUTTON_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
79             AceType::MakeRefPtr<ButtonPattern>());
80         buttonNode->SetInternal();
81 
82         if (isButtonVisible) {
83             SetDefaultBackgroundButton(buttonNode, style.backgroundType);
84         } else {
85             SetInvisibleBackgroundButton(buttonNode);
86         }
87         frameNode->AddChild(buttonNode);
88 
89         if (style.icon != static_cast<int32_t>(SecurityComponentIconStyle::ICON_NULL)) {
90             auto imageIcon = FrameNode::CreateFrameNode(
91                 V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<ImagePattern>());
92             imageIcon->SetInternal();
93             InternalResource::ResourceId iconId;
94             if (GetIconResource(style.icon, iconId)) {
95                 SetDefaultIconStyle(imageIcon, iconId, isButtonVisible);
96             }
97             frameNode->AddChild(imageIcon);
98         }
99 
100         if (style.text != static_cast<int32_t>(SecurityComponentDescription::TEXT_NULL)) {
101             auto textNode = FrameNode::CreateFrameNode(
102                 V2::TEXT_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<TextPattern>());
103             textNode->SetInternal();
104             std::string textStr = "";
105             GetTextResource(style.text, textStr);
106             SetDefaultTextStyle(textNode, textStr, isButtonVisible);
107             frameNode->AddChild(textNode);
108         }
109         InitLayoutProperty(frameNode, style.text, style.icon, style.backgroundType);
110     }
111     auto property = frameNode->GetLayoutProperty<SecurityComponentLayoutProperty>();
112     CHECK_NULL_RETURN(property, nullptr);
113     property->UpdatePropertyChangeFlag(PROPERTY_UPDATE_MEASURE);
114     auto pipeline = AceType::DynamicCast<PipelineContext>(PipelineBase::GetCurrentContext());
115     CHECK_NULL_RETURN(pipeline, nullptr);
116     pipeline->AddWindowStateChangedCallback(nodeId);
117     return frameNode;
118 }
119 
CreateCommon(const std::string & tag,int32_t text,int32_t icon,int32_t backgroundType,const std::function<RefPtr<Pattern> (void)> & patternCreator)120 void SecurityComponentModelNG::CreateCommon(const std::string& tag, int32_t text, int32_t icon,
121     int32_t backgroundType, const std::function<RefPtr<Pattern>(void)>& patternCreator)
122 {
123     auto stack = ViewStackProcessor::GetInstance();
124     auto nodeId = stack->ClaimNodeId();
125     SecurityComponentElementStyle style = {
126         .text = text,
127         .icon = icon,
128         .backgroundType = backgroundType
129     };
130     auto frameNode = CreateNode(tag, nodeId, style, patternCreator);
131     CHECK_NULL_VOID(frameNode);
132     stack->Push(frameNode);
133 }
134 
SetDefaultTextStyle(const RefPtr<FrameNode> & textNode,const std::string & text,bool isButtonVisible)135 void SecurityComponentModelNG::SetDefaultTextStyle(const RefPtr<FrameNode>& textNode, const std::string& text,
136     bool isButtonVisible)
137 {
138     auto secCompTheme = GetTheme();
139     CHECK_NULL_VOID(secCompTheme);
140     auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
141     CHECK_NULL_VOID(textLayoutProperty);
142     textLayoutProperty->UpdateContent(text);
143     textLayoutProperty->UpdateMaxLines(1);
144     textLayoutProperty->UpdateFontSize(secCompTheme->GetFontSize());
145     textLayoutProperty->UpdateItalicFontStyle(Ace::FontStyle::NORMAL);
146     textLayoutProperty->UpdateFontWeight(FontWeight::MEDIUM);
147     std::vector<std::string> defaultFontFamily = { "HarmonyOS Sans" };
148     textLayoutProperty->UpdateFontFamily(defaultFontFamily);
149 
150     if (isButtonVisible) {
151         textLayoutProperty->UpdateTextColor(secCompTheme->GetFontColor());
152     } else {
153         textLayoutProperty->UpdateTextColor(secCompTheme->GetFontColorNoBg());
154     }
155 }
156 
SetDefaultIconStyle(const RefPtr<FrameNode> & imageNode,InternalResource::ResourceId id,bool isButtonVisible)157 void SecurityComponentModelNG::SetDefaultIconStyle(const RefPtr<FrameNode>& imageNode, InternalResource::ResourceId id,
158     bool isButtonVisible)
159 {
160     auto secCompTheme = GetTheme();
161     CHECK_NULL_VOID(secCompTheme);
162     ImageSourceInfo imageSourceInfo;
163     imageSourceInfo.SetResourceId(id);
164     if (isButtonVisible) {
165         imageSourceInfo.SetFillColor(secCompTheme->GetIconColor());
166     } else {
167         imageSourceInfo.SetFillColor(secCompTheme->GetIconColorNoBg());
168     }
169 
170     auto iconProp = imageNode->GetLayoutProperty<ImageLayoutProperty>();
171     CHECK_NULL_VOID(iconProp);
172     iconProp->UpdateImageSourceInfo(imageSourceInfo);
173     iconProp->UpdateUserDefinedIdealSize(
174         CalcSize(NG::CalcLength(secCompTheme->GetIconSize()), NG::CalcLength(secCompTheme->GetIconSize())));
175 }
176 
SetDefaultBackgroundButton(const RefPtr<FrameNode> & buttonNode,int32_t type)177 void SecurityComponentModelNG::SetDefaultBackgroundButton(const RefPtr<FrameNode>& buttonNode,
178     int32_t type)
179 {
180     auto buttonLayoutProperty = buttonNode->GetLayoutProperty<ButtonLayoutProperty>();
181     CHECK_NULL_VOID(buttonLayoutProperty);
182     const auto& renderContext = buttonNode->GetRenderContext();
183     CHECK_NULL_VOID(renderContext);
184     auto secCompTheme = GetTheme();
185     CHECK_NULL_VOID(secCompTheme);
186 
187     BorderColorProperty borderColor;
188     borderColor.SetColor(secCompTheme->GetBorderColor());
189     renderContext->UpdateBorderColor(borderColor);
190     BorderWidthProperty widthProp;
191     widthProp.SetBorderWidth(secCompTheme->GetBorderWidth());
192     buttonLayoutProperty->UpdateBorderWidth(widthProp);
193     BorderStyleProperty style;
194     style.SetBorderStyle(BorderStyle::NONE);
195     renderContext->UpdateBorderStyle(style);
196     auto buttonRadius = secCompTheme->GetBorderRadius();
197     buttonLayoutProperty->UpdateBorderRadius(BorderRadiusProperty(buttonRadius));
198     renderContext->UpdateBackgroundColor(secCompTheme->GetBackgroundColor());
199     buttonLayoutProperty->UpdateType(static_cast<ButtonType>(type));
200 }
201 
SetInvisibleBackgroundButton(const RefPtr<FrameNode> & buttonNode)202 void SecurityComponentModelNG::SetInvisibleBackgroundButton(const RefPtr<FrameNode>& buttonNode)
203 {
204     auto buttonLayoutProperty = buttonNode->GetLayoutProperty<ButtonLayoutProperty>();
205     CHECK_NULL_VOID(buttonLayoutProperty);
206     const auto& renderContext = buttonNode->GetRenderContext();
207     CHECK_NULL_VOID(renderContext);
208     renderContext->UpdateBackgroundColor(Color::TRANSPARENT);
209     buttonLayoutProperty->UpdateType(ButtonType::NORMAL);
210 }
211 
212 template<typename T>
GetChildLayoutProprty(const std::string & tag)213 RefPtr<T> GetChildLayoutProprty(const std::string& tag)
214 {
215     auto node = GetCurSecCompChildNode(tag);
216     CHECK_NULL_RETURN(node, nullptr);
217     return node->GetLayoutProperty<T>();
218 }
219 
IsBackgroundVisible()220 bool SecurityComponentModelNG::IsBackgroundVisible()
221 {
222     auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode();
223     CHECK_NULL_RETURN(frameNode, false);
224     auto prop = frameNode->GetLayoutProperty<SecurityComponentLayoutProperty>();
225     if (prop) {
226         return (prop->GetBackgroundType() != BUTTON_TYPE_NULL);
227     }
228     return false;
229 }
230 
SetIconSize(const Dimension & value)231 void SecurityComponentModelNG::SetIconSize(const Dimension& value)
232 {
233     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty, IconSize, value);
234 }
235 
SetIconColor(const Color & value)236 void SecurityComponentModelNG::SetIconColor(const Color& value)
237 {
238     ACE_UPDATE_PAINT_PROPERTY(SecurityComponentPaintProperty, IconColor, value);
239 }
240 
SetFontSize(const Dimension & value)241 void SecurityComponentModelNG::SetFontSize(const Dimension& value)
242 {
243     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty, FontSize, value);
244 }
245 
SetFontStyle(const Ace::FontStyle & value)246 void SecurityComponentModelNG::SetFontStyle(const Ace::FontStyle& value)
247 {
248     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty, FontStyle, value);
249 }
250 
SetFontWeight(const FontWeight & value)251 void SecurityComponentModelNG::SetFontWeight(const FontWeight& value)
252 {
253     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty, FontWeight, value);
254 }
255 
SetFontFamily(const std::vector<std::string> & fontFamilies)256 void SecurityComponentModelNG::SetFontFamily(const std::vector<std::string>& fontFamilies)
257 {
258     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty, FontFamily, fontFamilies);
259 }
260 
SetFontColor(const Color & value)261 void SecurityComponentModelNG::SetFontColor(const Color& value)
262 {
263     ACE_UPDATE_PAINT_PROPERTY(SecurityComponentPaintProperty, FontColor, value);
264 }
265 
SetBackgroundColor(const Color & value)266 void SecurityComponentModelNG::SetBackgroundColor(const Color& value)
267 {
268     if (!IsBackgroundVisible()) {
269         LOGW("background is not exist");
270         return;
271     }
272     ACE_UPDATE_PAINT_PROPERTY(SecurityComponentPaintProperty, BackgroundColor, value);
273 }
274 
SetBackgroundBorderWidth(const Dimension & value)275 void SecurityComponentModelNG::SetBackgroundBorderWidth(const Dimension& value)
276 {
277     if (!IsBackgroundVisible()) {
278         LOGW("background is not exist");
279         return;
280     }
281 
282     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty, BackgroundBorderWidth, value);
283 }
284 
SetBackgroundBorderColor(const Color & value)285 void SecurityComponentModelNG::SetBackgroundBorderColor(const Color& value)
286 {
287     if (!IsBackgroundVisible()) {
288         LOGW("background is not exist");
289         return;
290     }
291     ACE_UPDATE_PAINT_PROPERTY(SecurityComponentPaintProperty, BackgroundBorderColor, value);
292 }
293 
SetBackgroundBorderStyle(const BorderStyle & value)294 void SecurityComponentModelNG::SetBackgroundBorderStyle(const BorderStyle& value)
295 {
296     if (!IsBackgroundVisible()) {
297         LOGW("background is not exist");
298         return;
299     }
300     ACE_UPDATE_PAINT_PROPERTY(SecurityComponentPaintProperty, BackgroundBorderStyle, value);
301 }
302 
SetBackgroundBorderRadius(const Dimension & value)303 void SecurityComponentModelNG::SetBackgroundBorderRadius(const Dimension& value)
304 {
305     if (!IsBackgroundVisible()) {
306         LOGW("background is not exist");
307         return;
308     }
309 
310     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty, BackgroundBorderRadius, value);
311 }
312 
SetBackgroundPadding(const std::optional<Dimension> & left,const std::optional<Dimension> & right,const std::optional<Dimension> & top,const std::optional<Dimension> & bottom)313 void SecurityComponentModelNG::SetBackgroundPadding(const std::optional<Dimension>& left,
314     const std::optional<Dimension>& right, const std::optional<Dimension>& top,
315     const std::optional<Dimension>& bottom)
316 {
317     if (!IsBackgroundVisible()) {
318         LOGW("Can not set background padding without background");
319         return;
320     }
321 
322     auto secCompTheme = GetTheme();
323     CHECK_NULL_VOID(secCompTheme);
324     if (left.has_value()) {
325         ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty,
326             BackgroundLeftPadding, left.value());
327     }
328 
329     if (right.has_value()) {
330         ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty,
331             BackgroundRightPadding, right.value());
332     }
333     if (top.has_value()) {
334         ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty,
335             BackgroundTopPadding, top.value());
336     }
337 
338     if (bottom.has_value()) {
339         ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty,
340             BackgroundBottomPadding, bottom.value());
341     }
342 }
343 
SetBackgroundPadding(const std::optional<Dimension> & padding)344 void SecurityComponentModelNG::SetBackgroundPadding(const std::optional<Dimension>& padding)
345 {
346     SetBackgroundPadding(padding, padding, padding, padding);
347 }
348 
SetTextIconSpace(const Dimension & value)349 void SecurityComponentModelNG::SetTextIconSpace(const Dimension& value)
350 {
351     if ((GetCurSecCompChildNode(V2::TEXT_ETS_TAG) == nullptr) ||
352         (GetCurSecCompChildNode(V2::IMAGE_ETS_TAG) == nullptr)) {
353         LOGW("Can not set text icon padding without text and icon");
354         return;
355     }
356     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty, TextIconSpace, value);
357 }
358 
SetTextIconLayoutDirection(const SecurityComponentLayoutDirection & value)359 void SecurityComponentModelNG::SetTextIconLayoutDirection(const SecurityComponentLayoutDirection& value)
360 {
361     ACE_UPDATE_LAYOUT_PROPERTY(SecurityComponentLayoutProperty, TextIconLayoutDirection, value);
362 }
363 } // namespace OHOS::Ace::NG
364