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 "frameworks/bridge/declarative_frontend/jsview/js_sec_button_base.h"
17 
18 #include "base/log/ace_scoring_log.h"
19 #include "bridge/common/utils/utils.h"
20 #include "core/common/container.h"
21 #include "core/components/common/properties/text_style.h"
22 #include "core/components_ng/base/view_abstract_model.h"
23 #include "core/components_ng/pattern/security_component/security_component_theme.h"
24 
25 using OHOS::Ace::NG::SecurityComponentModelNG;
26 using OHOS::Ace::NG::SecurityComponentTheme;
27 
28 namespace OHOS::Ace::Framework {
SetIconSize(const JSCallbackInfo & info)29 void JSSecButtonBase::SetIconSize(const JSCallbackInfo& info)
30 {
31     auto theme = GetTheme<SecurityComponentTheme>();
32     CHECK_NULL_VOID(theme);
33 
34     CalcDimension value;
35     if (!ParseJsDimensionVp(info[0], value)) {
36         SecurityComponentModelNG::SetIconSize(theme->GetIconSize());
37     } else {
38         SecurityComponentModelNG::SetIconSize(value);
39     }
40 }
41 
SetIconColor(const JSCallbackInfo & info)42 void JSSecButtonBase::SetIconColor(const JSCallbackInfo& info)
43 {
44     auto theme = GetTheme<SecurityComponentTheme>();
45     CHECK_NULL_VOID(theme);
46 
47     Color color;
48     if (!ParseJsColor(info[0], color)) {
49         color = theme->GetIconColor();
50     }
51     SecurityComponentModelNG::SetIconColor(color);
52 }
53 
SetFontSize(const JSCallbackInfo & info)54 void JSSecButtonBase::SetFontSize(const JSCallbackInfo& info)
55 {
56     auto theme = GetTheme<SecurityComponentTheme>();
57     CHECK_NULL_VOID(theme);
58 
59     CalcDimension value;
60     if (!ParseJsDimensionFp(info[0], value)) {
61         SecurityComponentModelNG::SetFontSize(theme->GetFontSize());
62     } else {
63         SecurityComponentModelNG::SetFontSize(value);
64     }
65 }
66 
SetFontStyle(const JSCallbackInfo & info)67 void JSSecButtonBase::SetFontStyle(const JSCallbackInfo& info)
68 {
69     if (!info[0]->IsNumber()) {
70         SecurityComponentModelNG::SetFontStyle(Ace::FontStyle::NORMAL);
71         return;
72     }
73     uint32_t value = info[0]->ToNumber<uint32_t>();
74     if (value < static_cast<uint32_t>(Ace::FontStyle::NORMAL) ||
75         value > static_cast<uint32_t>(Ace::FontStyle::ITALIC)) {
76         SecurityComponentModelNG::SetFontStyle(Ace::FontStyle::NORMAL);
77         return;
78     }
79     SecurityComponentModelNG::SetFontStyle(static_cast<Ace::FontStyle>(value));
80 }
81 
SetFontWeight(const JSCallbackInfo & info)82 void JSSecButtonBase::SetFontWeight(const JSCallbackInfo& info)
83 {
84     if (!info[0]->IsString()) {
85         SecurityComponentModelNG::SetFontWeight(FontWeight::MEDIUM);
86         return;
87     }
88     std::string value = info[0]->ToString();
89     SecurityComponentModelNG::SetFontWeight(ConvertStrToFontWeight(value));
90 }
91 
SetFontFamily(const JSCallbackInfo & info)92 void JSSecButtonBase::SetFontFamily(const JSCallbackInfo& info)
93 {
94     std::vector<std::string> fontFamilies;
95     if (!ParseJsFontFamilies(info[0], fontFamilies)) {
96         fontFamilies.emplace_back("HarmonyOS Sans");
97     }
98     SecurityComponentModelNG::SetFontFamily(fontFamilies);
99 }
100 
SetFontColor(const JSCallbackInfo & info)101 void JSSecButtonBase::SetFontColor(const JSCallbackInfo& info)
102 {
103     auto theme = GetTheme<SecurityComponentTheme>();
104     CHECK_NULL_VOID(theme);
105 
106     Color color;
107     if (!ParseJsColor(info[0], color)) {
108         color = theme->GetFontColor();
109     }
110     SecurityComponentModelNG::SetFontColor(color);
111 }
112 
SetLayoutDirection(const JSCallbackInfo & info)113 void JSSecButtonBase::SetLayoutDirection(const JSCallbackInfo& info)
114 {
115     if (!info[0]->IsNumber()) {
116         SecurityComponentModelNG::SetTextIconLayoutDirection(
117             SecurityComponentLayoutDirection::HORIZONTAL);
118         return;
119     }
120     int32_t value = info[0]->ToNumber<int32_t>();
121     if ((value < static_cast<int32_t>(SecurityComponentLayoutDirection::HORIZONTAL)) ||
122         (value > static_cast<int32_t>(SecurityComponentLayoutDirection::VERTICAL))) {
123         SecurityComponentModelNG::SetTextIconLayoutDirection(
124             SecurityComponentLayoutDirection::HORIZONTAL);
125         return;
126     }
127     SecurityComponentModelNG::SetTextIconLayoutDirection(
128         static_cast<SecurityComponentLayoutDirection>(value));
129 }
130 
SetBackgroundColor(const JSCallbackInfo & info)131 void JSSecButtonBase::SetBackgroundColor(const JSCallbackInfo& info)
132 {
133     auto theme = GetTheme<SecurityComponentTheme>();
134     CHECK_NULL_VOID(theme);
135 
136     Color color;
137     if (!ParseJsColor(info[0], color)) {
138         color = theme->GetBackgroundColor();
139     }
140     SecurityComponentModelNG::SetBackgroundColor(color);
141 }
142 
SetBackgroundBorderStyle(const JSCallbackInfo & info)143 void JSSecButtonBase::SetBackgroundBorderStyle(const JSCallbackInfo& info)
144 {
145     if (!info[0]->IsNumber()) {
146         SecurityComponentModelNG::SetBackgroundBorderStyle(BorderStyle::NONE);
147         return;
148     }
149     int32_t value = info[0]->ToNumber<int32_t>();
150     if ((value < static_cast<int32_t>(BorderStyle::SOLID)) ||
151         (value > static_cast<int32_t>(BorderStyle::NONE))) {
152         SecurityComponentModelNG::SetBackgroundBorderStyle(BorderStyle::NONE);
153         return;
154     }
155     SecurityComponentModelNG::SetBackgroundBorderStyle(static_cast<BorderStyle>(value));
156 }
157 
SetBackgroundBorderWidth(const JSCallbackInfo & info)158 void JSSecButtonBase::SetBackgroundBorderWidth(const JSCallbackInfo& info)
159 {
160     auto theme = GetTheme<SecurityComponentTheme>();
161     CHECK_NULL_VOID(theme);
162 
163     CalcDimension value;
164     if (!ParseJsDimensionVp(info[0], value)) {
165         SecurityComponentModelNG::SetBackgroundBorderWidth(theme->GetBorderWidth());
166     } else {
167         SecurityComponentModelNG::SetBackgroundBorderWidth(value);
168     }
169 }
170 
SetBackgroundBorderColor(const JSCallbackInfo & info)171 void JSSecButtonBase::SetBackgroundBorderColor(const JSCallbackInfo& info)
172 {
173     auto theme = GetTheme<SecurityComponentTheme>();
174     CHECK_NULL_VOID(theme);
175 
176     Color borderColor;
177     if (!ParseJsColor(info[0], borderColor)) {
178         borderColor = theme->GetBorderColor();
179     }
180     SecurityComponentModelNG::SetBackgroundBorderColor(borderColor);
181 }
182 
SetBackgroundBorderRadius(const JSCallbackInfo & info)183 void JSSecButtonBase::SetBackgroundBorderRadius(const JSCallbackInfo& info)
184 {
185     auto theme = GetTheme<SecurityComponentTheme>();
186     CHECK_NULL_VOID(theme);
187 
188     CalcDimension value;
189     if (!ParseJsDimensionVp(info[0], value)) {
190         SecurityComponentModelNG::SetBackgroundBorderRadius(theme->GetBorderRadius());
191     } else {
192         SecurityComponentModelNG::SetBackgroundBorderRadius(value);
193     }
194 }
195 
SetBackgroundPadding(const JSCallbackInfo & info)196 void JSSecButtonBase::SetBackgroundPadding(const JSCallbackInfo& info)
197 {
198     if (info[0]->IsObject()) {
199         std::optional<CalcDimension> left;
200         std::optional<CalcDimension> right;
201         std::optional<CalcDimension> top;
202         std::optional<CalcDimension> bottom;
203         JSRef<JSObject> paddingObj = JSRef<JSObject>::Cast(info[0]);
204 
205         CalcDimension leftDimen;
206         if (ParseJsDimensionVp(paddingObj->GetProperty("left"), leftDimen)) {
207             left = leftDimen;
208         }
209         CalcDimension rightDimen;
210         if (ParseJsDimensionVp(paddingObj->GetProperty("right"), rightDimen)) {
211             right = rightDimen;
212         }
213         CalcDimension topDimen;
214         if (ParseJsDimensionVp(paddingObj->GetProperty("top"), topDimen)) {
215             top = topDimen;
216         }
217         CalcDimension bottomDimen;
218         if (ParseJsDimensionVp(paddingObj->GetProperty("bottom"), bottomDimen)) {
219             bottom = bottomDimen;
220         }
221         if (left.has_value() || right.has_value() || top.has_value() || bottom.has_value()) {
222             SecurityComponentModelNG::SetBackgroundPadding(left, right, top, bottom);
223             return;
224         }
225     }
226 
227     CalcDimension length;
228     if (!ParseJsDimensionVp(info[0], length)) {
229         SecurityComponentModelNG::SetBackgroundPadding(std::nullopt);
230     } else {
231         SecurityComponentModelNG::SetBackgroundPadding(length);
232     }
233 }
234 
SetTextIconSpace(const JSCallbackInfo & info)235 void JSSecButtonBase::SetTextIconSpace(const JSCallbackInfo& info)
236 {
237     auto theme = GetTheme<SecurityComponentTheme>();
238     CHECK_NULL_VOID(theme);
239 
240     CalcDimension length;
241     if (!ParseJsDimensionVp(info[0], length) || LessNotEqual(length.ConvertToPx(), 0.0)) {
242         SecurityComponentModelNG::SetTextIconSpace(theme->GetTextIconSpace());
243     } else {
244         SecurityComponentModelNG::SetTextIconSpace(length);
245     }
246 }
247 }
248