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