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