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_container_span.h"
17
18 #include "core/common/resource/resource_parse_utils.h"
19 #include "core/components_ng/pattern/text/span_model.h"
20 #include "core/components_ng/pattern/text/span_model_ng.h"
21
22 namespace OHOS::Ace::Framework {
23
SetTextBackgroundStyle(const JSCallbackInfo & info)24 void JSContainerSpan::SetTextBackgroundStyle(const JSCallbackInfo& info)
25 {
26 auto textBackgroundStyle = JSContainerSpan::ParseTextBackgroundStyle(info);
27 SpanModel::GetInstance()->SetTextBackgroundStyle(textBackgroundStyle);
28 }
29
ParseTextBackgroundStyle(const JSCallbackInfo & info)30 TextBackgroundStyle JSContainerSpan::ParseTextBackgroundStyle(const JSCallbackInfo& info)
31 {
32 TextBackgroundStyle textBackgroundStyle;
33 auto jsValue = info[0];
34 if (!jsValue->IsObject()) {
35 return textBackgroundStyle;
36 }
37 JSRef<JSObject> obj = JSRef<JSObject>::Cast(jsValue);
38 return ParseTextBackgroundStyle(obj);
39 }
40
ParseTextBackgroundStyle(const JSRef<JSObject> & obj)41 TextBackgroundStyle JSContainerSpan::ParseTextBackgroundStyle(const JSRef<JSObject>& obj)
42 {
43 TextBackgroundStyle textBackgroundStyle;
44 JSRef<JSVal> colorValue = obj->GetProperty("color");
45 Color colorVal;
46 RefPtr<ResourceObject> colorResObj;
47 if (ParseJsColor(colorValue, colorVal, colorResObj)) {
48 textBackgroundStyle.backgroundColor = colorVal;
49 }
50 if (SystemProperties::ConfigChangePerform() && colorResObj) {
51 auto&& updateFunc = [](const RefPtr<ResourceObject>& colorResObj, TextBackgroundStyle& textBackgroundStyle) {
52 Color color;
53 ResourceParseUtils::ParseResColor(colorResObj, color);
54 textBackgroundStyle.backgroundColor = color;
55 };
56 textBackgroundStyle.AddResource("textBackgroundStyle.color", colorResObj, std::move(updateFunc));
57 }
58 JSRef<JSVal> radiusValue = obj->GetProperty("radius");
59 if (!radiusValue->IsString() && !radiusValue->IsNumber() && !radiusValue->IsObject()) {
60 return textBackgroundStyle;
61 }
62 CalcDimension radius;
63 RefPtr<ResourceObject> radiusResObj;
64 if (ParseJsDimensionVp(radiusValue, radius, radiusResObj)) {
65 if (radius.Unit() == DimensionUnit::PERCENT) {
66 radius.Reset();
67 }
68 textBackgroundStyle.backgroundRadius = { radius, radius, radius, radius };
69 textBackgroundStyle.backgroundRadius->multiValued = false;
70 if (SystemProperties::ConfigChangePerform() && radiusResObj) {
71 auto&& updateFunc = [](const RefPtr<ResourceObject>& radiusResObj,
72 TextBackgroundStyle& textBackgroundStyle) {
73 CalcDimension radius;
74 ResourceParseUtils::ParseResDimensionVp(radiusResObj, radius);
75 textBackgroundStyle.backgroundRadius = { radius, radius, radius, radius };
76 textBackgroundStyle.backgroundRadius->multiValued = false;
77 };
78 textBackgroundStyle.AddResource("textBackgroundStyle.radius", radiusResObj, std::move(updateFunc));
79 }
80 } else if (radiusValue->IsObject()) {
81 JSRef<JSObject> object = JSRef<JSObject>::Cast(radiusValue);
82 CalcDimension topLeft;
83 CalcDimension topRight;
84 CalcDimension bottomLeft;
85 CalcDimension bottomRight;
86 ParseAllBorderRadiuses(object, topLeft, topRight, bottomLeft, bottomRight, textBackgroundStyle);
87 textBackgroundStyle.backgroundRadius = { topLeft, topRight, bottomRight, bottomLeft };
88 textBackgroundStyle.backgroundRadius->multiValued = true;
89 }
90 return textBackgroundStyle;
91 }
92
JSBind(BindingTarget globalObj)93 void JSContainerSpan::JSBind(BindingTarget globalObj)
94 {
95 JSClass<JSContainerSpan>::Declare("ContainerSpan");
96 MethodOptions opt = MethodOptions::NONE;
97 JSClass<JSContainerSpan>::StaticMethod("create", &JSContainerSpan::Create, opt);
98 JSClass<JSContainerSpan>::StaticMethod("textBackgroundStyle", &JSContainerSpan::SetTextBackgroundStyle, opt);
99 JSClass<JSContainerSpan>::InheritAndBind<JSContainerBase>(globalObj);
100 }
101
Create(const JSCallbackInfo & info)102 void JSContainerSpan::Create(const JSCallbackInfo& info)
103 {
104 SpanModel::GetInstance()->CreateContainSpan();
105 }
106 } // namespace OHOS::Ace::Framework
107