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 {
30 namespace {
31 const std::vector<TextHeightAdaptivePolicy> HEIGHT_ADAPTIVE_POLICY = { TextHeightAdaptivePolicy::MAX_LINES_FIRST,
32 TextHeightAdaptivePolicy::MIN_FONT_SIZE_FIRST, TextHeightAdaptivePolicy::LAYOUT_CONSTRAINT_FIRST };
33 }
34
SetIconSize(const JSCallbackInfo & info)35 void JSSecButtonBase::SetIconSize(const JSCallbackInfo& info)
36 {
37 auto theme = GetTheme<SecurityComponentTheme>();
38 CHECK_NULL_VOID(theme);
39
40 CalcDimension value;
41 if (!ParseJsDimensionVpNG(info[0], value, false) || value.IsNegative()) {
42 SecurityComponentModelNG::SetIconSize(theme->GetIconSize());
43 } else {
44 SecurityComponentModelNG::SetIconSize(value);
45 }
46 }
47
SetIconColor(const JSCallbackInfo & info)48 void JSSecButtonBase::SetIconColor(const JSCallbackInfo& info)
49 {
50 auto theme = GetTheme<SecurityComponentTheme>();
51 CHECK_NULL_VOID(theme);
52
53 Color color;
54 if (!ParseJsColor(info[0], color)) {
55 color = theme->GetIconColor();
56 }
57 SecurityComponentModelNG::SetIconColor(color);
58 }
59
SetFontSize(const JSCallbackInfo & info)60 void JSSecButtonBase::SetFontSize(const JSCallbackInfo& info)
61 {
62 auto theme = GetTheme<SecurityComponentTheme>();
63 CHECK_NULL_VOID(theme);
64
65 CalcDimension value;
66 if (!ParseJsDimensionFpNG(info[0], value, false) || value.IsNegative()) {
67 SecurityComponentModelNG::SetFontSize(theme->GetFontSize());
68 } else {
69 SecurityComponentModelNG::SetFontSize(value);
70 }
71 }
72
SetFontStyle(const JSCallbackInfo & info)73 void JSSecButtonBase::SetFontStyle(const JSCallbackInfo& info)
74 {
75 if (!info[0]->IsNumber()) {
76 SecurityComponentModelNG::SetFontStyle(Ace::FontStyle::NORMAL);
77 return;
78 }
79 uint32_t value = info[0]->ToNumber<uint32_t>();
80 if (value < static_cast<uint32_t>(Ace::FontStyle::NORMAL) ||
81 value > static_cast<uint32_t>(Ace::FontStyle::ITALIC)) {
82 SecurityComponentModelNG::SetFontStyle(Ace::FontStyle::NORMAL);
83 return;
84 }
85 SecurityComponentModelNG::SetFontStyle(static_cast<Ace::FontStyle>(value));
86 }
87
SetFontWeight(const JSCallbackInfo & info)88 void JSSecButtonBase::SetFontWeight(const JSCallbackInfo& info)
89 {
90 if (!info[0]->IsString()) {
91 SecurityComponentModelNG::SetFontWeight(FontWeight::MEDIUM);
92 return;
93 }
94 std::string value = info[0]->ToString();
95 SecurityComponentModelNG::SetFontWeight(ConvertStrToFontWeight(value));
96 }
97
SetFontFamily(const JSCallbackInfo & info)98 void JSSecButtonBase::SetFontFamily(const JSCallbackInfo& info)
99 {
100 std::vector<std::string> fontFamilies;
101 if (!ParseJsFontFamilies(info[0], fontFamilies)) {
102 fontFamilies.emplace_back("HarmonyOS Sans");
103 }
104 SecurityComponentModelNG::SetFontFamily(fontFamilies);
105 }
106
SetFontColor(const JSCallbackInfo & info)107 void JSSecButtonBase::SetFontColor(const JSCallbackInfo& info)
108 {
109 auto theme = GetTheme<SecurityComponentTheme>();
110 CHECK_NULL_VOID(theme);
111
112 Color color;
113 if (!ParseJsColor(info[0], color)) {
114 color = theme->GetFontColor();
115 }
116 SecurityComponentModelNG::SetFontColor(color);
117 }
118
SetLayoutDirection(const JSCallbackInfo & info)119 void JSSecButtonBase::SetLayoutDirection(const JSCallbackInfo& info)
120 {
121 if (!info[0]->IsNumber()) {
122 SecurityComponentModelNG::SetTextIconLayoutDirection(
123 SecurityComponentLayoutDirection::HORIZONTAL);
124 return;
125 }
126 int32_t value = info[0]->ToNumber<int32_t>();
127 if ((value < static_cast<int32_t>(SecurityComponentLayoutDirection::HORIZONTAL)) ||
128 (value > static_cast<int32_t>(SecurityComponentLayoutDirection::VERTICAL))) {
129 SecurityComponentModelNG::SetTextIconLayoutDirection(
130 SecurityComponentLayoutDirection::HORIZONTAL);
131 return;
132 }
133 SecurityComponentModelNG::SetTextIconLayoutDirection(
134 static_cast<SecurityComponentLayoutDirection>(value));
135 }
136
SetBackgroundColor(const JSCallbackInfo & info)137 void JSSecButtonBase::SetBackgroundColor(const JSCallbackInfo& info)
138 {
139 auto theme = GetTheme<SecurityComponentTheme>();
140 CHECK_NULL_VOID(theme);
141
142 Color color;
143 if (!ParseJsColor(info[0], color)) {
144 color = theme->GetBackgroundColor();
145 }
146 SecurityComponentModelNG::SetBackgroundColor(color);
147 }
148
SetBackgroundBorderStyle(const JSCallbackInfo & info)149 void JSSecButtonBase::SetBackgroundBorderStyle(const JSCallbackInfo& info)
150 {
151 if (!info[0]->IsNumber()) {
152 SecurityComponentModelNG::SetBackgroundBorderStyle(BorderStyle::NONE);
153 return;
154 }
155 int32_t value = info[0]->ToNumber<int32_t>();
156 if ((value < static_cast<int32_t>(BorderStyle::SOLID)) ||
157 (value > static_cast<int32_t>(BorderStyle::NONE))) {
158 SecurityComponentModelNG::SetBackgroundBorderStyle(BorderStyle::NONE);
159 return;
160 }
161 SecurityComponentModelNG::SetBackgroundBorderStyle(static_cast<BorderStyle>(value));
162 }
163
SetBackgroundBorderWidth(const JSCallbackInfo & info)164 void JSSecButtonBase::SetBackgroundBorderWidth(const JSCallbackInfo& info)
165 {
166 auto theme = GetTheme<SecurityComponentTheme>();
167 CHECK_NULL_VOID(theme);
168
169 CalcDimension value;
170 if (!ParseJsDimensionVp(info[0], value)) {
171 SecurityComponentModelNG::SetBackgroundBorderWidth(theme->GetBorderWidth());
172 } else {
173 SecurityComponentModelNG::SetBackgroundBorderWidth(value);
174 }
175 }
176
SetBackgroundBorderColor(const JSCallbackInfo & info)177 void JSSecButtonBase::SetBackgroundBorderColor(const JSCallbackInfo& info)
178 {
179 auto theme = GetTheme<SecurityComponentTheme>();
180 CHECK_NULL_VOID(theme);
181
182 Color borderColor;
183 if (!ParseJsColor(info[0], borderColor)) {
184 borderColor = theme->GetBorderColor();
185 }
186 SecurityComponentModelNG::SetBackgroundBorderColor(borderColor);
187 }
188
SetBackgroundBorderRadius(const JSCallbackInfo & info)189 void JSSecButtonBase::SetBackgroundBorderRadius(const JSCallbackInfo& info)
190 {
191 if (info[0]->IsObject()) {
192 std::optional<CalcDimension> topLeft;
193 std::optional<CalcDimension> topRight;
194 std::optional<CalcDimension> bottomLeft;
195 std::optional<CalcDimension> bottomRight;
196 JSRef<JSObject> paddingObj = JSRef<JSObject>::Cast(info[0]);
197
198 CalcDimension topLeftDimen;
199 if (ParseJsDimensionVp(paddingObj->GetProperty("topLeft"), topLeftDimen)) {
200 topLeft = topLeftDimen;
201 }
202 CalcDimension topRightDimen;
203 if (ParseJsDimensionVp(paddingObj->GetProperty("topRight"), topRightDimen)) {
204 topRight = topRightDimen;
205 }
206 CalcDimension bottomLeftDimen;
207 if (ParseJsDimensionVp(paddingObj->GetProperty("bottomLeft"), bottomLeftDimen)) {
208 bottomLeft = bottomLeftDimen;
209 }
210 CalcDimension bottomRightDimen;
211 if (ParseJsDimensionVp(paddingObj->GetProperty("bottomRight"), bottomRightDimen)) {
212 bottomRight = bottomRightDimen;
213 }
214
215 if (topLeft.has_value() || topRight.has_value() || bottomLeft.has_value() || bottomRight.has_value()) {
216 SecurityComponentModelNG::SetBackgroundBorderRadius(topLeft, topRight, bottomLeft, bottomRight);
217 return;
218 }
219 }
220 auto theme = GetTheme<SecurityComponentTheme>();
221 CHECK_NULL_VOID(theme);
222
223 CalcDimension value;
224 if (!ParseJsDimensionVp(info[0], value)) {
225 SecurityComponentModelNG::SetBackgroundBorderRadius(theme->GetBorderRadius());
226 } else {
227 SecurityComponentModelNG::SetBackgroundBorderRadius(value);
228 }
229 }
230
SetBackgroundPadding(const JSCallbackInfo & info)231 void JSSecButtonBase::SetBackgroundPadding(const JSCallbackInfo& info)
232 {
233 if (info[0]->IsObject()) {
234 std::optional<CalcDimension> left;
235 std::optional<CalcDimension> right;
236 std::optional<CalcDimension> top;
237 std::optional<CalcDimension> bottom;
238 JSRef<JSObject> paddingObj = JSRef<JSObject>::Cast(info[0]);
239
240 CalcDimension leftDimen;
241 if (ParseJsDimensionVp(paddingObj->GetProperty("left"), leftDimen)) {
242 left = leftDimen;
243 }
244 CalcDimension rightDimen;
245 if (ParseJsDimensionVp(paddingObj->GetProperty("right"), rightDimen)) {
246 right = rightDimen;
247 }
248 CalcDimension topDimen;
249 if (ParseJsDimensionVp(paddingObj->GetProperty("top"), topDimen)) {
250 top = topDimen;
251 }
252 CalcDimension bottomDimen;
253 if (ParseJsDimensionVp(paddingObj->GetProperty("bottom"), bottomDimen)) {
254 bottom = bottomDimen;
255 }
256 if (left.has_value() || right.has_value() || top.has_value() || bottom.has_value()) {
257 SecurityComponentModelNG::SetBackgroundPadding(left, right, top, bottom);
258 return;
259 }
260 }
261
262 CalcDimension length;
263 if (!ParseJsDimensionVp(info[0], length)) {
264 SecurityComponentModelNG::SetBackgroundPadding(std::nullopt);
265 } else {
266 SecurityComponentModelNG::SetBackgroundPadding(length);
267 }
268 }
269
SetTextIconSpace(const JSCallbackInfo & info)270 void JSSecButtonBase::SetTextIconSpace(const JSCallbackInfo& info)
271 {
272 auto theme = GetTheme<SecurityComponentTheme>();
273 CHECK_NULL_VOID(theme);
274
275 CalcDimension length;
276 if (!ParseJsDimensionVp(info[0], length) || LessNotEqual(length.ConvertToPx(), 0.0)) {
277 SecurityComponentModelNG::SetTextIconSpace(theme->GetTextIconSpace());
278 } else {
279 SecurityComponentModelNG::SetTextIconSpace(length);
280 }
281 }
282
SetAlign(const JSCallbackInfo & info)283 void JSSecButtonBase::SetAlign(const JSCallbackInfo& info)
284 {
285 Alignment alignment;
286 if (!info[0]->IsNumber()) {
287 alignment = Alignment::CENTER;
288 } else {
289 auto value = info[0]->ToNumber<int32_t>();
290 alignment = ParseAlignment(value);
291 }
292 SecurityComponentModelNG::SetAlign(alignment);
293 }
294
SetMaxFontScale(const JSCallbackInfo & info)295 void JSSecButtonBase::SetMaxFontScale(const JSCallbackInfo& info)
296 {
297 double maxFontScale;
298 if (info.Length() < 1 || !ParseJsDouble(info[0], maxFontScale)) {
299 return;
300 }
301 if (LessOrEqual(maxFontScale, 1.0f)) {
302 SecurityComponentModelNG::SetMaxFontScale(1.0f);
303 return;
304 }
305 SecurityComponentModelNG::SetMaxFontScale(static_cast<float>(maxFontScale));
306 }
307
SetMinFontScale(const JSCallbackInfo & info)308 void JSSecButtonBase::SetMinFontScale(const JSCallbackInfo& info)
309 {
310 double minFontScale;
311 if (info.Length() < 1 || !ParseJsDouble(info[0], minFontScale)) {
312 return;
313 }
314 if (LessOrEqual(minFontScale, 0.0f)) {
315 SecurityComponentModelNG::SetMinFontScale(0.0f);
316 return;
317 }
318 if (GreatOrEqual(minFontScale, 1.0f)) {
319 SecurityComponentModelNG::SetMinFontScale(1.0f);
320 return;
321 }
322 SecurityComponentModelNG::SetMinFontScale(static_cast<float>(minFontScale));
323 }
324
SetMaxLines(const JSCallbackInfo & info)325 void JSSecButtonBase::SetMaxLines(const JSCallbackInfo& info)
326 {
327 JSRef<JSVal> args = info[0];
328 auto value = Infinity<int32_t>();
329 if (args->ToString() != "Infinity") {
330 ParseJsInt32(args, value);
331 }
332 if (value <= 0) {
333 return;
334 }
335 SecurityComponentModelNG::SetMaxLines(value);
336 }
337
SetMaxFontSize(const JSCallbackInfo & info)338 void JSSecButtonBase::SetMaxFontSize(const JSCallbackInfo& info)
339 {
340 if (info.Length() < 1) {
341 return;
342 }
343 CalcDimension maxFontSize;
344 JSRef<JSVal> args = info[0];
345 if (!ParseJsDimensionFpNG(args, maxFontSize, false)) {
346 return;
347 }
348 if (maxFontSize.IsNegative()) {
349 return;
350 }
351 SecurityComponentModelNG::SetAdaptMaxFontSize(maxFontSize);
352 }
353
SetMinFontSize(const JSCallbackInfo & info)354 void JSSecButtonBase::SetMinFontSize(const JSCallbackInfo& info)
355 {
356 if (info.Length() < 1) {
357 return;
358 }
359 CalcDimension minFontSize;
360 JSRef<JSVal> args = info[0];
361 if (!ParseJsDimensionFpNG(args, minFontSize, false)) {
362 return;
363 }
364 if (minFontSize.IsNegative()) {
365 return;
366 }
367 SecurityComponentModelNG::SetAdaptMinFontSize(minFontSize);
368 }
369
SetHeightAdaptivePolicy(int32_t value)370 void JSSecButtonBase::SetHeightAdaptivePolicy(int32_t value)
371 {
372 if (value < 0 || value >= static_cast<int32_t>(HEIGHT_ADAPTIVE_POLICY.size())) {
373 value = 0;
374 }
375 SecurityComponentModelNG::SetHeightAdaptivePolicy(HEIGHT_ADAPTIVE_POLICY[value]);
376 }
377 }
378